Type-safe middleware composition
-ing
#3890
iam-kevin
started this conversation in
RFC / Ideas
Replies: 2 comments 1 reply
-
Current work around is adding export async function createContext({
req,
}: trpcExpress.CreateExpressContextOptions) {
return {
query: req.query,
} as {
canPost?: boolean,
authorization?: string
}
} But that section gets bloated pretty quickly when adding multiple partial information for other features you'd want. export async function createContext({
req,
}: trpcExpress.CreateExpressContextOptions) {
return {
query: req.query,
} as {
canPost?: boolean,
authorization?: string,
scope?: string[],
teacher?: { id: string, name: string },
student?: { id: string, name: string },
bot?: { id: string, key: string },
...
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Have you seen https://trpc.io/docs/middlewares#extending-middlewares? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey there. Great fan of your work and love trpc.
Wanted to know if there could be a feature that allowed inferred middleware composition.
The composed function should be able to defined what types it expects from the composing function.
Consider the following scenario when you are have a school system where you have either a
student
,teacher
andbot
entity. You then want to create reusable procedures that can be type infered.Using a
extendContext
function to tell middleware that that its should expect the function it's composed with will have certain other properties.When being composed as a procedure, it should be type-safe like this
Beta Was this translation helpful? Give feedback.
All reactions