From 9d07dab57d89c64bf085808ec15c6fe6fa2aa890 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Sat, 29 Oct 2022 19:12:01 -0700 Subject: [PATCH] add custom fetch function for node 18 --- src/runtime/client.ts | 15 +++++++++++++++ test.js | 23 ----------------------- 2 files changed, 15 insertions(+), 23 deletions(-) delete mode 100644 test.js diff --git a/src/runtime/client.ts b/src/runtime/client.ts index ae3d165..bc17bbf 100644 --- a/src/runtime/client.ts +++ b/src/runtime/client.ts @@ -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(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), + })) +} diff --git a/test.js b/test.js deleted file mode 100644 index 8ebf707..0000000 --- a/test.js +++ /dev/null @@ -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;