Skip to content

Commit

Permalink
Changes the generate function to return a Promise<FileForgeClient.Res…
Browse files Browse the repository at this point in the history
…ponse>
  • Loading branch information
pierredge committed May 22, 2024
1 parent c26b40f commit d90ad99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Binary file modified output.pdf
Binary file not shown.
16 changes: 13 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export declare namespace FileForgeClient {
timeoutInSeconds?: number;
maxRetries?: number;
}

interface Response{
file?: string;
url?:string;
}
}

export class FileForgeClient {
Expand All @@ -37,7 +42,7 @@ export class FileForgeClient {
files: File[] | fs.ReadStream[],
request: FileForge.GenerateRequest,
requestOptions?: FileForgeClient.RequestOptions
): Promise<Buffer>{
): Promise<FileForgeClient.Response>{
const _request = core.newFormData();
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
unrecognizedObjectKeys: "passthrough",
Expand Down Expand Up @@ -69,6 +74,7 @@ export class FileForgeClient {
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
});

if (_response.ok) {
const chunks: any[] = [];

Expand All @@ -77,8 +83,12 @@ export class FileForgeClient {
}

const buffer: Buffer = Buffer.concat(chunks);

return buffer;

if (request.options?.host !== true){
return {"file": buffer.toString()};
}else{
return JSON.parse(buffer.toString())
}
}

if (_response.error.reason === "status-code") {
Expand Down
19 changes: 5 additions & 14 deletions tests/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ describe("test", () => {
apiKey: FILEFORGE_API_KEY
});

const pdf = await ff.generate(
const pdf:FileForgeClient.Response = await ff.generate(
[htmlFile, cssFile],
{
options: {}
}
);

await writeFile("output.pdf", pdf);
await writeFile("output.pdf", pdf.file!);
}, 10_000_000);


Expand All @@ -71,7 +71,7 @@ describe("test", () => {
apiKey: FILEFORGE_API_KEY
});

const pdf:Buffer = await ff.generate(
const pdf:FileForgeClient.Response = await ff.generate(
[htmlFile, cssFile],
{
options: {
Expand All @@ -80,13 +80,7 @@ describe("test", () => {
}
);

const chunks: any[] = [];
for await (let chunk of pdf) {
chunks.push(chunk);
}
const result = JSON.parse(pdf.toString());

expect(result.url).not.toBeNull();
expect(pdf.url).not.toBeNull();

}, 10_000_000);

Expand All @@ -104,7 +98,7 @@ describe("test", () => {
apiKey: "blabla_invalid_key"
});
try {
const pdf:Buffer = await ff.generate(
const pdf = await ff.generate(
[htmlFile, cssFile],
{
options: {
Expand All @@ -116,10 +110,7 @@ describe("test", () => {
}catch(e){
expect(e).not.toBeNull();
if (e instanceof error.FileForgeError) {
console.log(`Error status code: ${e.statusCode}`);
console.log(`Error body: ${e.body}`);
expect(e.statusCode).toBe(401);

}
}

Expand Down

0 comments on commit d90ad99

Please sign in to comment.