sebastienbarreS
tRPC3mo ago
11 replies
sebastienbarre

Struggling to refactor into sub-routers

Howdy. I'm struggling to refactor the creation of a sub-router into a function that is independent of the server instance.

import { publicProcedure, router } from "./trpc.ts";
import { actorRepo } from "../../../Domain/Actor/Persistence/Drizzle/SQLite/ActorDrizzleSQLiteRepository.ts";

export const appRouter = router({
  actor: {
    findMany: publicProcedure.query(actorRepo.findMany),
  },
});

export type AppRouter = typeof appRouter;


I would like to turn the above into:

import { createActorTRPCRouter } from "../../../Domain/Actor/API/tRPC/createActorTRPCRouter.ts";

export const appRouter = router({
  actor: createActorTRPCRouter({ router, publicProcedure, actorRepo }),
});


But TypeScript is very unhappy about my createActorTRPCRouter.ts:
import type { TRPCProcedureBuilder, TRPCRouterBuilder } from "@trpc/server";
import type { ActorRepositoryInterface } from "../../Persistence/ActorRepositoryInterface.ts";

export const createActorTRPCRouter = ({
  router,
  publicProcedure,
  actorRepo,
}: {
  router: TRPCRouterBuilder<any>;
  publicProcedure: TRPCProcedureBuilder<any, any, any, any, any, any, any, any>;
  actorRepo: ActorRepositoryInterface;
}) =>
  router({
    findMany: publicProcedure.query(actorRepo.findMany),
  });

I'm getting the following error:
Type '((input: any) => Promise<any>) | QueryProcedure<{ input: any; output: any; meta: any; }>' is not assignable to type 'CreateRouterOptions | AnyProcedure | AnyRouter | (() => Promise<AnyRouter>)'.
  Type '(input: any) => Promise<any>' is not assignable to type 'CreateRouterOptions | AnyProcedure | AnyRouter | (() => Promise<AnyRouter>)'.
    Type '(input: any) => Promise<any>' is missing the following properties from type 'QueryProcedure<any>': _def, meta
Was this page helpful?