-
Notifications
You must be signed in to change notification settings - Fork 1
/
etools-iron-request.js
106 lines (90 loc) · 3 KB
/
etools-iron-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
/* eslint-disable linebreak-style */
import {tryJsonParse} from './etools-ajax-utils';
import {logWarn} from '@unicef-polymer/etools-behaviors/etools-logging';
import '@polymer/iron-ajax/iron-request.js';
let activeAjaxRequests = [];
/**
* Fire new http request using iron-request
* @param {
* url: string,
* method?: string,
* async?: boolean,
* body?: ArrayBuffer | ArrayBufferView | Blob | Document | FormData | string | object | null,
* headers?: object | null,
* handleAs?: string,
* jsonPrefix?: string,
* withCredentials?: boolean,
* timeout?: number,
* rejectWithRequest?: boolean
* } ironRequestConfigOptions
* @param {string} requestKey
*/
export function doHttpRequest(ironRequestConfigOptions, requestKey) {
const ironRequestElement = document.createElement('iron-request'); // typeof IronRequestElement
ironRequestElement.send(ironRequestConfigOptions);
_addToActiveAjaxRequests(requestKey, ironRequestElement);
return ironRequestElement.completes
.then((request) => {
let responseData = request.response;
if (ironRequestConfigOptions.handleAs === 'json' && typeof responseData === 'string') {
responseData = tryJsonParse(responseData);
}
_cleanUp(requestKey);
return responseData;
})
.catch((err) => {
const error = err.error;
const request = err.request;
if (!request.aborted && request.xhr.status === 0) {
// not an error, this is an asynchronous request that is not completed yet
return;
}
_cleanUp(requestKey);
// check request aborted, no error handling in this case
if (!request.aborted) {
throw new EtoolsRequestError(error, request.xhr.status, request.xhr.statusText, request.xhr.response);
} else {
throw new EtoolsRequestError(error, 0, 'Request aborted', null);
}
});
}
function _cleanUp(requestKey) {
_removeActiveRequestFromList(requestKey);
}
export function EtoolsRequestError(error, statusCode, statusText, response) {
this.error = error;
this.status = statusCode;
this.statusText = statusText;
this.response = tryJsonParse(response);
}
function _addToActiveAjaxRequests(key, request) {
if (key) {
activeAjaxRequests.push({key: key, request: request});
}
}
function _removeActiveRequestFromList(key) {
if (key) {
activeAjaxRequests = activeAjaxRequests.filter((a) => a.key !== key);
}
}
function getActiveRequestByKey(key) {
return activeAjaxRequests.find((a) => a.key === key);
}
export function abortRequestByKey(key) {
// abort request by key
if (key) {
const activeReq = getActiveRequestByKey(key);
if (activeReq) {
abortRequest(activeReq);
} else {
logWarn('No active request found by this key: ' + key + '.', 'EtoolsAjaxRequestMixin:abortRequest');
}
} else {
logWarn('Aborting request by key requires a key.', 'EtoolsAjax:abortRequestByKey');
}
}
function abortRequest(activeReqMapObj) {
if (activeReqMapObj.request) {
activeReqMapObj.request.abort();
}
}