Skip to content

Commit

Permalink
Try without explicit return
Browse files Browse the repository at this point in the history
  • Loading branch information
rzats committed Dec 18, 2024
1 parent cc81acb commit 9e4cb11
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/api/EpiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,16 @@ 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> {
try {
if (response.status == 429) {
throw new Error(
'Rate limit exceeded for anonymous queries. To remove this limit, register a free API key at https://api.delphi.cmu.edu/epidata/admin/registration_form</p>',
);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return response.json();
} catch (error) {
throw new Error(`[${response.status}] ${response.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) => {
return processResponse(d);
try {
return d.json();
} catch (error) {
throw new Error(`[${d.status}] ${d.text()}`);
}
});
}
const params = new URLSearchParams(url.searchParams);
Expand All @@ -74,7 +64,11 @@ export function fetchImpl<T>(url: URL): Promise<T> {
method: 'POST',
body: params,
}).then((d) => {
return processResponse(d);
try {
return d.json();
} catch (error) {
throw new Error(`[${d.status}] ${d.text()}`);
}
});
}

Expand Down Expand Up @@ -209,7 +203,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>.
Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/><i>${error}</i>
</div>`,
)
.then(() => null);
Expand Down

0 comments on commit 9e4cb11

Please sign in to comment.