View Source Web3.Middleware behaviour (web3 v0.1.6)

Middleware provides an extension point to add functions that you want to be called for every method JSON RPC API.

Implement the Web3.Middleware behaviour in your module and define the before_dispatch/1, after_dispatch/1, and after_failure/1 callback functions.

Middleware inspired by

example-middleware

Example middleware

defmodule NoOpMiddleware do
  @behaviour Web3.Middleware

  alias Web3.Middleware.Pipeline
  import Pipeline

  def before_dispatch(%Pipeline{method: method} = pipeline) do
    pipeline
  end

  def after_dispatch(%Pipeline{method: method} = pipeline) do
    pipeline
  end

  def after_failure(%Pipeline{method: method} = pipeline) do
    pipeline
  end
end

Import the Web3.Middleware.Pipeline module to access convenience functions.

  • assign/3 - puts a key and value into the assigns map
  • halt/1 - stops execution of further middleware downstream and prevents dispatch of the method when used in a before_dispatch callback

Link to this section Summary

Link to this section Types

@type pipeline() :: %Web3.Middleware.Pipeline{
  app_id: term(),
  args: term(),
  assigns: term(),
  chain_id: term(),
  halted: term(),
  json_rpc_arguments: term(),
  metadata: term(),
  method: term(),
  method_name: term(),
  request: term(),
  response: term(),
  return_fn: term()
}

Link to this section Callbacks

Link to this callback

after_dispatch(pipeline)

View Source
@callback after_dispatch(pipeline()) :: pipeline()
@callback after_failure(pipeline()) :: pipeline()
Link to this callback

before_dispatch(pipeline)

View Source
@callback before_dispatch(pipeline()) :: pipeline()