@@ -7,15 +7,16 @@ import * as core from "../../../../core";
77import * as fs from "fs" ;
88import * as FileForge from "../../../index" ;
99import * as stream from "stream" ;
10+ import { default as FormData } from "form-data" ;
1011import urlJoin from "url-join" ;
1112import * as errors from "../../../../errors/index" ;
1213import * as serializers from "../../../../serialization/index" ;
1314
1415export declare namespace Pdf {
1516 interface Options {
1617 environment ?: core . Supplier < environments . FileForgeEnvironment | string > ;
17- username : core . Supplier < string > ;
18- password : core . Supplier < string > ;
18+ username ? : core . Supplier < string | undefined > ;
19+ password ? : core . Supplier < string | undefined > ;
1920 apiKey : core . Supplier < string > ;
2021 }
2122
@@ -50,15 +51,14 @@ export class Pdf {
5051 * @throws {@link FileForge.UnauthorizedError }
5152 * @throws {@link FileForge.InternalServerError }
5253 */
53- public async convertsADocOrDocxDocumentToPdf (
54+ public async convertDocx (
5455 file : File | fs . ReadStream ,
55- request : FileForge . PostPdfDocxRequest ,
56+ request : FileForge . PdfConvertDocxRequest ,
5657 requestOptions ?: Pdf . RequestOptions
5758 ) : Promise < stream . Readable > {
58- const _request = new core . FormDataWrapper ( ) ;
59- await _request . append ( "options" , JSON . stringify ( request . options ) ) ;
60- await _request . append ( "file" , file ) ;
61- const _maybeEncodedRequest = _request . getRequest ( ) ;
59+ const _request = new FormData ( ) ;
60+ _request . append ( "options" , JSON . stringify ( request . options ) ) ;
61+ _request . append ( "file" , file ) ;
6262 const _response = await core . fetcher < stream . Readable > ( {
6363 url : urlJoin (
6464 ( await core . Supplier . get ( this . _options . environment ) ) ?? environments . FileForgeEnvironment . Default ,
@@ -70,12 +70,12 @@ export class Pdf {
7070 "X-API-Key" : await core . Supplier . get ( this . _options . apiKey ) ,
7171 "X-Fern-Language" : "JavaScript" ,
7272 "X-Fern-SDK-Name" : "fileforge" ,
73- "X-Fern-SDK-Version" : "0.0.1 " ,
73+ "X-Fern-SDK-Version" : "0.0.3 " ,
7474 "X-Fern-Runtime" : core . RUNTIME . type ,
7575 "X-Fern-Runtime-Version" : core . RUNTIME . version ,
76- ...( await _maybeEncodedRequest . getHeaders ( ) ) ,
7776 } ,
78- body : await _maybeEncodedRequest . getBody ( ) ,
77+ contentType : "multipart/form-data; boundary=" + _request . getBoundary ( ) ,
78+ body : _request ,
7979 responseType : "streaming" ,
8080 timeoutMs : requestOptions ?. timeoutInSeconds != null ? requestOptions . timeoutInSeconds * 1000 : 60000 ,
8181 maxRetries : requestOptions ?. maxRetries ,
@@ -130,22 +130,118 @@ export class Pdf {
130130 }
131131
132132 /**
133+ * Generates a PDF document from web assets.
133134 * @throws {@link FileForge.BadRequestError }
134135 * @throws {@link FileForge.UnauthorizedError }
135136 * @throws {@link FileForge.InternalServerError }
137+ * @throws {@link FileForge.BadGatewayError }
136138 */
137- public async postPdfMerge (
139+ public async generate (
138140 files : File [ ] | fs . ReadStream [ ] ,
139- request : FileForge . PostPdfMergeRequest ,
141+ request : FileForge . PdfGenerateRequest ,
140142 requestOptions ?: Pdf . RequestOptions
141143 ) : Promise < stream . Readable > {
142- const _request = new core . FormDataWrapper ( ) ;
143- await _request . append ( "options" , JSON . stringify ( request . options ) ) ;
144+ const _request = new FormData ( ) ;
145+ _request . append ( "options" , JSON . stringify ( request . options ) ) ;
144146 for ( const _file of files ) {
145- await _request . append ( "files" , _file ) ;
147+ _request . append ( "files" , _file ) ;
148+ }
149+
150+ const _response = await core . fetcher < stream . Readable > ( {
151+ url : urlJoin (
152+ ( await core . Supplier . get ( this . _options . environment ) ) ?? environments . FileForgeEnvironment . Default ,
153+ "pdf/generate/"
154+ ) ,
155+ method : "POST" ,
156+ headers : {
157+ Authorization : await this . _getAuthorizationHeader ( ) ,
158+ "X-API-Key" : await core . Supplier . get ( this . _options . apiKey ) ,
159+ "X-Fern-Language" : "JavaScript" ,
160+ "X-Fern-SDK-Name" : "fileforge" ,
161+ "X-Fern-SDK-Version" : "0.0.3" ,
162+ "X-Fern-Runtime" : core . RUNTIME . type ,
163+ "X-Fern-Runtime-Version" : core . RUNTIME . version ,
164+ } ,
165+ contentType : "multipart/form-data; boundary=" + _request . getBoundary ( ) ,
166+ body : _request ,
167+ responseType : "streaming" ,
168+ timeoutMs : requestOptions ?. timeoutInSeconds != null ? requestOptions . timeoutInSeconds * 1000 : 60000 ,
169+ maxRetries : requestOptions ?. maxRetries ,
170+ } ) ;
171+ if ( _response . ok ) {
172+ return _response . body ;
173+ }
174+
175+ if ( _response . error . reason === "status-code" ) {
176+ switch ( _response . error . statusCode ) {
177+ case 400 :
178+ throw new FileForge . BadRequestError (
179+ await serializers . ErrorSchema . parseOrThrow ( _response . error . body , {
180+ unrecognizedObjectKeys : "passthrough" ,
181+ allowUnrecognizedUnionMembers : true ,
182+ allowUnrecognizedEnumValues : true ,
183+ breadcrumbsPrefix : [ "response" ] ,
184+ } )
185+ ) ;
186+ case 401 :
187+ throw new FileForge . UnauthorizedError (
188+ await serializers . ErrorSchema . parseOrThrow ( _response . error . body , {
189+ unrecognizedObjectKeys : "passthrough" ,
190+ allowUnrecognizedUnionMembers : true ,
191+ allowUnrecognizedEnumValues : true ,
192+ breadcrumbsPrefix : [ "response" ] ,
193+ } )
194+ ) ;
195+ case 500 :
196+ throw new FileForge . InternalServerError ( _response . error . body ) ;
197+ case 502 :
198+ throw new FileForge . BadGatewayError (
199+ await serializers . ErrorSchema . parseOrThrow ( _response . error . body , {
200+ unrecognizedObjectKeys : "passthrough" ,
201+ allowUnrecognizedUnionMembers : true ,
202+ allowUnrecognizedEnumValues : true ,
203+ breadcrumbsPrefix : [ "response" ] ,
204+ } )
205+ ) ;
206+ default :
207+ throw new errors . FileForgeError ( {
208+ statusCode : _response . error . statusCode ,
209+ body : _response . error . body ,
210+ } ) ;
211+ }
212+ }
213+
214+ switch ( _response . error . reason ) {
215+ case "non-json" :
216+ throw new errors . FileForgeError ( {
217+ statusCode : _response . error . statusCode ,
218+ body : _response . error . rawBody ,
219+ } ) ;
220+ case "timeout" :
221+ throw new errors . FileForgeTimeoutError ( ) ;
222+ case "unknown" :
223+ throw new errors . FileForgeError ( {
224+ message : _response . error . errorMessage ,
225+ } ) ;
226+ }
227+ }
228+
229+ /**
230+ * @throws {@link FileForge.BadRequestError }
231+ * @throws {@link FileForge.UnauthorizedError }
232+ * @throws {@link FileForge.InternalServerError }
233+ */
234+ public async merge (
235+ files : File [ ] | fs . ReadStream [ ] ,
236+ request : FileForge . PdfMergeRequest ,
237+ requestOptions ?: Pdf . RequestOptions
238+ ) : Promise < stream . Readable > {
239+ const _request = new FormData ( ) ;
240+ _request . append ( "options" , JSON . stringify ( request . options ) ) ;
241+ for ( const _file of files ) {
242+ _request . append ( "files" , _file ) ;
146243 }
147244
148- const _maybeEncodedRequest = _request . getRequest ( ) ;
149245 const _response = await core . fetcher < stream . Readable > ( {
150246 url : urlJoin (
151247 ( await core . Supplier . get ( this . _options . environment ) ) ?? environments . FileForgeEnvironment . Default ,
@@ -157,12 +253,12 @@ export class Pdf {
157253 "X-API-Key" : await core . Supplier . get ( this . _options . apiKey ) ,
158254 "X-Fern-Language" : "JavaScript" ,
159255 "X-Fern-SDK-Name" : "fileforge" ,
160- "X-Fern-SDK-Version" : "0.0.1 " ,
256+ "X-Fern-SDK-Version" : "0.0.3 " ,
161257 "X-Fern-Runtime" : core . RUNTIME . type ,
162258 "X-Fern-Runtime-Version" : core . RUNTIME . version ,
163- ...( await _maybeEncodedRequest . getHeaders ( ) ) ,
164259 } ,
165- body : await _maybeEncodedRequest . getBody ( ) ,
260+ contentType : "multipart/form-data; boundary=" + _request . getBoundary ( ) ,
261+ body : _request ,
166262 responseType : "streaming" ,
167263 timeoutMs : requestOptions ?. timeoutInSeconds != null ? requestOptions . timeoutInSeconds * 1000 : 60000 ,
168264 maxRetries : requestOptions ?. maxRetries ,
@@ -217,9 +313,15 @@ export class Pdf {
217313 }
218314
219315 protected async _getAuthorizationHeader ( ) : Promise < string | undefined > {
220- return core . BasicAuth . toAuthorizationHeader ( {
221- username : await core . Supplier . get ( this . _options . username ) ,
222- password : await core . Supplier . get ( this . _options . password ) ,
223- } ) ;
316+ const username = await core . Supplier . get ( this . _options . username ) ;
317+ const password = await core . Supplier . get ( this . _options . password ) ;
318+ if ( username != null && password != null ) {
319+ return core . BasicAuth . toAuthorizationHeader ( {
320+ username : username ,
321+ password : password ,
322+ } ) ;
323+ }
324+
325+ return undefined ;
224326 }
225327}
0 commit comments