Serverless.Conn

Functions for querying and updating connections.


type Conn config model route

[]

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.String

Universally unique connection identifier.


Table of Contents

Processing Application Data

Query and update your application specific data.


config : Serverless.Conn.Conn config model route -> config

Application defined configuration


model : Serverless.Conn.Conn config model route -> model

Application defined model


updateModel : (model -> model) -> Serverless.Conn.Conn config model route -> Serverless.Conn.Conn config model route

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


Querying the Request

Get details about the HTTP request.


request : Serverless.Conn.Conn config model route -> Serverless.Conn.Request.Request

Request


id : Serverless.Conn.Conn config model route -> Serverless.Conn.Id

Universally unique Conn identifier


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

Request HTTP method


header : String.String -> Serverless.Conn.Conn config model route -> Maybe.Maybe String.String

Get a request header by name


route : Serverless.Conn.Conn config model route -> route

Parsed route


Responding

Update the response and send it.


respond : ( Serverless.Conn.Response.Status, Serverless.Conn.Body.Body ) -> Serverless.Conn.Conn config model route -> ( Serverless.Conn.Conn config model route, 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 : (Serverless.Conn.Response.Response -> Serverless.Conn.Response.Response) -> Serverless.Conn.Conn config model route -> Serverless.Conn.Conn config model route

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 : Serverless.Conn.Conn config model route -> ( Serverless.Conn.Conn config model route, Platform.Cmd.Cmd msg )

Sends a connection response through the given port


toSent : Serverless.Conn.Conn config model route -> Serverless.Conn.Conn config model route

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 : Serverless.Conn.Conn config model route -> Maybe.Maybe (Serverless.Conn.Conn config model route)

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


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

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


Response Body

Use these constructors to create response bodies with different content types.


textBody : String.String -> Serverless.Conn.Body.Body

A plain text body.


jsonBody : Json.Encode.Value -> Serverless.Conn.Body.Body

A JSON body.


binaryBody : String.String -> String.String -> Serverless.Conn.Body.Body

A binary file.


Misc

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


init : Serverless.Conn.Id -> config -> model -> route -> Serverless.Conn.Request.Request -> Serverless.Conn.Conn config model route

Initialize a new Conn.


jsonEncodedResponse : Serverless.Conn.Conn config model route -> 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.