Skip to content

Instantly share code, notes, and snippets.

@rimzzlabs
Created November 23, 2022 03:15
Show Gist options
  • Save rimzzlabs/71a7eae2a8223e392e59b93527af8b6b to your computer and use it in GitHub Desktop.
Save rimzzlabs/71a7eae2a8223e392e59b93527af8b6b to your computer and use it in GitHub Desktop.
import Axios, { AxiosRequestConfig } from 'axios'
import type { AxiosError } from 'axios'
type Method = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'PUT' | 'OPTIONS'
type HttpReturn<T> = Promise<[T | null, null | AxiosError<T, T>]>
export const httpInstance = Axios.create({
baseURL: import.meta.env.VITE_APP_API_URL
})
export const http = async <T>(
path: string,
method: Method,
config?: AxiosRequestConfig,
data?: unknown
): HttpReturn<T> => {
try {
const res = await httpInstance<T>({ url: path, method, data: data as unknown as typeof data, ...config })
return [res.data, null]
} catch (err) {
return [null, err as AxiosError<T>]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment