omar-dulaimi / trpc-shield

🛡 A tRPC tool to ease the creation of permission layer.
MIT License
402 stars 10 forks source link

Typescript Support? #2

Closed cmaycumber closed 1 year ago

cmaycumber commented 2 years ago

Feature request

Is your feature request related to a problem? Please describe

Typescript support for shield method

Describe the solution you'd like

Typed records inside of the shield based on AppRouter

Describe alternatives you've considered

No typescript support

omar-dulaimi commented 2 years ago

Hey @cmaycumber

Could you provide an example of what you mean?

You should check this project https://github.com/omar-dulaimi/prisma-trpc-shield-generator to automate creation of shield.

giladv commented 2 years ago

for instance, rule function has no way of passing generics to describe the context shape

douira commented 1 year ago

I agree that having better type inference would be good. The prisma generator can't always be used since not everyone uses prisma or doesn't want to have auto-generated trpc endpoints.

douira commented 1 year ago

For further elaboration: the type of the passed context, which is pretty important when working with authentication, can't be inferred since this package only declares MiddlewareFunction<any, any, any>; as the return type of the middleware. Furthermore, when constructing the IRules argument of shield, it can't do type inference using the given context and instead just uses a default.

omar-dulaimi commented 1 year ago

Yeah @douira, I guess that is a useful enhancement to implement. I'm planning to do so after upgrading the tRPC generator to v10 soon.

omar-dulaimi commented 1 year ago

Released in https://github.com/omar-dulaimi/trpc-shield/releases/tag/0.3.0

Check the README to learn how to correctly use it.

I tested it and everything seems to be working as expected. You get full type safety just like tRPC. Please feel free to open new issues if you face any.

Also, consider sponsoring this or any of my other projects for continued support and features :)

Least you can do is star the project!