Skip to content

Commit d90ad99

Browse files
committed
Changes the generate function to return a Promise<FileForgeClient.Response>
1 parent c26b40f commit d90ad99

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

output.pdf

12.1 KB
Binary file not shown.

src/Client.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export declare namespace FileForgeClient {
2121
timeoutInSeconds?: number;
2222
maxRetries?: number;
2323
}
24+
25+
interface Response{
26+
file?: string;
27+
url?:string;
28+
}
2429
}
2530

2631
export class FileForgeClient {
@@ -37,7 +42,7 @@ export class FileForgeClient {
3742
files: File[] | fs.ReadStream[],
3843
request: FileForge.GenerateRequest,
3944
requestOptions?: FileForgeClient.RequestOptions
40-
): Promise<Buffer>{
45+
): Promise<FileForgeClient.Response>{
4146
const _request = core.newFormData();
4247
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
4348
unrecognizedObjectKeys: "passthrough",
@@ -69,6 +74,7 @@ export class FileForgeClient {
6974
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
7075
maxRetries: requestOptions?.maxRetries,
7176
});
77+
7278
if (_response.ok) {
7379
const chunks: any[] = [];
7480

@@ -77,8 +83,12 @@ export class FileForgeClient {
7783
}
7884

7985
const buffer: Buffer = Buffer.concat(chunks);
80-
81-
return buffer;
86+
87+
if (request.options?.host !== true){
88+
return {"file": buffer.toString()};
89+
}else{
90+
return JSON.parse(buffer.toString())
91+
}
8292
}
8393

8494
if (_response.error.reason === "status-code") {

tests/custom.test.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ describe("test", () => {
4646
apiKey: FILEFORGE_API_KEY
4747
});
4848

49-
const pdf = await ff.generate(
49+
const pdf:FileForgeClient.Response = await ff.generate(
5050
[htmlFile, cssFile],
5151
{
5252
options: {}
5353
}
5454
);
5555

56-
await writeFile("output.pdf", pdf);
56+
await writeFile("output.pdf", pdf.file!);
5757
}, 10_000_000);
5858

5959

@@ -71,7 +71,7 @@ describe("test", () => {
7171
apiKey: FILEFORGE_API_KEY
7272
});
7373

74-
const pdf:Buffer = await ff.generate(
74+
const pdf:FileForgeClient.Response = await ff.generate(
7575
[htmlFile, cssFile],
7676
{
7777
options: {
@@ -80,13 +80,7 @@ describe("test", () => {
8080
}
8181
);
8282

83-
const chunks: any[] = [];
84-
for await (let chunk of pdf) {
85-
chunks.push(chunk);
86-
}
87-
const result = JSON.parse(pdf.toString());
88-
89-
expect(result.url).not.toBeNull();
83+
expect(pdf.url).not.toBeNull();
9084

9185
}, 10_000_000);
9286

@@ -104,7 +98,7 @@ describe("test", () => {
10498
apiKey: "blabla_invalid_key"
10599
});
106100
try {
107-
const pdf:Buffer = await ff.generate(
101+
const pdf = await ff.generate(
108102
[htmlFile, cssFile],
109103
{
110104
options: {
@@ -116,10 +110,7 @@ describe("test", () => {
116110
}catch(e){
117111
expect(e).not.toBeNull();
118112
if (e instanceof error.FileForgeError) {
119-
console.log(`Error status code: ${e.statusCode}`);
120-
console.log(`Error body: ${e.body}`);
121113
expect(e.statusCode).toBe(401);
122-
123114
}
124115
}
125116

0 commit comments

Comments
 (0)