@allmaps/stdlib
Version:
Allmaps Standard Library
41 lines (40 loc) • 1.33 kB
JavaScript
export async function fetchUrl(input, init, fetchFn) {
let response;
if (typeof fetchFn === 'function') {
response = await fetchFn(input, init);
}
else {
response = await fetch(input, init);
}
if (!response.ok) {
const json = await response.json();
if (json && json.error) {
throw new Error(json.error);
}
else if (response.statusText) {
throw new Error(response.statusText);
}
else if (response.status === 404) {
throw new Error(`Not found: ${input} (404)`);
}
else if (response.status === 500) {
throw new Error('Internal server error (500)');
}
else {
throw new Error(`Failed to fetch: ${input} (${response.status})`);
}
}
return response;
}
export async function fetchJson(input, init, fetchFn) {
const response = await fetchUrl(input, init, fetchFn);
return await response.json();
}
export async function fetchImageInfo(imageUri, init, fetchFn) {
return await fetchJson(`${imageUri}/info.json`, init, fetchFn);
}
export async function fetchImageBitmap(input, init, fetchFn) {
const response = await fetchUrl(input, init, fetchFn);
const blob = await response.blob();
return await createImageBitmap(blob);
}