add custom fetch function for node 18

This commit is contained in:
Robert Soriano
2022-10-29 19:12:01 -07:00
parent 7257842438
commit 9d07dab57d
2 changed files with 15 additions and 23 deletions

View File

@@ -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
View File

@@ -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;