Inngest.Middleware behaviour (Inngest v0.3.0)

Copy Markdown View Source

Behaviour for SDK middleware.

Middleware modules can implement any subset of the callbacks below. Configure middleware on a client with use Inngest.Client, middleware: [...] or on an individual function with middleware in Inngest.FnOpts.

Middleware entries run in registration order. Client-level entries run before function-level entries for function execution and step sends.

Entries may be modules or {module, opts} tuples:

use Inngest.Client,
  id: "my-app",
  funcs: [MyApp.Functions.Sync],
  middleware: [
    MyApp.Inngest.RequestLogger,
    {MyApp.Inngest.TenantMiddleware, tenant_header: "x-tenant-id"}
  ]

Hook Shape

Callbacks receive an args map and the middleware entry opts. Transform hooks return the updated args map. Wrap hooks receive args.next, a zero-arity function that continues the middleware chain, and return the wrapped result.

Middleware modules only need to define the callbacks they use. All callbacks are optional, so a middleware can implement a single hook without defining no-op functions for the rest.

This follows the current TypeScript SDK middleware model while using Elixir behaviour modules instead of classes.

Summary

Callbacks

Runs once when memoization replay has ended for this request.

Runs when middleware is registered on a client or function.

Runs after function code completes successfully.

Runs after function code returns or raises an error.

Runs before fresh function code begins for this request.

Runs after a fresh step handler completes successfully.

Runs after a fresh step handler raises.

Runs before a fresh step handler executes.

Mutates the function context, input, and memoized step map for this request.

Mutates outbound events before they are sent to Inngest.

Mutates step metadata and arguments before the step ID is hashed.

Wraps the user function handler.

Wraps an incoming function request.

Wraps event sending.

Wraps a step request.

Wraps a fresh step handler.

Types

args()

@type args() :: map()

entry()

@type entry() :: module() | {module(), opts()}

normalized_entry()

@type normalized_entry() :: {module(), opts()}

opts()

@type opts() :: Keyword.t()

result()

@type result() :: {:ok, term()} | {:error, term()}

Callbacks

on_memoization_end(args, opts)

(optional)
@callback on_memoization_end(args(), opts()) :: term()

Runs once when memoization replay has ended for this request.

In this SDK this hook fires after function input transformation, before fresh function code starts.

on_register(args, opts)

(optional)
@callback on_register(args(), opts()) :: term()

Runs when middleware is registered on a client or function.

args.client contains the runtime client. args.function is nil for client-level registration and the function module for function-level registration.

on_run_complete(args, opts)

(optional)
@callback on_run_complete(args(), opts()) :: term()

Runs after function code completes successfully.

on_run_error(args, opts)

(optional)
@callback on_run_error(args(), opts()) :: term()

Runs after function code returns or raises an error.

on_run_start(args, opts)

(optional)
@callback on_run_start(args(), opts()) :: term()

Runs before fresh function code begins for this request.

on_step_complete(args, opts)

(optional)
@callback on_step_complete(args(), opts()) :: term()

Runs after a fresh step handler completes successfully.

on_step_error(args, opts)

(optional)
@callback on_step_error(args(), opts()) :: term()

Runs after a fresh step handler raises.

on_step_start(args, opts)

(optional)
@callback on_step_start(args(), opts()) :: term()

Runs before a fresh step handler executes.

transform_function_input(args, opts)

(optional)
@callback transform_function_input(args(), opts()) :: args() | {:ok, args()}

Mutates the function context, input, and memoized step map for this request.

Return the updated args map with :ctx, :input, and :steps keys.

transform_send_event(args, opts)

(optional)
@callback transform_send_event(args(), opts()) :: args() | {:ok, args()}

Mutates outbound events before they are sent to Inngest.

This hook runs for both Inngest.Client.send/2 and Inngest.StepTool.send_event/3. Return the updated args map with an :events key.

transform_step_input(args, opts)

(optional)
@callback transform_step_input(args(), opts()) :: args() | {:ok, args()}

Mutates step metadata and arguments before the step ID is hashed.

Return the updated args map with :step_id, :step_type, :input, and :options keys where applicable.

wrap_function_handler(args, opts)

(optional)
@callback wrap_function_handler(args(), opts()) :: result()

Wraps the user function handler.

args.next returns the function result tuple, such as {:ok, value} or {:error, reason}.

wrap_request(args, opts)

(optional)
@callback wrap_request(args(), opts()) :: term()

Wraps an incoming function request.

args.request contains the framework request information when available and args.next returns the generated SDK response.

wrap_send_event(args, opts)

(optional)
@callback wrap_send_event(args(), opts()) :: Inngest.Client.send_result()

Wraps event sending.

args.next sends the events and returns the normal Inngest.Client.send/2 result tuple. Use this hook for metrics, backups, or result shaping around the HTTP send.

wrap_step(args, opts)

(optional)
@callback wrap_step(args(), opts()) :: term()

Wraps a step request.

This hook runs for both fresh and memoized step values. args.next returns the step value visible to user code.

wrap_step_handler(args, opts)

(optional)
@callback wrap_step_handler(args(), opts()) :: term()

Wraps a fresh step handler.

args.next executes the step body and returns its output. This hook does not run for memoized step values.