Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/runtime/composables/useStrapiClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import type { FetchError, FetchOptions } from 'ofetch'
import { stringify } from 'qs'
import type { Strapi4Error } from '../types/v4'
import type { Strapi3Error } from '../types/v3'
import type { Strapi4Error } from '../types/v4'
import type { Strapi5Error } from '../types/v5'
import { useStrapiUrl } from './useStrapiUrl'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiToken } from './useStrapiToken'
import { useNuxtApp } from '#imports'

const defaultErrors = (err: FetchError) => ({
v5: {
error: {
status: 500,
name: 'UnknownError',
message: err.message,
details: err
}
},
v4: {
error: {
status: 500,
Expand Down Expand Up @@ -57,7 +66,7 @@ export const useStrapiClient = () => {
}
})
} catch (err) {
const e: Strapi4Error | Strapi3Error = err.data || defaultErrors(err)[version]
const e: Strapi3Error | Strapi4Error | Strapi5Error = err.data || defaultErrors(err)[version]

nuxt.hooks.callHook('strapi:error', e)
throw e
Expand All @@ -67,6 +76,6 @@ export const useStrapiClient = () => {

declare module '#app' {
interface RuntimeNuxtHooks {
'strapi:error': (error: Strapi3Error | Strapi4Error) => void
'strapi:error': (error: Strapi3Error | Strapi4Error | Strapi5Error) => void
}
}