T

tRPC

ā“-help

Join Server

how can i get procedure name?

CCaptain1/8/2023
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?
Nnlucas1/8/2023
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)
CCaptain1/9/2023
im looking for a way to inject a service in each procedure without having to add the same middleware to procedure
CCaptain1/9/2023
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
CCaptain1/9/2023
in another procedure, i have a service which does multiple things grouped by a procedure.
CCaptain1/9/2023
how can i achieve this without having to add middleware to each procedure
CCaptain1/9/2023
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
Nnlucas1/9/2023
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
CCaptain1/9/2023
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?
CCaptain1/9/2023
whit meta i need to declare it at every route. i want the service to be used in all routes inside the route object
CCaptain1/9/2023
my app router looks like this.
CCaptain1/9/2023
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
});
CCaptain1/9/2023
auth and organisation are objects which export auth.signup and organisation.create etcc
CCaptain1/9/2023
auth.signup and auth.sigin or any other property of auth shoulde use the same exact service
CCaptain1/9/2023
im doing this for code organisation and also follow the principle of dry
CCaptain1/9/2023
this is my auth route object
CCaptain1/9/2023
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')
        }
    })
})
CCaptain1/9/2023
right now im injecting the service using a middleware. but this means i'd have to reate a new procedure for each route group