Skip to content

Instantly share code, notes, and snippets.

@KATT
Last active July 14, 2022 04:52
Show Gist options
  • Save KATT/461f53a9343beff828b2c51da140806d to your computer and use it in GitHub Desktop.
Save KATT/461f53a9343beff828b2c51da140806d to your computer and use it in GitHub Desktop.
infer UseTRPCQueryOptions
import {
createReactQueryHooks,
TRPCClientErrorLike,
UseTRPCMutationOptions,
UseTRPCQueryOptions,
} from '@trpc/react';
import type { inferProcedureInput, inferProcedureOutput } from '@trpc/server';
import { NextPageContext } from 'next';
// ℹ️ Type-only import:
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
import type { AppRouter } from '~/server/routers/_app';
/**
* Extend `NextPageContext` with meta data that can be picked up by `responseMeta()` when server-side rendering
*/
export interface SSRContext extends NextPageContext {
/**
* Set HTTP Status code
* @usage
* const utils = trpc.useContext();
* if (utils.ssrContext) {
* utils.ssrContext.status = 404;
* }
*/
status?: number;
}
/**
* A set of strongly-typed React hooks from your `AppRouter` type signature with `createReactQueryHooks`.
* @link https://trpc.io/docs/react#3-create-trpc-hooks
*/
export const trpc = createReactQueryHooks<AppRouter, SSRContext>();
// export const transformer = superjson;
/**
* This is a helper method to infer the output of a query resolver
* @example type HelloOutput = inferQueryOutput<'hello'>
*/
export type inferQueryOutput<
TRouteKey extends keyof AppRouter['_def']['queries'],
> = inferProcedureOutput<AppRouter['_def']['queries'][TRouteKey]>;
export type inferQueryInput<
TRouteKey extends keyof AppRouter['_def']['queries'],
> = inferProcedureInput<AppRouter['_def']['queries'][TRouteKey]>;
export type inferMutationOutput<
TRouteKey extends keyof AppRouter['_def']['mutations'],
> = inferProcedureOutput<AppRouter['_def']['mutations'][TRouteKey]>;
export type inferMutationInput<
TRouteKey extends keyof AppRouter['_def']['mutations'],
> = inferProcedureInput<AppRouter['_def']['mutations'][TRouteKey]>;
type ClientError = TRPCClientErrorLike<AppRouter>;
export type inferUseTRPCMutationOptions<
TRouteKey extends keyof AppRouter['_def']['mutations'],
TContext = undefined,
> = UseTRPCMutationOptions<
inferMutationInput<TRouteKey>,
ClientError,
inferMutationOutput<TRouteKey>,
TContext
>;
export type inferUseTRPCQueryOptions<
TRouteKey extends keyof AppRouter['_def']['queries'],
> = UseTRPCQueryOptions<
TRouteKey,
inferQueryInput<TRouteKey>,
inferQueryOutput<TRouteKey>,
inferQueryOutput<TRouteKey>,
ClientError
>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment