Current section
Files
Jump to
Current section
Files
src/bliss@middleware.erl
-module(bliss@middleware).
-compile(no_auto_import).
-export([cors/2]).
-spec add_cors_headers(gleam@http@response:response(HAR), binary()) -> gleam@http@response:response(HAR).
add_cors_headers(Response, Origin) ->
_pipe = Response,
_pipe@1 = gleam@http@response:prepend_header(
_pipe,
<<"Access-Control-Allow-Origin"/utf8>>,
Origin
),
_pipe@2 = gleam@http@response:prepend_header(
_pipe@1,
<<"Access-Control-Allow-Methods"/utf8>>,
<<"DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT"/utf8>>
),
_pipe@3 = gleam@http@response:prepend_header(
_pipe@2,
<<"Access-Control-Allow-Headers"/utf8>>,
<<"Content-Type"/utf8>>
),
gleam@http@response:prepend_header(
_pipe@3,
<<"Access-Control-Allow-Credentials"/utf8>>,
<<"true"/utf8>>
).
-spec cors(
fun((gleam@http@request:request(bitstring()), HAV) -> {ok,
gleam@http@response:response(gleam@bit_builder:bit_builder())} |
{error, HBB}),
binary()
) -> fun((gleam@http@request:request(bitstring()), HAV) -> {ok,
gleam@http@response:response(gleam@bit_builder:bit_builder())} |
{error, HBB}).
cors(Handler, Origin) ->
fun(Req, Ctx) ->
Resp@1 = case erlang:element(2, Req) of
options ->
Resp = begin
_pipe = gleam@http@response:new(202),
gleam@http@response:set_body(
_pipe,
gleam@bit_builder:from_string(<<""/utf8>>)
)
end,
{ok, Resp};
_@1 ->
Handler(Req, Ctx)
end,
_pipe@1 = Resp@1,
gleam@result:map(
_pipe@1,
fun(_capture) -> add_cors_headers(_capture, Origin) end
)
end.