API Interceptors

To make API requests, we use apiClient, which is a type of singleton instance. Thanks to this, you can set up common options and global interceptors for all requests made by the nuxt-typo3 app.

All requests are handled by native fetch wrapped by ofetch

ofetch provides four interceptors:

To do this, you can utilize useT3Api composable and setOption function.

plugins/setInterceptors.ts
export default defineNuxtPlugin(() => {
  const { setOption } = useT3Api()

  setOption('onRequest', (context) => {
    console.log('on request: ', context)
  })

  setOption('onRequestError', (context) => {
    console.log('on request error: ', context)
  })

  setOption('onResponse', (context) => {
    console.log('on response: ', context)
  })

  setOption('onResponseError', (context) => {
    console.log('on response error: ', context)
  })
})
Please note that you can only set up global interceptors once, as a second setup will override the previous one. If you wish to establish multiple interceptors, you can achieve this by integrating our solution with additional external handling.