Aino.Token (aino v0.6.0)
The token is what flows through the entire web request
This module contains helper functions for dealing with the token, setting common fields for responses or looking up request fields.
At the end of a middleware chain, the token must contain three keys:
:response_status
:response_headers
:response_body
These keys are used for generating the request's response.
Link to this section Summary
Functions
Add a key/value pair to the assigns
map on a token
Start a token from an :elli
request
Reduce a token over a set of middleware.
Get a request header from the token
Set the response body
Get a response header from the token
Append a response header to the token
Set all response headers on the token
Set a response status on the token
Link to this section Types
@type t() :: map()
Link to this section Functions
assign(token, key, value)
Add a key/value pair to the assigns
map on a token
iex> token = %{assigns: %{}}
iex> Token.assign(token, :name, "User")
%{assigns: %{name: "User"}}
from_request(request)
Start a token from an :elli
request
The token gains the keys [:request]
iex> request = %Aino.Request{}
iex> token = Token.from_request(request)
iex> token.request == request
true
reduce(token, middleware)
Reduce a token over a set of middleware.
Takes a list of middleware, that may be either another list of middleware or a function that has an arity of 1.
Optionally you can use a two element tuple, the first element is a middleware function reference, with the second element being a list of options.
Options available for middleware during runtime are:
ignore_halt
, default false, if set to true the middleware will always run and not be short circuited by a halted token.
For example
middleware = [
Aino.Middleware.common(),
&Aino.Middleware.Routes.routes(&1, routes),
&Aino.Middleware.Routes.match_route/1,
&Aino.Middleware.params/1,
&Aino.Middleware.Routes.handle_route/1,
{&Aino.Middleware.logging/1, ignore_halt: true}
]
reduce(token, middleware)
request_header(token, request_header)
Get a request header from the token
This must be used with Aino.Middleware.headers/1
since that middleware sets
up the token to include a :headers
key that is downcased.
The request header that is searched for is lower cased and compared against request headers, filtering down to matching headers.
iex> token = %{headers: [{"content-type", "text/html"}, {"location", "/"}]}
iex> Token.request_header(token, "Content-Type")
["text/html"]
response_body(token, body)
Set the response body
When setting a response body, you should also set a Content-Type
header.
This way the client can know what type of data it received.
The token gains or modifies the keys [:response_body]
iex> token = %{}
iex> Token.response_body(token, "html")
%{response_body: "html"}
response_header(token, response_header)
Get a response header from the token
This must be used with Aino.Middleware.headers/1
since that middleware sets
up the token to include a :headers
key that is downcased.
The response header that is searched for is lower cased and compared against response headers, filtering down to matching headers.
iex> token = %{response_headers: [{"content-type", "text/html"}, {"location", "/"}]}
iex> Token.response_header(token, "Content-Type")
["text/html"]
response_header(token, key, value)
Append a response header to the token
Response headers default to an empty list if this is the first header set
The token gains or modifies the keys [:response_headers]
iex> token = %{}
iex> Token.response_header(token, "Content-Type", "application/json")
%{response_headers: [{"Content-Type", "application/json"}]}
iex> token = %{response_headers: [{"Content-Type", "text/html"}]}
iex> Token.response_header(token, "Location", "/")
%{response_headers: [{"Content-Type", "text/html"}, {"Location", "/"}]}
response_headers(token, headers)
Set all response headers on the token
If response headers are present, they are cleared. This directly sets the
:response_headers
key on the token.
The token gains or modifies the keys [:response_headers]
iex> token = %{}
iex> Token.response_headers(token, [{"Content-Type", "application/json"}])
%{response_headers: [{"Content-Type", "application/json"}]}
iex> token = %{response_headers: [{"Content-Type", "text/html"}]}
iex> Token.response_headers(token, [{"Location", "/"}])
%{response_headers: [{"Location", "/"}]}
response_status(token, status)
Set a response status on the token
The token gains the keys [:response_status]
iex> token = %{}
iex> Token.response_status(token, 200)
%{response_status: 200}