-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): short-hand routers and spread support #5404
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
? DecoratedProcedureRecord<TRouter, TProcedures[TKey]['_def']['record']> | ||
: TProcedures[TKey] extends AnyProcedure | ||
? DecorateProcedure<TRouter['_def']['_config']['$types'], TProcedures[TKey]> | ||
[TKey in keyof TRecord]: TRecord[TKey] extends infer $Value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need this extends infer
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just think it makes the "looping" clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only need to grab TRecord[Key]
once, it's like a declaring a variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only need to grab
TRecord[Key]
once, it's like a declaring a variable
aah i see, how is the compiler performance comparatively?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea tbh 🙈
const merged = t.router({ | ||
...router1, | ||
...router2, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
niceee - can we deprecate this monstosity then?
or maybe just make it an alias?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes that'd be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could alias it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, only nitpick I'd point out is that there's a lot of these:
TRouter['_def']['_config']['$types'],
TRouter['_def']['record']
don't we have inference helpers for those?
inferRootTypes<TRouter>
...
?
I'd be fine to change that, I'd also be happy if we changed the router so the grabbing would just be e.g.
I think that sort of approach would be even nicer than using a helper - it feels like an unnecessary abstraction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see that you can't spread actual routers:
// router/post.ts
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
export const authRouter = createTRPCRouter({
getSession: publicProcedure.query(({ ctx }) => {
return ctx.session;
}),
getSecretMessage: protectedProcedure.query(() => {
// testing type validation of overridden next-auth Session in @acme/auth package
return "you can see this secret message!";
}),
});
// root.ts
import { authRouter } from "./router/auth";
import { postRouter } from "./router/post";
import { createTRPCRouter } from "./trpc";
export const appRouter = createTRPCRouter({
auth: authRouter,
post: postRouter,
...authRouter, // <-- doesn't work
});
// export type definition of API
export type AppRouter = typeof appRouter;
how do you make the typesystem / proxy understand spread? if we to alias mergeRouters to this implementation we should support spreading routers and not just records?
No idea, but I think most people use sub-routers with the other way, so might be a non-issue |
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
.d.ts
-files for people who usesatisfies TRPCRouterRecord
Closes #3630
Could help make #4129 easier
How it looks like
This will allow you to define routers like this: