@macss/modular-api 0.4.8 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -101,6 +101,12 @@ class HelloPlugin implements Plugin {
101
101
  method: 'GET',
102
102
  path: '/hello-plugin',
103
103
  visibility: 'custom',
104
+ // Optional OpenAPI Operation object — when present, the official
105
+ // OpenApiPlugin merges the route into /openapi.json and /docs (ADR-0003).
106
+ openapi: {
107
+ summary: 'Hello from a plugin route',
108
+ responses: { '200': { description: 'OK' } },
109
+ },
104
110
  handler: () => ({
105
111
  status: 200,
106
112
  body: { ok: true, basePath: host.metadata().basePath },
@@ -2,7 +2,7 @@ import type { GraphqlOptions } from '../graphql/runtime/graphql_runtime_options'
2
2
  import type { HealthService } from './health/health_service';
3
3
  import type { Counter, Gauge, Histogram } from './metrics/metric';
4
4
  import type { MetricRegistry } from './metrics/metric_registry';
5
- import type { Plugin } from './plugin';
5
+ import type { Plugin, RegisteredPluginRouteView } from './plugin';
6
6
  export interface OperationalRoutePaths {
7
7
  healthPath: string;
8
8
  docsPath: string;
@@ -31,5 +31,11 @@ interface BuildRuntimePluginsOptions {
31
31
  };
32
32
  }
33
33
  export declare function operationalRoutePaths(basePath: string, metricsPath?: string): OperationalRoutePaths;
34
+ /**
35
+ * Merges plugin-contributed routes into the generated OpenAPI spec (ADR-0003).
36
+ * Only `custom` and `transport` routes that declare an `openapi` operation are
37
+ * documented; `operational` routes do not belong to the business contract.
38
+ */
39
+ export declare function mergePluginRoutesIntoSpec(spec: Record<string, unknown>, routes: RegisteredPluginRouteView[]): void;
34
40
  export declare function buildRuntimePlugins(options: BuildRuntimePluginsOptions): Plugin[];
35
41
  export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.operationalRoutePaths = operationalRoutePaths;
4
+ exports.mergePluginRoutesIntoSpec = mergePluginRoutesIntoSpec;
4
5
  exports.buildRuntimePlugins = buildRuntimePlugins;
5
6
  const openapi_1 = require("../openapi/openapi");
6
7
  const swagger_docs_1 = require("../openapi/swagger_docs");
@@ -18,6 +19,23 @@ function operationalRoutePaths(basePath, metricsPath) {
18
19
  metricsPath: metricsPath ? joinPath(basePath, metricsPath) : undefined,
19
20
  };
20
21
  }
22
+ /**
23
+ * Merges plugin-contributed routes into the generated OpenAPI spec (ADR-0003).
24
+ * Only `custom` and `transport` routes that declare an `openapi` operation are
25
+ * documented; `operational` routes do not belong to the business contract.
26
+ */
27
+ function mergePluginRoutesIntoSpec(spec, routes) {
28
+ const paths = (spec.paths ?? {});
29
+ spec.paths = paths;
30
+ for (const route of routes) {
31
+ if (route.visibility === 'operational' || !route.openapi) {
32
+ continue;
33
+ }
34
+ const pathItem = (paths[route.path] ?? {});
35
+ pathItem[route.method.toLowerCase()] = route.openapi;
36
+ paths[route.path] = pathItem;
37
+ }
38
+ }
21
39
  function buildRuntimePlugins(options) {
22
40
  const plugins = [
23
41
  new HealthRuntimePlugin(options.healthService),
@@ -99,7 +117,12 @@ class MetricsRuntimePlugin {
99
117
  requestsInFlight: this.options.requestsInFlight,
100
118
  requestDuration: this.options.requestDuration,
101
119
  excludedRoutes,
102
- registeredPaths: registry_1.apiRegistry.routes.map((route) => route.path),
120
+ // Plugin routes also get their real route label instead of UNMATCHED (ADR-0003).
121
+ // User plugins run setup() before official plugins, so their routes are visible here.
122
+ registeredPaths: [
123
+ ...registry_1.apiRegistry.routes.map((route) => route.path),
124
+ ...host.routes().map((route) => route.path),
125
+ ],
103
126
  }),
104
127
  });
105
128
  host.registerRoute({
@@ -133,6 +156,7 @@ class OpenApiRuntimePlugin {
133
156
  version: this.options.version,
134
157
  servers: this.options.servers,
135
158
  });
159
+ mergePluginRoutesIntoSpec(spec, host.routes());
136
160
  const yaml = (0, openapi_1.jsonToYaml)(spec);
137
161
  const specUrl = operationalRoutePaths(this.options.basePath).openApiJsonPath;
138
162
  host.exposeCapability({
@@ -74,6 +74,13 @@ export interface PluginRoute {
74
74
  method: HttpMethod;
75
75
  path: string;
76
76
  visibility: PluginRouteVisibility;
77
+ /**
78
+ * Optional standard OpenAPI Operation object (summary, parameters, requestBody,
79
+ * responses — including binary content types). When present on a `custom` or
80
+ * `transport` route, the official OpenApiPlugin merges it into the generated
81
+ * spec so the route appears in /openapi.json and /docs (ADR-0003).
82
+ */
83
+ openapi?: Record<string, unknown>;
77
84
  handler(context: PluginRequestContext): PluginResponse | Promise<PluginResponse>;
78
85
  }
79
86
  export interface PluginMiddleware {
@@ -91,10 +98,21 @@ export interface RegisteredUseCaseView {
91
98
  method: string;
92
99
  path: string;
93
100
  }
101
+ /** Read view of a plugin route already registered on the host (ADR-0003). */
102
+ export interface RegisteredPluginRouteView {
103
+ pluginId?: string;
104
+ id: string;
105
+ method: HttpMethod;
106
+ /** Absolute mounted path (basePath already joined). */
107
+ path: string;
108
+ visibility: PluginRouteVisibility;
109
+ openapi?: Record<string, unknown>;
110
+ }
94
111
  export interface PluginHost {
95
112
  metadata(): HostMetadata;
96
113
  modules(): RegisteredModuleView[];
97
114
  useCases(): RegisteredUseCaseView[];
115
+ routes(): RegisteredPluginRouteView[];
98
116
  registerRoute(route: PluginRoute): void;
99
117
  registerMiddleware(middleware: PluginMiddleware): void;
100
118
  exposeCapability(capability: Capability): void;
@@ -124,7 +142,7 @@ interface RuntimePluginHostOptions {
124
142
  }
125
143
  export declare class RuntimePluginHost implements PluginHost {
126
144
  private readonly metadataValue;
127
- private readonly routes;
145
+ private readonly registeredRoutes;
128
146
  private readonly middlewares;
129
147
  private readonly capabilitiesMap;
130
148
  private readonly extensionPoints;
@@ -140,6 +158,7 @@ export declare class RuntimePluginHost implements PluginHost {
140
158
  modules(): RegisteredModuleView[];
141
159
  useCases(): RegisteredUseCaseView[];
142
160
  registerRoute(route: PluginRoute): void;
161
+ routes(): RegisteredPluginRouteView[];
143
162
  registerMiddleware(middleware: PluginMiddleware): void;
144
163
  beginPluginSetup(pluginId: string): void;
145
164
  endPluginSetup(): void;
@@ -22,7 +22,7 @@ class PluginHostError extends Error {
22
22
  exports.PluginHostError = PluginHostError;
23
23
  class RuntimePluginHost {
24
24
  metadataValue;
25
- routes = [];
25
+ registeredRoutes = [];
26
26
  middlewares = [];
27
27
  capabilitiesMap = new Map();
28
28
  extensionPoints = new Map();
@@ -63,7 +63,17 @@ class RuntimePluginHost {
63
63
  throw new PluginHostError('ROUTE_CONFLICT', `Route conflict for ${key}`, undefined, key);
64
64
  }
65
65
  this.routeKeys.add(key);
66
- this.routes.push({ finalPath, route });
66
+ this.registeredRoutes.push({ finalPath, route, pluginId: this.activePluginId });
67
+ }
68
+ routes() {
69
+ return this.registeredRoutes.map((registration) => ({
70
+ pluginId: registration.pluginId,
71
+ id: registration.route.id,
72
+ method: registration.route.method,
73
+ path: registration.finalPath,
74
+ visibility: registration.route.visibility,
75
+ openapi: registration.route.openapi,
76
+ }));
67
77
  }
68
78
  registerMiddleware(middleware) {
69
79
  this.assertMutable();
@@ -142,7 +152,7 @@ class RuntimePluginHost {
142
152
  }
143
153
  }
144
154
  applyRoutes(router) {
145
- for (const registration of this.routes) {
155
+ for (const registration of this.registeredRoutes) {
146
156
  const method = registration.route.method.toLowerCase();
147
157
  router[method](registration.finalPath, buildPluginRouteHandler(registration.route, this.capabilitiesMap));
148
158
  }
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export { InputValidationError } from './core/input_validation_error';
7
7
  export { ModularApi } from './core/modular_api';
8
8
  export type { ModularApiOptions } from './core/modular_api';
9
9
  export { HOST_API_VERSION, PluginHostError } from './core/plugin';
10
- export type { Capability, CapabilityHandle, HostMetadata, HttpMethod, MiddlewareSlot, ModuleExtensionContribution, ModuleExtensionPoint, Plugin, PluginHost, PluginManifest, PluginMiddleware, PluginRequestContext, PluginRequirement, PluginResponse, PluginRoute, PluginRouteVisibility, PluginValidationResult, RegisteredModuleView, RegisteredUseCaseView, } from './core/plugin';
10
+ export type { Capability, CapabilityHandle, HostMetadata, HttpMethod, MiddlewareSlot, ModuleExtensionContribution, ModuleExtensionPoint, Plugin, PluginHost, PluginManifest, PluginMiddleware, PluginRequestContext, PluginRequirement, PluginResponse, PluginRoute, PluginRouteVisibility, PluginValidationResult, RegisteredModuleView, RegisteredPluginRouteView, RegisteredUseCaseView, } from './core/plugin';
11
11
  export { ModuleBuilder } from './core/module_builder';
12
12
  export type { UseCaseOptions } from './core/module_builder';
13
13
  export { cors } from './middlewares/cors';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@macss/modular-api",
3
- "version": "0.4.8",
3
+ "version": "0.5.0",
4
4
  "description": "Use-case-centric toolkit for building modular APIs with Express. Define UseCase classes (input → validate → execute → output), connect them to HTTP routes, and expose Swagger/OpenAPI documentation automatically.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",