Skip to content

Instantly share code, notes, and snippets.

@vczb
Created March 18, 2022 23:40
Show Gist options
  • Select an option

  • Save vczb/d9af9665359efe07b2de5e0efa103a73 to your computer and use it in GitHub Desktop.

Select an option

Save vczb/d9af9665359efe07b2de5e0efa103a73 to your computer and use it in GitHub Desktop.
fetcher.ts
type FetchRequest = {
url: string;
method?: "GET" | "POST";
body?: object;
headers?: {
[key: string]: string;
};
};
type Error = {
status: number;
name: string;
message: string;
details: unknown;
};
type FechResponse = {
error: Error;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
export default async function fetcher(
params: FetchRequest): Promise<FechResponse> {
const { url, method = "GET", body = {}, headers = {
"Content-Type": "application/json",
} } = params;
return await fetch(url, {
method,
headers,
body: JSON.stringify(body),
})
.then((res) => res.json())
.then((res) => res)
.catch((err) => err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment