Skip to content
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

[rpc] add Middleware / (public|unpublic)Procedure #4033

Open
MrOxMasTer opened this issue Nov 30, 2024 · 4 comments
Open

[rpc] add Middleware / (public|unpublic)Procedure #4033

MrOxMasTer opened this issue Nov 30, 2024 · 4 comments
Labels
enhancement New feature or request rpc

Comments

@MrOxMasTer
Copy link

MrOxMasTer commented Nov 30, 2024

What is the problem this feature would solve?

no possibility to create middleware / procedure

What is the feature you are proposing to solve the problem?

import { RpcMiddleware } from "@effect/rpc";

class AuthMiddleware extends RpcMiddleware () {
...
}

class UserByEmail extends S.TaggedRequest<UserByEmail>()('UserByEmail', {
  failure: S.Union(
    S.instanceOf(UnknownDatabaseError),
    S.instanceOf(UserNotFoundError),
  ),
  success: selectUserSchema,
  payload: { email: userEmailSchema },
  middleware: [AuthMiddleware] // <---------------------------- AuthMiddleware
}) {}

export const userRouter = RpcRouter.make(
  Rpc.effect(UserByEmail, ({ email, ctx }) => // <---------------- ctx
    Effect.gen(function* (_) {
     ...
    })
  ),
);

I know there is middleware for http, but it doesn't work for me because I don't plan to use http. I only need to use the server side (server components and server react actions) and I don't need http for that and I don't need to create endpoints either

What alternatives have you considered?

trpc: https://trpc.io/docs/server/procedures#reusable-base-procedures

@MrOxMasTer MrOxMasTer added the enhancement New feature or request label Nov 30, 2024
@patroza
Copy link
Member

patroza commented Dec 1, 2024

I think middleware configuration metadata on the request is fine, but since it’s shared with the client I think an implementation instance should not exist on the Request but somewhere on the server side.

Until there is official support, I would suggest to use a HOC at your handler definitions,
possibly you make a clone of RPC.effect with the HOC built in

@MrOxMasTer
Copy link
Author

I think middleware configuration metadata on the request is fine, but since it’s shared with the client I think an implementation instance should not exist on the Request but somewhere on the server side.

For many reasons, I'm not comfortable with rpc anyway. No naming (organization by entities), no documentation, no middleware, and difficult to cache and not library ready

@patroza
Copy link
Member

patroza commented Dec 2, 2024

For many reasons, I'm not comfortable with rpc anyway. No naming (organization by entities), no documentation, no middleware, and difficult to cache and not library ready

have you considered multiple RPC endpoints for the naming/organization?

@MrOxMasTer
Copy link
Author

have you considered multiple RPC endpoints for the naming/organization?

If you mean about RpcRouter.make(RpcRouter.make) - then yes and there is no naming or closing into a single object.
If you mean about creating a set of RpcRouter, then in order to use it, you need to initialize it - and this is a nightmare. There are 3 objects for each router, and there is no common single object, which is inconvenient.

@gcanti gcanti added the rpc label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rpc
Projects
None yet
Development

No branches or pull requests

3 participants