This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.request.js
285 lines (277 loc) · 7.8 KB
/
app.request.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(function() {
'use strict';
/*******************************************************************************
* Copyright 2016 Pawel Psztyc, The ARC team
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
/* global HttpParser, Request, Headers */
/**
* {@link ArcRequest} class behaves the same way as JavaScript's Request class
* but it won't set guqards for Headers object.
*/
class ArcRequest {
/**
* The ArcRequest() constructor creates a new {@link ArcRequest} object.
*
* @constructor
* @param {String|ArcRequest} input Defines the resource that you wish to fetch.
* This can either be:
* - A USVString containing the direct URL of the resource you want to fetch.
* - An {@link ArcRequest} object.
* @param {Object} init (Optional) An options object containing any custom settings that you
* want to apply to the request. The possible options are:
* - method: The request method, e.g., GET, POST. Default to GET.
* - headers: Any headers you want to add to your request, contained within a Headers object or
* an object literal with ByteString values.
* - body: Any body that you want to add to your request: this can be a Blob, BufferSource,
* FormData, URLSearchParams, or USVString object. Note that a request using the GET or HEAD
* method cannot have a body.
* - redirect: The redirect mode to use: follow or error. If follow is set the result will
* contain redairect information.
*/
constructor(input, init) {
/**
* A HTTP method
*
* @type {String}
*/
this._method = 'GET';
/**
* A Headers object.
*
* @type {Headers}
*/
this._headers = undefined;
/**
* A body to send with the request.
* Note that a request using the GET or HEAD method cannot have a body.
*
* @type {Blob|BufferSource|FormData|URLSearchParams|USVString}
*/
this._body = undefined;
/**
* The redirect mode to use: follow (default) or error.
*/
this._redirect = 'follow';
/**
* A number of milliseconds for connection timeout.
* Note that the timer run at the moment when connection was established.
*
* @type {Number}
*/
this._timeout = undefined;
/**
* Defines the resource to fetch
*
* @type {String}
*/
this._url = undefined;
/**
* Set to true if the request may carry a payload.
* It does not means that it is.
* This is read only.
*
* @type {Boolean}
*/
this._payloadRequest = undefined;
/**
* A string representation of the message sent to the server.
*
* @type {String}
*/
this._messageSent = undefined;
if (input instanceof ArcRequest ||
input instanceof Request) {
this._assignFromInstance(input);
} else {
this.url = input;
this._assignInit(init);
}
}
/**
* Assign initial values.
*
* @param {Object} init an object with passed initial values.
*/
_assignInit(init) {
if ('method' in init) {
this.method = init.method;
}
if ('headers' in init) {
this.headers = init.headers;
}
if ('body' in init) {
this.body = init.body;
}
if ('redirect' in init) {
this.redirect = init.redirect;
}
if ('timeout' in init) {
this.timeout = init.timeout;
}
if ('messageSent' in init) {
this.messageSent = init.messageSent;
}
if ('auth' in init) {
this.auth = init.auth;
}
}
/**
* Assign initial value from existing {@link ArcRequest} object
* (without error checking)
*
* @param {ArcRequest} input An existing instance.
*/
_assignFromInstance(input) {
input = Object.assign({}, input);
this._url = input._url;
this._method = input._method;
this._headers = input._headers;
this._body = input._body;
this._redirect = input._redirect;
this._timeout = input._timeout;
this._messageSent = input._messageSent;
this._auth = input._auth;
}
/**
* Sets a HTTP method to this {@link ArcRequest} object.
*
* @param {String} method A method to set.
*/
set method(method) {
if (method) {
method = method.trim();
}
if (!HttpParser.isValidHTTPToken(method)) {
throw new Error(`"${method}" is not a valid HTTP method.`);
}
// if (HttpParser.isForbiddenMethod(method)) {
// throw new Error(`"${method}" HTTP method is unsupported.`);
// }
this._method = method.toUpperCase();
this._payloadRequest = ['GET', 'HEADER'].indexOf(this._method) === -1;
}
/**
* @return {String} Method name.
*/
get method() {
return this._method;
}
/**
* Readonly.
*
* @return {Boolean} True if the request can carry a payload. It does not means that it is.
*/
get payloadRequest() {
return this._payloadRequest;
}
/**
* @param {Blob|BufferSource|FormData|URLSearchParams|USVString} body A body to send
*/
set body(body) {
// if (!body) {
// throw new Error('Passed body is undefined.');
// }
if (this._method === 'GET' || this._method === 'HEAD') {
throw new Error('Request with GET/HEAD method cannot have body.');
}
this._body = body;
}
get body() {
return this._body;
}
/**
* A redirect value.
* Can be follow or error.
*
* @type {String} redirect A redirect value to set.
*/
set redirect(redirect) {
if (!redirect) {
throw new Error('Passed redirect is undefined.');
}
if (['follow', 'error'].indexOf(redirect) === -1) {
throw new Error(`${redirect} is unsupported redirect.`);
}
}
get redirect() { return this._redirect; }
set url(url) {
if (!url) {
throw new Error('Url can not be undefined.');
}
this._uri = new URL(url);
this._url = url;
}
get url() {
return this._url;
}
/**
* Set a headers list.
* This function will throw an error as Headers object would.
*
* @param {Object|Headers} headers A list of headers to set.
*/
set headers(headers) {
this._headers = new Headers(headers);
}
get headers() {
return this._headers;
}
/**
* @return {URI} A parsed URL by URI library..
*/
get uri() {
return this._uri;
}
set timeout(timeout) {
if (isNaN(timeout)) {
console.warn(`Timeout of ${timeout} is not a number`);
return;
}
this._timeout = timeout;
}
get timeout() {
return this._timeout;
}
set messageSent(message) {
this._messageSent = message;
}
get messageSent() {
return this._messageSent;
}
/**
* This is a setup for for auth options.
* The library will attempt to authenticate the user with given credentials.
*
* @param {Object} opts An object containng:
* {String} uid An user ID
* {String} passwd User password
* {String} method One of basic, ntlm, digest. Lowercase.
* {String} domain Optional. Auth domain. Default undefined.
* {Boolean} proxy Optional. True for proxy authentication. Default to false.
*/
set auth(opts) {
if (!opts.uid || !opts.passwd || !opts.method) {
console.warn('Invalid auth options. uid, passwd and method are required');
}
opts.domain = opts.domain || undefined;
opts.proxy = opts.proxy || false;
this._auth = opts;
}
get auth() {
return this._auth;
}
}
window.ArcRequest = ArcRequest;
})();