-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.js
More file actions
21 lines (18 loc) · 698 Bytes
/
response.js
File metadata and controls
21 lines (18 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { polyfillMethod } from './utils.js';
polyfillMethod(Response, 'json', (data, { status = 200, statusText = '', headers = new Headers() } = {}) => {
if (! (headers instanceof Headers)) {
return Response.json(data, { status, statusText, headers: new Headers(headers) });
} else {
headers.set('Content-Type', 'application/json');
return new Response(JSON.stringify(data), { status, statusText, headers });
}
});
polyfillMethod(Response, 'redirect', (url, status = 302) => {
return new Response(null, {
status,
headers: new Headers({ Location: url }),
});
});
polyfillMethod(Response.prototype, 'bytes', async function() {
return new Uint8Array(await this.arrayBuffer());
});