eberfreitas / elm-express / Express.Request

This module exposes ways of dealing with requests. Requests are only "acquired" when a new request incomes to the application. You should be able to access most of the original attributes of a request coming from Express with the methods exposed in this method.

Everything should be "read-only" so there is no way to change a request, only read from its attributes.

Types


type alias Request =
Express.Internal.Request.Request

The Request is an opaque type that holds the data from the request. It should have most of the readable attributes as described in the Express API reference.


type Method
    = Get
    | Head
    | Post
    | Put
    | Delete
    | Patch
    | Checkout
    | Copy
    | Lock
    | Merge
    | Mkactivity
    | Mkcol
    | Move
    | Notify
    | Options
    | Purge
    | Report
    | Search
    | Subscribe
    | Trace
    | Unlock
    | Unsubscribe

Defines the possible HTTP methods from a request. The available methods are described in this documentation for Express.

Reading from requests

id : Request -> String

All requests have an attached unique identifier. This function exposes such id of the request being passed. Id's are just UUID (v4) strings and they are used to track requests inside and outside the Elm application.

myId =
    Express.Request.id request

method : Request -> Method

Returns the request's method. Nice to use when pattern matching for specific routes. For a good example on how this could be used, look at the /example folder in the repository/source.

method =
    Express.Request.method request

url : Request -> Url

The url function will return an Elm Url describing the current request details. For more information, please refer to Elm's original docs for URLs.

Because elm-express does not have a dedicated router, you can use the URL data to perform routing like you would if you were creating an SPA with Elm. Elm's guide on Parsing URLs should be a very good starting point to understand how we can leverage Elm's APIs with elm-express for routing.

For a more detailed example, look at the /example folder in the repository/source.

url =
    Express.Request.url request

body : Request -> String

Gives access to the body of the request.

elm-express will always return the body as a String so it is your job to decode whatever is the content into the desired type. If you are getting a JSON payload, you can leverage Elm's own Json.Decode.decodeString and if it is a form payload, Elm's Url.Parser.Query should be up for the job.

simpleBody =
    Express.Request.body request

now : Request -> Time.Posix

This exposes the Time.Posix of the moment the request arrived, relative to the server time.

rightNow =
    Express.Request.now request

headers : Request -> Dict String String

Returns all the request's headers in a Dict.

headers =
    Express.Request.headers request

header : String -> Request -> Maybe String

Fetches a specific header from the request.

isAjax =
    request
        |> Express.Request.header "X-Requested-With"
        |> Maybe.map ((==) "XMLHttpRequest")
        |> Maybe.withDefault False

cookies : Request -> Dict String String

Returns all the request's cookies in a Dict. Although Express makes a distinction between signed and unsigned cookies, making them accessible with different attributes, all cookies, signed or not, will be be available in this Dict.

cookies =
    Express.Request.cookies request

cookie : String -> Request -> Maybe String

Gets the value of a specific cookie. All cookies, signed or not, can be accessed through this function.

lastVisit =
    Express.Request.cookie "lastVisit" request

session : String -> Request -> Maybe String

Allows access to specific session keys. Details on how you can setup your session are in the documentation for the JavaScript portion of this library. Please refer to the README for that.

user =
    Express.Request.session "user" request

Helpers

decodeRequestId : Json.Decode.Value -> Result Json.Decode.Error String

This is a helper function that takes a Json.Decode.Value (most likely a JSON object from JavaScript) and extracts the requestId key from such Value. It is very useful when defining the decodeRequestId function when creating your application with Express.application. For an example on how this can be used, check the /example folder in the repository/source.