marcodaniels/elm-aws-cloudfront/1.0.0

CloudFront

The cloudFront function requires an input and output ports as a tuple parameter in order to communicate with the JavaScript handler code.

port module MyModule exposing (..)


import CloudFront exposing (cloudFront)
import Json.Decode as Decode
import Json.Encode as Encode


port inputEvent : (Decode.Value -> msg) -> Sub msg


port outputEvent : Encode.Value -> Cmd msg

The ports must have the same type as the example.

(flags -> Maybe InputOrigin -> OutputEvent)
-> ( (Value -> Msg) -> Sub Msg, Value -> Cmd Msg )
-> Program flags (Model flags) Msg

Create a CloudFront origin handler to handle the request/response of your CloudFront distribution:

import CloudFront.Lambda exposing (originRequest, toRequest)


( inputPort, outputPort )
    |> (originRequest
            (\{ request } _ -> request |> toRequest)
            |> cloudFront
       )

Core Model and Msg

type alias Model a =
{ event : Maybe InputEvent
, flags : a
}

Model contains the decoded InputEvent and the optional flags passed on init.

type Msg
= Input (Result Error InputEvent)

Msg is used to contain the result/error of decoding the input event from ports.

(flags -> Maybe InputOrigin -> OutputEvent)
-> ( (Value -> Msg) -> Sub Msg, Value -> Cmd Msg )
-> { init : flags -> ( Model flags, Cmd Msg )
, update : Msg -> Model flags -> ( Model flags, Cmd Msg )
, subscriptions : Model flags -> Sub Msg
}