Skip to content

Commit

Permalink
fix(useAxios): exception when args incorrect (#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
okxiaoliang4 committed May 3, 2022
1 parent 89c9e53 commit 81f355d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/integrations/useAxios/index.ts
@@ -1,6 +1,6 @@
import type { Ref, ShallowRef } from 'vue-demi'
import { ref, shallowRef } from 'vue-demi'
import { until } from '@vueuse/shared'
import { isString, until } from '@vueuse/shared'
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelTokenSource } from 'axios'
import axios from 'axios'

Expand Down Expand Up @@ -104,28 +104,31 @@ export function useAxios<T = any>(config?: AxiosRequestConfig, instance?: AxiosI
*/
export function useAxios<T = any>(...args: any[]): OverallUseAxiosReturn<T> & PromiseLike<OverallUseAxiosReturn<T>> {
const url: string | undefined = typeof args[0] === 'string' ? args[0] : undefined
const argsPlaceholder = url ? 1 : 0
const argsPlaceholder = isString(url) ? 1 : 0
let defaultConfig: AxiosRequestConfig = {}
let instance: AxiosInstance = axios
let options: UseAxiosOptions = { immediate: !!argsPlaceholder }

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

if (args.length > 0 + argsPlaceholder) {
/**
* Unable to use `instanceof` here becuase of (https://github.com/axios/axios/issues/737)
* so instead we are checking if there is a `requset` on the object to see if it is an
* axios instance
*/
if ('request' in args[0 + argsPlaceholder])
if (isAxiosInstance(args[0 + argsPlaceholder]))
instance = args[0 + argsPlaceholder]
else
defaultConfig = args[0 + argsPlaceholder]
}

if (args.length > 1 + argsPlaceholder) {
if ('request' in args[1 + argsPlaceholder])
if (isAxiosInstance(args[1 + argsPlaceholder]))
instance = args[1 + argsPlaceholder]
}
if (
(args.length === 2 + argsPlaceholder && !('request' in args[1 + argsPlaceholder]))
(args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]))
|| args.length === 3 + argsPlaceholder
)
options = args[args.length - 1]
Expand Down

0 comments on commit 81f355d

Please sign in to comment.