Skip to main content

Module cors

Module cors 

Source
Expand description

Typed helpers for CORS response headers (Access-Control-*).

CorsHeaders models the complete set of CORS response headers defined in the Fetch specification. A fluent builder API makes it easy to construct both simple and preflight responses.

§Example

use api_bones::cors::{CorsHeaders, CorsOrigin};

// Simple CORS response.
let cors = CorsHeaders::new()
    .allow_origin(CorsOrigin::Any)
    .allow_methods(["GET", "POST"])
    .allow_headers(["Content-Type", "Authorization"])
    .max_age(86_400);

assert_eq!(cors.allow_origin.as_ref().unwrap().to_string(), "*");
assert_eq!(cors.max_age, Some(86_400));

// Preflight response helper.
let preflight = CorsHeaders::preflight(
    CorsOrigin::Origin("https://example.com".into()),
    ["GET", "POST", "DELETE"],
    ["Content-Type"],
);
assert!(preflight.allow_credentials.is_none() || preflight.allow_credentials == Some(false));

Structs§

CorsHeaders
Structured CORS response headers.

Enums§

CorsOrigin
The value of the Access-Control-Allow-Origin header.