Skip to content

Commit

Permalink
feat: support response objects in InputFile (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf authored Jun 25, 2023
1 parent ecf1abe commit 29d6a88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/types.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class InputFile {
| string
| Blob
| Deno.FsFile
| Response
| URL
| URLLike
| Uint8Array
Expand Down Expand Up @@ -118,6 +119,11 @@ export class InputFile {
}
if (data instanceof Blob) return data.stream();
if (isDenoFile(data)) return iterateReader(data);
// Handle Response objects
if (data instanceof Response) {
if (data.body === null) throw new Error(`No response body!`);
return data.body;
}
// Handle URL and URLLike objects
if (data instanceof URL) return fetchFile(data);
if ("url" in data) return fetchFile(data.url);
Expand Down
12 changes: 12 additions & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ Deno.test({
},
});

Deno.test({
name: "convert Response to raw",
async fn() {
const bytes = new Uint8Array([65, 66, 67]);
const file0 = new InputFile(new Response(bytes));
const data0 = await file0.toRaw();
if (data0 instanceof Uint8Array) throw new Error("no itr");
const values0 = await readAll(readerFromIterable(data0));
assertEquals(values0, bytes);
},
});

Deno.test({
name: "convert supplier function to raw",
async fn() {
Expand Down

0 comments on commit 29d6a88

Please sign in to comment.