@x-titan/pipeline
v0.5.0
Published
Minimal TypeScript-first middleware pipeline for any protocol.
Downloads
503
Readme
x-pipe / Pipeline
Minimal TypeScript-first middleware pipeline for any protocol.
Example
import Pipeline from "x-pipe"
interface Context {
userId?: string
isAdmin?: boolean
}
const pipe = new Pipeline<Context>()
pipe
.use(async (ctx, next) => {
ctx.userId = "42"
await next()
})
.use(async (ctx, next) => {
ctx.isAdmin = true
await next()
})
await pipe.compose()({}) // ctx: { userId: "42", isAdmin: true }Methods
new Pipeline<ContextT>()
Creates a new pipeline with context type ContextT.
use<Extra extends object>(fn: Middleware<ContextT & Extra>)
Adds middleware to the pipeline and returns a new Pipeline with extended context ContextT & Extra.
pipe.use<ExtraContext>(async (ctx, next) => { ... })compose()
Returns a function (ctx: ContextT, next?: Next) => Promise<void> that runs the pipeline.
const fn = pipe.compose()
await fn(ctx)Types
type Next = () => Promise<void>
type Middleware<ContextT = unknown> = (ctx: ContextT, next: Next) => void | Promise<void>