A CORS extension for Hopper, an HTTP microframework for the Grain programming language.
This library is contained within a single Grain file, which should be installed into the same directory as your hopper.gr
curl https://raw.githubusercontent.com/alex-snezhko/hopper-cors/main/hopper-cors.gr -o hopper-cors.gr
The library exposes a single function, withCors
, taking a list of options and returning a middleware that enables CORS support for the routes in its scope. Consult the API docs for all of the possible options and default behaviors.
withCors
also automatically handles preflight requests for "non-simple" requests.
import Hopper from "./hopper"
import Cors from "./hopper-cors"
Hopper.serveWithMiddleware(Cors.withCors([]), [
Hopper.get("/hello", req => {
// ...
}),
Hopper.put("/preflighted", req => {
// ...
}),
])
import List from "list"
import Hopper from "./hopper"
import Cors from "./hopper-cors"
let withCorsDynamicOrigin = Cors.withCors([
Cors.AllowOrigin(origin => {
let allowedOrigins = // ...
List.contains(origin, allowedOrigins)
})
])
Hopper.serve([
Hopper.scope("/with-cors", withCorsDynamicOrigin, [
Hopper.get("/resource", req => {
// ...
})
])
])
grain doc
-generated API docs are available here
Aspects of the API and implementation of this library were adapted from Gin's Cors Library and Express' Cors Library.