Skip to content

Commit

Permalink
Try explicit return
Browse files Browse the repository at this point in the history
  • Loading branch information
rzats committed Dec 18, 2024
1 parent 20e8fc9 commit cc81acb
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/api/EpiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,26 @@ 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) => {
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 +74,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

0 comments on commit cc81acb

Please sign in to comment.