Skip to content

Commit

Permalink
Different order
Browse files Browse the repository at this point in the history
  • Loading branch information
rzats committed Dec 18, 2024
1 parent 9e4cb11 commit b0c541e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/api/EpiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,22 @@ const ENDPOINT = process.env.EPIDATA_ENDPOINT_URL;

export const fetchOptions: RequestInit = process.env.NODE_ENV === 'development' ? { cache: 'force-cache' } : {};

function processResponse<T>(response: Response): Promise<T> {
if (response.ok) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return response.json();
}
return response.text().then((text) => {
throw new Error(`[${response.status}] ${text}`);
});
}

export function fetchImpl<T>(url: URL): Promise<T> {
const urlGetS = url.toString();
if (urlGetS.length < 4096) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return fetch(url.toString(), fetchOptions).then((d) => {
try {
return d.json();
} catch (error) {
throw new Error(`[${d.status}] ${d.text()}`);
}
return processResponse(d);
});
}
const params = new URLSearchParams(url.searchParams);
Expand All @@ -64,11 +70,7 @@ export function fetchImpl<T>(url: URL): Promise<T> {
method: 'POST',
body: params,
}).then((d) => {
try {
return d.json();
} catch (error) {
throw new Error(`[${d.status}] ${d.text()}`);
}
return processResponse(d);
});
}

Expand Down Expand Up @@ -203,7 +205,7 @@ export function loadDataSet(
.alert(
`
<div class="uk-alert uk-alert-error">
Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/><i>${error}</i>
Failed to fetch API data from <a href="${url.href}">API Link</a>.
</div>`,
)
.then(() => null);
Expand Down

0 comments on commit b0c541e

Please sign in to comment.