feat: request pipeline with ctx accumulation, throw handling, lifecycle events

This commit is contained in:
chrnx committed 2026-06-26 00:47:55 -05:00
1 parent af4642d449
commit 23a31f9264
2 files changed
+73

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import { Bus } from './bus';
import { errorToResponse } from './transformers';
import type { TransformerFn } from './metadata';
export interface PipelineStep { name: string; origin: string; run: (ctx: any) => any }
export interface ResponseShape { status: number; headers: Record<string, string>; body: string }
export async function runPipeline(args: {
steps: PipelineStep[];
handler: (ctx: any) => unknown;
transformer: TransformerFn;
seed: Record<string, unknown>;
bus: Bus;
}): Promise<ResponseShape> {
const { steps, handler, transformer, seed, bus } = args;
let ctx: any = { ...seed };
try {
for (const s of steps) {
bus.emit('request:step:enter', { name: s.name, scope: s.origin });
const out = await s.run(ctx);
ctx = { ...ctx, ...out };
bus.emit('request:step:leave', { name: s.name, scope: s.origin });
}
const result = await handler(ctx);
const r = transformer(result);
return { status: r.status ?? 200, headers: r.headers ?? {}, body: r.body };
} catch (error) {
bus.emit('request:step:error', { name: 'pipeline', error });
return errorToResponse(error);
}
}