refactor: type helpers

This commit is contained in:
Robert Soriano
2022-05-20 11:22:19 -07:00
parent eb1bd0c700
commit f9b0aa002e
4 changed files with 10 additions and 10 deletions

View File

@@ -143,6 +143,7 @@ export const onError = (payload: OnErrorPayload<typeof router>) => {
- [Merging Routers](/recipes/merging-routers.md) - [Merging Routers](/recipes/merging-routers.md)
- [Error Handling](/recipes/error-handling.md) - [Error Handling](/recipes/error-handling.md)
- [Error Formatting](/recipes/error-formatting.md) - [Error Formatting](/recipes/error-formatting.md)
- [Inference Helpers](/recipes/inference-helpers.md)
Learn more about tRPC.io [here](https://trpc.io/docs). Learn more about tRPC.io [here](https://trpc.io/docs).

View File

@@ -20,7 +20,7 @@ export default defineNuxtModule<ModuleOptions>({
}, },
async setup(options, nuxt) { async setup(options, nuxt) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) 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 handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
const trpcOptionsPath = join(nuxt.options.rootDir, 'server/trpc') const trpcOptionsPath = join(nuxt.options.rootDir, 'server/trpc')
@@ -82,7 +82,7 @@ export default defineNuxtModule<ModuleOptions>({
addTemplate({ addTemplate({
filename: 'trpc-helpers.ts', filename: 'trpc-helpers.ts',
write: true, write: true,
src: join(runtimeDir, 'helpers.ts'), src: join(runtimeDir, 'helpers.d.ts'),
}) })
}, },
}) })

View File

@@ -10,11 +10,12 @@ import type { TRPCClientErrorLike } from '@trpc/client'
import { objectHash } from 'ohash' import { objectHash } from 'ohash'
import { useAsyncData, useState } from '#app' import { useAsyncData, useState } from '#app'
import { useClient } from '#build/trpc-client' import { useClient } from '#build/trpc-client'
// @ts-expect-error: Resolved by Nuxt
import type { router } from '~/server/trpc' import type { router } from '~/server/trpc'
type AppRouter = typeof router type AppRouter = typeof router
type inferProcedures< export type inferProcedures<
TObj extends ProcedureRecord<any, any, any, any, any, any>, TObj extends ProcedureRecord<any, any, any, any, any, any>,
> = { > = {
[TPath in keyof TObj]: { [TPath in keyof TObj]: {
@@ -23,10 +24,10 @@ type inferProcedures<
}; };
} }
type TQueries = AppRouter['_def']['queries'] export type TQueries = AppRouter['_def']['queries']
type TError = TRPCClientErrorLike<AppRouter> export type TError = TRPCClientErrorLike<AppRouter>
type TQueryValues = inferProcedures<AppRouter['_def']['queries']> export type TQueryValues = inferProcedures<AppRouter['_def']['queries']>
export async function useAsyncQuery< export async function useAsyncQuery<
TPath extends keyof TQueryValues & string, TPath extends keyof TQueryValues & string,

View File

@@ -1,10 +1,8 @@
import type { inferProcedureInput, inferProcedureOutput, inferSubscriptionOutput } from '@trpc/server' import type { inferProcedureInput, inferProcedureOutput, inferSubscriptionOutput } from '@trpc/server'
// @ts-expect-error: Resolved by Nuxt
import type { router } from '~/server/trpc' import type { router } from '~/server/trpc'
/** type AppRouter = typeof router
* The main router type
*/
export type AppRouter = typeof router
/** /**
* Enum containing all api query paths * Enum containing all api query paths