fix: return original FetchResponse when ohmyfetch throws error

This commit is contained in:
Robin Wong
2022-09-18 09:46:59 +00:00
parent 453845fb14
commit 7962c7f61e

View File

@@ -1,5 +1,6 @@
import * as trpc from '@trpc/client'
import { unref } from 'vue'
import { FetchError } from 'ohmyfetch'
import { useClientHeaders } from './client'
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
import type { router } from '~/server/trpc'
@@ -21,10 +22,17 @@ export default defineNuxtPlugin((nuxtApp) => {
}
},
fetch: (input, options) =>
globalThis.$fetch.raw(input.toString(), options).then(response => ({
...response,
json: () => Promise.resolve(response._data),
})),
globalThis.$fetch.raw(input.toString(), options)
.catch((e) => {
if (e instanceof FetchError && e.response)
return e.response
throw e
})
.then(response => ({
...response,
json: () => Promise.resolve(response._data),
})),
})
nuxtApp.provide('client', client)