Files
ame/src/common/fetch.ts
SuperSaltyGamer f2c1860215 Initial commit
2022-12-30 02:03:51 +02:00

35 lines
918 B
TypeScript

export function fetchCors(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
return new Promise<Response>((resolve, reject) => {
const fetchReq = new Request(input, init);
GM.xmlHttpRequest({
method: fetchReq.method as any,
url: fetchReq.url,
headers: Object.fromEntries(Array.from(fetchReq.headers)),
responseType: 'blob',
onload(res) {
const headers = res.responseHeaders
.split('\r\n')
.slice(0, -1)
.map(line => line.split(': '));
const fetchRes = new Response(res.response, {
headers: Object.fromEntries(headers),
status: res.status,
statusText: res.statusText
});
Object.defineProperty(fetchRes, 'url', { value: fetchReq.url });
resolve(fetchRes);
},
onerror() {
reject(new TypeError('Network request errored.'));
},
ontimeout() {
reject(new TypeError('Network request timed out.'));
}
});
});
}