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 flow

  1. Create a Client (or ClientBuilder) with a base URL.
  2. Start a request with Client::get / Client::post (flexible RequestBuilder) or Client::call (typed Endpoint routes).
  3. Configure path params, query, body, auth, retries on the builder.
  4. Execute with RequestBuilder::send (buffered Response), RequestBuilder::send_stream (incremental StreamingResponse), send_json, or EndpointRequestBuilder::send_json.

§Buffered vs streaming

  • send / send_json — full body in memory; hooks and retry predicates can read the body.
  • send_streambytes_stream() from reqwest; use StreamingResponse::collect to buffer when needed. See the streaming module for limits (hooks, custom retry predicates, Tower backend).

Use .get() when you want string paths and a typed JSON response (send_json::<T>()). Use Client::call when method, path, params, query, and response are bound to an Endpoint type.

§Cargo features

The client always uses reqwest as the default HTTP backend. Enable crate features to turn on reqwest capabilities and optional APIs.

FeatureDescription
json (default)JSON bodies, send_json, custom JsonParserFn
rustls-tls (default)TLS via rustls (enable native-tls instead, not both)
native-tlsTLS via the platform stack (do not combine with rustls-tls)
multipartRequestBuilder::multipart
towerTower transport stack via ClientBuilder::transport_stack (implies rustls-tls)
schemaSchemaRegistry route metadata
openapiOpenAPI 3.0 export from schema registry
validateGarde validation on JSON responses
blocking, cookiesPassed through to reqwest
macros#[derive(EndpointParamsDerive)], #[derive(EndpointQueryDerive)] proc-macros

See the repository README for full examples.

§Example (.get() — flexible path, typed response)

let client = Client::new("https://jsonplaceholder.typicode.com")?;

// send() returns Response for any status; json() fails on non-2xx
let todo: Todo = client
    .get("/todos/:id")
    .param("id", 1)
    .send()
    .await?
    .json()
    .await?;

// Or in one step:
let todo: Todo = client.get("/todos/:id").param("id", 1).send_json().await?;

§Example (typed endpoint — method, path, params, response)

define_params!(GetTodoParams for "/todos/:id" { id: u64 });

struct GetTodo;
impl Endpoint for GetTodo {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/todos/:id";
    type Response = Todo;
    type Params = GetTodoParams;
    type Query = ();
}

let client = Client::new("https://jsonplaceholder.typicode.com")?;
let todo = client
    .call::<GetTodo>()
    .params(GetTodoParams { id: 1 })
    .send_json()
    .await?;

Re-exports§

pub use auth::AsyncTokenProvider;
pub use auth::Auth;
pub use auth::TokenSource;
pub use backend::HttpBackend;
pub use backend::HttpBody;
pub use backend::HttpRequest;
pub use backend::HttpResponse;
pub use backend::HttpStreamingResponse;
pub use backend::ReqwestBackend;
pub use client::Client;
pub use client::ClientBuilder;
pub use client::ClientConfig;
pub use endpoint::Endpoint;
pub use endpoint::EndpointParams;
pub use endpoint::EndpointParamsInitial;
pub use endpoint::EndpointQuery;
pub use endpoint::EndpointRequestBuilder;
pub use endpoint::NeedsParams;
pub use endpoint::ParamsBuilderState;
pub use endpoint::Ready;
pub use error::Error;
pub use error::TransportKind;
pub use hooks::ErrorContext;
pub use hooks::Hooks;
pub use hooks::RequestContext;
pub use hooks::ResponseContext;
pub use hooks::StreamingResponseContext;
pub use hooks::StreamingResponseMeta;
pub use hooks::StreamingSuccessContext;
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::parse_retry_after;
pub use retry::RetryPolicy;
pub use retry::ShouldRetryFn;
pub use streaming::BodyStream;
pub use streaming::StreamingResponse;
pub use schema::EndpointSchema;schema
pub use schema::SchemaRegistry;schema
pub use openapi::OpenApiBuilder;openapi
pub use openapi::OpenApiComponents;openapi
pub use openapi::OpenApiDocument;openapi
pub use openapi::OpenApiInfo;openapi
pub use openapi::OpenApiOperation;openapi
pub use openapi::OpenApiSchemaRef;openapi
pub use openapi::OpenApiServer;openapi
pub use tower::BoxHttpService;tower
pub use tower::ReqwestHttpService;tower
pub use tower::ServiceBackend;tower

Modules§

auth
Authentication for clients and individual requests.
backend
HTTP transport abstraction.
cancel
Request cancellation (CancellationToken) compatible with cooperative async abort.
client
HTTP client, builder, and shared configuration.
endpoint
Typed API routes via the Endpoint trait.
error
Error types and helpers for HTTP, transport, hooks, and retries.
hooks
Lifecycle hooks for requests and responses.
multipartmultipart
Re-export of reqwest multipart types (feature multipart). multipart/form-data
openapiopenapi
OpenAPI 3.0 document builder (requires openapi feature).
plugin
Plugin hooks run after URL construction and auth, before request lifecycle hooks.
plugins
Built-in plugins.
request
Per-request fluent builder.
response
HTTP response wrapper with a fully buffered body.
retry
Retry policies for transport and HTTP failures.
schemaschema
Schema registry for endpoint metadata (requires schema feature).
streaming
Streaming HTTP responses (send_stream).
towertower
Tower transport integration (tower feature).

Macros§

define_params
Defines path parameters for a route and implements EndpointParams.
endpoint
Defines a simple Endpoint with optional typed params and query.
impl_serde_endpoint_queryjson
Implements EndpointQuery for a serde-serializable query struct (feature json).

Structs§

CancellationToken
A token which can be used to signal a cancellation request to one or more tasks.

Enums§

QueryValue
Query parameter value (scalar or repeated).

Functions§

json_parserjson
Wraps a custom JSON parse function for use with ClientBuilder::json_parser.
serde_json_parserjson
Default parser using serde_json::from_slice (same semantics as the fast path, as a JsonParserFn).

Type Aliases§

JsonParserFnjson
Parses response bytes into serde_json::Value before deserializing to T.
Result
Result alias using Error.

Derive Macros§

EndpointParamsDerivemacros
Derives EndpointParams for a struct with one field per :param segment in #[endpoint(path = "...")].
EndpointQueryDerivemacros
Derives EndpointQuery for a serde-serializable query struct.