Compare commits

...

10 Commits

Author SHA1 Message Date
Robert Soriano
c95d46f43a release v0.1.7 2022-05-20 11:44:02 -07:00
Robert Soriano
2844cc0bbd refactor: remove helper types 2022-05-20 11:43:58 -07:00
Robert Soriano
aeb2e1b8e3 docs: add inference helpers recipe 2022-05-20 11:42:40 -07:00
Robert Soriano
68d9eb2461 release v0.1.6 2022-05-20 11:30:09 -07:00
Robert Soriano
109a07a42d feat: add helper types 2022-05-20 11:30:04 -07:00
Robert Soriano
7775e59b0c release v0.1.5 2022-05-20 11:26:48 -07:00
Robert Soriano
c23af214a3 remove helpers 2022-05-20 11:26:43 -07:00
Robert Soriano
7851846ad5 release v0.1.4 2022-05-20 11:22:30 -07:00
Robert Soriano
f9b0aa002e refactor: type helpers 2022-05-20 11:22:19 -07:00
Robert Soriano
eb1bd0c700 feat: add trpc type helpers 2022-05-20 11:07:12 -07:00
5 changed files with 88 additions and 6 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

@@ -1,7 +1,7 @@
{ {
"name": "trpc-nuxt", "name": "trpc-nuxt",
"type": "module", "type": "module",
"version": "0.1.3", "version": "0.1.7",
"packageManager": "pnpm@7.1.1", "packageManager": "pnpm@7.1.1",
"license": "MIT", "license": "MIT",
"main": "./dist/module.cjs", "main": "./dist/module.cjs",

View File

@@ -0,0 +1,80 @@
## Inference Helpers
`@trpc/server` exports the following helper types to assist with inferring these types from the `router` exported in `~/server/trpc/index.ts`:
- `inferProcedureOutput<TProcedure>`
- `inferProcedureInput<TProcedure>`
- `inferSubscriptionOutput<TRouter, TPath>`
```ts
// ~/utils/trpc.ts
import type { router } from '~/server/trpc/index.ts'
type AppRouter = typeof router
/**
* Enum containing all api query paths
*/
export type TQuery = keyof AppRouter['_def']['queries']
/**
* Enum containing all api mutation paths
*/
export type TMutation = keyof AppRouter['_def']['mutations']
/**
* Enum containing all api subscription paths
*/
export type TSubscription = keyof AppRouter['_def']['subscriptions']
/**
* This is a helper method to infer the output of a query resolver
* @example type HelloOutput = InferQueryOutput<'hello'>
*/
export type InferQueryOutput<TRouteKey extends TQuery> = inferProcedureOutput<
AppRouter['_def']['queries'][TRouteKey]
>
/**
* This is a helper method to infer the input of a query resolver
* @example type HelloInput = InferQueryInput<'hello'>
*/
export type InferQueryInput<TRouteKey extends TQuery> = inferProcedureInput<
AppRouter['_def']['queries'][TRouteKey]
>
/**
* This is a helper method to infer the output of a mutation resolver
* @example type HelloOutput = InferMutationOutput<'hello'>
*/
export type InferMutationOutput<TRouteKey extends TMutation> =
inferProcedureOutput<AppRouter['_def']['mutations'][TRouteKey]>
/**
* This is a helper method to infer the input of a mutation resolver
* @example type HelloInput = InferMutationInput<'hello'>
*/
export type InferMutationInput<TRouteKey extends TMutation> =
inferProcedureInput<AppRouter['_def']['mutations'][TRouteKey]>
/**
* This is a helper method to infer the output of a subscription resolver
* @example type HelloOutput = InferSubscriptionOutput<'hello'>
*/
export type InferSubscriptionOutput<TRouteKey extends TSubscription> =
inferProcedureOutput<AppRouter['_def']['subscriptions'][TRouteKey]>
/**
* This is a helper method to infer the asynchronous output of a subscription resolver
* @example type HelloAsyncOutput = InferAsyncSubscriptionOutput<'hello'>
*/
export type InferAsyncSubscriptionOutput<TRouteKey extends TSubscription> =
inferSubscriptionOutput<AppRouter, TRouteKey>
/**
* This is a helper method to infer the input of a subscription resolver
* @example type HelloInput = InferSubscriptionInput<'hello'>
*/
export type InferSubscriptionInput<TRouteKey extends TSubscription> =
inferProcedureInput<AppRouter['_def']['subscriptions'][TRouteKey]>
```

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')

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,