the-sett / elm-serverless / Serverless.Conn

Functions for querying and updating connections.


type Conn config model route msg

A connection with a request and response.

Connections are parameterized with config and model record types which are specific to the application. Config is loaded once on app startup, while model is set to a provided initial value for each incomming request.


type alias Id =
String

Universally unique connection identifier.

Table of Contents

Processing Application Data

Query and update your application specific data.

config : Conn config model route msg -> config

Application defined configuration

model : Conn config model route msg -> model

Application defined model

updateModel : (model -> model) -> Conn config model route msg -> Conn config model route msg

Transform and update the application defined model stored in the connection.

Querying the Request

Get details about the HTTP request.

request : Conn config model route msg -> Request

Request

id : Conn config model route msg -> Id

Universally unique Conn identifier

method : Conn config model route msg -> Request.Method

Request HTTP method

header : String -> Conn config model route msg -> Maybe String

Get a request header by name

route : Conn config model route msg -> route

Parsed route

Responding

Update the response and send it.

respond : ( Response.Status, Body ) -> Conn config model route msg -> ( Conn config model route msg, Platform.Cmd.Cmd msg )

Update a response and send it.

import Serverless.Conn.Response exposing (setBody, setStatus)
import TestHelpers exposing (conn, responsePort)

-- The following two expressions produce the same result
conn
    |> respond ( 200, textBody "Ok" )
--> conn
-->     |> updateResponse
-->         ((setStatus 200) >> (setBody <| textBody "Ok"))
-->     |> send

updateResponse : (Response -> Response) -> Conn config model route msg -> Conn config model route msg

Applies the given transformation to the connection response.

Does not do anything if the response has already been sent.

import Serverless.Conn.Response exposing (addHeader)
import TestHelpers exposing (conn, getHeader)

conn
    |> updateResponse
        (addHeader ( "Cache-Control", "no-cache" ))
    |> getHeader "cache-control"
--> Just "no-cache"

send : Conn config model route msg -> ( Conn config model route msg, Platform.Cmd.Cmd msg )

Sends a connection response through the given port

toSent : Conn config model route msg -> Conn config model route msg

Converts a conn to a sent conn, making it immutable.

The connection will be sent once the current update loop completes. This function is intended to be used by middleware, which cannot issue side-effects.

import TestHelpers exposing (conn)

(unsent conn) == Just conn
--> True

(unsent <| toSent conn) == Nothing
--> True

unsent : Conn config model route msg -> Maybe (Conn config model route msg)

Return Just the same can if it has not been sent yet.

mapUnsent : (Conn config model route msg -> ( Conn config model route msg, Platform.Cmd.Cmd msg )) -> Conn config model route msg -> ( Conn config model route msg, Platform.Cmd.Cmd msg )

Apply an update function to a conn, but only if the conn is unsent.

Misc

These functions are typically not needed when building an application. They are used internally by the framework. They may be useful when debugging or writing unit tests.

init : Id -> config -> model -> route -> Request -> Conn config model route msg

Initialize a new Conn.

jsonEncodedResponse : Conn config model route msg -> Json.Encode.Value

Response as JSON encoded to a string.

This is the format the response takes when it gets sent through the response port.

createInteropContext : (Json.Encode.Value -> msg) -> Conn config model route msg -> ( Basics.Int, Conn config model route msg )

Adds a response message builder for an interop port call, under a unique sequence number on the connection.

consumeInteropContext : Basics.Int -> Conn config model route msg -> ( Maybe (Json.Encode.Value -> msg), Conn config model route msg )

Attemps to find the response message builder for an interop port call, by its unique sequence number, and removes this sequence number from the context store on the connection.