From 81f355d8ec8a2cc643969b3a98f2adcb0a4563d9 Mon Sep 17 00:00:00 2001 From: Jelf <353742991@qq.com> Date: Wed, 4 May 2022 00:29:51 +0800 Subject: [PATCH] fix(useAxios): exception when `args` incorrect (#1534) --- packages/integrations/useAxios/index.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/integrations/useAxios/index.ts b/packages/integrations/useAxios/index.ts index 0233e2c5efc..0836ba1a35a 100644 --- a/packages/integrations/useAxios/index.ts +++ b/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' @@ -104,28 +104,31 @@ export function useAxios(config?: AxiosRequestConfig, instance?: AxiosI */ export function useAxios(...args: any[]): OverallUseAxiosReturn & PromiseLike> { 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]