58 lines
2.6 KiB
JavaScript
58 lines
2.6 KiB
JavaScript
|
function createBrowserXhrRequester() {
|
||
|
return {
|
||
|
send(request) {
|
||
|
return new Promise((resolve) => {
|
||
|
const baseRequester = new XMLHttpRequest();
|
||
|
baseRequester.open(request.method, request.url, true);
|
||
|
Object.keys(request.headers).forEach(key => baseRequester.setRequestHeader(key, request.headers[key]));
|
||
|
const createTimeout = (timeout, content) => {
|
||
|
return setTimeout(() => {
|
||
|
baseRequester.abort();
|
||
|
resolve({
|
||
|
status: 0,
|
||
|
content,
|
||
|
isTimedOut: true,
|
||
|
});
|
||
|
}, timeout * 1000);
|
||
|
};
|
||
|
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout');
|
||
|
// eslint-disable-next-line functional/no-let
|
||
|
let responseTimeout;
|
||
|
// eslint-disable-next-line functional/immutable-data
|
||
|
baseRequester.onreadystatechange = () => {
|
||
|
if (baseRequester.readyState > baseRequester.OPENED && responseTimeout === undefined) {
|
||
|
clearTimeout(connectTimeout);
|
||
|
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout');
|
||
|
}
|
||
|
};
|
||
|
// eslint-disable-next-line functional/immutable-data
|
||
|
baseRequester.onerror = () => {
|
||
|
// istanbul ignore next
|
||
|
if (baseRequester.status === 0) {
|
||
|
clearTimeout(connectTimeout);
|
||
|
clearTimeout(responseTimeout);
|
||
|
resolve({
|
||
|
content: baseRequester.responseText || 'Network request failed',
|
||
|
status: baseRequester.status,
|
||
|
isTimedOut: false,
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
// eslint-disable-next-line functional/immutable-data
|
||
|
baseRequester.onload = () => {
|
||
|
clearTimeout(connectTimeout);
|
||
|
clearTimeout(responseTimeout);
|
||
|
resolve({
|
||
|
content: baseRequester.responseText,
|
||
|
status: baseRequester.status,
|
||
|
isTimedOut: false,
|
||
|
});
|
||
|
};
|
||
|
baseRequester.send(request.data);
|
||
|
});
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export { createBrowserXhrRequester };
|