Created
August 21, 2021 06:02
-
-
Save mortezasabihi/c820bae00ac9a873d8da7012ef6ccfb2 to your computer and use it in GitHub Desktop.
Vue 3 axios composition api
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
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | |
import { reactive, toRefs } from 'vue'; | |
import axios,{ AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; | |
export enum Status { | |
IDLE = 'idle', | |
LOADING = 'loading', | |
SUCCESS = 'success', | |
ERROR = 'error', | |
} | |
export interface ErrorType { | |
error: string; | |
message: string; | |
statusCode: number; | |
} | |
type Options = Pick<AxiosRequestConfig, 'url' | 'method' | 'data' | 'params'>; | |
interface Result<K> { | |
status: Status; | |
response: null | AxiosResponse<K>; | |
error: null | ErrorType; | |
} | |
export default function useApi<T>() { | |
const result = reactive<Result<T>>({ | |
status: Status.IDLE, | |
error: null, | |
response: null, | |
}); | |
async function call({ url, method, data, params }: Options) { | |
result.status = Status.LOADING; | |
try { | |
const requestResponse = await axios.request({ | |
url, | |
method, | |
data, | |
params, | |
}); | |
result.response = requestResponse; | |
result.status = Status.SUCCESS; | |
} catch (error) { | |
const { response } = error as AxiosError; | |
result.error = response?.data; | |
result.status = Status.ERROR; | |
if (result.error?.statusCode === 500) { | |
console.log('Something went wrong!') | |
} | |
} | |
} | |
return { ...toRefs(result), call }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment