datastar_gleam/wisp

Types

Possible errors when reading signals from a Wisp request.

pub type ReadSignalsError {
  MissingHeader
  InvalidQuery
  InvalidBody
  JsonDecodeError(json.DecodeError)
}

Constructors

  • MissingHeader

    The request does not have the datastar-request header.

  • InvalidQuery

    The GET query does not contain a datastar parameter.

  • InvalidBody

    The POST/PUT/PATCH body could not be read.

  • JsonDecodeError(json.DecodeError)

    The JSON could not be decoded with the provided decoder.

Values

pub fn is_datastar_request(
  req: request.Request(wisp.Connection),
) -> Bool

Check whether a Wisp request was initiated by Datastar.

This inspects the datastar-request header that the Datastar frontend automatically adds to every outgoing request.

pub fn read_signals(
  req: request.Request(wisp.Connection),
  decoder: decode.Decoder(a),
) -> Result(a, ReadSignalsError)

Read Datastar signals from a Wisp request.

For GET requests, expects ?datastar=url_encoded_json. For POST/PUT/PATCH requests, expects a JSON body.

import gleam/dynamic/decode

let decoder = decode.at(["delay"], decode.int)
case datastar_wisp.read_signals(req, decoder) {
  Ok(signals) -> // use signals
  Error(datastar_wisp.MissingHeader) -> // not a datastar request
}
Search Document