Skip to content

Commit

Permalink
Adds valid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredge committed May 26, 2024
1 parent 9d217a1 commit e5c9c74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Binary file modified output.pdf
Binary file not shown.
21 changes: 3 additions & 18 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export declare namespace FileForgeClient {
maxRetries?: number;
}

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

export class FileForgeClient {
Expand All @@ -42,7 +38,7 @@ export class FileForgeClient {
files: File[] | fs.ReadStream[],
request: FileForge.GenerateRequest,
requestOptions?: FileForgeClient.RequestOptions
): Promise<FileForgeClient.Response>{
): Promise<stream.Readable>{
const _request = core.newFormData();
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
unrecognizedObjectKeys: "passthrough",
Expand Down Expand Up @@ -76,19 +72,8 @@ export class FileForgeClient {
});

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

for await (let chunk of _response.body) {
chunks.push(chunk);
}

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

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

return _response.body
}

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

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

const chunks: any[] = [];

for await (let chunk of pdf) {
chunks.push(chunk);
}

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

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


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

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

expect(pdf.url).not.toBeNull();
const chunks: any[] = [];

for await (let chunk of pdf) {
chunks.push(chunk);
}

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

expect(JSON.parse(buffer.toString()).url).not.toBeNull();

}, 10_000_000);

Expand Down

0 comments on commit e5c9c74

Please sign in to comment.