@zodash/down_load
Version:
Download File in Browser
46 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unfetch = void 0;
function unfetch(url, options) {
const _options = options || {};
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
const keys = [];
const all = [];
const headers = {};
const response = () => ({
ok: (request.status / 100 | 0) == 2,
statusText: request.statusText,
status: request.status,
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
clone: response,
headers: {
keys: () => keys,
entries: () => all,
get: (name) => headers[name.toLowerCase()],
has: (name) => name.toLowerCase() in headers,
},
});
request.open(_options.method || 'get', url, true);
request.onload = () => {
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push(key = key.toLowerCase());
all.push([key, value]);
headers[key] = headers[key] ? `${headers[key]},${value}` : value;
return '';
});
resolve(response()); // @TODO not the same as fetch Response
};
request.onerror = reject;
request.withCredentials = _options.credentials === 'include';
for (const key in _options.headers) {
request.setRequestHeader(key, _options.headers[key]);
}
request.send(_options.body || null);
});
}
exports.unfetch = unfetch;
//# sourceMappingURL=unfetch.js.map