trpc-v10-migrate-codemod icon indicating copy to clipboard operation
trpc-v10-migrate-codemod copied to clipboard

Write parameters from the router to the procedure

Open jprokay-tb opened this issue 1 year ago • 2 comments

We have some functions that create routers. These inject additional resources into the context and are dependent on route. For example:

const areas = createRepoRouter(AreaRepo)
  .query('getAll', {
    input: TakeoffID,
    async resolve({ input, ctx }) {
      const { repo } = ctx;
      const organizationId = ctx.user.organizationId;
      return await repo.getAll(organizationId, input);
    },
  })
  .mutation('create', {
    input: AreaCreateInput,
    async resolve({ input, ctx }) {
      const { repo } = ctx;
      const organizationId = ctx.user.organizationId;
      return await repo.create(organizationId, input);
    },
  }));

export default areas;

It would be useful if the codemod copied the AreaRepo parameter to each procedure call. So we end up with something like this:

const areas = router({
  getAll: repoProcedure(AreaRepo)
    .input(TakeoffID)
    .query(async ({ input, ctx }) => {
      const { repo } = ctx;
      const organizationId = ctx.user.organizationId;
      return await repo.getAll(organizationId, input);
    }),
  create: repoProcedure(AreaRepo)
    .input(AreaCreateInput)
    .mutation(async ({ input, ctx }) => {
      const { repo } = ctx;
      const organizationId = ctx.user.organizationId;
      return await repo.create(organizationId, input);
    }),
});
export default areas;

jprokay-tb avatar Aug 07 '23 15:08 jprokay-tb

Is this something you can do yourself easily? I think it would take a bit of time to implement properly.

sachinraja avatar Aug 11 '23 04:08 sachinraja

Yeah, we can do it manually without too much trouble

jprokay-tb avatar Aug 13 '23 01:08 jprokay-tb