Skip to content

Global request and response interceptors #56

Answered by johannschopplich
jony1993 asked this question in Q&A
Discussion options

You must be logged in to vote

Sure! Since both generated composables useMyApiData and $myApi use ofetch under the hood just like useAsyncData, respectively $fetch, you can use the supported interceptors.

  • onRequest
  • onResponse
  • etc.

See below for a full example. The real API is wrapped to handle auth state:

import { useAuthStore } from '~/stores/auth'
import { toValue } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import type { MaybeRefOrGetter } from '@vueuse/core'
import type { ApiFetchOptions } from 'nuxt-api-party/dist/runtime/composables/$api'
import type { UseApiDataOptions } from 'nuxt-api-party/dist/runtime/composables/useApiData'

export async function $api<T = any>(
  path: string,
  opts: ApiFetch…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by johannschopplich
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants