refrakt/cors

CORS middleware for Refrakt applications.

Experimental. This module is lightly used and may change or move to its own package; see https://github.com/raskell-io/refrakt/issues/28.

Usage

import refrakt/cors

fn middleware(req, next) {
  use <- cors.allow(
    req,
    origins: ["http://localhost:3000", "https://myapp.com"],
    methods: ["GET", "POST", "PUT", "DELETE"],
  )
  next(req)
}

Types

CORS configuration.

pub type CorsConfig {
  CorsConfig(
    origins: List(String),
    methods: List(String),
    headers: List(String),
    max_age: Int,
    credentials: Bool,
  )
}

Constructors

  • CorsConfig(
      origins: List(String),
      methods: List(String),
      headers: List(String),
      max_age: Int,
      credentials: Bool,
    )

Values

pub fn allow(
  req: request.Request(wisp.Connection),
  origins origins: List(String),
  methods methods: List(String),
  handler next: fn() -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)

Simple CORS middleware — allow specific origins and methods.

pub fn default_config() -> CorsConfig

Default CORS config — allows common methods and headers.

pub fn middleware(
  req: request.Request(wisp.Connection),
  config: CorsConfig,
  next: fn() -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)

CORS middleware with full configuration.

✨ Search Document