Skip to main content

better_fetch/
lib.rs

1//! # better-fetch
2//!
3//! Typed HTTP client layer on top of [reqwest](https://docs.rs/reqwest), inspired by
4//! [@better-fetch/fetch](https://better-fetch.vercel.app/docs). This crate is not affiliated
5//! with the upstream TypeScript project.
6//!
7//! ## Quick start
8//!
9//! ```no_run
10//! use better_fetch::{Client, Result};
11//! use serde::Deserialize;
12//!
13//! #[derive(Debug, Deserialize)]
14//! struct Todo {
15//!     user_id: u64,
16//!     id: u64,
17//!     title: String,
18//!     completed: bool,
19//! }
20//!
21//! #[tokio::main]
22//! async fn main() -> Result<()> {
23//!     let client = Client::new("https://jsonplaceholder.typicode.com")?;
24//!     let todo: Todo = client
25//!         .get("/todos/:id")
26//!         .param("id", 1)
27//!         .send()
28//!         .await?
29//!         .json()
30//!         .await?;
31//!     println!("{todo:#?}");
32//!     Ok(())
33//! }
34//! ```
35
36mod url_build;
37
38pub mod auth;
39pub mod backend;
40pub mod client;
41pub mod endpoint;
42pub mod error;
43pub mod hooks;
44#[cfg(feature = "json")]
45mod json_parser;
46
47pub mod plugin;
48pub mod plugins;
49pub mod request;
50pub mod response;
51pub mod retry;
52#[cfg(feature = "validate")]
53mod validate_json;
54
55#[cfg(feature = "schema")]
56pub mod schema;
57
58#[cfg(feature = "openapi")]
59pub mod openapi;
60
61#[cfg(feature = "tower")]
62pub mod tower;
63
64pub use auth::{AsyncTokenProvider, Auth, TokenSource};
65pub use backend::{HttpBackend, HttpRequest, HttpResponse, ReqwestBackend};
66pub use client::{Client, ClientBuilder, ClientConfig};
67pub use endpoint::Endpoint;
68pub use error::Error;
69pub use hooks::{ErrorContext, Hooks, RequestContext, ResponseContext, SuccessContext};
70#[cfg(feature = "json")]
71pub use json_parser::{json_parser, serde_json_parser, JsonParserFn};
72pub use plugin::{Plugin, PluginRegistry, PreparedRequest};
73pub use plugins::LoggerPlugin;
74pub use request::RequestBuilder;
75pub use response::Response;
76pub use retry::{default_should_retry, RetryPolicy, ShouldRetryFn};
77
78#[cfg(feature = "schema")]
79pub use schema::{EndpointSchema, SchemaRegistry};
80
81#[cfg(feature = "openapi")]
82pub use openapi::{
83    OpenApiBuilder, OpenApiComponents, OpenApiDocument, OpenApiInfo, OpenApiOperation, OpenApiSchemaRef,
84    OpenApiServer,
85};
86
87#[cfg(feature = "tower")]
88pub use tower::{BoxHttpService, ReqwestHttpService, ServiceBackend};
89
90/// Result alias using [`Error`].
91pub type Result<T> = std::result::Result<T, Error>;