offstage/offstage_core

Types

The generic connection type for both mist and wisp connections.

pub type Connection {
  MistConnection(mist.Connection)
  WispConnection(wisp.Connection)
}

Constructors

pub type ErrorResponse {
  ErrorResponse(status: String, reason: String)
}

Constructors

  • ErrorResponse(status: String, reason: String)

The response type for Mist.

pub type MistResponse {
  MistResponse(response.Response(mist.ResponseData))
}

Constructors

pub opaque type OpenAPIScope

The common request type that supports both Wisp and Mist.

pub type Request =
  request.Request(Connection)

The type representing the capability of a route. The capability provides some resources that are retrieved from the request, it holds a get function which is used to obtain the resource.

Example

let capability = .. // Get some capability object

use resource <- capability.get(request)
pub type RouteCapability(data, object) {
  RouteCapability(
    get: fn(
      request.Request(Connection),
      fn(data) -> Result(object, response.Response(wisp.Body)),
    ) -> Result(object, response.Response(wisp.Body)),
  )
}

Constructors

The type representing a route specification.

pub type RouteSpec {
  RouteSpec(
    path: String,
    method: http.Method,
    doc: fn(openapi.OpenAPI) -> OpenAPIScope,
    route: fn(request.Request(Connection)) -> response.Response(
      wisp.Body,
    ),
  )
}

Constructors

The type representing a builder for a route specification.

pub type RouteSpecBuilder {
  RouteSpecBuilder(
    path: String,
    method: http.Method,
    doc: fn(openapi.OpenAPI) -> OpenAPIScope,
  )
}

Constructors

pub type StatusCode {
  StatusCode(
    bad_request: Int,
    unauthorized: Int,
    not_found: Int,
    conflict: Int,
    internal_server_error: Int,
    service_unavailable: Int,
    timeout: Int,
    ok: Int,
    created: Int,
  )
}

Constructors

  • StatusCode(
      bad_request: Int,
      unauthorized: Int,
      not_found: Int,
      conflict: Int,
      internal_server_error: Int,
      service_unavailable: Int,
      timeout: Int,
      ok: Int,
      created: Int,
    )

The response type for Wisp.

pub type WispResponse =
  response.Response(wisp.Body)

Values

pub fn bad_request(
  message: String,
) -> response.Response(wisp.Body)

Return a 400 Bad Request response with the given message.

pub fn capability(
  get: fn(request.Request(Connection)) -> Result(
    data,
    response.Response(wisp.Body),
  ),
) -> RouteCapability(data, object)

Create a capability which provides a resource. This function handles forwarding the request to the next handler.

pub fn conflict(message: String) -> response.Response(wisp.Body)

Return a 409 Conflict response with the given message.

pub fn create_scope(
  doc doc: openapi.OpenAPI,
  method method: http.Method,
  route route: String,
) -> OpenAPIScope

Create a new OpenAPIScope using the given OpenAPI doc, http method and route path.

pub fn create_spec_builder(
  route route: String,
  method method: http.Method,
  doc builder: fn(openapi.OpenAPI) -> openapi.OpenAPI,
) -> RouteSpecBuilder
pub fn created() -> response.Response(wisp.Body)

Return a 201 Created response.

pub fn error_response_serializer(
  status: option.Option(String),
) -> serialize.Serializer(ErrorResponse)
pub fn get_authorization(
  req: request.Request(Connection),
) -> Result(String, Nil)

Require the given token from the request headers and execute the given function accordingly

pub fn get_content_type(
  req: request.Request(Connection),
) -> Result(String, Nil)

Get the content type of the request

pub fn get_json_body(
  req: request.Request(Connection),
  decoder: decode.Decoder(a),
) -> Result(a, response.Response(wisp.Body))

Get the body of the request

pub fn get_multipart_body(
  req: request.Request(Connection),
  decoder: decode.Decoder(a),
) -> Result(a, response.Response(wisp.Body))
pub fn get_raw_json_body(
  req: request.Request(Connection),
) -> Result(String, response.Response(wisp.Body))
pub fn internal_server_error(
  message: String,
) -> response.Response(wisp.Body)

Return a 500 Internal Server Error response with the given message.

pub fn is_content_type(
  req: request.Request(Connection),
  expected: String,
) -> Bool

Check wether the content type of the request matches the expected one

pub fn json_body(
  response: response.Response(wisp.Body),
  json: json.Json,
) -> response.Response(wisp.Body)

Set the given JSON to the body of the response, setting the content-type to application/json

pub fn json_response_internal(
  spec: RouteSpecBuilder,
  code: Int,
  encoder: serialize.Encoder(response),
  summary: String,
  response: fn(
    fn(response) -> response.Response(wisp.Body),
    fn(response, Bool, fn() -> response.Response(wisp.Body)) -> response.Response(
      wisp.Body,
    ),
  ) -> data,
  next: fn(RouteSpecBuilder, RouteCapability(data, object)) -> RouteSpec,
) -> RouteSpec

Add a JSON response with the given encoder to the route

pub fn mist_response_to_wisp(
  response: MistResponse,
) -> response.Response(wisp.Body)

Converts a MistResponse to a WispResponse. Note: currently only Bytes reponses are supported.

pub fn modify_doc(
  scope: OpenAPIScope,
  fun: fn(openapi.OpenAPI) -> openapi.OpenAPI,
) -> OpenAPIScope

Modify the OpenAPI document in the given [scope]

pub fn modify_operation(
  scope: OpenAPIScope,
  fun: fn(openapi.Operation) -> openapi.Operation,
) -> OpenAPIScope

Modify the OpenAPI operation in the given [scope]

pub fn modify_spec(
  spec: RouteSpecBuilder,
  transform: fn(OpenAPIScope) -> OpenAPIScope,
) -> RouteSpecBuilder

Modify the route specification using the given transformation function.

pub fn not_found(message: String) -> response.Response(wisp.Body)

Return a 404 Not Found response with the given message.

pub fn ok() -> response.Response(wisp.Body)

Return a 200 OK response.

pub fn openapi_scope_doc(scope: OpenAPIScope) -> openapi.OpenAPI

Get the OpenAPI document from the given [scope]

pub fn openapi_scope_path(scope: OpenAPIScope) -> String
pub fn require_authorization(
  req: request.Request(Connection),
) -> Result(String, response.Response(wisp.Body))

Require the given token from the request headers and execute the given function accordingly

pub fn service_unavailable(
  message: String,
) -> response.Response(wisp.Body)

Return a 503 Service Unavailable response with the given message.

pub const status_code: StatusCode
pub fn text_body(
  response: response.Response(wisp.Body),
  text: String,
) -> response.Response(wisp.Body)

Set the given text to the body of the response, setting the content-type to text/plain

pub fn timeout(message: String) -> response.Response(wisp.Body)

Return a 504 Timeout response with the given message.

pub fn try(in: Result(a, b), fun: fn(a) -> b) -> b

Attempt to execute the given function with the value inside the Result. This function is similar to the result.try function, returning the

pub fn unauthorized(
  message: String,
) -> response.Response(wisp.Body)

Return a 401 Unauthorized response with the given message.

✨ Search Document