createMiddleware

Function: createMiddleware()

ts
function createMiddleware<TCtxIn>(): CreateMiddlewareBuilder<TCtxIn>;
function createMiddleware<TCtxIn>(): CreateMiddlewareBuilder<TCtxIn>;

Defined in: packages/workflow-core/src/middleware/create-middleware.ts:46

Build a middleware that extends the workflow ctx. Type-level accumulation makes the extension visible to downstream middleware and the handler.

plaintext
const traced = createMiddleware().server(async ({ ctx, next }) => {
  const trace = startTrace(ctx.runId)
  try {
    return await next({ context: { trace } })
  } finally {
    trace.end()
  }
})
const traced = createMiddleware().server(async ({ ctx, next }) => {
  const trace = startTrace(ctx.runId)
  try {
    return await next({ context: { trace } })
  } finally {
    trace.end()
  }
})

For middleware that should compose on top of an already-extended ctx, type the generic explicitly:

plaintext
createMiddleware<{ user: User }>().server(async ({ ctx, next }) => {
  // ctx.user is typed
})
createMiddleware<{ user: User }>().server(async ({ ctx, next }) => {
  // ctx.user is typed
})

Type Parameters

TCtxIn

TCtxIn = unknown

Returns

CreateMiddlewareBuilder<TCtxIn>