Skip to content

Commit

Permalink
fix(useAxios): ignore undefined options (#3662)
Browse files Browse the repository at this point in the history
Co-authored-by: Doctorwu <44631608+Doctor-wu@users.noreply.github.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
Co-authored-by: Doctorwu <wuzhouchun@qq.com>
Co-authored-by: Doctorwu <doctorwu@moego.pet>
  • Loading branch information
5 people committed Jan 9, 2024
1 parent 2171098 commit 37eaea3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/integrations/useAxios/index.test.ts
Expand Up @@ -346,6 +346,10 @@ describe.skipIf(isBelowNode18)('useAxios', () => {
expect(data.value).toEqual({ value: 1 })
})

it('should not crash when options is undefined', async () => {
await useAxios(url, config, undefined)
})

it('should reset data when execute', async () => {
interface ResType {
id: number
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/useAxios/index.ts
Expand Up @@ -118,12 +118,13 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(config?: AxiosR
export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[]): OverallUseAxiosReturn<T, R, D> & Promise<OverallUseAxiosReturn<T, R, D>> {
const url: string | undefined = typeof args[0] === 'string' ? args[0] : undefined
const argsPlaceholder = typeof url === 'string' ? 1 : 0
let defaultConfig: AxiosRequestConfig<D> = {}
let instance: AxiosInstance = axios
let options: UseAxiosOptions<T> = {
const defaultOptions: UseAxiosOptions<T> = {
immediate: !!argsPlaceholder,
shallow: true,
}
let defaultConfig: AxiosRequestConfig<D> = {}
let instance: AxiosInstance = axios
let options: UseAxiosOptions<T> = defaultOptions

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

Expand All @@ -147,7 +148,7 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[])
(args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]))
|| args.length === 3 + argsPlaceholder
)
options = args[args.length - 1]
options = args[args.length - 1] || defaultOptions

const {
initialData,
Expand Down

0 comments on commit 37eaea3

Please sign in to comment.