Files
core/src/flow.ts
T
chrnx d6c5cee0ee feat: beta-hardening — routing, degradation, mesh gate, uploads, @header
Restructure first: split the two oversized files by cohesion (http.ts 449
→ http/{router,request,stream,ws,server,types}; app.ts 556 → app/{types,
introspect,index}), rename cryptic locals, JSDoc the public API, and
extract helpers so no function exceeds the complexity gate (createApp
187 → <15, listen 113 → 6). Public import paths are unchanged (barrels).

On that structure, five features that close the biggest gaps vs
Express/Fastify/Nest for a comparable beta:

- routing: `:name*` catch-all, specificity precedence (static ▸ param ▸
  catch-all, not registration order), and 405 + Allow when a path exists
  under a different method (was 404)
- visible degradation: optional providers that fail boot are summarized on
  listen() and queryable via app.degraded(), instead of a silent warn
- mesh is alpha — gated behind `experimental: true`; createApp throws if
  `mesh` is configured without it
- multipart moves to the optional `busboy` peer dependency (clear 501 when
  absent), keeping the core at one runtime dependency
- `@header('name')` as a singular alias of `@headers`

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-07-07 12:00:15 -05:00

44 lines
1.5 KiB
TypeScript

/** A single flow step: receives the accumulated context and returns the fields it contributes. */
export type StepFn<In, Out> = (ctx: In) => Out | Promise<Out>;
/** A built flow ready to execute: its ordered steps, terminal handler, and a `run` entry point. */
export interface CompiledFlow<Seed, Acc> {
steps: { name: string; fn: StepFn<any, any> }[];
handler: (ctx: Acc) => unknown;
run(seed: Seed): Promise<unknown>;
}
/** Immutable builder that chains typed steps, each widening the accumulated context type. */
export class Flow<Seed, Acc> {
constructor(private readonly steps: { name: string; fn: StepFn<any, any> }[] = []) {}
/** Appends a step and returns a new Flow whose context now includes the step's output. */
step<Out>(name: string, fn: StepFn<Acc, Out>): Flow<Seed, Acc & Out> {
return new Flow<Seed, Acc & Out>([...this.steps, { name, fn }]);
}
/** Seals the flow with a terminal handler, producing a runnable CompiledFlow. */
handle(handler: (ctx: Acc) => unknown): CompiledFlow<Seed, Acc> {
const steps = this.steps;
return {
steps,
handler,
async run(seed: Seed): Promise<unknown> {
let ctx: any = { ...seed };
for (const step of steps) {
const output = await step.fn(ctx);
ctx = { ...ctx, ...output };
}
return handler(ctx);
},
};
}
}
/** Starts a new flow whose seed and initial context are the same type. */
export function flow<Seed extends object>(): Flow<Seed, Seed> {
return new Flow<Seed, Seed>();
}