Skip to content

Commit

Permalink
Bases fetcher response type on header content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredge committed May 27, 2024
1 parent 4551db3 commit 2c6305e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
Binary file modified output.pdf
Binary file not shown.
Binary file modified output_helper.pdf
Binary file not shown.
Binary file modified output_merged.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class FileForgeClient {
files: File[] | fs.ReadStream[],
request: FileForge.GenerateRequest,
requestOptions?: FileForgeClient.RequestOptions
): Promise<any>{
): Promise<stream.Readable | any>{
const _request = core.newFormData();
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
unrecognizedObjectKeys: "passthrough",
Expand Down
33 changes: 12 additions & 21 deletions src/core/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ async function* readableStreamAsyncIterator(stream: ReadableStream<Uint8Array>)
}
}

function isStringifiedJSON(str: string): boolean {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}


const INITIAL_RETRY_DELAY = 1;
const MAX_RETRY_DELAY = 60;
Expand Down Expand Up @@ -154,23 +145,23 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
body = await response.blob();
} else if (response.body != null && args.responseType === "streaming") {

const chunks: any[] = [];

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

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

if (bufferString.includes("%%EOF")){
body = {"file":buffer};
if (response.headers.get('content-type')!.includes("application/json")){

const chunks: any[] = [];

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

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

}else if (isStringifiedJSON(bufferString)){
body = JSON.parse(bufferString)

}else{
body = bufferString

body = response.body

}

Expand Down
40 changes: 32 additions & 8 deletions tests/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ describe("test", () => {
options: {}
}
);


await writeFile("output.pdf", pdf.file);
const chunks: any[] = [];

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

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

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


Expand All @@ -82,7 +90,6 @@ describe("test", () => {
}
);


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

}, 10_000_000);
Expand Down Expand Up @@ -143,9 +150,18 @@ describe("test", () => {
test:false
}

);
);
const chunks: any[] = [];

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

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


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

it("should generate a PDF url from helper", async () => {
Expand Down Expand Up @@ -197,10 +213,18 @@ describe("test", () => {
{
options: {},
}
);
);

const chunks: any[] = [];

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

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

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

});

0 comments on commit 2c6305e

Please sign in to comment.