Skip to content

Pipeline

David Ciulla edited this page Nov 7, 2026 · 1 revision

Pipeline

Laravex.Pipeline is a passable pipeline that sends a value through a sequence of pipes and returns the result to a final destination callback. It mirrors the Laravel Illuminate\Pipeline\Pipeline interface.


Basic Usage

alias Laravex.Pipeline

result =
  Pipeline.send(passable)
  |> Pipeline.through([PipeA, PipeB, PipeC])
  |> Pipeline.then(fn value -> value end)

send/1 accepts any value. through/2 accepts a list of pipes. then/2 runs the pipeline and passes the result to the destination function.


Defining Pipes

A pipe can be:

Form Behaviour
fn value -> new_value end 1-arity anonymous function
MyModule MyModule.handle(value) is called (default method)
{MyModule, :my_fun} MyModule.my_fun(value) is called
defmodule App.Pipes.TrimInput do
  def handle(value) when is_binary(value), do: String.trim(value)
  def handle(value), do: value
end

defmodule App.Pipes.Uppercase do
  def handle(value) when is_binary(value), do: String.upcase(value)
  def handle(value), do: value
end

result =
  Pipeline.send("  hello world  ")
  |> Pipeline.through([App.Pipes.TrimInput, App.Pipes.Uppercase])
  |> Pipeline.then(fn value -> value end)
# => "HELLO WORLD"

via/2 — Custom Method Name

By default, Laravex.Pipeline calls the :handle method on module pipes. Use via/2 to change the method name:

Pipeline.send(request)
|> Pipeline.through([CheckAuth, LogRequest])
|> Pipeline.via(:process)
|> Pipeline.then(fn req -> req end)

Each module pipe will now be called as Module.process(value) instead of Module.handle(value).


Early Halt with {:halt, value}

A pipe may return {:halt, value} to stop the pipeline immediately. The halted value is still passed to the then/2 destination:

defmodule App.Pipes.CheckMaintenance do
  def handle(request) do
    if Laravex.Foundation.Maintenance.active?() do
      {:halt, Laravex.Http.Response.html("Down for maintenance", 503)}
    else
      request
    end
  end
end

response =
  Pipeline.send(request)
  |> Pipeline.through([App.Pipes.CheckMaintenance, App.Pipes.Authenticate])
  |> Pipeline.then(fn req -> handle_request(req) end)

If CheckMaintenance returns {:halt, response}, Authenticate is not called and the destination receives the halted value directly.


Composing Pipelines

through/2 accumulates pipes, so you can compose partial pipelines:

base =
  Pipeline.send(input)
  |> Pipeline.through([PipeA, PipeB])

result =
  base
  |> Pipeline.through([PipeC, PipeD])
  |> Pipeline.then(fn value -> value end)

See Also

  • Middleware — HTTP middleware is a pipeline under the hood
  • Events — Listeners form a similar pipe-and-dispatch pattern

Clone this wiki locally