11import stream from "stream" ;
2+ import * as core from "../src/core" ;
23import { FileForgeClient } from "../src" ;
4+ import * as error from "../src/errors/index" ;
35import fs from "fs" ;
6+ import { writeFile } from "fs/promises" ;
47
8+ const FILEFORGE_API_KEY = process . env . FILEFORGE_API_KEY ! ;
9+
10+ const HTML = `<!DOCTYPE html>
11+ <html>
12+ <head>
13+ <title>My First Web Page</title>
14+ <link href="style.css" rel="stylesheet" />
15+ </head>
16+ <body>
17+ <h1>Hello World!</h1>
18+ </body>
19+ </html>
20+ ` ;
21+
22+ const CSS = `body{
23+ background-color: lightblue;
24+ }
25+ ` ;
526/**
627 * This is a custom test file, if you wish to add more tests
728 * to your SDK.
@@ -11,45 +32,89 @@ import fs from "fs";
1132 * you will have tests automatically generated for you.
1233 */
1334describe ( "test" , ( ) => {
14- it ( "should generate a PDF" , async ( ) => {
15- const apiKeySupplier = async ( ) => "ebec0c4c-214f-4796-afd2-b5c9b12281b6" ; // Replace with your actual API key supplier
16- const environmentSupplier = async ( ) => "https://api.fileforge.com" ; // Replace with your actual environment endpoint
17-
18- const client = new FileForgeClient ( {
19- apiKey : apiKeySupplier ,
20- environment : environmentSupplier ,
21- } ) ;
22-
23- // Write HTML content to a file
24- const htmlContent = "<h1>Hello World!</h1>" ;
25- const blob = new Blob ( [ htmlContent ] , { type : "text/html" } ) ;
26- const file = new File ( [ blob ] , "index.html" , { type : "text/html" } ) ;
27-
28- const request = {
29- options : {
30- fileName : "test.pdf" ,
31- test : false ,
32- host : false ,
33- } ,
34- } ;
35-
36- const pdfStream : stream . Readable = await client . generate ( [ file ] , request ) ;
37- console . log ( pdfStream ) ;
38- const pdfFilePath = "output.pdf" ;
39- const writeStream = fs . createWriteStream ( pdfFilePath ) ;
40-
41- pdfStream . pipe ( writeStream ) ;
42-
43- return new Promise ( ( resolve , reject ) => {
44- writeStream . on ( "finish" , ( ) => {
45- console . log ( "PDF generated and saved to" , pdfFilePath ) ;
46- resolve ( true ) ;
47- } ) ;
48-
49- writeStream . on ( "error" , ( error ) => {
50- console . error ( "Error generating PDF:" , error ) ;
51- reject ( error ) ;
52- } ) ;
53- } ) ;
54- } ) ;
35+ it ( "should generate a PDF buffer" , async ( ) => {
36+ const htmlBlob = new Blob ( [ HTML ] , {
37+ type : "text/html" ,
38+ } ) ;
39+ const cssBlob = new Blob ( [ CSS ] , {
40+ type : "text/css" ,
41+ } ) ;
42+ const htmlFile = new File ( [ htmlBlob ] , "index.html" , { type : "text/html" } ) ;
43+ const cssFile = new File ( [ cssBlob ] , "style.css" , { type : "text/css" } ) ;
44+
45+ const ff = new FileForgeClient ( {
46+ apiKey : FILEFORGE_API_KEY
47+ } ) ;
48+
49+ const pdf :FileForgeClient . Response = await ff . generate (
50+ [ htmlFile , cssFile ] ,
51+ {
52+ options : { }
53+ }
54+ ) ;
55+
56+ await writeFile ( "output.pdf" , pdf . file ! ) ;
57+ } , 10_000_000 ) ;
58+
59+
60+ it ( "should generate a PDF link" , async ( ) => {
61+ const htmlBlob = new Blob ( [ HTML ] , {
62+ type : "text/html" ,
63+ } ) ;
64+ const cssBlob = new Blob ( [ CSS ] , {
65+ type : "text/css" ,
66+ } ) ;
67+ const htmlFile = new File ( [ htmlBlob ] , "index.html" , { type : "text/html" } ) ;
68+ const cssFile = new File ( [ cssBlob ] , "style.css" , { type : "text/css" } ) ;
69+
70+ const ff = new FileForgeClient ( {
71+ apiKey : FILEFORGE_API_KEY
72+ } ) ;
73+
74+ const pdf :FileForgeClient . Response = await ff . generate (
75+ [ htmlFile , cssFile ] ,
76+ {
77+ options : {
78+ host : true ,
79+ }
80+ }
81+ ) ;
82+
83+ expect ( pdf . url ) . not . toBeNull ( ) ;
84+
85+ } , 10_000_000 ) ;
86+
87+ it ( "should fail because of invalid api key" , async ( ) => {
88+ const htmlBlob = new Blob ( [ HTML ] , {
89+ type : "text/html" ,
90+ } ) ;
91+ const cssBlob = new Blob ( [ CSS ] , {
92+ type : "text/css" ,
93+ } ) ;
94+ const htmlFile = new File ( [ htmlBlob ] , "index.html" , { type : "text/html" } ) ;
95+ const cssFile = new File ( [ cssBlob ] , "style.css" , { type : "text/css" } ) ;
96+
97+ const ff = new FileForgeClient ( {
98+ apiKey : "blabla_invalid_key"
99+ } ) ;
100+ try {
101+ const pdf = await ff . generate (
102+ [ htmlFile , cssFile ] ,
103+ {
104+ options : {
105+ host : true ,
106+ }
107+ }
108+ ) ;
109+
110+ } catch ( e ) {
111+ expect ( e ) . not . toBeNull ( ) ;
112+ if ( e instanceof error . FileForgeError ) {
113+ expect ( e . statusCode ) . toBe ( 401 ) ;
114+ }
115+ }
116+
117+ } , 10_000_000 ) ;
118+
119+
55120} ) ;
0 commit comments