Created
April 4, 2022 20:00
-
-
Save deyvisonborges/9682cf541651ff8f2e1004b7d01eaed4 to your computer and use it in GitHub Desktop.
Reusable FecthAPI Client
This file contains 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
interface RequestOptions { | |
baseUrl?: string | |
prefix?: string | |
endpoint: string | |
config?: RequestInit | |
} | |
export async function request<T, TError>({ | |
...props | |
}: RequestOptions): Promise<{ | |
data: T | |
}> { | |
try { | |
const __BASEURL = props.baseUrl ?? '' | |
const __PREFIX = props.prefix ?? '' | |
const __ENDPOINT = props.endpoint | |
const url = __BASEURL + __PREFIX + __ENDPOINT | |
const response = await fetch(url, props.config) | |
return await response.json() | |
} catch (err: TError | any) { | |
return err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment