Skip to main content

Crate better_fetch

Crate better_fetch 

Source
Expand description

§better-fetch

Typed HTTP client layer on top of reqwest, inspired by @better-fetch/fetch. This crate is not affiliated with the upstream TypeScript project.

§Quick start

use better_fetch::{Client, Result};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Todo {
    user_id: u64,
    id: u64,
    title: String,
    completed: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    let client = Client::new("https://jsonplaceholder.typicode.com")?;
    let todo: Todo = client
        .get("/todos/:id")
        .param("id", 1)
        .send()
        .await?
        .json()
        .await?;
    println!("{todo:#?}");
    Ok(())
}

Re-exports§

pub use auth::AsyncTokenProvider;
pub use auth::Auth;
pub use auth::TokenSource;
pub use backend::HttpBackend;
pub use backend::HttpRequest;
pub use backend::HttpResponse;
pub use backend::ReqwestBackend;
pub use client::Client;
pub use client::ClientBuilder;
pub use client::ClientConfig;
pub use endpoint::Endpoint;
pub use error::Error;
pub use hooks::ErrorContext;
pub use hooks::Hooks;
pub use hooks::RequestContext;
pub use hooks::ResponseContext;
pub use hooks::SuccessContext;
pub use plugin::Plugin;
pub use plugin::PluginRegistry;
pub use plugin::PreparedRequest;
pub use plugins::LoggerPlugin;
pub use request::RequestBuilder;
pub use response::Response;
pub use retry::default_should_retry;
pub use retry::RetryPolicy;
pub use retry::ShouldRetryFn;

Modules§

auth
backend
client
endpoint
error
hooks
plugin
plugins
request
response
retry

Macros§

endpoint
Helper macro for simple endpoint definitions.

Functions§

json_parser
Wraps a custom JSON parse function for use with ClientBuilder::json_parser.
serde_json_parser
Default parser using serde_json::from_slice.

Type Aliases§

JsonParserFn
Parses response bytes into JSON before deserializing to T.
Result
Result alias using Error.