Skip to content

Commit 4c48e57

Browse files
committed
Deprecate HTTP method handlers in favor of query and mutation
1 parent b705720 commit 4c48e57

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

  • crates/rapid-web-codegen/src

crates/rapid-web-codegen/src/lib.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct Handler {
5151
is_nested: bool,
5252
}
5353

54-
// Currently, the rapid file-based router will only support GET, POST, DELETE, and PUT request formats (we could support patch if needed)
54+
// The recommended handlers are `Query` and `Mutation`. All other handlers are deprecated and will be removed in a future version.
5555
enum RouteHandler {
5656
Query(Handler),
5757
Mutation(Handler),
@@ -65,7 +65,8 @@ enum RouteHandler {
6565
/// Macro for generated rapid route handlers based on the file system
6666
///
6767
/// This macro will look through the specified path and codegen route handlers for each one
68-
/// Currently, there is only logic inplace to support GET, POST, DELETE, and PUT requests as well as middleware via a "_middleware.rs" file
68+
/// The recommended handlers are `query` (for GET requests) and `mutation` (for POST, PUT, PATCH, DELETE requests)
69+
/// Individual HTTP method handlers (get, post, delete, put, patch) are deprecated and will display warnings
6970
///
7071
/// * `item` - A string slice that holds the path to the file system routes root directory (ex: "src/routes")
7172
/// # Examples
@@ -407,8 +408,8 @@ fn generate_handler_tokens(route_handler: Handler, parsed_path: &str, handler_ty
407408
.route(#rapid_routes_path, web::delete().to(#handler::#parsed_handler_type)#(#middleware_idents)*)
408409
)
409410
}
410-
// Currently we still support declaring handlers with a very specific HTTP type (ex: `get` or `post` etc)
411-
// ^^^ Eventually, what was described above should get deprecated
411+
// DEPRECATED: Specific HTTP handlers (get, post, delete, put, patch) are deprecated
412+
// Use `query` for GET requests and `mutation` for POST, PUT, PATCH, DELETE instead
412413
_ => quote!(.route(#rapid_routes_path, web::#parsed_handler_type().to(#handler::#parsed_handler_type)#(#middleware_idents)*)),
413414
}
414415
}
@@ -417,21 +418,34 @@ fn generate_handler_tokens(route_handler: Handler, parsed_path: &str, handler_ty
417418
/// If it does, we want to push the valid handler to the handlers array
418419
/// Note: no need to support HEAD and OPTIONS requests
419420
fn parse_handlers(route_handlers: &mut Vec<RouteHandler>, file_contents: String, handler: Handler) {
420-
// TODO: we need to depricate everything except for `query` and `mutation`
421-
if file_contents.contains("async fn get") && validate_route_handler(&file_contents) {
421+
// First check for the recommended handlers - `query` and `mutation`
422+
if file_contents.contains("async fn query") && validate_route_handler(&file_contents) {
423+
route_handlers.push(RouteHandler::Query(handler))
424+
} else if file_contents.contains("async fn mutation") && validate_route_handler(&file_contents) {
425+
route_handlers.push(RouteHandler::Mutation(handler))
426+
}
427+
// Deprecated handlers - these will be removed in a future version
428+
// Use `query` for GET requests and `mutation` for POST, PUT, PATCH, DELETE instead
429+
else if file_contents.contains("async fn get") && validate_route_handler(&file_contents) {
430+
// Deprecated: Use `query` instead
431+
eprintln!("Warning: The 'get' handler is deprecated. Use 'query' instead for GET requests.");
422432
route_handlers.push(RouteHandler::Get(handler))
423433
} else if file_contents.contains("async fn post") && validate_route_handler(&file_contents) {
434+
// Deprecated: Use `mutation` instead
435+
eprintln!("Warning: The 'post' handler is deprecated. Use 'mutation' instead for POST requests.");
424436
route_handlers.push(RouteHandler::Post(handler))
425437
} else if file_contents.contains("async fn delete") && validate_route_handler(&file_contents) {
438+
// Deprecated: Use `mutation` instead
439+
eprintln!("Warning: The 'delete' handler is deprecated. Use 'mutation' instead for DELETE requests.");
426440
route_handlers.push(RouteHandler::Delete(handler))
427441
} else if file_contents.contains("async fn put") && validate_route_handler(&file_contents) {
442+
// Deprecated: Use `mutation` instead
443+
eprintln!("Warning: The 'put' handler is deprecated. Use 'mutation' instead for PUT requests.");
428444
route_handlers.push(RouteHandler::Put(handler))
429445
} else if file_contents.contains("async fn patch") && validate_route_handler(&file_contents) {
446+
// Deprecated: Use `mutation` instead
447+
eprintln!("Warning: The 'patch' handler is deprecated. Use 'mutation' instead for PATCH requests.");
430448
route_handlers.push(RouteHandler::Patch(handler))
431-
} else if file_contents.contains("async fn query") && validate_route_handler(&file_contents) {
432-
route_handlers.push(RouteHandler::Query(handler))
433-
} else if file_contents.contains("async fn mutation") && validate_route_handler(&file_contents) {
434-
route_handlers.push(RouteHandler::Mutation(handler))
435449
}
436450
}
437451

0 commit comments

Comments
 (0)