Show exterior cells edited in ModList
Also abstracts jsonFetcher and csvFetcher into common lib functions.
This commit is contained in:
34
lib/api.ts
Normal file
34
lib/api.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
export async function jsonFetcher<T>(url: string): Promise<T | null> {
|
||||
const res = await fetch(url);
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
const error = new Error("An error occurred while fetching the data.");
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export async function jsonFetcherWithLastModified<T>(url: string): Promise<{ data: T, lastModified: string | null } | null> {
|
||||
const res = await fetch(url);
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
const error = new Error("An error occurred while fetching the data.");
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
lastModified: res.headers.get("Last-Modified"),
|
||||
data: await res.json(),
|
||||
};
|
||||
}
|
||||
|
||||
export async function csvFetcher(url: string): Promise<string> {
|
||||
return (await fetch(url)).text();
|
||||
}
|
||||
Reference in New Issue
Block a user