trpc-v10-migrate-codemod
trpc-v10-migrate-codemod copied to clipboard
Write parameters from the router to the procedure
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;
Is this something you can do yourself easily? I think it would take a bit of time to implement properly.
Yeah, we can do it manually without too much trouble