forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
24 lines (23 loc) · 826 Bytes
/
tools.js
File metadata and controls
24 lines (23 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
});
}