forked from JakeChampion/fetch
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRequest.js
More file actions
119 lines (90 loc) · 3.03 KB
/
Request.js
File metadata and controls
119 lines (90 loc) · 3.03 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Body from "./Body";
import Headers from "./Headers";
class Request {
credentials = "same-origin";
method = "GET";
constructor(input, options = {}) {
this.url = input;
if (input instanceof Request) {
if (input._body.bodyUsed) {
throw new TypeError("Already read");
}
this.__handleRequestInput(input, options);
}
this._body = this._body ?? new Body(options.body);
this.method = options.method ?? this.method;
this.method = this.method.toUpperCase();
if (this._body._bodyInit && ["GET", "HEAD"].includes(this.method)) {
throw new TypeError("Body not allowed for GET or HEAD requests");
}
if (this._body._bodyReadableStream) {
throw new TypeError("Streaming request bodies is not supported");
}
this.credentials = options.credentials ?? this.credentials;
this.headers = this.headers ?? new Headers(options.headers);
this.signal = options.signal ?? this.signal;
if (!this.headers.has("content-type") && this._body._mimeType) {
this.headers.set("content-type", this._body._mimeType);
}
this.__handleCacheOption(options.cache);
}
__handleRequestInput(request, options) {
this.url = request.url;
this.credentials = request.credentials;
this.method = request.method;
this.signal = request.signal;
this.headers = new Headers(options.headers ?? request.headers);
if (options.body === undefined && request._body._bodyInit) {
this._body = new Body(request._body._bodyInit);
request._body.bodyUsed = true;
}
}
__handleCacheOption(cache) {
if (!["GET", "HEAD"].includes(this.method)) {
return;
}
if (!["no-store", "no-cache"].includes(cache)) {
return;
}
const currentTime = Date.now();
// Search for a '_' parameter in the query string
const querySearchRegExp = /([?&])_=[^&]*/;
if (querySearchRegExp.test(this.url)) {
this.url = this.url.replace(
querySearchRegExp,
`$1_=${currentTime}`
);
return;
}
const hasQueryRegExp = /\?/;
const querySeparator = hasQueryRegExp.test(this.url) ? "&" : "?";
this.url += `${querySeparator}_=${currentTime}`;
}
get bodyUsed() {
return this._body.bodyUsed;
}
clone() {
if (this.bodyUsed) {
throw new TypeError("Already read");
}
const newRequest = new Request(this, { body: null });
newRequest._body = this._body.clone();
return newRequest;
}
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();
}
}
export default Request;