Created
March 18, 2022 23:40
-
-
Save vczb/d9af9665359efe07b2de5e0efa103a73 to your computer and use it in GitHub Desktop.
fetcher.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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