mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
rename api to server
This commit is contained in:
@@ -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>
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
}
|
||||
Reference in New Issue
Block a user