Skip to content

Commit

Permalink
Adds merge + test case for merging
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredge committed May 26, 2024
1 parent 5be9995 commit a866307
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
Binary file modified output.pdf
Binary file not shown.
Binary file modified output_helper.pdf
Binary file not shown.
Binary file added output_merged.pdf
Binary file not shown.
94 changes: 94 additions & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export declare namespace FileForgeClient {
interface RequestOptions {
timeoutInSeconds?: number;
maxRetries?: number;
abortSignal?: AbortSignal;

}

}
Expand Down Expand Up @@ -97,4 +99,96 @@ export class FileForgeClient {
});
}
}

/**
* @throws {@link Fileforge.BadRequestError}
* @throws {@link Fileforge.UnauthorizedError}
* @throws {@link Fileforge.InternalServerError}
*/
public async merge(
files: File[] | fs.ReadStream[],
request: FileForge.MergeRequest,
requestOptions?: FileForgeClient.RequestOptions
): Promise<any> {
const _request = core.newFormData();
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: false,
allowUnrecognizedEnumValues: false,
breadcrumbsPrefix: [""],
});
await _request.append("options", new Blob([JSON.stringify(options)], { type: "application/json" }));
for (const _file of files) {
await _request.append("files", _file);
}

const _response = await core.fetcher<stream.Readable>({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
"pdf/merge/"
),
method: "POST",
headers: {
Authorization: await core.Supplier.get(this._options.apiKey),
"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.12",
"X-Fern-Runtime": core.RUNTIME.type,
"X-Fern-Runtime-Version": core.RUNTIME.version,
...(await _request.getHeaders()),
},
body: await _request.getBody(),
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);
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,
});
}
}
}
27 changes: 27 additions & 0 deletions tests/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,32 @@ describe("test", () => {
expect(pdf.url).not.toBeNull();
}, 10_000_000);

it("should merge two PDFs", async () => {
const PDF1 = await fs.promises.readFile("./output.pdf");
const PDF2 = await fs.promises.readFile("./output_helper.pdf");

const pdfBlob1= new Blob([PDF1], {
type: "application/pdf",
});
const pdfBlob2 = new Blob([PDF2], {
type: "application/pdf",
});
const file1 = new File([pdfBlob1], "pdf1.pdf", { type: "application/pdf" });
const file2 = new File([pdfBlob2], "pdf2.pdf", { type: "application/pdf" });

const ff = new FileForgeClient({
apiKey: FILEFORGE_API_KEY
});

const pdf = await ff.merge(
[file1, file2],
{
options: {},
}
);

await writeFile("output_merged.pdf", pdf.file);
expect(pdf.file).not.toBeNull();
}, 10_000_000);

});

0 comments on commit a866307

Please sign in to comment.