mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
27 lines
721 B
TypeScript
27 lines
721 B
TypeScript
export interface ErrorType<T> {
|
|
error?: T;
|
|
status: Response['status'];
|
|
statusText: Response['statusText'];
|
|
}
|
|
|
|
export default function clientFetch<Success, Error>(path: string, init?: RequestInit): Promise<Success | ErrorType<Error>> {
|
|
return fetch(path, init).then(response => {
|
|
if (!response.ok) {
|
|
return response.json().then(
|
|
(jsonError) => Promise.reject({
|
|
error: jsonError as Error,
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
}),
|
|
() => Promise.reject({
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
}),
|
|
);
|
|
|
|
} else {
|
|
return response.json() as Promise<Success>;
|
|
}
|
|
});
|
|
}
|