@webpieces/http-routing 0.2.12 → 0.2.13
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/package.json +3 -3
- package/src/RESTApiRoutes.d.ts +0 -13
- package/src/RESTApiRoutes.js +3 -37
- package/src/RESTApiRoutes.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/http-routing",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "Decorator-based routing with auto-wiring for WebPieces",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@webpieces/core-meta": "0.2.
|
|
25
|
-
"@webpieces/http-api": "0.2.
|
|
24
|
+
"@webpieces/core-meta": "0.2.13",
|
|
25
|
+
"@webpieces/http-api": "0.2.13"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/RESTApiRoutes.d.ts
CHANGED
|
@@ -56,19 +56,6 @@ export declare class RESTApiRoutes<TApi = any, TController extends TApi = any> i
|
|
|
56
56
|
* Register a single route with the route builder.
|
|
57
57
|
*/
|
|
58
58
|
private registerRoute;
|
|
59
|
-
/**
|
|
60
|
-
* Create a typed route handler for a specific route.
|
|
61
|
-
*
|
|
62
|
-
* The handler:
|
|
63
|
-
* 1. Resolves the controller from the DI container
|
|
64
|
-
* 2. Invokes the controller method with extracted parameters
|
|
65
|
-
* 3. Returns the controller method result
|
|
66
|
-
*
|
|
67
|
-
* Type parameter TResult represents the return type of the controller method.
|
|
68
|
-
* At runtime, we can't enforce this statically, but TypeScript will infer it
|
|
69
|
-
* from the method signature on the API interface.
|
|
70
|
-
*/
|
|
71
|
-
private createRouteHandler;
|
|
72
59
|
/**
|
|
73
60
|
* Get the filepath of the controller source file.
|
|
74
61
|
* Uses a heuristic based on the controller class name.
|
package/src/RESTApiRoutes.js
CHANGED
|
@@ -84,43 +84,9 @@ class RESTApiRoutes {
|
|
|
84
84
|
}
|
|
85
85
|
// Extract controller filepath for filter matching
|
|
86
86
|
const controllerFilepath = this.getControllerFilepath();
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
routeBuilder.addRoute(new core_meta_1.RouteDefinition(route.httpMethod, route.path, routeHandler, controllerFilepath));
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Create a typed route handler for a specific route.
|
|
94
|
-
*
|
|
95
|
-
* The handler:
|
|
96
|
-
* 1. Resolves the controller from the DI container
|
|
97
|
-
* 2. Invokes the controller method with extracted parameters
|
|
98
|
-
* 3. Returns the controller method result
|
|
99
|
-
*
|
|
100
|
-
* Type parameter TResult represents the return type of the controller method.
|
|
101
|
-
* At runtime, we can't enforce this statically, but TypeScript will infer it
|
|
102
|
-
* from the method signature on the API interface.
|
|
103
|
-
*/
|
|
104
|
-
createRouteHandler(route) {
|
|
105
|
-
const controllerClass = this.controllerClass;
|
|
106
|
-
return new class extends core_meta_1.RouteHandler {
|
|
107
|
-
async execute(context) {
|
|
108
|
-
const { container, params } = context;
|
|
109
|
-
// Resolve controller instance from DI container
|
|
110
|
-
const controller = container.get(controllerClass);
|
|
111
|
-
// Get the controller method
|
|
112
|
-
const method = controller[route.methodName];
|
|
113
|
-
if (typeof method !== 'function') {
|
|
114
|
-
const controllerName = controllerClass.name || 'Unknown';
|
|
115
|
-
throw new Error(`Method ${route.methodName} not found on controller ${controllerName}`);
|
|
116
|
-
}
|
|
117
|
-
// Invoke the method with parameters and return the result
|
|
118
|
-
// TypeScript trusts that the method returns Promise<TResult> based on
|
|
119
|
-
// the interface definition (e.g., SaveApi.save returns Promise<SaveResponse>)
|
|
120
|
-
const result = await method.apply(controller, params);
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
};
|
|
87
|
+
// Pass controller class and method name to RouteBuilder
|
|
88
|
+
// RouteBuilder will resolve the controller from DI and create the handler
|
|
89
|
+
routeBuilder.addRoute(new core_meta_1.RouteDefinition(route, this.controllerClass, controllerFilepath));
|
|
124
90
|
}
|
|
125
91
|
/**
|
|
126
92
|
* Get the filepath of the controller source file.
|
package/src/RESTApiRoutes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RESTApiRoutes.js","sourceRoot":"","sources":["../../../../../packages/http/http-routing/src/RESTApiRoutes.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"RESTApiRoutes.js","sourceRoot":"","sources":["../../../../../packages/http/http-routing/src/RESTApiRoutes.ts"],"names":[],"mappings":";;;AAAA,oDAA6E;AAC7E,kDAA+E;AAC/E,6CAAqD;AAOrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAa,aAAa;IAIxB;;;;;OAKG;IACH,YAAY,YAA6B,EAAE,eAAuC;QAChF,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QAEvC,wDAAwD;QACxD,IAAI,CAAC,IAAA,yBAAc,EAAC,YAAY,CAAC,EAAE,CAAC;YAClC,MAAM,SAAS,GAAI,YAAoB,CAAC,IAAI,IAAI,SAAS,CAAC;YAC1D,MAAM,IAAI,KAAK,CACb,SAAS,SAAS,yCAAyC,CAC5D,CAAC;QACJ,CAAC;QAED,yEAAyE;QACzE,IAAI,CAAC,+BAA+B,EAAE,CAAC;IACzC,CAAC;IAED;;OAEG;IACK,+BAA+B;QACrC,MAAM,MAAM,GAAG,IAAA,oBAAS,EAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAE5C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC;YAE3D,IAAI,OAAO,mBAAmB,CAAC,KAA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,eAAe,CACrB,CAAC;QACF,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,iCAAiC;QACjC,MAAM,SAAS,GAAI,IAAI,CAAC,eAAuB,CAAC,IAAI,CAAC;QACrD,OAAO,SAAS,CAAC,CAAC,CAAC,MAAM,SAAS,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IACtD,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,kBAAkB;QAChB,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;CACF;AAtHD,sCAsHC","sourcesContent":["import { Routes, RouteBuilder, RouteDefinition } from '@webpieces/core-meta';\nimport { getRoutes, isApiInterface, RouteMetadata } from '@webpieces/http-api';\nimport { ROUTING_METADATA_KEYS } from './decorators';\n\n/**\n * Type representing a class constructor (abstract or concrete).\n */\nexport type ClassType<T = any> = Function & { prototype: T };\n\n/**\n * RESTApiRoutes - Automatically wire API interfaces to controllers.\n * Similar to Java WebPieces RESTApiRoutes.\n *\n * This class uses reflection (reflect-metadata) to read decorators from\n * an API interface class and automatically register routes that dispatch\n * to the corresponding controller methods.\n *\n * Usage:\n * ```typescript\n * // In your ServerMeta:\n * getRoutes(): Routes[] {\n * return [\n * new RESTApiRoutes(SaveApiPrototype, SaveController),\n * // ... more routes\n * ];\n * }\n * ```\n *\n * The API interface and controller must follow this pattern:\n * - API interface class has @ApiInterface() decorator\n * - Methods have @Post()/@Get()/etc and @Path() decorators\n * - Controller class implements the same interface\n * - Controller class has @Controller() decorator\n *\n * Type Parameters:\n * - TApi: The API prototype class type (abstract class with decorators)\n * - TController: The controller class type (must extend TApi)\n */\nexport class RESTApiRoutes<TApi = any, TController extends TApi = any> implements Routes {\n private apiMetaClass: ClassType<TApi>;\n private controllerClass: ClassType<TController>;\n\n /**\n * Create a new RESTApiRoutes.\n *\n * @param apiMetaClass - The API interface class with decorators (e.g., SaveApiPrototype)\n * @param controllerClass - The controller class that implements the API (e.g., SaveController)\n */\n constructor(apiMetaClass: ClassType<TApi>, controllerClass: ClassType<TController>) {\n this.apiMetaClass = apiMetaClass;\n this.controllerClass = controllerClass;\n\n // Validate that apiMetaClass is marked as @ApiInterface\n if (!isApiInterface(apiMetaClass)) {\n const className = (apiMetaClass as any).name || 'Unknown';\n throw new Error(\n `Class ${className} must be decorated with @ApiInterface()`\n );\n }\n\n // Validate that controllerClass implements the methods from apiMetaClass\n this.validateControllerImplementsApi();\n }\n\n /**\n * Validate that the controller implements all methods from the API interface.\n */\n private validateControllerImplementsApi(): void {\n const routes = getRoutes(this.apiMetaClass);\n\n for (const route of routes) {\n const controllerPrototype = this.controllerClass.prototype;\n\n if (typeof controllerPrototype[route.methodName] !== 'function') {\n const controllerName = (this.controllerClass as any).name || 'Unknown';\n const apiName = (this.apiMetaClass as any).name || 'Unknown';\n throw new Error(\n `Controller ${controllerName} must implement method ${route.methodName} from API ${apiName}`\n );\n }\n }\n }\n\n /**\n * Configure routes by reading metadata from the API interface.\n */\n configure(routeBuilder: RouteBuilder): void {\n const routes = getRoutes(this.apiMetaClass);\n\n for (const route of routes) {\n this.registerRoute(routeBuilder, route);\n }\n }\n\n /**\n * Register a single route with the route builder.\n */\n private registerRoute(routeBuilder: RouteBuilder, route: RouteMetadata): void {\n if (!route.httpMethod || !route.path) {\n const apiName = (this.apiMetaClass as any).name || 'Unknown';\n throw new Error(\n `Method ${route.methodName} in ${apiName} must have both @HttpMethod and @Path decorators`\n );\n }\n\n // Extract controller filepath for filter matching\n const controllerFilepath = this.getControllerFilepath();\n\n // Pass controller class and method name to RouteBuilder\n // RouteBuilder will resolve the controller from DI and create the handler\n routeBuilder.addRoute(\n new RouteDefinition(\n route,\n this.controllerClass,\n controllerFilepath\n )\n );\n }\n\n /**\n * Get the filepath of the controller source file.\n * Uses a heuristic based on the controller class name.\n *\n * Since TypeScript doesn't provide source file paths at runtime,\n * we use the class name to create a pattern that filters can match against.\n *\n * @returns Filepath pattern or undefined\n */\n private getControllerFilepath(): string | undefined {\n // Check for explicit @SourceFile decorator metadata\n const filepath = Reflect.getMetadata(\n ROUTING_METADATA_KEYS.SOURCE_FILEPATH,\n this.controllerClass\n );\n if (filepath) {\n return filepath;\n }\n\n // Fallback to class name pattern\n const className = (this.controllerClass as any).name;\n return className ? `**/${className}.ts` : undefined;\n }\n\n /**\n * Get the API interface class.\n */\n getApiClass(): any {\n return this.apiMetaClass;\n }\n\n /**\n * Get the controller class.\n */\n getControllerClass(): any {\n return this.controllerClass;\n }\n}\n"]}
|