From f9b0aa002e5d3c1074bca4327b0644bcdfdf67bd Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Fri, 20 May 2022 11:22:19 -0700 Subject: [PATCH] refactor: type helpers --- README.md | 1 + src/module.ts | 4 ++-- src/runtime/client.ts | 9 +++++---- src/runtime/{helpers.ts => helpers.d.ts} | 6 ++---- 4 files changed, 10 insertions(+), 10 deletions(-) rename src/runtime/{helpers.ts => helpers.d.ts} (97%) diff --git a/README.md b/README.md index dba7999..8ad8789 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ export const onError = (payload: OnErrorPayload) => { - [Merging Routers](/recipes/merging-routers.md) - [Error Handling](/recipes/error-handling.md) - [Error Formatting](/recipes/error-formatting.md) +- [Inference Helpers](/recipes/inference-helpers.md) Learn more about tRPC.io [here](https://trpc.io/docs). diff --git a/src/module.ts b/src/module.ts index 42680af..cba0f04 100644 --- a/src/module.ts +++ b/src/module.ts @@ -20,7 +20,7 @@ export default defineNuxtModule({ }, async setup(options, nuxt) { const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) - nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-client', '#build/trpc-handler') + nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-client', '#build/trpc-handler', '#build/trpc-helpers') const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts') const trpcOptionsPath = join(nuxt.options.rootDir, 'server/trpc') @@ -82,7 +82,7 @@ export default defineNuxtModule({ addTemplate({ filename: 'trpc-helpers.ts', write: true, - src: join(runtimeDir, 'helpers.ts'), + src: join(runtimeDir, 'helpers.d.ts'), }) }, }) diff --git a/src/runtime/client.ts b/src/runtime/client.ts index daa7df3..66c421a 100644 --- a/src/runtime/client.ts +++ b/src/runtime/client.ts @@ -10,11 +10,12 @@ import type { TRPCClientErrorLike } from '@trpc/client' import { objectHash } from 'ohash' import { useAsyncData, useState } from '#app' import { useClient } from '#build/trpc-client' +// @ts-expect-error: Resolved by Nuxt import type { router } from '~/server/trpc' type AppRouter = typeof router -type inferProcedures< +export type inferProcedures< TObj extends ProcedureRecord, > = { [TPath in keyof TObj]: { @@ -23,10 +24,10 @@ type inferProcedures< }; } -type TQueries = AppRouter['_def']['queries'] -type TError = TRPCClientErrorLike +export type TQueries = AppRouter['_def']['queries'] +export type TError = TRPCClientErrorLike -type TQueryValues = inferProcedures +export type TQueryValues = inferProcedures export async function useAsyncQuery< TPath extends keyof TQueryValues & string, diff --git a/src/runtime/helpers.ts b/src/runtime/helpers.d.ts similarity index 97% rename from src/runtime/helpers.ts rename to src/runtime/helpers.d.ts index 2379fd6..f405d26 100644 --- a/src/runtime/helpers.ts +++ b/src/runtime/helpers.d.ts @@ -1,10 +1,8 @@ import type { inferProcedureInput, inferProcedureOutput, inferSubscriptionOutput } from '@trpc/server' +// @ts-expect-error: Resolved by Nuxt import type { router } from '~/server/trpc' -/** - * The main router type - */ -export type AppRouter = typeof router +type AppRouter = typeof router /** * Enum containing all api query paths