rename api to server

This commit is contained in:
Robert Soriano
2022-10-29 19:12:44 -07:00
parent 9d07dab57d
commit b0e7cc1854
2 changed files with 0 additions and 75 deletions

View File

@@ -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<AppRouter>({
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<AppRouter>
}
}

View File

@@ -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> = T | Promise<T>
@@ -91,26 +88,3 @@ export function createTRPCHandler<Router extends AnyRouter>({
return body
})
}
export function createTRPCNuxtClient<R extends AnyRouter>(opts: CreateTRPCClientOptions<R>) {
const client = createTRPCProxyClient(opts)
// Object.keys(client).forEach((path) => {
// clientWithOther[path] = {}
// Object.keys(client[path]).forEach((action) => {
// clientWithOther[path][action] = (input: inferRouterInputs<R>) => {
// // @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<R>
}