Skip to content

Commit

Permalink
fix(useAxios)!: error should return type unknown (#2807)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
jbaubree and antfu committed Mar 4, 2023
1 parent fff4532 commit d8d732e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/integrations/useAxios/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('useAxios', () => {
const { isLoading, isFinished, isAborted, execute, abort } = useAxios(url, config, options)
expect(isLoading.value).toBeFalsy()
execute('https://jsonplaceholder.typicode.com/todos/2').then((result) => {
expect(result.error.value?.message).toBe('aborted')
expect((result.error.value as Error)?.message).toBe('aborted')
expect(isFinished.value).toBeTruthy()
expect(isLoading.value).toBeFalsy()
expect(isAborted.value).toBeTruthy()
Expand Down
10 changes: 7 additions & 3 deletions packages/integrations/useAxios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface UseAxiosReturn<T, R = AxiosResponse<T>, D = any> {
/**
* Any errors that may have occurred
*/
error: ShallowRef<AxiosError<T, D> | undefined>
error: ShallowRef<unknown | undefined>

/**
* Aborts the current request
Expand Down Expand Up @@ -92,6 +92,7 @@ export interface UseAxiosOptions<T = any> {
*
*/
immediate?: boolean

/**
* Use shallowRef.
*
Expand Down Expand Up @@ -128,7 +129,10 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[])
const argsPlaceholder = isString(url) ? 1 : 0
let defaultConfig: RawAxiosRequestConfig<D> = {}
let instance: AxiosInstance = axios
let options: UseAxiosOptions<T> = { immediate: !!argsPlaceholder, shallow: true }
let options: UseAxiosOptions<T> = {
immediate: !!argsPlaceholder,
shallow: true,
}

const isAxiosInstance = (val: any) => !!val?.request

Expand Down Expand Up @@ -159,7 +163,7 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[])
const isFinished = ref(false)
const isLoading = ref(false)
const isAborted = ref(false)
const error = shallowRef<AxiosError<T>>()
const error = shallowRef<unknown>()

const cancelTokenSource = axios.CancelToken.source
let cancelToken: CancelTokenSource = cancelTokenSource()
Expand Down

0 comments on commit d8d732e

Please sign in to comment.