Captain
Captain2y ago

how can i get procedure name?

is it possible to get procedure name? i would like to append a service to ctx based on procedure name? is this possible in trpc?
6 Replies
Nick
Nick2y ago
Sounds like you want to create a new base procedure which is used in certain situations. Middlewares can add/modify to the context type IIRC. (Could be misremembering that last bit though)
Nick
Nick2y ago
Define Procedures | tRPC
- A publicProcedure can be viewed as the equivalent of a REST-endpoint or a function.
Nick
Nick2y ago
Context | tRPC
Your context holds data that all of your tRPC procedures will have access to, and is a great place to put things like database connections or authentication information.
Captain
CaptainOP2y ago
im looking for a way to inject a service in each procedure without having to add the same middleware to procedure for example, i have a auth serivce, whihc does both signup, signin, getSession and signout. i'd like to inject the service into the aut procedure in another procedure, i have a service which does multiple things grouped by a procedure. how can i achieve this without having to add middleware to each procedure im using next js middleware which makes sure a user is logged in. this means i'll have 1 procedure for my entire application. i want each route in this procedure to implement or share a service
Nick
Nick2y ago
You add the middleware to a base procedure once and export it to re-use everywhere. If you need the behaviour of the middleware to change based on the procedure (for instance for authorisation user/admin/super) then you can use "Meta" on your routes to declare the access level needed and the middleware can read that Meta data
Captain
CaptainOP2y ago
sounds perfect. i tried this btw. what if i have a auth route object which exprts auth.signup and auth.sign etcc and i want to inject the service at object level and not at property level? is something like that possible? whit meta i need to declare it at every route. i want the service to be used in all routes inside the route object my app router looks like this.
export const appRouter = router({
hello: procedure.input(
input
)
.query(({ input, ctx }) => {

const { req } = ctx

const randomNumber = Math.floor(Math.random() * 1000)

return {
greeting: `hello ${input.text} ${randomNumber}`,
};
}),

auth,
Organisation
});
export const appRouter = router({
hello: procedure.input(
input
)
.query(({ input, ctx }) => {

const { req } = ctx

const randomNumber = Math.floor(Math.random() * 1000)

return {
greeting: `hello ${input.text} ${randomNumber}`,
};
}),

auth,
Organisation
});
auth and organisation are objects which export auth.signup and organisation.create etcc auth.signup and auth.sigin or any other property of auth shoulde use the same exact service im doing this for code organisation and also follow the principle of dry this is my auth route object
export const auth = router({

signIn: authProcedure.input({

parse(input) {

try {
SignInSchema.parse(input);

return input;
} catch (e) {
if (e instanceof ZodError) {
throw new ZodError<TsignUp>(e.issues)
}
}
},

}).mutation(async ({ input, ctx }) => {

const { authService } = ctx;

const { email, password } = input;

try {

await authService.signIn({ email, password });

return true

} catch (error) {

if (error instanceof AuthError) {
const { message, name, stack } = error;

throw new TRPCError({
code: "BAD_REQUEST",
message: message
})
}

throw Error('Something went wrong')
}
}),

signUp: authProcedure.input({
parse(input) {
try {
SignUpSchema.parse(input);

return input;
} catch (e) {
if (e instanceof ZodError) {
throw new ZodError<TsignUp>(e.issues)
}
}
},
}).mutation(async ({ input, ctx }) => {
const { authService } = ctx;

const { email, password } = input;

try {

await authService.signUp({ email, password, hasOrganization: false });

return true

} catch (error) {

if (error instanceof AuthError) {
const { message } = error;

throw new TRPCError({
code: "BAD_REQUEST",
message: message
})
}

throw Error('Something went wrong')
}
})
})
export const auth = router({

signIn: authProcedure.input({

parse(input) {

try {
SignInSchema.parse(input);

return input;
} catch (e) {
if (e instanceof ZodError) {
throw new ZodError<TsignUp>(e.issues)
}
}
},

}).mutation(async ({ input, ctx }) => {

const { authService } = ctx;

const { email, password } = input;

try {

await authService.signIn({ email, password });

return true

} catch (error) {

if (error instanceof AuthError) {
const { message, name, stack } = error;

throw new TRPCError({
code: "BAD_REQUEST",
message: message
})
}

throw Error('Something went wrong')
}
}),

signUp: authProcedure.input({
parse(input) {
try {
SignUpSchema.parse(input);

return input;
} catch (e) {
if (e instanceof ZodError) {
throw new ZodError<TsignUp>(e.issues)
}
}
},
}).mutation(async ({ input, ctx }) => {
const { authService } = ctx;

const { email, password } = input;

try {

await authService.signUp({ email, password, hasOrganization: false });

return true

} catch (error) {

if (error instanceof AuthError) {
const { message } = error;

throw new TRPCError({
code: "BAD_REQUEST",
message: message
})
}

throw Error('Something went wrong')
}
})
})
right now im injecting the service using a middleware. but this means i'd have to reate a new procedure for each route group

Did you find this page helpful?