Skip to content

Instantly share code, notes, and snippets.

@deyvisonborges
Created April 4, 2022 20:00
Show Gist options
  • Save deyvisonborges/9682cf541651ff8f2e1004b7d01eaed4 to your computer and use it in GitHub Desktop.
Save deyvisonborges/9682cf541651ff8f2e1004b7d01eaed4 to your computer and use it in GitHub Desktop.
Reusable FecthAPI Client
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