Skip to content

Commit 2c6305e

Browse files
committed
Bases fetcher response type on header content-type
1 parent 4551db3 commit 2c6305e

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

output.pdf

12.1 KB
Binary file not shown.

output_helper.pdf

8.68 KB
Binary file not shown.

output_merged.pdf

20.4 KB
Binary file not shown.

src/Client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class FileForgeClient {
4040
files: File[] | fs.ReadStream[],
4141
request: FileForge.GenerateRequest,
4242
requestOptions?: FileForgeClient.RequestOptions
43-
): Promise<any>{
43+
): Promise<stream.Readable | any>{
4444
const _request = core.newFormData();
4545
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
4646
unrecognizedObjectKeys: "passthrough",

src/core/fetcher/Fetcher.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ async function* readableStreamAsyncIterator(stream: ReadableStream<Uint8Array>)
5555
}
5656
}
5757

58-
function isStringifiedJSON(str: string): boolean {
59-
try {
60-
JSON.parse(str);
61-
return true;
62-
} catch (e) {
63-
return false;
64-
}
65-
}
66-
6758

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

157-
const chunks: any[] = [];
158-
159-
for await (let chunk of readableStreamAsyncIterator(response.body)) {
160-
chunks.push(chunk);
161-
}
162-
163-
const buffer: Buffer = Buffer.concat(chunks);
164-
const bufferString = buffer.toString();
165148

166-
if (bufferString.includes("%%EOF")){
167-
body = {"file":buffer};
149+
if (response.headers.get('content-type')!.includes("application/json")){
150+
151+
const chunks: any[] = [];
152+
153+
for await (let chunk of readableStreamAsyncIterator(response.body)) {
154+
chunks.push(chunk);
155+
}
156+
157+
const buffer: Buffer = Buffer.concat(chunks);
158+
const bufferString = buffer.toString();
168159

169-
}else if (isStringifiedJSON(bufferString)){
170160
body = JSON.parse(bufferString)
171161

172162
}else{
173-
body = bufferString
163+
164+
body = response.body
174165

175166
}
176167

tests/custom.test.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ describe("test", () => {
5353
options: {}
5454
}
5555
);
56-
5756

58-
await writeFile("output.pdf", pdf.file);
57+
const chunks: any[] = [];
58+
59+
for await (let chunk of pdf) {
60+
chunks.push(chunk);
61+
}
62+
63+
const buffer: Buffer = Buffer.concat(chunks);
64+
const bufferString = buffer.toString()
65+
66+
await writeFile("output.pdf", bufferString);
5967
}, 10_000_000);
6068

6169

@@ -82,7 +90,6 @@ describe("test", () => {
8290
}
8391
);
8492

85-
8693
expect(pdf.url).not.toBeNull();
8794

8895
}, 10_000_000);
@@ -143,9 +150,18 @@ describe("test", () => {
143150
test:false
144151
}
145152

146-
);
153+
);
154+
const chunks: any[] = [];
155+
156+
for await (let chunk of pdf) {
157+
chunks.push(chunk);
158+
}
159+
160+
const buffer: Buffer = Buffer.concat(chunks);
161+
const bufferString = buffer.toString()
162+
147163

148-
await writeFile("output_helper.pdf", pdf.file!);
164+
await writeFile("output_helper.pdf", bufferString);
149165
}, 10_000_000);
150166

151167
it("should generate a PDF url from helper", async () => {
@@ -197,10 +213,18 @@ describe("test", () => {
197213
{
198214
options: {},
199215
}
200-
);
216+
);
217+
218+
const chunks: any[] = [];
219+
220+
for await (let chunk of pdf) {
221+
chunks.push(chunk);
222+
}
223+
224+
const buffer: Buffer = Buffer.concat(chunks);
225+
const bufferString = buffer.toString()
201226

202-
await writeFile("output_merged.pdf", pdf.file);
203-
expect(pdf.file).not.toBeNull();
227+
await writeFile("output_merged.pdf", bufferString);
204228
}, 10_000_000);
205229

206230
});

0 commit comments

Comments
 (0)