This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
ajax-request.js
542 lines (481 loc) · 14.3 KB
/
ajax-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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import Ember from 'ember';
import {
AjaxError,
UnauthorizedError,
InvalidError,
ForbiddenError,
BadRequestError,
NotFoundError,
TimeoutError,
AbortError,
ServerError,
isAjaxError,
isUnauthorizedError,
isForbiddenError,
isInvalidError,
isBadRequestError,
isNotFoundError,
isServerError,
isSuccess
} from './errors';
import parseResponseHeaders from './utils/parse-response-headers';
import { RequestURL } from './utils/url-helpers';
import ajax from './utils/ajax';
const {
$,
Error: EmberError,
RSVP: { Promise },
get,
isNone,
merge,
run,
Test,
testing
} = Ember;
const JSONAPIContentType = 'application/vnd.api+json';
function isJSONAPIContentType(header) {
if (isNone(header)) {
return false;
}
return header.indexOf(JSONAPIContentType) === 0;
}
export default class AjaxRequest {
constructor() {
this.init();
}
init() {
this.pendingRequestCount = 0;
if (testing) {
Test.registerWaiter(() => this.pendingRequestCount === 0);
}
}
request(url, options) {
const hash = this.options(url, options);
return new Promise((resolve, reject) => {
this.raw(url, hash)
.then(({ response }) => {
resolve(response);
})
.catch(({ response }) => {
reject(response);
});
}, `ember-ajax: ${hash.type} ${hash.url} response`);
}
raw(url, options) {
const hash = this.options(url, options);
const requestData = {
type: hash.type,
url: hash.url
};
if (isJSONAPIContentType(hash.headers['Content-Type']) && requestData.type !== 'GET') {
if (typeof hash.data === 'object') {
hash.data = JSON.stringify(hash.data);
}
}
return new Promise((resolve, reject) => {
hash.success = (payload, textStatus, jqXHR) => {
let response = this.handleResponse(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);
this.pendingRequestCount--;
if (isAjaxError(response)) {
run.join(null, reject, { payload, textStatus, jqXHR, response });
} else {
run.join(null, resolve, { payload, textStatus, jqXHR, response });
}
};
hash.error = (jqXHR, textStatus, errorThrown) => {
const payload = this.parseErrorResponse(jqXHR.responseText) || errorThrown;
let response;
if (errorThrown instanceof Error) {
response = errorThrown;
} else if (textStatus === 'timeout') {
response = new TimeoutError();
} else if (textStatus === 'abort') {
response = new AbortError();
} else {
response = this.handleResponse(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);
}
this.pendingRequestCount--;
run.join(null, reject, { payload, textStatus, jqXHR, errorThrown, response });
};
this.pendingRequestCount++;
ajax(hash);
}, `ember-ajax: ${hash.type} ${hash.url}`);
}
/**
* calls `request()` but forces `options.type` to `POST`
* @public
*/
post(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'POST'));
}
/**
* calls `request()` but forces `options.type` to `PUT`
* @public
*/
put(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'PUT'));
}
/**
* calls `request()` but forces `options.type` to `PATCH`
* @public
*/
patch(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'PATCH'));
}
/**
* calls `request()` but forces `options.type` to `DELETE`
* @public
*/
del(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'DELETE'));
}
/**
* calls `request()` but forces `options.type` to `DELETE`
* alias for `del()`
* @public
*/
delete() {
return this.del(...arguments);
}
/**
* Wrap the `.get` method so that we issue a warning if
*
* Since `.get` is both an AJAX pattern _and_ an Ember pattern, we want to try
* to warn users when they try using `.get` to make a request
*
* @method get
* @public
*/
get(url) {
if (arguments.length > 1 || url.charAt(0) === '/') {
throw new EmberError('It seems you tried to use `.get` to make a request! Use the `.request` method instead.');
}
return this._super(...arguments);
}
// forcibly manipulates the options hash to include the HTTP method on the type key
_addTypeToOptionsFor(options, method) {
options = options || {};
options.type = method;
return options;
}
/**
* @method _getFullHeadersHash
* @private
* @param {Object} headers
* @return {Object}
*/
_getFullHeadersHash(headers) {
const classHeaders = get(this, 'headers') || {};
const _headers = merge({}, classHeaders);
return merge(_headers, headers);
}
/**
* @method options
* @private
* @param {String} url
* @param {Object} options
* @return {Object}
*/
options(url, options = {}) {
options.url = this._buildURL(url, options);
options.type = options.type || 'GET';
options.dataType = options.dataType || 'json';
options.context = this;
if (this._shouldSendHeaders(options)) {
options.headers = this._getFullHeadersHash(options.headers);
} else {
options.headers = options.headers || {};
}
return options;
}
_buildURL(url, options) {
const host = options.host || get(this, 'host');
const namespace = get(this, 'namespace');
const urlObject = new RequestURL(url);
// If the URL passed is not relative, return the whole URL
if (urlObject.isAbsolute) {
return urlObject.href;
}
let _url = this._normalizePath(url);
let _namespace = this._normalizePath(namespace);
return [ host, _namespace, _url ].join('');
}
_normalizePath(path) {
if (path) {
// make sure path starts with `/`
if (path.charAt(0) !== '/') {
path = `/${path}`;
}
// remove end `/`
if (path.charAt(path.length - 1) === '/') {
path = path.slice(0, -1);
}
}
return path;
}
/**
* Takes an ajax response, and returns the json payload or an error.
*
* By default this hook just returns the json payload passed to it.
* You might want to override it in two cases:
*
* 1. Your API might return useful results in the response headers.
* Response headers are passed in as the second argument.
*
* 2. Your API might return errors as successful responses with status code
* 200 and an Errors text or object.
*
* @method handleResponse
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @param {Object} requestData the original request information
* @return {Object | AjaxError} response
*/
handleResponse(status, headers, payload, requestData) {
payload = payload || {};
const errors = this.normalizeErrorResponse(status, headers, payload);
if (this.isSuccess(status, headers, payload)) {
return payload;
} else if (this.isUnauthorizedError(status, headers, payload)) {
return new UnauthorizedError(errors);
} else if (this.isForbiddenError(status, headers, payload)) {
return new ForbiddenError(errors);
} else if (this.isInvalidError(status, headers, payload)) {
return new InvalidError(errors);
} else if (this.isBadRequestError(status, headers, payload)) {
return new BadRequestError(errors);
} else if (this.isNotFoundError(status, headers, payload)) {
return new NotFoundError(errors);
} else if (this.isServerError(status, headers, payload)) {
return new ServerError(errors);
}
const detailedMessage = this.generateDetailedMessage(status, headers, payload, requestData);
return new AjaxError(errors, detailedMessage);
}
/**
* Match the host to a provided array of strings or regexes that can match to a host
*
* @method matchHosts
* @private
* @param {String} host the host you are sending too
* @param {RegExp | String} matcher a string or regex that you can match the host to.
* @returns {Boolean} if the host passed the matcher
*/
_matchHosts(host, matcher) {
if (matcher.constructor === RegExp) {
return matcher.test(host);
} else if (typeof matcher === 'string') {
return matcher === host;
} else {
Ember.Logger.warn('trustedHosts only handles strings or regexes.', matcher, 'is neither.');
return false;
}
}
/**
* Determine whether the headers should be added for this request
*
* This hook is used to help prevent sending headers to every host, regardless
* of the destination, since this could be a security issue if authentication
* tokens are accidentally leaked to third parties.
*
* To avoid that problem, subclasses should utilize the `headers` computed
* property to prevent authentication from being sent to third parties, or
* implement this hook for more fine-grain control over when headers are sent.
*
* By default, the headers are sent if the host of the request matches the
* `host` property designated on the class.
*
* @method _shouldSendHeaders
* @private
* @property {Object} hash request options hash
* @returns {Boolean} whether or not headers should be sent
*/
_shouldSendHeaders({ url, host }) {
url = url || '';
host = host || get(this, 'host') || '';
const urlObject = new RequestURL(url);
const trustedHosts = get(this, 'trustedHosts') || Ember.A();
// Add headers on relative URLs
if (!urlObject.isAbsolute) {
return true;
} else if (trustedHosts.find((matcher) => this._matchHosts(urlObject.hostname, matcher))) {
return true;
}
// Add headers on matching host
const hostObject = new RequestURL(host);
return urlObject.sameHost(hostObject);
}
/**
* Generates a detailed ("friendly") error message, with plenty
* of information for debugging (good luck!)
* @method generateDetailedMessage
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @param {Object} requestData the original request information
* @return {Object} request information
*/
generateDetailedMessage(status, headers, payload, requestData) {
let shortenedPayload;
const payloadContentType = headers['Content-Type'] || 'Empty Content-Type';
if (payloadContentType === 'text/html' && payload.length > 250) {
shortenedPayload = '[Omitted Lengthy HTML]';
} else {
shortenedPayload = JSON.stringify(payload);
}
const requestDescription = `${requestData.type} ${requestData.url}`;
const payloadDescription = `Payload (${payloadContentType})`;
return [
`Ember Data Request ${requestDescription} returned a ${status}`,
payloadDescription,
shortenedPayload
].join('\n');
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a an authorized error.
* @method isUnauthorizedError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isUnauthorizedError(status) {
return isUnauthorizedError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a forbidden error.
* @method isForbiddenError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isForbiddenError(status) {
return isForbiddenError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a an invalid error.
* @method isInvalidError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isInvalidError(status) {
return isInvalidError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a bad request error.
* @method isBadRequestError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isBadRequestError(status) {
return isBadRequestError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a "not found" error.
* @method isNotFoundError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isNotFoundError(status) {
return isNotFoundError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a server error.
* @method isServerError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isServerError(status) {
return isServerError(status);
}
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a success.
* @method isSuccess
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isSuccess(status) {
return isSuccess(status);
}
/**
* @method parseErrorResponse
* @private
* @param {String} responseText
* @return {Object}
*/
parseErrorResponse(responseText) {
let json = responseText;
try {
json = $.parseJSON(responseText);
} catch (e) {}
return json;
}
/**
* @method normalizeErrorResponse
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Array} errors payload
*/
normalizeErrorResponse(status, headers, payload) {
if (payload && typeof payload === 'object' && payload.errors) {
if (!Ember.isArray(payload.errors)) {
return payload.errors;
}
return payload.errors.map(function(error) {
let ret = merge({}, error);
if (typeof ret.status === 'number') {
ret.status = `${ret.status}`;
}
return ret;
});
} else {
return [
{
status: `${status}`,
title: 'The backend responded with an error',
detail: payload
}
];
}
}
}