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

Documentation on how to make custom extractors #374

Open
sergiomeneses opened this issue Sep 26, 2024 · 3 comments
Open

Documentation on how to make custom extractors #374

sergiomeneses opened this issue Sep 26, 2024 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@sergiomeneses
Copy link

sergiomeneses commented Sep 26, 2024

Is your feature request related to a problem? Please describe.
I want to create a custom extrators for my app.

Describe the solution you'd like
Add an example of a extrator that access state.

Describe alternatives you've considered
Just docs

Additional context
I need to create my custom extrators but i found so far the following issues:

  1. When i implement FromMessageParts (or any of these handler traits), i can't get the json_serde::Value andbytes::Bytes types (you should export these), i currently using sqlx::types::JsonValue andaxum::body::Bytes.
  2. If i need my custom extrator to access socket state, the only solution at the moment is to create the state from the current message (also for other type handlers) i.e. State::<T>::from_message_parts(s, v, p, ack_id) which returns Result<Self, StateNotFound<T>>, so i have consum the result and covert to option using ok(), and finally unwrap() the option, which is safe in my context but i think we need a better way to do this kind of extractors (axum gets the state as parameter in the from_request_parts function).
  3. Also we need a way to multiple extrator like axums does i.e `Data((one, two, three)): Data<(TypeOne, TypeTwo, TypeThree)>
@sergiomeneses sergiomeneses added the enhancement New feature or request label Sep 26, 2024
@Totodore
Copy link
Owner

Totodore commented Sep 26, 2024

I agree that custom extractor implementations lack examples and docs.

However I'm curious about what you are trying to do, because if you want to access the axum state you simply have to make a shared state and add it to both axum and socketioxide with with_state and then you can use the State extractor provided by socketioxide.
But maybe I didn't understand your issue.

@Totodore Totodore added the documentation Improvements or additions to documentation label Sep 28, 2024
@sergiomeneses
Copy link
Author

I want to access socket state not Axum state, I want to inject my database repositories in handler arguments (just I do with Axum) so I can't skip boilerplate in my socket handlers

@Totodore
Copy link
Owner

For your first point:
I re-exported the socketioxide Value type #382 to easily implement extractor traits. It will be available in the next major version. However if you want to manually deserialize it to a specific type you need to use Data::from_{connect,message,disconnect}_parts.

For you second point:
I agree that accessing the state is currently complex. The state system deserve an improvement to match the axum state system. I still want to wait before modifying this part.

For your third point:
I don't understand what you want to do? Data<(Type1, Type2, Type3)> will deserialize data to a tuple of three types according to the incoming payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants