Struct worker_route::Query
source · pub struct Query<T>(_);Expand description
Extract typed information with the supplied struct from the query string from Request
To extract information from Request, T must implement Deserialize trait.
Panics
Currently only regular structs are supported.
If the given T is not a regular struct (eg: tuple, unit) it will panic at runtime.
use serde::{Deserialize, Serialize};
use worker::{Response, Result, RouteContext};
use worker_route::{get, Query};
#[derive(Debug, Serialize, Deserialize)]
struct StructFoo {
foo: String,
}
#[get("/foo-struct")]
async fn struct_foo(req: Query<StructFoo>, _: RouteContext<()>) -> Result<Response> {
// works
let Foo { foo } = req.into_inner();
// rest code
}
#[derive(Debug, Serialize, Deserialize)]
struct TupleFoo(String);
#[get("/foo-tuple")]
async fn tuple_foo(req: Query<TupleFoo>, _: RouteContext<()>) -> Result<Response> {
// you won't even get here
let TupleFoo ( foo ) = req.into_inner();
// rest code
}
Notes
Request can be an ommited from the parameter too. When ommitting either of them, the sequence must always be in the correct order.
The correct orders are:
- (
Request,RouteContext<D>) - (
Query<T>,RouteContext<D>) - (
Query<T>,Request,RouteContext<D>)
use serde::{Deserialize, Serialize};
use worker::{Response, Result, RouteContext};
use worker_route::{get, Query};
#[derive(Debug, Serialize, Deserialize)]
struct Foo {
foo: String,
}
#[get("/foo-query")]
async fn without_req(req: Query<Foo>, _: RouteContext<()>) -> Result<Response> {
// rest code
}
#[get("/foo-with-request")]
async fn with_request(req: Query<Foo>, _: Request, _: RouteContext<()>) -> Result<Response> {
// rest code
}Implementations§
source§impl<T> Query<T>where
T: for<'a> Deserialize<'a> + Debug,
impl<T> Query<T>where T: for<'a> Deserialize<'a> + Debug,
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Acess the owned T
sourcepub fn from<D>(req: &Request, ctx: &RouteContext<D>) -> Result<Self, Error>
pub fn from<D>(req: &Request, ctx: &RouteContext<D>) -> Result<Self, Error>
Deserialize the given T from the URL query string.
use serde::{Deserialize, Serialize};
use worker::{console_log, Request, Response, Result, RouteContext};
use worker_route::{get, Query};
#[derive(Debug, Deserialize, Serialize)]
struct Person {
name: String,
age: usize,
}
#[get("/persons/:name/:age")]
async fn person(req: Request, ctx: RouteContext<()>) -> Result<Response> {
let person = Query::<Person>::from(&req, &ctx);
let Person { name, age } = person.unwrap().into_inner();
console_log!("name: {name}, age: {age}");
Response::empty()
}
Auto Trait Implementations§
impl<T> RefUnwindSafe for Query<T>where T: RefUnwindSafe,
impl<T> Send for Query<T>where T: Send,
impl<T> Sync for Query<T>where T: Sync,
impl<T> Unpin for Query<T>where T: Unpin,
impl<T> UnwindSafe for Query<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more