pub struct Query<T>(/* private fields */);
Expand description
Extract typed information with the supplied struct and deserialize it with worker::Url
.
To extract typed data from worker::Url
, T
must implement
the DeserializeOwned
trait.
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 StructFoo { 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: Params>
) - (
Query<T>
,RouteContext<D: Params>
) - (
Query<T>
,Request
,RouteContext<D: Params>
)
use serde::{Deserialize, Serialize};
use worker::{Response, Request, 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
Response::empty()
}
#[get("/foo-with-request")]
async fn with_request(req: Query<Foo>, _: Request, _: RouteContext<()>) -> Result<Response> {
// rest code
Response::empty()
}
Implementations§
Source§impl<T> Query<T>
impl<T> Query<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Acess the owned T
Source§impl<T: DeserializeOwned> Query<T>
impl<T: DeserializeOwned> Query<T>
Sourcepub fn from_query_path<D: Params>(
url: &Url,
ctx: &D,
strict: bool,
) -> Result<Self, Error>
pub fn from_query_path<D: Params>( url: &Url, ctx: &D, strict: bool, ) -> 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_query_path(&req.url().unwrap(), &ctx, true);
let Person { name, age } = person.unwrap().into_inner();
console_log!("name: {name}, age: {age}");
Response::empty()
}
§Errors
Currently only regular structs are supported.
Errors are returned if the given T
is not a regular struct (eg: tuple, unit).
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Query<T>where
T: Freeze,
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