Documentation for PlugHTTPValidator.
Summary
Functions
Sets HTTP validators using the object(s) passed into parameter
Types
Functions
@spec set(Plug.Conn.t(), objects() | object(), opts()) :: Plug.Conn.t()
Sets HTTP validators using the object(s) passed into parameter
Call this plug before sending the response, for example:
def index(conn, _params) do
posts = MyApp.list_posts()
conn
|> PlugHTTPValidator.set(posts)
|> render("index.json", posts: posts)
end
def create(conn, params) do
# 201 status code is not cacheable by default
with {:ok, post} <- MyApp.create_post(params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.post_path(conn, :show, post))
|> render("show.json", post: post)
end
end
def show(conn, %{"id" => id}) do
with {:ok, post} = MyApp.get_post(id) do
conn
|> PlugHTTPValidator.set(post)
|> render("show.txt", post: post)
end
endBy default, this function sets the last-modified response header using the :updated_at
field of the object(s), taking the most recent if there are more than one object.
Options
:updated_at_field: the date field to use to set thelast-modifiedheader. Defaults to:update_at. The field must be aDateTime.t/0struct:etag_field: the field to set the etag from. No default, that is by default noetagresponse header is set. The etag field must be present in the object(s) passed as a parameter, otherwise the call will crash. Make sure to understand what an etag is before using it:etag_strength: the strenght of the etag,:weakor:strong. Defaults to:weak