the-sett / elm-serverless / Serverless.Plug

A pipeline is a sequence of functions which transform the connection, optionally sending back an HTTP response at each step. We use the term plug to mean a single function that is part of the pipeline. But a pipeline is also just a plug and so pipelines can be composed from other pipelines.

Examples below assume the following imports:

import Serverless.Conn exposing (updateResponse)
import Serverless.Conn.Body exposing (text)
import Serverless.Conn.Response exposing (addHeader, setBody, setStatus)


type Plug config model route msg

Represents a pipeline or section of a pipeline.

Building Pipelines

Use these functions to build your pipelines.

pipeline : Plug config model route msg

Begins a pipeline.

Build the pipeline by chaining plugs with plug, loop, fork, and nest.

size pipeline
--> 0

plug : (Serverless.Conn.Conn config model route msg -> Serverless.Conn.Conn config model route msg) -> Plug config model route msg -> Plug config model route msg

Extend the pipeline with a simple plug.

A plug just transforms the connection. For example,

pipeline
    |> plug (updateResponse <| setBody <| text "Ok" )
    |> plug (updateResponse <| setStatus 200)
    |> size
--> 2

nest : Plug config model route msg -> Plug config model route msg -> Plug config model route msg

Extends the pipeline with a plug.

This is the most general of the pipeline building functions. Since it just accepts a plug, and since a plug can be a pipeline, it can be used to extend a pipeline with a group of plugs.

pipeline
    |> nest (pipeline
                |> plug (updateResponse <| addHeader ( "key", "value" ))
                -- ...
            )
    |> plug (updateResponse <| setStatus 200)
    |> size
--> 2

Applying Pipelines

apply : Plug config model route msg -> Serverless.Conn.Conn config model route msg -> Serverless.Conn.Conn config model route msg

Basic pipeline update function.

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.

size : Plug config model route msg -> Basics.Int

The number of plugs in a pipeline