Skip to content

Commit

Permalink
Release 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed May 14, 2024
1 parent ea01ce4 commit 73f2d14
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 99 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fileforge",
"version": "0.0.1",
"version": "0.0.3",
"private": false,
"repository": "https://github.com/OnedocLabs/fileforge-node-sdk",
"license": "MIT",
Expand All @@ -15,10 +15,8 @@
"dependencies": {
"url-join": "4.0.1",
"form-data": "4.0.0",
"formdata-node": "^6.0.3",
"node-fetch": "2.7.0",
"qs": "6.11.2",
"form-data-encoder": "^4.0.2",
"js-base64": "3.7.2"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion src/api/client/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/api/client/requests/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./resources";
export * from "./types";
export * from "./errors";
export * from "./client";
150 changes: 126 additions & 24 deletions src/api/resources/pdf/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import * as core from "../../../../core";
import * as fs from "fs";
import * as FileForge from "../../../index";
import * as stream from "stream";
import { default as FormData } from "form-data";
import urlJoin from "url-join";
import * as errors from "../../../../errors/index";
import * as serializers from "../../../../serialization/index";

export declare namespace Pdf {
interface Options {
environment?: core.Supplier<environments.FileForgeEnvironment | string>;
username: core.Supplier<string>;
password: core.Supplier<string>;
username?: core.Supplier<string | undefined>;
password?: core.Supplier<string | undefined>;
apiKey: core.Supplier<string>;
}

Expand Down Expand Up @@ -50,15 +51,14 @@ export class Pdf {
* @throws {@link FileForge.UnauthorizedError}
* @throws {@link FileForge.InternalServerError}
*/
public async convertsADocOrDocxDocumentToPdf(
public async convertDocx(
file: File | fs.ReadStream,
request: FileForge.PostPdfDocxRequest,
request: FileForge.PdfConvertDocxRequest,
requestOptions?: Pdf.RequestOptions
): Promise<stream.Readable> {
const _request = new core.FormDataWrapper();
await _request.append("options", JSON.stringify(request.options));
await _request.append("file", file);
const _maybeEncodedRequest = _request.getRequest();
const _request = new FormData();
_request.append("options", JSON.stringify(request.options));
_request.append("file", file);
const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
Expand All @@ -70,12 +70,12 @@ export class Pdf {
"X-API-Key": await core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "fileforge",
"X-Fern-SDK-Version": "0.0.1",
"X-Fern-SDK-Version": "0.0.3",
"X-Fern-Runtime": core.RUNTIME.type,
"X-Fern-Runtime-Version": core.RUNTIME.version,
...(await _maybeEncodedRequest.getHeaders()),
},
body: await _maybeEncodedRequest.getBody(),
contentType: "multipart/form-data; boundary=" + _request.getBoundary(),
body: _request,
responseType: "streaming",
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
Expand Down Expand Up @@ -130,22 +130,118 @@ export class Pdf {
}

/**
* Generates a PDF document from web assets.
* @throws {@link FileForge.BadRequestError}
* @throws {@link FileForge.UnauthorizedError}
* @throws {@link FileForge.InternalServerError}
* @throws {@link FileForge.BadGatewayError}
*/
public async postPdfMerge(
public async generate(
files: File[] | fs.ReadStream[],
request: FileForge.PostPdfMergeRequest,
request: FileForge.PdfGenerateRequest,
requestOptions?: Pdf.RequestOptions
): Promise<stream.Readable> {
const _request = new core.FormDataWrapper();
await _request.append("options", JSON.stringify(request.options));
const _request = new FormData();
_request.append("options", JSON.stringify(request.options));
for (const _file of files) {
await _request.append("files", _file);
_request.append("files", _file);
}

const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
"pdf/generate/"
),
method: "POST",
headers: {
Authorization: await this._getAuthorizationHeader(),
"X-API-Key": await core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "fileforge",
"X-Fern-SDK-Version": "0.0.3",
"X-Fern-Runtime": core.RUNTIME.type,
"X-Fern-Runtime-Version": core.RUNTIME.version,
},
contentType: "multipart/form-data; boundary=" + _request.getBoundary(),
body: _request,
responseType: "streaming",
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
});
if (_response.ok) {
return _response.body;
}

if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new FileForge.BadRequestError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 401:
throw new FileForge.UnauthorizedError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
case 500:
throw new FileForge.InternalServerError(_response.error.body);
case 502:
throw new FileForge.BadGatewayError(
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
})
);
default:
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}

switch (_response.error.reason) {
case "non-json":
throw new errors.FileForgeError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.FileForgeTimeoutError();
case "unknown":
throw new errors.FileForgeError({
message: _response.error.errorMessage,
});
}
}

/**
* @throws {@link FileForge.BadRequestError}
* @throws {@link FileForge.UnauthorizedError}
* @throws {@link FileForge.InternalServerError}
*/
public async merge(
files: File[] | fs.ReadStream[],
request: FileForge.PdfMergeRequest,
requestOptions?: Pdf.RequestOptions
): Promise<stream.Readable> {
const _request = new FormData();
_request.append("options", JSON.stringify(request.options));
for (const _file of files) {
_request.append("files", _file);
}

const _maybeEncodedRequest = _request.getRequest();
const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
Expand All @@ -157,12 +253,12 @@ export class Pdf {
"X-API-Key": await core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "fileforge",
"X-Fern-SDK-Version": "0.0.1",
"X-Fern-SDK-Version": "0.0.3",
"X-Fern-Runtime": core.RUNTIME.type,
"X-Fern-Runtime-Version": core.RUNTIME.version,
...(await _maybeEncodedRequest.getHeaders()),
},
body: await _maybeEncodedRequest.getBody(),
contentType: "multipart/form-data; boundary=" + _request.getBoundary(),
body: _request,
responseType: "streaming",
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
Expand Down Expand Up @@ -217,9 +313,15 @@ export class Pdf {
}

protected async _getAuthorizationHeader(): Promise<string | undefined> {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
password: await core.Supplier.get(this._options.password),
});
const username = await core.Supplier.get(this._options.username);
const password = await core.Supplier.get(this._options.password);
if (username != null && password != null) {
return core.BasicAuth.toAuthorizationHeader({
username: username,
password: password,
});
}

return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as FileForge from "../../../../index";
* @example
* {}
*/
export interface PostPdfDocxRequest {
export interface PdfConvertDocxRequest {
/**
* Conversion options. This field is required even if empty.
*
Expand All @@ -34,5 +34,5 @@ export interface PostPdfDocxRequest {
*
* There will not be an error if a variable is not found in the document, nor if variables found in the document are not in the options.
*/
options: FileForge.PostPdfDocxRequestOptions;
options: FileForge.PdfConvertDocxRequestOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* This file was auto-generated by Fern from our API Definition.
*/

import * as FileForge from "../../index";
import * as FileForge from "../../../../index";

/**
* @example
* {}
*/
export interface GenerateRequest {
export interface PdfGenerateRequest {
/** Conversion options. This field is required even if empty. */
options: FileForge.GenerateRequestOptions;
options: FileForge.PdfGenerateRequestOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import * as FileForge from "../../../../index";
* @example
* {}
*/
export interface PostPdfMergeRequest {
options: FileForge.PostPdfMergeRequestOptions;
export interface PdfMergeRequest {
options: FileForge.PdfMergeRequestOptions;
}
5 changes: 3 additions & 2 deletions src/api/resources/pdf/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { type PostPdfDocxRequest } from "./PostPdfDocxRequest";
export { type PostPdfMergeRequest } from "./PostPdfMergeRequest";
export { type PdfConvertDocxRequest } from "./PdfConvertDocxRequest";
export { type PdfGenerateRequest } from "./PdfGenerateRequest";
export { type PdfMergeRequest } from "./PdfMergeRequest";
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* There will not be an error if a variable is not found in the document, nor if variables found in the document are not in the options.
*/
export interface PostPdfDocxRequestOptions {
export interface PdfConvertDocxRequestOptions {
/** Map of template literals to replace in the document. */
templateLiterals?: Record<string, string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Conversion options. This field is required even if empty.
*/
export interface GenerateRequestOptions {
export interface PdfGenerateRequestOptions {
/** Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true. */
test?: boolean;
/** If enabled, the document will be hosted by FileForge and a presigned URL will be returned. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* This file was auto-generated by Fern from our API Definition.
*/

export interface PostPdfMergeRequestOptions {}
export interface PdfMergeRequestOptions {}
5 changes: 3 additions & 2 deletions src/api/resources/pdf/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./PostPdfDocxRequestOptions";
export * from "./PostPdfMergeRequestOptions";
export * from "./PdfConvertDocxRequestOptions";
export * from "./PdfGenerateRequestOptions";
export * from "./PdfMergeRequestOptions";
1 change: 0 additions & 1 deletion src/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./GenerateRequestOptions";
export * from "./ErrorSchema";
28 changes: 7 additions & 21 deletions src/core/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { default as FormData } from "form-data";
import qs from "qs";
import { RUNTIME } from "../runtime";
import { APIResponse } from "./APIResponse";
Expand Down Expand Up @@ -66,28 +67,13 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
: args.url;

let body: BodyInit | undefined = undefined;
const maybeStringifyBody = (body: any) => {
if (body instanceof Uint8Array) {
return body;
} else {
return JSON.stringify(body);
}
};

if (RUNTIME.type === "node") {
if (args.body instanceof (await import("formdata-node")).FormData) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
if (args.body instanceof FormData) {
// @ts-expect-error
body = args.body;
} else if (args.body instanceof Uint8Array) {
body = args.body;
} else {
if (args.body instanceof (await import("form-data")).default) {
// @ts-expect-error
body = args.body;
} else {
body = maybeStringifyBody(args.body);
}
body = JSON.stringify(args.body);
}

// In Node.js environments, the SDK always uses`node-fetch`.
Expand Down
1 change: 0 additions & 1 deletion src/core/form-data-utils/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./fetcher";
export * from "./form-data-utils";
export * from "./runtime";
export * from "./auth";
export * as serialization from "./schemas";
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import * as serializers from "../../../index";
import * as FileForge from "../../../../api/index";
import * as core from "../../../../core";

export const PostPdfDocxRequestOptions: core.serialization.ObjectSchema<
serializers.PostPdfDocxRequestOptions.Raw,
FileForge.PostPdfDocxRequestOptions
export const PdfConvertDocxRequestOptions: core.serialization.ObjectSchema<
serializers.PdfConvertDocxRequestOptions.Raw,
FileForge.PdfConvertDocxRequestOptions
> = core.serialization.object({
templateLiterals: core.serialization.record(core.serialization.string(), core.serialization.string()).optional(),
});

export declare namespace PostPdfDocxRequestOptions {
export declare namespace PdfConvertDocxRequestOptions {
interface Raw {
templateLiterals?: Record<string, string> | null;
}
Expand Down
Loading

0 comments on commit 73f2d14

Please sign in to comment.