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 / Client::call.
  3. Configure path params, query, body, auth, retries on RequestBuilder.
  4. Execute with RequestBuilder::send (returns Response) or RequestBuilder::send_json (deserializes JSON and fails on non-2xx).

§Cargo features

FeatureDescription
json (default)JSON bodies, send_json, custom JsonParserFn
reqwest / rustls-tls (default)Reqwest backend
multipart[RequestBuilder::multipart]
towerTower transport stack via [ClientBuilder::transport_stack]
schema[SchemaRegistry] route metadata
openapiOpenAPI 3.0 export from schema registry
validateGarde validation on JSON responses
macrosProc-macro helpers (reserved)

See the repository README for full examples.

§Example

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?;

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::ReqwestBackend;
pub use client::Client;
pub use client::ClientBuilder;
pub use client::ClientConfig;
pub use endpoint::Endpoint;
pub use endpoint::EndpointParams;
pub use endpoint::EndpointQuery;
pub use endpoint::EndpointRequestBuilder;
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::parse_retry_after;
pub use retry::RetryPolicy;
pub use retry::ShouldRetryFn;

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.
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 buffered body.
retry
Retry policies for transport and HTTP failures.

Macros§

endpoint
Defines a simple Endpoint with no params or query.

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_parser
Wraps a custom JSON parse function for use with ClientBuilder::json_parser.
serde_json_parser
Default parser using serde_json::from_slice (same semantics as the fast path, as a JsonParserFn).

Type Aliases§

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