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): support RequestConfig for execute #2152

Merged
merged 2 commits into from Sep 2, 2022
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
9 changes: 9 additions & 0 deletions packages/integrations/useAxios/index.md
Expand Up @@ -62,6 +62,15 @@ const { execute } = useAxios(url1, {}, { immediate: false })
execute(url2)
```

The `execute` function can accept `config` only.
```ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })
execute({ params: { key: 1 } })
execute({ params: { key: 2 } })
```

The `execute` function resolves with a result of network request.
```ts
import { useAxios } from '@vueuse/integrations/useAxios'
Expand Down
25 changes: 25 additions & 0 deletions packages/integrations/useAxios/index.test.ts
Expand Up @@ -185,4 +185,29 @@ describe('useAxios', () => {
expect(onRejected).toBeCalledTimes(0)
}, onRejected)
})

test('calling axios with config change(param/data etc.) only', async () => {
const { isLoading, then, execute } = useAxios('/comments', config, instance, options)
expect(isLoading.value).toBeFalsy()
const paramConfig: AxiosRequestConfig = { params: { postId: 1 } }
execute(paramConfig)
expect(isLoading.value).toBeTruthy()
const onRejected = vitest.fn()

await then((result) => {
expect(result.data.value[0].postId).toBe(1)
expect(isLoading.value).toBeFalsy()
expect(onRejected).toBeCalledTimes(0)
}, onRejected)

paramConfig.params = { postId: 2 }
execute(paramConfig)
expect(isLoading.value).toBeTruthy()

await then((result) => {
expect(result.data.value[0].postId).toBe(2)
expect(isLoading.value).toBeFalsy()
expect(onRejected).toBeCalledTimes(0)
}, onRejected)
})
})
4 changes: 2 additions & 2 deletions packages/integrations/useAxios/index.ts
Expand Up @@ -78,7 +78,7 @@ export interface StrictUseAxiosReturn<T> extends UseAxiosReturn<T> {
/**
* Manually call the axios request
*/
execute: (url?: string, config?: AxiosRequestConfig) => PromiseLike<StrictUseAxiosReturn<T>>
execute: (url?: string | AxiosRequestConfig, config?: AxiosRequestConfig) => PromiseLike<StrictUseAxiosReturn<T>>
}
export interface EasyUseAxiosReturn<T> extends UseAxiosReturn<T> {
/**
Expand Down Expand Up @@ -174,7 +174,7 @@ export function useAxios<T = any>(...args: any[]): OverallUseAxiosReturn<T> & Pr
? executeUrl
: url ?? ''
loading(true)
instance(_url, { ...defaultConfig, ...config, cancelToken: cancelToken.token })
instance(_url, { ...defaultConfig, ...typeof executeUrl === 'object' ? executeUrl : config, cancelToken: cancelToken.token })
.then((r: any) => {
response.value = r
data.value = r.data
Expand Down