From b0e7cc18544f310d084c94388ef28876ac9f1a86 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Sat, 29 Oct 2022 19:12:44 -0700 Subject: [PATCH] rename api to server --- src/runtime/plugin.ts | 49 ------------------------------- src/runtime/{api.ts => server.ts} | 26 ---------------- 2 files changed, 75 deletions(-) delete mode 100644 src/runtime/plugin.ts rename src/runtime/{api.ts => server.ts} (71%) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts deleted file mode 100644 index 393f003..0000000 --- a/src/runtime/plugin.ts +++ /dev/null @@ -1,49 +0,0 @@ -import type { inferRouterProxyClient } from '@trpc/client' -import { createTRPCProxyClient, httpBatchLink } from '@trpc/client' -import { unref } from 'vue' -import { FetchError } from 'ohmyfetch' -import { useClientHeaders } from './client' -import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app' -import type { AppRouter } from '~/server/trpc' - -export default defineNuxtPlugin((nuxtApp) => { - const config = useRuntimeConfig().public.trpc - const headers = useRequestHeaders() - const otherHeaders = useClientHeaders() - - const baseURL = process.server ? '' : config.baseURL - - const client = createTRPCProxyClient({ - links: [ - httpBatchLink({ - url: `${baseURL}${config.endpoint}`, - headers: () => { - return { - ...unref(otherHeaders), - ...headers, - } - }, - fetch: (input, options) => - 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) -}) - -declare module '#app' { - interface NuxtApp { - $client: inferRouterProxyClient - } -} diff --git a/src/runtime/api.ts b/src/runtime/server.ts similarity index 71% rename from src/runtime/api.ts rename to src/runtime/server.ts index e07ef7f..d500cb1 100644 --- a/src/runtime/api.ts +++ b/src/runtime/server.ts @@ -11,9 +11,6 @@ import { createURL } from 'ufo' import type { H3Event } from 'h3' import { defineEventHandler, isMethod, readBody } from 'h3' import type { TRPCResponse } from '@trpc/server/rpc' -import type { CreateTRPCClientOptions, inferRouterProxyClient } from '@trpc/client' -import { createTRPCProxyClient } from '@trpc/client' -import { toRaw } from 'vue' type MaybePromise = T | Promise @@ -91,26 +88,3 @@ export function createTRPCHandler({ return body }) } - -export function createTRPCNuxtClient(opts: CreateTRPCClientOptions) { - const client = createTRPCProxyClient(opts) - - // Object.keys(client).forEach((path) => { - // clientWithOther[path] = {} - // Object.keys(client[path]).forEach((action) => { - // clientWithOther[path][action] = (input: inferRouterInputs) => { - // // @ts-expect-error: asd - // return useAsyncData(`${path}-${action}`, () => client[path][action](input)) - // } - // }) - // }) - - const proxiedClient = new Proxy({}, { - get(target, property) { - // @ts-expect-error: Nuxt - return () => useAsyncData(`${target}-${property}`, () => client.getTodos.query()) - }, - }) - - return proxiedClient as inferRouterProxyClient -}