worker_route

Struct Query

Source
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>

Source

pub fn into_inner(self) -> T

Acess the owned T

Source§

impl<T: DeserializeOwned> Query<T>

Source

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§

Source§

impl<T> AsMut<T> for Query<T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Query<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for Query<T>

Source§

fn clone(&self) -> Query<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Query<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Query<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for Query<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: Display> Display for Query<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,