mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-28 02:40:33 +01:00
fix: return original FetchResponse when ohmyfetch throws error
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import * as trpc from '@trpc/client'
|
import * as trpc from '@trpc/client'
|
||||||
import { unref } from 'vue'
|
import { unref } from 'vue'
|
||||||
|
import { FetchError } from 'ohmyfetch'
|
||||||
import { useClientHeaders } from './client'
|
import { useClientHeaders } from './client'
|
||||||
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
@@ -21,10 +22,17 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
fetch: (input, options) =>
|
fetch: (input, options) =>
|
||||||
globalThis.$fetch.raw(input.toString(), options).then(response => ({
|
globalThis.$fetch.raw(input.toString(), options)
|
||||||
...response,
|
.catch((e) => {
|
||||||
json: () => Promise.resolve(response._data),
|
if (e instanceof FetchError && e.response)
|
||||||
})),
|
return e.response
|
||||||
|
|
||||||
|
throw e
|
||||||
|
})
|
||||||
|
.then(response => ({
|
||||||
|
...response,
|
||||||
|
json: () => Promise.resolve(response._data),
|
||||||
|
})),
|
||||||
})
|
})
|
||||||
|
|
||||||
nuxtApp.provide('client', client)
|
nuxtApp.provide('client', client)
|
||||||
|
|||||||
Reference in New Issue
Block a user