+52
-0
lines changedFilter options
+52
-0
lines changed Original file line number Diff line number Diff line change
@@ -196,6 +196,7 @@ export class Response extends BodyMixin {
196
196
197
197
static error(): Response;
198
198
static redirect(url: string, status?: number): Response;
199
+
static json(data: any, init?: ResponseInit): Response;
199
200
}
200
201
201
202
export class FetchError extends Error {
Original file line number Diff line number Diff line change
@@ -93,6 +93,11 @@ async function run() {
93
93
94
94
expectType<Response>(Response.redirect('https://google.com'));
95
95
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
+
}));
96
101
}
97
102
98
103
run().finally(() => {
Original file line number Diff line number Diff line change
@@ -124,6 +124,25 @@ export default class Response extends Body {
124
124
return response;
125
125
}
126
126
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
+
127
146
get [Symbol.toStringTag]() {
128
147
return 'Response';
129
148
}
Original file line number Diff line number Diff line change
@@ -2281,6 +2281,33 @@ describe('node-fetch', () => {
2281
2281
const res = await fetch(url);
2282
2282
expect(res.url).to.equal(`${base}m%C3%B6bius`);
2283
2283
});
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
+
});
2284
2311
});
2285
2312
2286
2313
describe('node-fetch using IPv6', () => {
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4