import type { FetchError , FetchOptions } from 'ohmyfetch' ; import { stringify } from 'qs' ; import type { Strapi4Error } from '../types/v4' ; import type { Strapi3Error } from '../types/v3' ; import { useStrapiUrl } from './useStrapiUrl' ; import { useStrapiVersion } from './useStrapiVersion' ; import { useStrapiToken } from './useStrapiToken' ; import { useNuxtApp } from '#app' ; const defaultErrors = ( err ) => ( { v4 : { error : { status : 500 , name : 'UnknownError' , message : err . message , details : err } } , v3 : { error : 'UnknownError' , message : err . message , statusCode : 500 } } ) ; export const useStrapiClient = ( ) => { const nuxt = useNuxtApp ( ) ; const baseURL = useStrapiUrl ( ) ; const version = useStrapiVersion ( ) ; const token = useStrapiToken ( ) ; return async < T > ( url , fetchOptions = { } ) : Promise < T > => { const headers = { } ; if ( token && token . value ) { headers . Authorization = `Bearer ${ token . value }` ; } if ( version === 'v4' && fetchOptions . params ) { url = `${ url }?${ stringify ( fetchOptions . params , { encodeValuesOnly : true } ) }` ; delete fetchOptions . params ; } try { return await $fetch < T > ( url , { retry : 0 , baseURL , ... fetchOptions , headers : { ... headers , ... fetchOptions . headers } } ) ; } catch ( err ) { const e = err . response ?. data || defaultErrors ( err ) [ version ] ; nuxt . hooks . callHook ( 'strapi:error' as any , e ) ; throw e ; } } ; } ;