Created
November 23, 2022 03:15
-
-
Save rimzzlabs/71a7eae2a8223e392e59b93527af8b6b to your computer and use it in GitHub Desktop.
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
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