Skip to main content

src/aws@lambda@response.erl

-module(aws@lambda@response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aws/lambda/response.gleam").
-export([proxy_response/2, proxy_to_json/1, sqs_batch_to_json/1]).
-export_type([proxy_response/0, sqs_batch_response/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Typed responses for Lambda integrations that expect a structured reply,\n"
    " with encoders that produce the JSON Lambda marshals back to the caller.\n"
    "\n"
    " Use these as the `encode` argument to `aws/lambda.start_json`.\n"
).

-type proxy_response() :: {proxy_response,
        integer(),
        gleam@dict:dict(binary(), binary()),
        list(binary()),
        binary(),
        boolean()}.

-type sqs_batch_response() :: {sqs_batch_response, list(binary())}.

-file("src/aws/lambda/response.gleam", 33).
?DOC(
    " A `200 OK` proxy response with a body and no extra headers. Set further\n"
    " fields with record-update syntax:\n"
    "\n"
    " ```gleam\n"
    " response.proxy_response(201, \"{}\")\n"
    " |> fn(r) { response.ProxyResponse(..r, headers: my_headers) }\n"
    " ```\n"
).
-spec proxy_response(integer(), binary()) -> proxy_response().
proxy_response(Status_code, Body) ->
    {proxy_response, Status_code, maps:new(), [], Body, false}.

-file("src/aws/lambda/response.gleam", 79).
-spec string_dict_to_json(gleam@dict:dict(binary(), binary())) -> gleam@json:json().
string_dict_to_json(Values) ->
    _pipe = Values,
    _pipe@1 = maps:to_list(_pipe),
    _pipe@2 = gleam@list:map(
        _pipe@1,
        fun(Pair) ->
            {erlang:element(1, Pair),
                gleam@json:string(erlang:element(2, Pair))}
        end
    ),
    gleam@json:object(_pipe@2).

-file("src/aws/lambda/response.gleam", 44).
?DOC(" Encode a [`ProxyResponse`](#ProxyResponse) to its Lambda JSON form.\n").
-spec proxy_to_json(proxy_response()) -> gleam@json:json().
proxy_to_json(Response) ->
    Base = [{<<"statusCode"/utf8>>, gleam@json:int(erlang:element(2, Response))},
        {<<"headers"/utf8>>, string_dict_to_json(erlang:element(3, Response))},
        {<<"body"/utf8>>, gleam@json:string(erlang:element(5, Response))},
        {<<"isBase64Encoded"/utf8>>,
            gleam@json:bool(erlang:element(6, Response))}],
    Fields = case erlang:element(4, Response) of
        [] ->
            Base;

        Cookies ->
            [{<<"cookies"/utf8>>,
                    gleam@json:array(Cookies, fun gleam@json:string/1)} |
                Base]
    end,
    gleam@json:object(Fields).

-file("src/aws/lambda/response.gleam", 68).
?DOC(" Encode an [`SqsBatchResponse`](#SqsBatchResponse) to its Lambda JSON form.\n").
-spec sqs_batch_to_json(sqs_batch_response()) -> gleam@json:json().
sqs_batch_to_json(Response) ->
    gleam@json:object(
        [{<<"batchItemFailures"/utf8>>,
                gleam@json:array(
                    erlang:element(2, Response),
                    fun(Id) ->
                        gleam@json:object(
                            [{<<"itemIdentifier"/utf8>>, gleam@json:string(Id)}]
                        )
                    end
                )}]
    ).