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,

source

pub fn into_inner(self) -> T

Acess the owned T

source

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.