File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -196,6 +196,7 @@ export class Response extends BodyMixin {
196196
197197 static error ( ) : Response ;
198198 static redirect ( url : string , status ?: number ) : Response ;
199+ static json ( data : any , init ?: ResponseInit ) : Response ;
199200}
200201
201202export class FetchError extends Error {
Original file line number Diff line number Diff line change @@ -93,6 +93,11 @@ async function run() {
9393
9494 expectType < Response > ( Response . redirect ( 'https://google.com' ) ) ;
9595 expectType < Response > ( Response . redirect ( 'https://google.com' , 301 ) ) ;
96+
97+ expectType < Response > ( Response . json ( { foo : 'bar' } ) ) ;
98+ expectType < Response > ( Response . json ( { foo : 'bar' } , {
99+ status : 301
100+ } ) ) ;
96101}
97102
98103run ( ) . finally ( ( ) => {
Original file line number Diff line number Diff line change @@ -124,6 +124,25 @@ export default class Response extends Body {
124124 return response ;
125125 }
126126
127+ static json ( data = undefined , init = { } ) {
128+ const body = JSON . stringify ( data ) ;
129+
130+ if ( body === undefined ) {
131+ throw new TypeError ( 'data is not JSON serializable' ) ;
132+ }
133+
134+ const headers = new Headers ( init && init . headers ) ;
135+
136+ if ( ! headers . has ( 'content-type' ) ) {
137+ headers . set ( 'content-type' , 'application/json' ) ;
138+ }
139+
140+ return new Response ( body , {
141+ ...init ,
142+ headers
143+ } ) ;
144+ }
145+
127146 get [ Symbol . toStringTag ] ( ) {
128147 return 'Response' ;
129148 }
Original file line number Diff line number Diff line change @@ -2281,6 +2281,33 @@ describe('node-fetch', () => {
22812281 const res = await fetch ( url ) ;
22822282 expect ( res . url ) . to . equal ( `${ base } m%C3%B6bius` ) ;
22832283 } ) ;
2284+
2285+ it ( 'static Response.json should work' , async ( ) => {
2286+ const response = Response . json ( { foo : 'bar' } ) ;
2287+ expect ( response . status ) . to . equal ( 200 ) ;
2288+ expect ( response . headers . get ( 'content-type' ) ) . to . equal ( 'application/json' ) ;
2289+ expect ( await response . text ( ) ) . to . equal ( JSON . stringify ( { foo : 'bar' } ) ) ;
2290+
2291+ const response1 = Response . json ( null , {
2292+ status : 301 ,
2293+ statusText : 'node-fetch' ,
2294+ headers : {
2295+ 'Content-Type' : 'text/plain'
2296+ }
2297+ } ) ;
2298+
2299+ expect ( response1 . headers . get ( 'content-type' ) ) . to . equal ( 'text/plain' ) ;
2300+ expect ( response1 . status ) . to . equal ( 301 ) ;
2301+ expect ( response1 . statusText ) . to . equal ( 'node-fetch' ) ;
2302+
2303+ const response2 = Response . json ( null , {
2304+ headers : {
2305+ 'CoNtEnT-TypE' : 'text/plain'
2306+ }
2307+ } ) ;
2308+
2309+ expect ( response2 . headers . get ( 'content-type' ) ) . to . equal ( 'text/plain' ) ;
2310+ } ) ;
22842311} ) ;
22852312
22862313describe ( 'node-fetch using IPv6' , ( ) => {
You can’t perform that action at this time.
0 commit comments