brazier/api

Types

The request an API route, a middleware or a ws_auth hook receives.

Reference: wisp.Request.

pub type Request =
  request.Request(server.Connection)

The response they return. Build one with json, json_value, text or empty, then adjust it with gleam/http/response.

Reference: wisp.Response.

pub type Response =
  response.Response(server.ResponseBody)

A single API route declaration.

Construct with get, post, put, patch, or delete, then pass the list to routes.

A matched route tags the request with its declared path (for example /api/items) for the [brazier, http, request] telemetry span, so the span never carries the raw request path.

pub opaque type Route

Values

pub fn delete(
  path: String,
  handler: fn(request.Request(server.Connection)) -> response.Response(
    server.ResponseBody,
  ),
) -> Route

Declare a DELETE route.

pub fn empty(
  status: Int,
) -> response.Response(server.ResponseBody)

Create an empty response.

pub fn form_field(
  fields: List(#(String, String)),
  name: String,
) -> Result(String, String)

Find a required form field.

pub fn get(
  path: String,
  handler: fn(request.Request(server.Connection)) -> response.Response(
    server.ResponseBody,
  ),
) -> Route

Declare a GET route.

pub fn json(
  status: Int,
  body: String,
) -> response.Response(server.ResponseBody)

Create a JSON response.

pub fn json_value(
  status: Int,
  body: json.Json,
) -> response.Response(server.ResponseBody)

Create a JSON response from a typed gleam/json value.

pub fn patch(
  path: String,
  handler: fn(request.Request(server.Connection)) -> response.Response(
    server.ResponseBody,
  ),
) -> Route

Declare a PATCH route.

pub fn post(
  path: String,
  handler: fn(request.Request(server.Connection)) -> response.Response(
    server.ResponseBody,
  ),
) -> Route

Declare a POST route.

pub fn put(
  path: String,
  handler: fn(request.Request(server.Connection)) -> response.Response(
    server.ResponseBody,
  ),
) -> Route

Declare a PUT route.

pub fn read_form(
  req: request.Request(server.Connection),
  max_bytes: Int,
) -> Result(List(#(String, String)), String)

Read an application/x-www-form-urlencoded request body.

pub fn read_text(
  req: request.Request(server.Connection),
  max_bytes: Int,
) -> Result(String, String)

Read a UTF-8 request body with an explicit byte cap. The error says why there is no body: no or an invalid Content-Length, a body over max_bytes, a body already read, a failed or timed-out read, or bytes that are not UTF-8. Each is logged where it is found.

pub fn routes(
  route_list: List(Route),
) -> fn(request.Request(server.Connection)) -> option.Option(
  response.Response(server.ResponseBody),
)

Build the handler expected by brazier.api_routes.

Routes are matched in declaration order. Unknown method/path combinations return None, so Brazier continues to SSR/static routing.

pub fn text(
  status: Int,
  body: String,
) -> response.Response(server.ResponseBody)

Create a text response.

✨ Search Document