forked from JakeChampion/fetch
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathResponse.js
73 lines (58 loc) · 1.67 KB
/
Response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Body from "./Body";
import Headers from "./Headers";
class Response {
constructor(body, options = {}) {
this.type = "basic";
this.status = options.status ?? 200;
this.ok = this.status >= 200 && this.status < 300;
this.statusText = options.statusText ?? "";
this.headers = new Headers(options.headers);
this.url = options.url ?? "";
this._body = new Body(body);
if (!this.headers.has("content-type") && this._body._mimeType) {
this.headers.set("content-type", this._body._mimeType);
}
}
get bodyUsed() {
return this._body.bodyUsed;
}
clone() {
return new Response(this._body._bodyInit, {
status: this.status,
statusText: this.statusText,
headers: new Headers(this.headers),
url: this.url,
});
}
blob() {
return this._body.blob();
}
arrayBuffer() {
return this._body.arrayBuffer();
}
text() {
return this._body.text();
}
json() {
return this._body.json();
}
formData() {
return this._body.formData();
}
get body() {
return this._body.body;
}
}
Response.error = () => {
const response = new Response(null, { status: 0, statusText: "" });
response.type = "error";
return response;
};
Response.redirect = (url, status) => {
const redirectStatuses = [301, 302, 303, 307, 308];
if (!redirectStatuses.includes(status)) {
throw new RangeError(`Invalid status code: ${status}`);
}
return new Response(null, { status: status, headers: { location: url } });
};
export default Response;