Skip to content

⬅️ Back to Table of Contents

πŸ“„ useAxios

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 9
πŸ“¦ Imports 11
⚑ Async/Await Patterns 2
πŸ“ Interfaces 5
πŸ“‘ Type Aliases 2

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/integrations/useAxios/index.ts

πŸ“¦ Imports

Name Source
AxiosInstance axios
AxiosRequestConfig axios
AxiosResponse axios
Ref vue
ShallowRef vue
noop @vueuse/shared
until @vueuse/shared
AxiosError axios
axios axios
deepRef vue
shallowRef vue

Async/Await Patterns

Type Function Await Expressions Promise Chains
promise-chain waitUntilFinished none new Promise(...), until(isFinished).toBe(true).then
promise-chain execute none instance(_url, { ...defaultConfig, ...typeof executeUrl === 'object' ? execut...

Functions

useAxios(url: string, config: AxiosRequestConfig<D>, options: O): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosRe…

Parameters:

  • url string
  • config AxiosRequestConfig<D>
  • options O

Returns: StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>

Code
export function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsWithInitialData<T> = UseAxiosOptionsWithInitialData<T>>(url: string, config?: AxiosRequestConfig<D>, options?: O): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>

Internal helpers

Declared inside another function in this file.

isAxiosInstance(val: any): boolean

Parameters:

  • val any

Returns: boolean

Code
(val: any) => !!val?.request

abort(message: string): void

Parameters:

  • message string

Returns: void

Calls:

  • abortController.abort
Code
(message?: string) => {
    if (isFinished.value || !isLoading.value)
      return

    abortController.abort(message)
    abortController = new AbortController()
    isAborted.value = true
    isLoading.value = false
    isFinished.value = false
  }

loading(loading: boolean): void

Parameters:

  • loading boolean

Returns: void

Code
(loading: boolean) => {
    isLoading.value = loading
    isFinished.value = !loading
  }

resetData(): void

Returns: void

Code
() => {
    if (resetOnExecute)
      data.value = initialData!
  }

waitUntilFinished(): Promise<OverallUseAxiosReturn<T, R, D>>

Returns: Promise<OverallUseAxiosReturn<T, R, D>>

Code
() =>
    new Promise<OverallUseAxiosReturn<T, R, D>>((resolve, reject) => {
      until(isFinished).toBe(true).then(() => error.value
        ? reject(error.value)
        // eslint-disable-next-line ts/no-use-before-define
        : resolve(result))
    })

then(args: [onfulfilled?: (value: OverallUseAxiosR…): Promise<TResult1 | TResult2>

Parameters:

  • args [onfulfilled?: (value: OverallUseAxiosReturn<T, R, D>) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<...>]

Returns: Promise<TResult1 | TResult2>

Calls:

  • waitUntilFinished().then
Code
(...args) => waitUntilFinished().then(...args)

catch(args: [onrejected?: (reason: any) => TResult …): Promise<OverallUseAxiosReturn<T, R, D> | TResult>

Parameters:

  • args [onrejected?: (reason: any) => TResult | PromiseLike<TResult>]

Returns: Promise<OverallUseAxiosReturn<T, R, D> | TResult>

Calls:

  • waitUntilFinished().catch
Code
(...args) => waitUntilFinished().catch(...args)

execute(executeUrl: string | AxiosRequestConfig<D> | undefi…, config: AxiosRequestConfig<D>): Promise<OverallUseAxiosReturn<T, R, D>>

Parameters:

  • executeUrl string | AxiosRequestConfig<D> | undefined
  • config AxiosRequestConfig<D>

Returns: Promise<OverallUseAxiosReturn<T, R, D>>

Calls:

  • resetData
  • abort
  • loading
  • instance(_url, { ...defaultConfig, ...typeof executeUrl === 'object' ? executeUrl : config, signal: abortController.signal }) .then((r: any) => { if (isAborted.value) return response.value = r const result = r?.data data.value = result onSuccess(result) }) .catch((e: any) => { error.value = e onError(e) }) .finally
  • options.onFinish
Code
(executeUrl: string | AxiosRequestConfig<D> | undefined = url, config: AxiosRequestConfig<D> = {}) => {
    error.value = undefined
    const _url = typeof executeUrl === 'string'
      ? executeUrl
      : url ?? config.url

    if (_url === undefined) {
      error.value = new AxiosError(AxiosError.ERR_INVALID_URL)
      isFinished.value = true
      return promise
    }
    resetData()

    if (options.abortPrevious !== false)
      abort()

    loading(true)

    executeCounter += 1
    const currentExecuteCounter = executeCounter
    isAborted.value = false

    instance(_url, { ...defaultConfig, ...typeof executeUrl === 'object' ? executeUrl : config, signal: abortController.signal })
      .then((r: any) => {
        if (isAborted.value)
          return
        response.value = r
        const result = r?.data
        data.value = result
        onSuccess(result)
      })
      .catch((e: any) => {
        error.value = e
        onError(e)
      })
      .finally(() => {
        options.onFinish?.()
        if (currentExecuteCounter === executeCounter)
          loading(false)
      })
    return promise
  }

Interfaces

UseAxiosReturn<T, R = AxiosResponse<T>, _D = any, O extends UseAxiosOptions = UseAxiosOptions<T>>

Interface Code
export interface UseAxiosReturn<T, R = AxiosResponse<T>, _D = any, O extends UseAxiosOptions = UseAxiosOptions<T>> {
  /**
   * Axios Response
   */
  response: ShallowRef<R | undefined>

  /**
   * Axios response data
   */
  data: O extends UseAxiosOptionsWithInitialData<T> ? Ref<T> : Ref<T | undefined>

  /**
   * Indicates if the request has finished
   */
  isFinished: Ref<boolean>

  /**
   * Indicates if the request is currently loading
   */
  isLoading: Ref<boolean>

  /**
   * Indicates if the request was canceled
   */
  isAborted: Ref<boolean>

  /**
   * Any errors that may have occurred
   */
  error: ShallowRef<unknown | undefined>

  /**
   * Aborts the current request
   */
  abort: (message?: string | undefined) => void

  /**
   * Alias to `abort`
   */
  cancel: (message?: string | undefined) => void

  /**
   * Alias to `isAborted`
   */
  isCanceled: Ref<boolean>
}

Properties

Name Type Optional Description
response ShallowRef<R \| undefined> βœ— not shown
data O extends UseAxiosOptionsWithInitialData<T> ? Ref<T> : Ref<T \| undefined> βœ— not shown
isFinished Ref<boolean> βœ— not shown
isLoading Ref<boolean> βœ— not shown
isAborted Ref<boolean> βœ— not shown
error ShallowRef<unknown \| undefined> βœ— not shown
abort (message?: string \| undefined) => void βœ— not shown
cancel (message?: string \| undefined) => void βœ— not shown
isCanceled Ref<boolean> βœ— not shown

StrictUseAxiosReturn<T, R, D, O extends UseAxiosOptions = UseAxiosOptions<T>>

Interface Code
export interface StrictUseAxiosReturn<T, R, D, O extends UseAxiosOptions = UseAxiosOptions<T>> extends UseAxiosReturn<T, R, D, O> {
  /**
   * Manually call the axios request
   */
  execute: (url?: string | AxiosRequestConfig<D>, config?: AxiosRequestConfig<D>) => Promise<StrictUseAxiosReturn<T, R, D, O>>
}

Properties

Name Type Optional Description
execute (url?: string \| AxiosRequestConfig<D>, config?: AxiosRequestConfig<D>) => Pr... βœ— not shown

EasyUseAxiosReturn<T, R, D>

Interface Code
export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
  /**
   * Manually call the axios request
   */
  execute: (url: string, config?: AxiosRequestConfig<D>) => Promise<EasyUseAxiosReturn<T, R, D>>
}

Properties

Name Type Optional Description
execute (url: string, config?: AxiosRequestConfig<D>) => Promise<EasyUseAxiosReturn<T... βœ— not shown

UseAxiosOptionsBase<T = any>

Interface Code
export interface UseAxiosOptionsBase<T = any> {
  /**
   * Will automatically run axios request when `useAxios` is used
   *
   */
  immediate?: boolean

  /**
   * Use shallowRef.
   *
   * @default true
   */
  shallow?: boolean

  /**
   * Abort previous request when a new request is made.
   *
   * @default true
   */
  abortPrevious?: boolean

  /**
   * Callback when error is caught.
   */
  onError?: (e: unknown) => void

  /**
   * Callback when success is caught.
   */
  onSuccess?: (data: T) => void

  /**
   * Sets the state to initialState before executing the promise.
   */
  resetOnExecute?: boolean

  /**
   * Callback when request is finished.
   */
  onFinish?: () => void
}

Properties

Name Type Optional Description
immediate boolean βœ“ not shown
shallow boolean βœ“ not shown
abortPrevious boolean βœ“ not shown
onError (e: unknown) => void βœ“ not shown
onSuccess (data: T) => void βœ“ not shown
resetOnExecute boolean βœ“ not shown
onFinish () => void βœ“ not shown

UseAxiosOptionsWithInitialData<T>

Interface Code
export interface UseAxiosOptionsWithInitialData<T> extends UseAxiosOptionsBase<T> {
  /**
   * Initial data
   */
  initialData: T
}

Properties

Name Type Optional Description
initialData T βœ— not shown

Type Aliases

UseAxiosOptions<T = any>

type UseAxiosOptions<T = any> = UseAxiosOptionsBase<T> | UseAxiosOptionsWithInitialData<T>;

OverallUseAxiosReturn<T, R, D>

type OverallUseAxiosReturn<T, R, D> = StrictUseAxiosReturn<T, R, D> | EasyUseAxiosReturn<T, R, D>;

Generated by Syntax Scribe