From 24782347e828569c0e4290ce068b47fa4882f9d3 Mon Sep 17 00:00:00 2001 From: Fabian Cook Date: Sat, 29 Jan 2022 12:24:05 +1300 Subject: [PATCH 1/3] Response.json as per https://github.com/whatwg/fetch/issues/1389 --- src/response.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/response.ts b/src/response.ts index 9249e77..4ea3117 100644 --- a/src/response.ts +++ b/src/response.ts @@ -78,6 +78,17 @@ class Response extends Body { return response; } + static json(body: unknown, init?: ResponseInit) { + const headers = new Headers(init?.headers); + if (!headers.has("Content-Type")) { + headers.set("Content-Type", "application/json"); + } + return new Response(JSON.stringify(body), { + ...init, + headers + }); + } + } export { Response }; From 15c05d49b71d3a2be706a173dccecd3d3259496f Mon Sep 17 00:00:00 2001 From: Fabian Cook Date: Sat, 29 Jan 2022 23:06:53 +1300 Subject: [PATCH 2/3] Include arrayBuffer, formData, blob, text, as per https://github.com/whatwg/fetch/issues/1389\#issuecomment-1024842588 --- src/response.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/response.ts b/src/response.ts index 4ea3117..702c0eb 100644 --- a/src/response.ts +++ b/src/response.ts @@ -1,5 +1,6 @@ import { Body, BodyInit } from "./body"; import { Headers, HeadersInit } from "./headers"; +import * as Buffer from "buffer"; export type ResponseInit = Response | { status?: number; @@ -89,6 +90,50 @@ class Response extends Body { }); } + static blob(body: Blob, init?: ResponseInit) { + const headers = new Headers(init?.headers); + if (!headers.has("Content-Type")) { + headers.set("Content-Type", body.type ?? "application/octet-stream"); + } + return new Response(body, { + ...init, + headers + }); + } + + static arrayBuffer(body: ArrayBuffer | Buffer, init?: ResponseInit) { + const headers = new Headers(init?.headers); + if (!headers.has("Content-Type")) { + headers.set("Content-Type", "application/octet-stream"); + } + return new Response(body, { + ...init, + headers + }); + } + + static text(body: string, init?: ResponseInit) { + const headers = new Headers(init?.headers); + if (!headers.has("Content-Type")) { + headers.set("Content-Type", "text/plain"); + } + return new Response(body, { + ...init, + headers + }); + } + + static formData(body: FormData, init?: ResponseInit) { + const headers = new Headers(init?.headers); + if (!headers.has("Content-Type")) { + headers.set("Content-Type", "multipart/form-data"); + } + return new Response(body, { + ...init, + headers + }); + } + } export { Response }; From 00b1fb3906741e28b450a293cd7b5ff177607acd Mon Sep 17 00:00:00 2001 From: Fabian Cook Date: Sat, 29 Jan 2022 23:08:14 +1300 Subject: [PATCH 3/3] remove buffer reference --- src/response.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/response.ts b/src/response.ts index 702c0eb..f7f1356 100644 --- a/src/response.ts +++ b/src/response.ts @@ -1,6 +1,5 @@ import { Body, BodyInit } from "./body"; import { Headers, HeadersInit } from "./headers"; -import * as Buffer from "buffer"; export type ResponseInit = Response | { status?: number;