feat: pipeline returns StreamResult for async-iterable handlers
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+40
-6
No files matched your search
+9
-1
@@ -1,9 +1,16 @@
|
||||
import { Bus } from './bus';
|
||||
import { errorToResponse } from './transformers';
|
||||
import { isAsyncIterable } from './channel';
|
||||
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 interface StreamResult { stream: AsyncIterable<unknown> }
|
||||
export type PipelineResult = ResponseShape | StreamResult;
|
||||
|
||||
export function isStreamResult(r: PipelineResult): r is StreamResult {
|
||||
return 'stream' in r;
|
||||
}
|
||||
|
||||
export async function runPipeline(args: {
|
||||
steps: PipelineStep[];
|
||||
@@ -11,7 +18,7 @@ export async function runPipeline(args: {
|
||||
transformer: TransformerFn;
|
||||
seed: Record<string, unknown>;
|
||||
bus: Bus;
|
||||
}): Promise<ResponseShape> {
|
||||
}): Promise<PipelineResult> {
|
||||
const { steps, handler, transformer, seed, bus } = args;
|
||||
let ctx: any = { ...seed };
|
||||
try {
|
||||
@@ -22,6 +29,7 @@ export async function runPipeline(args: {
|
||||
bus.emit('request:step:leave', { name: s.name, scope: s.origin });
|
||||
}
|
||||
const result = await handler(ctx);
|
||||
if (isAsyncIterable(result)) return { stream: result }; // stream path: transformer bypassed
|
||||
const r = transformer(result);
|
||||
return { status: r.status ?? 200, headers: r.headers ?? {}, body: r.body };
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user