Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useAxios): added onFinish callback #2829

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/integrations/useAxios/index.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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 @@ -211,7 +215,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