Skip to content

Commit

Permalink
🔧 Updated doc
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Jul 29, 2024
1 parent cbf17bb commit 2b45467
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ add_route!(app, "/hello-world", get(hello));

### Step 3: Generate API

Generate the API file using the `api!` macro. This macro generates the TypeScript file at compile time.
Generate the API file using the `api!` macro. This macro generates the TypeScript file at compile time and needs to be at the end of all macros due to the nature of rusts macro parsing.

```rust
use gluer::api;
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ToTokens for MethodCall {
}
}

/// Generates an api ts file from the routes added with `add_route!`. Specify the path to save the api to.
/// Generates an api ts file from the routes added with `add_route!`. Specify the path to save the api to. Note: This macro should be used only once and needs to be at the end of all macros due to the nature of rusts macro parsing.
#[proc_macro]
pub fn api(input: pc::TokenStream) -> pc::TokenStream {
match api_inner(input.into()) {
Expand Down Expand Up @@ -214,17 +214,17 @@ fn api_inner(input: TokenStream) -> syn::Result<TokenStream> {
)
})?;

let ty = collect_params(&function, span, &mut ts_interfaces)?;
let params_type = collect_params(&function.params, span, &mut ts_interfaces)?;

let response_type = collect_response_type(&function.response, span, &mut ts_interfaces)?;
let response_type = collect_response(&function.response, span, &mut ts_interfaces)?;

let params_str = if !ty.is_empty() {
format!("params: {}", ty)
let params_str = if !params_type.is_empty() {
format!("params: {}", params_type)
} else {
String::new()
};

let body_assignment = if !ty.is_empty() {
let body_assignment = if !params_type.is_empty() {
"JSON.stringify(params)"
} else {
"undefined"
Expand Down Expand Up @@ -260,11 +260,11 @@ fn api_inner(input: TokenStream) -> syn::Result<TokenStream> {
}

fn collect_params(
function: &Function,
params: &BTreeMap<String, String>,
span: proc_macro2::Span,
ts_interfaces: &mut BTreeMap<String, String>,
) -> syn::Result<String> {
for param in &function.params {
for param in params {
if param.1.contains("Json") {
let struct_name = extract_struct_name(span, param.1)?;
if let Some(fields) = GlobalState::get_struct(&struct_name) {
Expand All @@ -291,7 +291,7 @@ fn collect_params(
Ok(String::new())
}

fn collect_response_type(
fn collect_response(
response: &str,
span: proc_macro2::Span,
ts_interfaces: &mut BTreeMap<String, String>,
Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async fn main_test() {

add_route!(app, "/", get(fetch_root).post(add_root));

// at the end of all macros so no issue here
api!("tests/api.ts");

let listener = tokio::net::TcpListener::bind("127.0.0.1:8080")
Expand Down

0 comments on commit 2b45467

Please sign in to comment.