espresso/service

Service module that’s mostly the same as the one in gleam/http but instead of taking gleam/http request and response it takes espresso versions.

Forked from https://github.com/gleam-lang/http/blob/v3.2.0/src/gleam/http/service.gleam

Types

Middleware are functions that take a before req/res and return a new req/res. They are used to transform requests and responses.

pub type Middleware(
  before_req,
  assigns,
  session,
  before_resp,
  after_req,
  after_resp,
) =
  fn(Service(before_req, assigns, session, before_resp)) ->
    Service(after_req, assigns, session, after_resp)

Services are functions that take a request and return a response. They are the core building block of web applications.

pub type Service(in, assigns, session, out) =
  fn(Request(in, assigns, session)) -> Response(out)

Functions

pub fn map_response_body(service: fn(Request(a, b, c)) ->
    Response(d), with mapper: fn(d) -> e) -> fn(Request(a, b, c)) ->
  Response(e)

A middleware that transforms the response body returned by the service using a given function.

pub fn method_override(service: fn(Request(a, b, c)) ->
    Response(d)) -> fn(Request(a, b, c)) -> Response(d)

A middleware that overrides an incoming POST request with a method given in the request’s _method query paramerter. This is useful as web browsers typically only support GET and POST requests, but our application may expect other HTTP methods that are more semantically correct.

The methods PUT, PATCH, and DELETE are accepted for overriding, all others are ignored.

The _method query paramerter can be specified in a HTML form like so:

<form method="POST" action="/item/1?_method=DELETE">
  <button type="submit">Delete item</button>
</form>
pub fn prepend_response_header(service: fn(Request(a, b, c)) ->
    Response(d), key: String, value: String) -> fn(
  Request(a, b, c),
) -> Response(d)

A middleware that prepends a header to the request.

Search Document