Skip to content

Commit

Permalink
✨ Removed paths in macro expansion && Improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Aug 17, 2024
1 parent 1a6505a commit 2d1a9dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ async fn book_state() -> Json<BookState> {
}
```

### Step 2: Add Routes && Generate Api
### Step 2: Add Routes && Generate API

Use the `generate!` macro to define your router and other telemetry to generate the Api. You have to define the `output` location of the TypeScript file and the `routes`. Note that inline functions cannot be used in the `router` field because the function names in the generated TypeScript file are inferred from the handler function names.
Use the `generate!` macro to define your router and other telemetry to generate the API. You have to define the `output` location of the TypeScript file and the `routes`. Note that inline functions cannot be used in the `router` field because the function names in the generated TypeScript file are inferred from the handler function names.

```rust, ignore
use axum::{
Expand Down Expand Up @@ -156,11 +156,13 @@ let mut app: Router<()> = generate! {
.route("/", get(root));
```

### More Notes
### Additional Notes

The `generate!` macro includes several optional fields, such as `prefix`, which allows you to modify the URL prefix. By default, this value is set to `""`, but you can change it to something like `"/api"`. Please note that the `prefix` should not end with a `/`. Additionally, you can customize the `files` field to specify the Rust project directories containing the source files that define the handler functions and dependencies. This can be a single string literal (e.g., `"src"`) or an array of string literals (e.g., `["src/db", "src"]`). These paths are used to extract type information for the TypeScript client. The default for it is `"src"` which should work in most cases.
The `generate!` macro includes several optional fields that you can customize to fit your needs. One of these is `prefix`, which allows you to set a URL prefix for your API routes. By default, this is an empty string (`""`), but you can change it to something like `"/api"`. Be aware that the prefix should not end with a `/`.

And now you can just simply use the router generated by the macro to start your server or do different things, the API should be already generated by your LSP!
Another customizable option is `files`, which defines the Rust project files that contain the source code for handler functions and dependencies. This can be a single string literal (e.g., `"src"`) or an array of string literals (e.g., `["src/db", "src", "src/error.rs"]`). These paths are used to extract type information for the TypeScript client. The default value is `"src"`, which should be suitable in most scenarios.

Finally, once the router is generated by the macro, along with the API, you can immediately use it to start your server or perform other operations.

## Complete Examples

Expand All @@ -169,6 +171,7 @@ Below is a complete example demonstrating the use of `gluer` with `axum` or look
```rust
use axum::{
extract::{Path, Query},
routing::get,
Json, Router,
};
use gluer::{generate, metadata};
Expand Down
20 changes: 5 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl syn::parse::Parse for MetadataAttr {
///
/// - `prefix`: An optional parameter that allows you to specify a prefix for all generated routes. This can be useful if your API is hosted under a common base path (e.g., `/api`).
/// - `routes`: A required parameter that specifies the API routes for which the TypeScript client and resulting Router will be generated. Each route is defined by a URL path (which can include parameters) followed by one or more HTTP methods (e.g., `get`, `post`) and their corresponding handler functions.
/// - `files`: An optional parameter that specifies the directory or directories containing the Rust source files that define the handlers and dependencies. This can be either a single string literal (e.g., `"src"`) or an array of string literals (e.g., `["src/db", "src"]`). These paths are used to extract type information for the TypeScript client. Ensure that these paths are correct and point to the appropriate directories. The default of `"src"` should handle most cases appropriately.
/// - `files`: An optional parameter that specifies the files containing the Rust source files that define the handlers and dependencies. This can be either a single string literal (e.g., `"src"`) or an array of string literals (e.g., `["src/db", "src", "src/error.rs"]`). These paths are used to extract type information for the TypeScript client. Ensure that these paths are correct and point to the appropriate directories or files. The default of `"src"` should handle most cases appropriately.
/// - `output`: A required parameter that specifies the path to the output file where the generated TypeScript client code will be written. Ensure that this path is correct and points to a writable location.
///
/// ## Note
Expand Down Expand Up @@ -496,31 +496,21 @@ impl ToTokens for MethodRoutes {
fn to_tokens(&self, tokens: &mut TokenStream) {
let routes = self.0.iter().map(|route| {
let url = &route.url;

let mut handlers_iter = route.methods.iter();
let first_handler = handlers_iter.next().map(|method| {
let method_name = &method.method;
let handler_name = &method.handler;
quote! {
axum::routing::#method_name(#handler_name)
}
});

let rest_handlers = handlers_iter.map(|method| {
let handlers = route.methods.iter().map(|method| {
let method_name = &method.method;
let handler_name = &method.handler;
quote! {
.#method_name(#handler_name)
#method_name(#handler_name)
}
});

quote! {
.route(#url, #first_handler #(#rest_handlers)*)
.route(#url, #(#handlers).*)
}
});

let expanded = quote! {
axum::Router::new()
Router::new()
#(#routes)*
};

Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::{
extract::{Path, Query},
routing::get,
Json, Router,
};
use gluer::{generate, metadata};
Expand Down

0 comments on commit 2d1a9dc

Please sign in to comment.