T
tRPC

how can i get procedure name?

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) https://trpc.io/docs/procedures#reusable-base-procedures Ah yes I’m remembering right https://trpc.io/docs/context#example-for-inner--outer-context
CCaptain1/9/2023
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
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? 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

Looking for more? Join the community!

T
tRPC

how can i get procedure name?

Join Server
Recommended Posts
tRPC sockets with reactTrying to make my React app work with socket with minimal server but getting error: "Uncaught TypeEReact Native UsageHello, I wanted to know if trpc can be used with react native ? And is it possible on a bare react ntransformers, tensor.js, PyTorch and tRPCdumb question: does anyone has experience with tensorflow.js? is there any major obstacle to use tensubscriptionHow do you guys Authenticate / Authorize Subscription ?NextJS and createProxySSGHelpers context type errorHi guys, do you guys know a better way of clean this typescript error? createProxySSGHelpers({ Validating PermissionsHi! A common operation that I'm doing in tRPC is validating that a person has permissions to perforAny typescript chads able know if it's possible to map a type to another type w genericsNot 100% sure if this is appropriate to ask here but I figured there's a lot of good TS developers ouseQuery enabled not working???Even setting the `enabled` to false --> `trpc.order.get({id: "123"}, {enabled: false})` still make`QueryClientProvider` not included in `withTRPC`?Trying to use `import { useIsMutating } from "react-query"` but it's throwing `Error: No QueryClientHandle React error boundarySeems like Im doing something wrong as I can't handle the error boundary from trpc query. I've queryAny tips for Typescript slowness?We successfully migrated from 9.x to 10 but are still seeing slow VS Code perf (10+ seconds to get aChange status of useMutation from 'success' back to 'idle'Hiya, I have a mutation for creating stuff that can run a few different ways on a page, and I want tIs it possible to call one procedure in another?I have multiple routers in my trpc server. For example userRouter (e.g. procedure: getUser) and postHandling Query Errors at Root of App (v9)I want to show an error toast on my `NextJS` frontend every time there is an error from `useQuery` itRPC Client webpack errorAs soon as I add the client part to my legacy app i get an error and Can't figure out what is wrong.cookies, headers and authenticationin express I can do something like `res.cookie("name", "value")` for example. alternatively I can d