fix: guard handler errors, dedupe runner names, export flow core, test optional provider
This commit is contained in:
4 files changed
+34
-6
No files matched your search
+7
-3
@@ -37,6 +37,10 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
||||
const providerNodes: GraphNode[] = [];
|
||||
const stepNodes: GraphNode[] = [];
|
||||
const runners = new Map<string, (ctx: any) => any>(); // node name -> runtime fn
|
||||
const setRunner = (name: string, fn: (ctx: any) => any) => {
|
||||
if (runners.has(name)) throw new Error(`duplicate provider/step name '${name}' — names must be unique across modules and plugins`);
|
||||
runners.set(name, fn);
|
||||
};
|
||||
const providerMeta = new Map<string, { optional: boolean }>();
|
||||
const routePlans: RoutePlan[] = [];
|
||||
|
||||
@@ -50,13 +54,13 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
||||
providerNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
||||
providerMeta.set(meta.provides, { optional: meta.optional });
|
||||
const inst: any = new P();
|
||||
runners.set(meta.provides, (ctx) => inst.provide(ctx));
|
||||
setRunner(meta.provides, (ctx) => inst.provide(ctx));
|
||||
}
|
||||
for (const S of m.steps ?? []) {
|
||||
const meta = getStepMeta(S)!;
|
||||
stepNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
||||
const inst: any = new S();
|
||||
runners.set(meta.provides, (ctx) => inst.run(ctx));
|
||||
setRunner(meta.provides, (ctx) => inst.run(ctx));
|
||||
}
|
||||
for (const C of m.controllers ?? []) {
|
||||
for (const route of getRoutes(C)) {
|
||||
@@ -79,7 +83,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
||||
for (const n of extraSteps) {
|
||||
const node = { name: n.name, needs: n.needs, provides: n.provides, origin: 'plugin' };
|
||||
if (n.kind === 'provider') providerNodes.push(node); else stepNodes.push(node);
|
||||
runners.set(n.name, n.run);
|
||||
setRunner(n.name, n.run);
|
||||
}
|
||||
|
||||
// For each route, resolve which providers/steps feed it via topo order.
|
||||
|
||||
+9
-3
@@ -1,5 +1,6 @@
|
||||
import http from 'http';
|
||||
import type { ResponseShape } from './pipeline';
|
||||
import { errorToResponse } from './transformers';
|
||||
|
||||
export type RouteHandler = (req: {
|
||||
method: string; url: string;
|
||||
@@ -36,9 +37,14 @@ export function createHttpServer(routes: RouteDef[]): http.Server {
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
return;
|
||||
}
|
||||
const result = await matched.handler({
|
||||
method: req.method ?? 'GET', url: req.url ?? '/', headers: req.headers, params: matched.params,
|
||||
});
|
||||
let result: ResponseShape;
|
||||
try {
|
||||
result = await matched.handler({
|
||||
method: req.method ?? 'GET', url: req.url ?? '/', headers: req.headers, params: matched.params,
|
||||
});
|
||||
} catch (error) {
|
||||
result = errorToResponse(error);
|
||||
}
|
||||
res.writeHead(result.status, result.headers);
|
||||
res.end(result.body);
|
||||
});
|
||||
|
||||
@@ -8,3 +8,5 @@ export type { Plugin } from './plugin';
|
||||
export { JsonTransformer } from './transformers';
|
||||
export { HttpError, Unauthorized, NotFound, Redirect, NotModified } from './signals';
|
||||
export { Bus } from './bus';
|
||||
export { flow, Flow } from './flow';
|
||||
export type { StepFn, CompiledFlow } from './flow';
|
||||
Reference in new issue
Block a user