Skip to content

Commit 55a4870

Browse files
authored
feat: add static Response.json (node-fetch#1670)
* feat: add static Response.json * fix: set content-type if it doesn't exist properly
1 parent c071406 commit 55a4870

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

@types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

201202
export class FetchError extends Error {

@types/index.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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

98103
run().finally(() => {

src/response.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

test/main.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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

22862313
describe('node-fetch using IPv6', () => {

0 commit comments

Comments
 (0)