Skip to content

Commit a866307

Browse files
committed
Adds merge + test case for merging
1 parent 5be9995 commit a866307

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

output.pdf

0 Bytes
Binary file not shown.

output_helper.pdf

0 Bytes
Binary file not shown.

output_merged.pdf

26.2 KB
Binary file not shown.

src/Client.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export declare namespace FileForgeClient {
2020
interface RequestOptions {
2121
timeoutInSeconds?: number;
2222
maxRetries?: number;
23+
abortSignal?: AbortSignal;
24+
2325
}
2426

2527
}
@@ -97,4 +99,96 @@ export class FileForgeClient {
9799
});
98100
}
99101
}
102+
103+
/**
104+
* @throws {@link Fileforge.BadRequestError}
105+
* @throws {@link Fileforge.UnauthorizedError}
106+
* @throws {@link Fileforge.InternalServerError}
107+
*/
108+
public async merge(
109+
files: File[] | fs.ReadStream[],
110+
request: FileForge.MergeRequest,
111+
requestOptions?: FileForgeClient.RequestOptions
112+
): Promise<any> {
113+
const _request = core.newFormData();
114+
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
115+
unrecognizedObjectKeys: "passthrough",
116+
allowUnrecognizedUnionMembers: false,
117+
allowUnrecognizedEnumValues: false,
118+
breadcrumbsPrefix: [""],
119+
});
120+
await _request.append("options", new Blob([JSON.stringify(options)], { type: "application/json" }));
121+
for (const _file of files) {
122+
await _request.append("files", _file);
123+
}
124+
125+
const _response = await core.fetcher<stream.Readable>({
126+
url: urlJoin(
127+
(await core.Supplier.get(this._options.environment)) ?? environments.FileForgeEnvironment.Default,
128+
"pdf/merge/"
129+
),
130+
method: "POST",
131+
headers: {
132+
Authorization: await core.Supplier.get(this._options.apiKey),
133+
"X-API-Key": await core.Supplier.get(this._options.apiKey),
134+
"X-Fern-Language": "JavaScript",
135+
"X-Fern-SDK-Name": "fileforge",
136+
"X-Fern-SDK-Version": "0.0.12",
137+
"X-Fern-Runtime": core.RUNTIME.type,
138+
"X-Fern-Runtime-Version": core.RUNTIME.version,
139+
...(await _request.getHeaders()),
140+
},
141+
body: await _request.getBody(),
142+
responseType: "streaming",
143+
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
144+
maxRetries: requestOptions?.maxRetries,
145+
});
146+
if (_response.ok) {
147+
return _response.body;
148+
}
149+
150+
if (_response.error.reason === "status-code") {
151+
switch (_response.error.statusCode) {
152+
case 400:
153+
throw new FileForge.BadRequestError(
154+
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
155+
unrecognizedObjectKeys: "passthrough",
156+
allowUnrecognizedUnionMembers: true,
157+
allowUnrecognizedEnumValues: true,
158+
breadcrumbsPrefix: ["response"],
159+
})
160+
);
161+
case 401:
162+
throw new FileForge.UnauthorizedError(
163+
await serializers.ErrorSchema.parseOrThrow(_response.error.body, {
164+
unrecognizedObjectKeys: "passthrough",
165+
allowUnrecognizedUnionMembers: true,
166+
allowUnrecognizedEnumValues: true,
167+
breadcrumbsPrefix: ["response"],
168+
})
169+
);
170+
case 500:
171+
throw new FileForge.InternalServerError(_response.error.body);
172+
default:
173+
throw new errors.FileForgeError({
174+
statusCode: _response.error.statusCode,
175+
body: _response.error.body,
176+
});
177+
}
178+
}
179+
180+
switch (_response.error.reason) {
181+
case "non-json":
182+
throw new errors.FileForgeError({
183+
statusCode: _response.error.statusCode,
184+
body: _response.error.rawBody,
185+
});
186+
case "timeout":
187+
throw new errors.FileForgeTimeoutError();
188+
case "unknown":
189+
throw new errors.FileForgeError({
190+
message: _response.error.errorMessage,
191+
});
192+
}
193+
}
100194
}

tests/custom.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,32 @@ describe("test", () => {
175175
expect(pdf.url).not.toBeNull();
176176
}, 10_000_000);
177177

178+
it("should merge two PDFs", async () => {
179+
const PDF1 = await fs.promises.readFile("./output.pdf");
180+
const PDF2 = await fs.promises.readFile("./output_helper.pdf");
181+
182+
const pdfBlob1= new Blob([PDF1], {
183+
type: "application/pdf",
184+
});
185+
const pdfBlob2 = new Blob([PDF2], {
186+
type: "application/pdf",
187+
});
188+
const file1 = new File([pdfBlob1], "pdf1.pdf", { type: "application/pdf" });
189+
const file2 = new File([pdfBlob2], "pdf2.pdf", { type: "application/pdf" });
190+
191+
const ff = new FileForgeClient({
192+
apiKey: FILEFORGE_API_KEY
193+
});
194+
195+
const pdf = await ff.merge(
196+
[file1, file2],
197+
{
198+
options: {},
199+
}
200+
);
201+
202+
await writeFile("output_merged.pdf", pdf.file);
203+
expect(pdf.file).not.toBeNull();
204+
}, 10_000_000);
178205

179206
});

0 commit comments

Comments
 (0)