Comments (9)
@KerryRitter Decorators are not really possible with tRpc, since decorators are applied in runtime, and tRpc benefits tremendously from its compile-time features.
You can't infer a typescript type based on decorators, so the only feasible way to achieve this if someone will generate the AppRouter type in build-time and save it locally similar to Prisma's solution (which isn't ideal either since this will require you to reset Typescript each time you change something).
The other option is to type the router AND the controller, in fact this is the implementation tsrest
landed on, see: https://ts-rest.com/docs/nest
Which is even worse then the solutions I've mentioned above since if I am already writing the router, why should I write the controller as well?
from trpc-nestjs-adapter.
Apologies for the late reply.
I'm not sure this is easy to achieve.
But if you bring over a PoC, I'm more than happy to work on merging it in.
from trpc-nestjs-adapter.
I did have some luck: https://github.com/macstr1k3r/trpc-nestjs-adapter/compare/master...KerryRitter:trpc-nestjs-adapter:decorators?expand=1
@TRPCQuery() works, and I think we can make @TRPCInput() and whatnot work - looks like this might be cool. Only downside is the inferred typing isn't quite as slick, but not too bad to pre-declare the object and use typeof
.
from trpc-nestjs-adapter.
I can see how this will get the server set-up and responding, but how do we achieve what tRPC primarily achieves, which is sharing the types between the client and the server?
from trpc-nestjs-adapter.
from trpc-nestjs-adapter.
I see, so somehow we need to get the type-safety passed into the AppRouter definition. I'll have a thought on this.
from trpc-nestjs-adapter.
Not having any good ideas coming to me on how to do this with decorators.
I am thinking it may be a good pattern to do a .trpc.ts file per modules, and then merge them in the app root. This will at least keep things module. So:
-- app.module.ts
-- app.trpc.ts
-- cats/
---- cats.module.ts
---- cats.trpc.ts
from trpc-nestjs-adapter.
is it possible to use something equivalent to @UseGuards(AuthGuard('jwt'))
I tried messing around with trpc context, middleware, nestjs execution context etc, but i'm very new to nestjs and trpc.
Here are two relevant links:
https://github.com/nestjs/passport/blob/020c82532cfcf75336118e6861555b3d016908b3/lib/auth.guard.ts
https://trpc.io/docs/authorization
export function createContext({
req,
res,
}: trpcNext.CreateNextContextOptions) {
// Create your context based on the request object
// Will be available as `ctx` in all your resolvers
// This is just an example of something you might want to do in your ctx fn
// async function getUserFromHeader() {
// if (req.headers.authorization) {
// const user = await decodeAndVerifyJwtToken(
// req.headers.authorization.split(' ')[1],
// );
// return user;
// }
// return null;
// }
// const user = await getUserFromHeader();
// return {
// user,
// };
// return Promise.resolve({name: 'zing'});
return 'wat';
}
type Context = InferContextType<typeof createContext>;
// type CtxType = InferContextType<typeof createContext>;
// const createContext = () => ({});
const trpc = initTRPC.context<Context>().create();
const isAuthed = trpc.middleware(async ({ next, ctx }) => {
let executionContext = await ctx.resolveNestDependency(???)
const memoizedGuard = new (AuthGuard('jwt'));
await memoizedGuard.canActivate(executionContext)
// if (!ctx.user?.isAdmin) {
// throw new TRPCError({ code: 'UNAUTHORIZED' });
// }
return next({
ctx: {
user: ??
},
});
});
const { router } = trpc;
const privateProcedure = trpc.procedure.use(isAuthed);
from trpc-nestjs-adapter.
Yep I had a completely off base understanding of tRPC - but a boy can dream haha. I will check out this other project. Thank you!
from trpc-nestjs-adapter.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trpc-nestjs-adapter.