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
846 lines (770 loc) · 22.6 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
import { A } from '@ember/array';
import EmberError from '@ember/error';
import Mixin from '@ember/object/mixin';
import { get } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { merge } from '@ember/polyfills';
import { run } from '@ember/runloop';
import { warn, runInDebug } from '@ember/debug';
import Ember from 'ember';
import {
AjaxError,
UnauthorizedError,
InvalidError,
ForbiddenError,
BadRequestError,
NotFoundError,
TimeoutError,
AbortError,
ConflictError,
ServerError,
isAjaxError,
isUnauthorizedError,
isForbiddenError,
isInvalidError,
isBadRequestError,
isNotFoundError,
isConflictError,
isAbortError,
isServerError,
isSuccess
} from '../errors';
import ajax from 'ember-ajax/utils/ajax';
import parseResponseHeaders from 'ember-ajax/-private/utils/parse-response-headers';
import getHeader from 'ember-ajax/-private/utils/get-header';
import {
isFullURL,
parseURL,
haveSameHost
} from 'ember-ajax/-private/utils/url-helpers';
import isString from 'ember-ajax/-private/utils/is-string';
import AJAXPromise from 'ember-ajax/-private/promise';
const { Logger, Test, testing } = Ember;
const JSONContentType = /^application\/(?:vnd\.api\+)?json/i;
function isJSONContentType(header) {
if (!isString(header)) {
return false;
}
return !!header.match(JSONContentType);
}
function isJSONStringifyable(method, { contentType, data, headers }) {
if (method === 'GET') {
return false;
}
if (
!isJSONContentType(contentType) &&
!isJSONContentType(getHeader(headers, 'Content-Type'))
) {
return false;
}
if (typeof data !== 'object') {
return false;
}
return true;
}
function startsWithSlash(string) {
return string.charAt(0) === '/';
}
function endsWithSlash(string) {
return string.charAt(string.length - 1) === '/';
}
function removeLeadingSlash(string) {
return string.substring(1);
}
function stripSlashes(path) {
// make sure path starts with `/`
if (startsWithSlash(path)) {
path = removeLeadingSlash(path);
}
// remove end `/`
if (endsWithSlash(path)) {
path = path.slice(0, -1);
}
return path;
}
let pendingRequestCount = 0;
if (testing) {
Test.registerWaiter(function() {
return pendingRequestCount === 0;
});
}
/**
* AjaxRequest Mixin
*
* @public
* @mixin
*/
export default Mixin.create({
/**
* The default value for the request `contentType`
*
* For now, defaults to the same value that jQuery would assign. In the
* future, the default value will be for JSON requests.
* @property {string} contentType
* @public
* @default
*/
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
/**
* Headers to include on the request
*
* Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
* headers can be set as key/value pairs on the `RESTAdapter`'s `headers`
* object and Ember Data will send them along with each ajax request.
*
* ```javascript
* // app/services/ajax.js
* import AjaxService from 'ember-ajax/services/ajax';
*
* export default AjaxService.extend({
* headers: {
* 'API_KEY': 'secret key',
* 'ANOTHER_HEADER': 'Some header value'
* }
* });
* ```
*
* `headers` can also be used as a computed property to support dynamic
* headers.
*
* ```javascript
* // app/services/ajax.js
* import Ember from 'ember';
* import AjaxService from 'ember-ajax/services/ajax';
*
* const {
* computed,
* get,
* inject: { service }
* } = Ember;
*
* export default AjaxService.extend({
* session: service(),
* headers: computed('session.authToken', function() {
* return {
* 'API_KEY': get(this, 'session.authToken'),
* 'ANOTHER_HEADER': 'Some header value'
* };
* })
* });
* ```
*
* In some cases, your dynamic headers may require data from some object
* outside of Ember's observer system (for example `document.cookie`). You
* can use the `volatile` function to set the property into a non-cached mode
* causing the headers to be recomputed with every request.
*
* ```javascript
* // app/services/ajax.js
* import Ember from 'ember';
* import AjaxService from 'ember-ajax/services/ajax';
*
* const {
* computed,
* get,
* inject: { service }
* } = Ember;
*
* export default AjaxService.extend({
* session: service(),
* headers: computed('session.authToken', function() {
* return {
* 'API_KEY': get(document.cookie.match(/apiKey\=([^;]*)/), '1'),
* 'ANOTHER_HEADER': 'Some header value'
* };
* }).volatile()
* });
* ```
*
* @property {Object} headers
* @public
* @default
*/
headers: {},
/**
* Make an AJAX request, ignoring the raw XHR object and dealing only with
* the response
*
* @method request
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
request(url, options) {
const hash = this.options(url, options);
const internalPromise = this._makeRequest(hash);
const ajaxPromise = new AJAXPromise((resolve, reject) => {
internalPromise
.then(({ response }) => {
resolve(response);
})
.catch(({ response }) => {
reject(response);
});
}, `ember-ajax: ${hash.type} ${hash.url} response`);
ajaxPromise.xhr = internalPromise.xhr;
return ajaxPromise;
},
/**
* Make an AJAX request, returning the raw XHR object along with the response
*
* @method raw
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
raw(url, options) {
const hash = this.options(url, options);
return this._makeRequest(hash);
},
/**
* Shared method to actually make an AJAX request
*
* @method _makeRequest
* @private
* @param {Object} hash The options for the request
* @param {string} hash.url The URL to make the request to
* @return {Promise} The result of the request
*/
_makeRequest(hash) {
const method = hash.method || hash.type || 'GET';
const requestData = { method, type: method, url: hash.url };
if (isJSONStringifyable(method, hash)) {
hash.data = JSON.stringify(hash.data);
}
pendingRequestCount = pendingRequestCount + 1;
const jqXHR = ajax(hash);
const promise = new AJAXPromise((resolve, reject) => {
jqXHR
.done((payload, textStatus, jqXHR) => {
const response = this.handleResponse(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);
if (isAjaxError(response)) {
run.join(null, reject, { payload, textStatus, jqXHR, response });
} else {
run.join(null, resolve, { payload, textStatus, jqXHR, response });
}
})
.fail((jqXHR, textStatus, errorThrown) => {
runInDebug(function() {
const message = `The server returned an empty string for ${
requestData.type
} ${
requestData.url
}, which cannot be parsed into a valid JSON. Return either null or {}.`;
const validJSONString = !(
textStatus === 'parsererror' && jqXHR.responseText === ''
);
warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON'
});
});
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
);
}
run.join(null, reject, {
payload,
textStatus,
jqXHR,
errorThrown,
response
});
})
.always(() => {
pendingRequestCount = pendingRequestCount - 1;
});
}, `ember-ajax: ${hash.type} ${hash.url}`);
promise.xhr = jqXHR;
return promise;
},
/**
* calls `request()` but forces `options.type` to `POST`
*
* @method post
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
post(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'POST'));
},
/**
* calls `request()` but forces `options.type` to `PUT`
*
* @method put
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
put(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'PUT'));
},
/**
* calls `request()` but forces `options.type` to `PATCH`
*
* @method patch
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
patch(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'PATCH'));
},
/**
* calls `request()` but forces `options.type` to `DELETE`
*
* @method del
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
del(url, options) {
return this.request(url, this._addTypeToOptionsFor(options, 'DELETE'));
},
/**
* calls `request()` but forces `options.type` to `DELETE`
*
* Alias for `del()`
*
* @method delete
* @public
* @param {string} url The url to make a request to
* @param {Object} options The options for the request
* @return {Promise} The result of the request
*/
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.indexOf('/') !== -1) {
throw new EmberError(
'It seems you tried to use `.get` to make a request! Use the `.request` method instead.'
);
}
return this._super(...arguments);
},
/**
* Manipulates the options hash to include the HTTP method on the type key
*
* @method _addTypeToOptionsFor
* @private
* @param {Object} options The original request options
* @param {string} method The method to enforce
* @return {Object} The new options, with the method set
*/
_addTypeToOptionsFor(options, method) {
options = options || {};
options.type = method;
return options;
},
/**
* Get the full "headers" hash, combining the service-defined headers with
* the ones provided for the request
*
* @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 = merge({}, options);
options.url = this._buildURL(url, options);
options.type = options.type || 'GET';
options.dataType = options.dataType || 'json';
options.contentType = isEmpty(options.contentType)
? get(this, 'contentType')
: options.contentType;
if (this._shouldSendHeaders(options)) {
options.headers = this._getFullHeadersHash(options.headers);
} else {
options.headers = options.headers || {};
}
return options;
},
/**
* Build a URL for a request
*
* If the provided `url` is deemed to be a complete URL, it will be returned
* directly. If it is not complete, then the segment provided will be combined
* with the `host` and `namespace` options of the request class to create the
* full URL.
*
* @private
* @param {string} url the url, or url segment, to request
* @param {Object} [options={}] the options for the request being made
* @param {string} [options.host] the host to use for this request
* @returns {string} the URL to make a request to
*/
_buildURL(url, options = {}) {
if (isFullURL(url)) {
return url;
}
const urlParts = [];
let host = options.host || get(this, 'host');
if (host) {
host = stripSlashes(host);
}
urlParts.push(host);
let namespace = options.namespace || get(this, 'namespace');
if (namespace) {
namespace = stripSlashes(namespace);
urlParts.push(namespace);
}
// If the URL has already been constructed (presumably, by Ember Data), then we should just leave it alone
const hasNamespaceRegex = new RegExp(`^(/)?${namespace}/`);
if (hasNamespaceRegex.test(url)) {
return url;
}
// *Only* remove a leading slash -- we need to maintain a trailing slash for
// APIs that differentiate between it being and not being present
if (startsWithSlash(url)) {
url = removeLeadingSlash(url);
}
urlParts.push(url);
return urlParts.join('/');
},
/**
* 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) {
if (this.isSuccess(status, headers, payload)) {
return payload;
}
// Allow overriding of error payload
payload = this.normalizeErrorResponse(status, headers, payload);
return this._createCorrectError(status, headers, payload, requestData);
},
_createCorrectError(status, headers, payload, requestData) {
let error;
if (this.isUnauthorizedError(status, headers, payload)) {
error = new UnauthorizedError(payload);
} else if (this.isForbiddenError(status, headers, payload)) {
error = new ForbiddenError(payload);
} else if (this.isInvalidError(status, headers, payload)) {
error = new InvalidError(payload);
} else if (this.isBadRequestError(status, headers, payload)) {
error = new BadRequestError(payload);
} else if (this.isNotFoundError(status, headers, payload)) {
error = new NotFoundError(payload);
} else if (this.isAbortError(status, headers, payload)) {
error = new AbortError(payload);
} else if (this.isConflictError(status, headers, payload)) {
error = new ConflictError(payload);
} else if (this.isServerError(status, headers, payload)) {
error = new ServerError(payload, status);
} else {
const detailedMessage = this.generateDetailedMessage(
status,
headers,
payload,
requestData
);
error = new AjaxError(payload, detailedMessage, status);
}
return error;
},
/**
* 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 {
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 trustedHosts = get(this, 'trustedHosts') || A();
const { hostname } = parseURL(url);
// Add headers on relative URLs
if (!isFullURL(url)) {
return true;
} else if (
trustedHosts.find(matcher => this._matchHosts(hostname, matcher))
) {
return true;
}
// Add headers on matching host
return haveSameHost(url, host);
},
/**
* 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 =
getHeader(headers, 'Content-Type') || 'Empty Content-Type';
if (
payloadContentType.toLowerCase() === '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 AJAX 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 an "abort" error.
*
* @method isAbortError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isAbortError(status) {
return isAbortError(status);
},
/**
* Default `handleResponse` implementation uses this hook to decide if the
* response is a "conflict" error.
*
* @method isConflictError
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Boolean}
*/
isConflictError(status) {
return isConflictError(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) {
try {
return JSON.parse(responseText);
} catch (e) {
return responseText;
}
},
/**
* Can be overwritten to allow re-formatting of error messages
*
* @method normalizeErrorResponse
* @private
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {*} error response
*/
normalizeErrorResponse(status, headers, payload) {
return payload;
}
});