mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
24 lines
793 B
JavaScript
24 lines
793 B
JavaScript
export const getResponseTypeFromFetchType = fetchEntry => {
|
|
if (fetchEntry === "python") return "text";
|
|
if (fetchEntry === "javascript") return "text";
|
|
if (fetchEntry === "css") return "text";
|
|
if (fetchEntry === "js") return "blob";
|
|
if (fetchEntry === "plugin") return "text";
|
|
if (fetchEntry === "bytes") return "arrayBuffer";
|
|
return fetchEntry;
|
|
};
|
|
|
|
export function genericFetch(path, fetchType) {
|
|
const responseType = getResponseTypeFromFetchType(fetchType);
|
|
return fetch(path)
|
|
.then(r => {
|
|
if (!r.ok) throw new Error(`${r.status} ${r.statusText} (${path})`);
|
|
return r[responseType]();
|
|
})
|
|
.then(r => {
|
|
if (fetchType === "bytes") {
|
|
return new Uint8Array(r);
|
|
}
|
|
return r;
|
|
});
|
|
} |