oaspec/util/http

Types

Type-safe HTTP status code representation. Replaces raw String status codes throughout the codebase.

pub type HttpStatusCode {
  Status(Int)
  StatusRange(Int)
  DefaultStatus
}

Constructors

  • Status(Int)

    An exact HTTP status code, e.g. 200, 404, 500.

  • StatusRange(Int)

    A wildcard range, e.g. 2XX is StatusRange(2), 4XX is StatusRange(4).

  • DefaultStatus

    The OpenAPI “default” catch-all response.

Values

pub fn parse_status_code(
  code: String,
) -> Result(HttpStatusCode, Nil)

Parse a raw status code string from an OpenAPI spec into an HttpStatusCode. Returns Error(Nil) for unrecognisable strings.

pub fn sort_response_entries(
  entries: List(#(HttpStatusCode, a)),
) -> List(#(HttpStatusCode, a))

Sort response entries so that exact codes come before ranges, and ranges come before the default catch-all. This ensures correct pattern matching order in generated case expressions.

pub fn status_code_suffix(code: HttpStatusCode) -> String

Shared HTTP status code utilities for code generation. Get a human-readable suffix for a given HTTP status code. Used to build anonymous type names like “ListPetsResponseOk”.

pub fn status_code_to_int(code: HttpStatusCode) -> String

Convert an HttpStatusCode to an integer string for use in ServerResponse. Range codes and DefaultStatus map to a representative status code.

pub fn status_code_to_int_pattern(code: HttpStatusCode) -> String

Convert an HttpStatusCode to a valid Gleam case pattern. Exact codes become integer literals, range codes become guard expressions, and DefaultStatus becomes “_” (catch-all).

pub fn status_code_to_string(code: HttpStatusCode) -> String

Convert an HttpStatusCode back to its canonical string form.

Search Document