Skip to content

Commit

Permalink
feat(useAxios): added onFinish callback (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Mar 28, 2023
1 parent e4ac088 commit a2f334d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/integrations/useAxios/index.test.ts
Expand Up @@ -300,4 +300,15 @@ describe('useAxios', () => {
expect(isFinished.value).toBeTruthy()
expect(isLoading.value).toBeFalsy()
})

test('should call onFinish', async () => {
const onFinish = vitest.fn()
const { execute, isLoading, isFinished } = useAxios(url, config, { ...options, onFinish })
expect(isLoading.value).toBeFalsy()

await execute()
expect(onFinish).toHaveBeenCalled()
expect(isFinished.value).toBeTruthy()
expect(isLoading.value).toBeFalsy()
})
})
9 changes: 8 additions & 1 deletion packages/integrations/useAxios/index.ts
Expand Up @@ -109,6 +109,10 @@ export interface UseAxiosOptions<T = any> {
* Callback when success is caught.
*/
onSuccess?: (data: T) => void
/**
* Callback when request is finished.
*/
onFinish?: () => void
}
type OverallUseAxiosReturn<T, R, D> = StrictUseAxiosReturn<T, R, D> | EasyUseAxiosReturn<T, R, D>

Expand Down Expand Up @@ -215,7 +219,10 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[])
error.value = e
options.onError?.(e)
})
.finally(() => loading(false))
.finally(() => {
options.onFinish?.()
loading(false)
})
return { then }
}
if (options.immediate && url)
Expand Down

0 comments on commit a2f334d

Please sign in to comment.