mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-19 22:41:41 +01:00
add custom fetch function for node 18
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { CreateTRPCClientOptions, TRPCClientErrorLike, inferRouterProxyClient } from '@trpc/client'
|
||||
import { createTRPCProxyClient } from '@trpc/client'
|
||||
import { FetchError } from 'ohmyfetch'
|
||||
import type {
|
||||
AnyMutationProcedure,
|
||||
AnyProcedure,
|
||||
@@ -111,3 +112,17 @@ export function createTRPCNuxtProxyClient<TRouter extends AnyRouter>(opts: Creat
|
||||
|
||||
return decoratedClient
|
||||
}
|
||||
|
||||
export function customFetch(input: RequestInfo | URL, options?: RequestInit) {
|
||||
return 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),
|
||||
}))
|
||||
}
|
||||
|
||||
23
test.js
23
test.js
@@ -1,23 +0,0 @@
|
||||
const data = {
|
||||
hello: {
|
||||
log() {
|
||||
return 'hello log';
|
||||
},
|
||||
},
|
||||
hi: {
|
||||
log() {
|
||||
return 'hi log'
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const blankObject = {};
|
||||
|
||||
const proxy = new Proxy(blankObject, {
|
||||
get(target, key) {
|
||||
if (key in data) return data[key]; // <---
|
||||
return target[key]; // default
|
||||
}
|
||||
});
|
||||
|
||||
console.log(proxy.hello.log()); // hello log;
|
||||
Reference in New Issue
Block a user