mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-19 14:31:45 +01:00
update types
This commit is contained in:
69
src/runtime/api.ts
Normal file
69
src/runtime/api.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { resolveHTTPResponse } from '@trpc/server'
|
||||
import type {
|
||||
AnyRouter,
|
||||
ProcedureType,
|
||||
ResponseMeta,
|
||||
TRPCError,
|
||||
inferRouterContext,
|
||||
inferRouterError,
|
||||
} from '@trpc/server'
|
||||
import { createURL } from 'ufo'
|
||||
import type { IncomingMessage } from 'h3'
|
||||
import type { TRPCResponse } from '@trpc/server/dist/declarations/src/rpc'
|
||||
import { isMethod, useBody } from 'h3'
|
||||
|
||||
type MaybePromise<T> = T | Promise<T>
|
||||
|
||||
type CreateContextFn<TRouter extends AnyRouter> = (req: IncomingMessage) => MaybePromise<inferRouterContext<TRouter>>
|
||||
|
||||
type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
|
||||
data: TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
||||
ctx?: inferRouterContext<TRouter>
|
||||
paths?: string[]
|
||||
type: ProcedureType | 'unknown'
|
||||
errors: TRPCError[]
|
||||
}) => ResponseMeta
|
||||
|
||||
export function createTRPCHandler<Router extends AnyRouter>({
|
||||
router,
|
||||
createContext,
|
||||
responseMeta,
|
||||
}: {
|
||||
router: Router
|
||||
createContext?: CreateContextFn<Router>
|
||||
responseMeta?: ResponseMetaFn<Router>
|
||||
}) {
|
||||
const url = '/trpc'
|
||||
|
||||
return async (event) => {
|
||||
const {
|
||||
req,
|
||||
res,
|
||||
} = event
|
||||
|
||||
const $url = createURL(req.url)
|
||||
|
||||
const httpResponse = await resolveHTTPResponse({
|
||||
router,
|
||||
req: {
|
||||
method: req.method,
|
||||
headers: req.headers,
|
||||
body: isMethod(event, 'GET') ? null : await useBody(event),
|
||||
query: $url.searchParams,
|
||||
},
|
||||
path: $url.pathname.substring(url.length + 1),
|
||||
createContext: async () => createContext?.(req),
|
||||
responseMeta,
|
||||
})
|
||||
|
||||
const { status, headers, body } = httpResponse
|
||||
|
||||
res.statusCode = status
|
||||
|
||||
Object.keys(headers).forEach((key) => {
|
||||
res.setHeader(key, headers[key])
|
||||
})
|
||||
|
||||
return body
|
||||
}
|
||||
}
|
||||
81
src/runtime/client.ts
Normal file
81
src/runtime/client.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { objectHash } from 'ohash'
|
||||
import type { TRPCClient } from '@trpc/client'
|
||||
import type { AnyRouter, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
|
||||
import type { AsyncData, KeyOfRes, PickFrom, _Transform } from 'nuxt/dist/app/composables/asyncData'
|
||||
// @ts-expect-error: Resolved by Nuxt
|
||||
import { useAsyncData, useLazyAsyncData } from '#imports'
|
||||
|
||||
export function createTRPCComposables<
|
||||
Router extends AnyRouter,
|
||||
Client extends TRPCClient<Router> = TRPCClient<Router>,
|
||||
TQuery extends keyof Router['_def']['queries'] = keyof Router['_def']['queries'],
|
||||
TMutation extends keyof Router['_def']['mutations'] = keyof Router['_def']['mutations'],
|
||||
>(
|
||||
client: Client
|
||||
): {
|
||||
useTrpcQuery: <TRouteKey extends TQuery>(
|
||||
args_0: TRouteKey,
|
||||
args_1: inferProcedureInput<Router['_def']['queries'][TRouteKey]>
|
||||
) => AsyncData<
|
||||
PickFrom<
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>,
|
||||
KeyOfRes<
|
||||
_Transform<
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>,
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>
|
||||
>
|
||||
>
|
||||
>,
|
||||
true | Error
|
||||
>
|
||||
useLazyTrpcQuery: <TRouteKey extends TQuery>(
|
||||
args_0: TRouteKey,
|
||||
args_1: inferProcedureInput<Router['_def']['queries'][TRouteKey]>
|
||||
) => AsyncData<
|
||||
PickFrom<
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>,
|
||||
KeyOfRes<
|
||||
_Transform<
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>,
|
||||
inferProcedureOutput<Router['_def']['queries'][TRouteKey]>
|
||||
>
|
||||
>
|
||||
>,
|
||||
true | Error
|
||||
>
|
||||
useTrpcMutation: <TRouteKey extends TMutation>(
|
||||
args_0: TRouteKey,
|
||||
args_1: inferProcedureInput<Router['_def']['mutations'][TRouteKey]>
|
||||
) => AsyncData<
|
||||
PickFrom<
|
||||
inferProcedureOutput<Router['_def']['mutations'][TRouteKey]>,
|
||||
KeyOfRes<
|
||||
_Transform<
|
||||
inferProcedureOutput<Router['_def']['mutations'][TRouteKey]>,
|
||||
inferProcedureOutput<Router['_def']['mutations'][TRouteKey]>
|
||||
>
|
||||
>
|
||||
>,
|
||||
true | Error
|
||||
>
|
||||
}
|
||||
|
||||
export function createTRPCComposables(client: any) {
|
||||
const useTrpcQuery = (...args) => {
|
||||
return useAsyncData(`trpc-${objectHash(args[0] + (args[1] ? JSON.stringify(args[1]) : ''))}`, () => client.query(...args))
|
||||
}
|
||||
|
||||
const useLazyTrpcQuery = (...args) => {
|
||||
return useLazyAsyncData(`trpc-${objectHash(args[0] + (args[1] ? JSON.stringify(args[1]) : ''))}`, () => client.query(...args))
|
||||
}
|
||||
|
||||
const useTrpcMutation = (...args) => {
|
||||
return useAsyncData(`trpc-${objectHash(args[0] + (args[1] ? JSON.stringify(args[1]) : ''))}`, () => client.mutation(...args))
|
||||
}
|
||||
|
||||
return {
|
||||
useTrpcQuery,
|
||||
useLazyTrpcQuery,
|
||||
useTrpcMutation,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user