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

feat: make struct name and path optional for server functions #1573

Merged
merged 2 commits into from
Aug 24, 2023

Conversation

gbj
Copy link
Collaborator

@gbj gbj commented Aug 23, 2023

This PR changes the behavior of the #[server] macro to do two things:

  1. makes the struct name argument optional and defaults to generating a PascalCase version of the snake_case function name
  2. changes the default path-prefix argument from / to /api

As a result, you can now write

#[server(DeleteTodo, "/api")]
pub async fn delete_todo(id: u16) -> Result<(), ServerFnError> {

as

#[server]
pub async fn delete_todo(id: u16) -> Result<(), ServerFnError> {

Generating struct names is arguably bad proc-macro hygiene, which is why I didn't do it in the first place. The #[component] macro also does this when it generates a type name for ComponentProps, but this type name doesn't need to be referred to by the user. Server function types are more often used explicitly:

// uh where does this type name come from?
let action = create_server_action::<DeleteTodo>();

Still, I think this is probably better. And rust-analyzer is, of course, smart enough to bring you directly to the #[server] macro for the correct function if you "Go to Definition" on DeleteTodo in this example.

Re: / vs /api, mounting server functions at / by default was probably never the right idea. Our starter templates all default to /api. This will be a breaking change for anyone who was relying on that default mount point, but is easily addressed by moving that to /api.

@gbj gbj force-pushed the server-fn-flexibility branch from 456d827 to 0bf2721 Compare August 24, 2023 10:57
@gbj gbj merged commit 7306ecc into main Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant