Skip to content

Instantly share code, notes, and snippets.

@bingo347
Created March 18, 2020 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bingo347/80f26760936f4f840ecb144627fa0b79 to your computer and use it in GitHub Desktop.
Save bingo347/80f26760936f4f840ecb144627fa0b79 to your computer and use it in GitHub Desktop.
function addGetParam(url, name, value) {
return url + (/\?/.test(url) ? '&' : '?') + encodeURIComponent(name) + '=' + encodeURIComponent(value);
}
function createXHR(win = window) {
if(typeof win.XDomainRequest === 'function') {
return new win.XDomainRequest();
}
return new win.XMLHttpRequest();
}
function openXHR(params = {}) {
const xhr = params.xhr || createXHR();
const method = (params.method || 'GET').toUpperCase();
const isAsync = typeof params.async === 'boolean' ? params.async : true;
const headers = params.headers || {};
const url = (params.url.startsWith('//') ? 'https:' + params.url : params.url);
xhr.open(method, url, isAsync);
xhr.withCredentials = true;
for(let hdrs = Object.getOwnPropertyNames(headers), i = hdrs.length; i--;) {
let header = hdrs[i];
let value = headers[header];
if(Array.isArray(value)) {
for(let i = value.length; i--;) {
if(typeof value[i] === 'string') {
xhr.setRequestHeader(header, value[i]);
}
}
} else if(typeof value === 'string') {
xhr.setRequestHeader(header, value);
}
}
return xhr;
}
function getJSON(url, disableCredentials = false) {
return new Promise((resolve, reject) => {
const xhr = openXHR({url});
if(disableCredentials) {
xhr.withCredentials = false;
}
xhr.onreadystatechange = function() {
if(xhr.readyState !== 4) { return; }
if(xhr.status < 200 || xhr.status > 299) {
const err = new Error('Response status: ' + xhr.status);
err.statusCode = xhr.status;
reject(err);
return;
}
resolve(xhr.responseText);
};
xhr.send(null);
}).then(responseText => JSON.parse(responseText));
}
var jsonpID = 0;
function getJSONP(url, timeout = 12000) {
return new Promise((resolve, reject) => {
const name = '_seedr_jsonp_' + jsonpID++;
const script = document.createElement('script');
function destroy() {
document.head.removeChild(script);
delete window[name];
}
const timeoutID = setTimeout(() => {
destroy();
reject(new Error('JSONP request timeout: ' + url));
}, timeout);
script.onerror = () => {
clearTimeout(timeoutID);
destroy();
reject(new Error('JSONP request error: ' + url));
};
window[name] = function jsonpCB(data) {
clearTimeout(timeoutID);
destroy();
resolve(data);
};
script.type = 'text/javascript';
script.src = addGetParam(url, 'callback', name);
document.head.appendChild(script);
});
}
function getXML(url) {
return new Promise((resolve, reject) => {
const xhr = openXHR({url});
xhr.onreadystatechange = function() {
if(xhr.readyState !== 4) { return; }
if(xhr.status < 200 || xhr.status > 299) {
const err = new Error('Response status: ' + xhr.status);
err.statusCode = xhr.status;
reject(err);
return;
}
resolve(xhr.responseXML);
};
xhr.send(null);
});
}
module.exports = {
createXHR,
openXHR,
getJSON,
getJSONP,
getXML
};
const {
createXHR,
openXHR,
getJSON,
getJSONP,
getXML
} = require('./xhr-lib.js');
// your jest test
// see https://jestjs.io/docs/ru/getting-started
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment