From 1c4028a381b397e97e88dfb3564616247312a888 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 18 May 2022 16:16:11 +0200 Subject: [PATCH] fix: add types for `Response.json` (#14655) --- cli/dts/lib.dom.d.ts | 1 + cli/tests/unit/fetch_test.ts | 9 +++++++++ ext/fetch/lib.deno_fetch.d.ts | 1 + 3 files changed, 11 insertions(+) diff --git a/cli/dts/lib.dom.d.ts b/cli/dts/lib.dom.d.ts index c5eafbec80..997e731b5c 100644 --- a/cli/dts/lib.dom.d.ts +++ b/cli/dts/lib.dom.d.ts @@ -11275,6 +11275,7 @@ interface Response extends Body { declare var Response: { prototype: Response; new(body?: BodyInit | null, init?: ResponseInit): Response; + json(data: unknown, init?: ResponseInit): Response; error(): Response; redirect(url: string | URL, status?: number): Response; }; diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index bfd5bc9911..0720a05318 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -1533,3 +1533,12 @@ Deno.test( assertEquals(length, 'Some("4")'); }, ); + +Deno.test(async function staticResponseJson() { + const data = { hello: "world" }; + const resp = Response.json(data); + assertEquals(resp.status, 200); + assertEquals(resp.headers.get("content-type"), "application/json"); + const res = await resp.json(); + assertEquals(res, data); +}); diff --git a/ext/fetch/lib.deno_fetch.d.ts b/ext/fetch/lib.deno_fetch.d.ts index 62f0eab39e..395300291c 100644 --- a/ext/fetch/lib.deno_fetch.d.ts +++ b/ext/fetch/lib.deno_fetch.d.ts @@ -383,6 +383,7 @@ type ResponseType = /** This Fetch API interface represents the response to a request. */ declare class Response implements Body { constructor(body?: BodyInit | null, init?: ResponseInit); + static json(data: unknown, init?: ResponseInit): Response; static error(): Response; static redirect(url: string, status?: number): Response;