Nex.Res
(nex_core v0.4.3)
Copy Markdown
Pipeline-style response builder for API routes.
An alternative to Nex.json/2, Nex.html/2, etc. that uses
Elixir pipe syntax to build responses incrementally.
Examples
def get(req) do
Nex.Res.new()
|> Nex.Res.json(%{users: list_users()})
end
def post(req) do
Nex.Res.new()
|> Nex.Res.status(201)
|> Nex.Res.json(%{created: true})
end
def delete(req) do
Nex.Res.new()
|> Nex.Res.status(204)
|> Nex.Res.send("")
end
def get(req) do
Nex.Res.new()
|> Nex.Res.redirect("/login")
end
def get(req) do
Nex.Res.new()
|> Nex.Res.set_header("x-request-id", "abc123")
|> Nex.Res.status(200)
|> Nex.Res.json(%{ok: true})
end
Summary
Functions
Sets an HTML response body and content type.
Sets a JSON response body and content type.
Creates a new response with default values (200 OK, empty body).
Sets a redirect response with Location header.
Sets the response body without changing content type.
Sets a response header.
Sets the HTTP status code.
Sets a plain text response body and content type.
Functions
@spec html(Nex.Response.t(), String.t()) :: Nex.Response.t()
Sets an HTML response body and content type.
@spec json(Nex.Response.t(), term()) :: Nex.Response.t()
Sets a JSON response body and content type.
@spec new() :: Nex.Response.t()
Creates a new response with default values (200 OK, empty body).
@spec redirect(Nex.Response.t(), String.t(), non_neg_integer()) :: Nex.Response.t()
Sets a redirect response with Location header.
@spec send(Nex.Response.t(), term()) :: Nex.Response.t()
Sets the response body without changing content type.
@spec set_header(Nex.Response.t(), String.t(), String.t()) :: Nex.Response.t()
Sets a response header.
@spec status(Nex.Response.t(), non_neg_integer()) :: Nex.Response.t()
Sets the HTTP status code.
@spec text(Nex.Response.t(), String.t()) :: Nex.Response.t()
Sets a plain text response body and content type.