@webpieces/http-routing 0.1.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 +19 -0
- package/package.json +27 -0
- package/src/RESTApiRoutes.d.ts +80 -0
- package/src/RESTApiRoutes.js +140 -0
- package/src/RESTApiRoutes.js.map +1 -0
- package/src/decorators.d.ts +58 -0
- package/src/decorators.js +81 -0
- package/src/decorators.js.map +1 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +25 -0
- package/src/index.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @webpieces/http-routing
|
|
2
|
+
|
|
3
|
+
> Decorator-based routing with auto-wiring for WebPieces
|
|
4
|
+
|
|
5
|
+
Part of the [WebPieces TypeScript](https://github.com/deanhiller/webpieces-ts) framework.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @webpieces/http-routing
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
See the main [WebPieces README](https://github.com/deanhiller/webpieces-ts#readme) for complete documentation and examples.
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
Apache-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webpieces/http-routing",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Decorator-based routing with auto-wiring for WebPieces",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"author": "Dean Hiller",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/deanhiller/webpieces-ts.git",
|
|
13
|
+
"directory": "packages/http/http-routing"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"webpieces",
|
|
17
|
+
"routing",
|
|
18
|
+
"decorators"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@webpieces/core-meta": "0.1.0",
|
|
25
|
+
"@webpieces/http-api": "0.1.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Routes, RouteBuilder } from '@webpieces/core-meta';
|
|
2
|
+
/**
|
|
3
|
+
* Type representing a class constructor (abstract or concrete).
|
|
4
|
+
*/
|
|
5
|
+
export type ClassType<T = any> = Function & {
|
|
6
|
+
prototype: T;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* RESTApiRoutes - Automatically wire API interfaces to controllers.
|
|
10
|
+
* Similar to Java WebPieces RESTApiRoutes.
|
|
11
|
+
*
|
|
12
|
+
* This class uses reflection (reflect-metadata) to read decorators from
|
|
13
|
+
* an API interface class and automatically register routes that dispatch
|
|
14
|
+
* to the corresponding controller methods.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // In your ServerMeta:
|
|
19
|
+
* getRoutes(): Routes[] {
|
|
20
|
+
* return [
|
|
21
|
+
* new RESTApiRoutes(SaveApiPrototype, SaveController),
|
|
22
|
+
* // ... more routes
|
|
23
|
+
* ];
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* The API interface and controller must follow this pattern:
|
|
28
|
+
* - API interface class has @ApiInterface() decorator
|
|
29
|
+
* - Methods have @Post()/@Get()/etc and @Path() decorators
|
|
30
|
+
* - Controller class implements the same interface
|
|
31
|
+
* - Controller class has @Controller() decorator
|
|
32
|
+
*
|
|
33
|
+
* Type Parameters:
|
|
34
|
+
* - TApi: The API prototype class type (abstract class with decorators)
|
|
35
|
+
* - TController: The controller class type (must extend TApi)
|
|
36
|
+
*/
|
|
37
|
+
export declare class RESTApiRoutes<TApi = any, TController extends TApi = any> implements Routes {
|
|
38
|
+
private apiMetaClass;
|
|
39
|
+
private controllerClass;
|
|
40
|
+
/**
|
|
41
|
+
* Create a new RESTApiRoutes.
|
|
42
|
+
*
|
|
43
|
+
* @param apiMetaClass - The API interface class with decorators (e.g., SaveApiPrototype)
|
|
44
|
+
* @param controllerClass - The controller class that implements the API (e.g., SaveController)
|
|
45
|
+
*/
|
|
46
|
+
constructor(apiMetaClass: ClassType<TApi>, controllerClass: ClassType<TController>);
|
|
47
|
+
/**
|
|
48
|
+
* Validate that the controller implements all methods from the API interface.
|
|
49
|
+
*/
|
|
50
|
+
private validateControllerImplementsApi;
|
|
51
|
+
/**
|
|
52
|
+
* Configure routes by reading metadata from the API interface.
|
|
53
|
+
*/
|
|
54
|
+
configure(routeBuilder: RouteBuilder): void;
|
|
55
|
+
/**
|
|
56
|
+
* Register a single route with the route builder.
|
|
57
|
+
*/
|
|
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
|
+
/**
|
|
73
|
+
* Get the API interface class.
|
|
74
|
+
*/
|
|
75
|
+
getApiClass(): any;
|
|
76
|
+
/**
|
|
77
|
+
* Get the controller class.
|
|
78
|
+
*/
|
|
79
|
+
getControllerClass(): any;
|
|
80
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RESTApiRoutes = void 0;
|
|
4
|
+
const core_meta_1 = require("@webpieces/core-meta");
|
|
5
|
+
const http_api_1 = require("@webpieces/http-api");
|
|
6
|
+
/**
|
|
7
|
+
* RESTApiRoutes - Automatically wire API interfaces to controllers.
|
|
8
|
+
* Similar to Java WebPieces RESTApiRoutes.
|
|
9
|
+
*
|
|
10
|
+
* This class uses reflection (reflect-metadata) to read decorators from
|
|
11
|
+
* an API interface class and automatically register routes that dispatch
|
|
12
|
+
* to the corresponding controller methods.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // In your ServerMeta:
|
|
17
|
+
* getRoutes(): Routes[] {
|
|
18
|
+
* return [
|
|
19
|
+
* new RESTApiRoutes(SaveApiPrototype, SaveController),
|
|
20
|
+
* // ... more routes
|
|
21
|
+
* ];
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The API interface and controller must follow this pattern:
|
|
26
|
+
* - API interface class has @ApiInterface() decorator
|
|
27
|
+
* - Methods have @Post()/@Get()/etc and @Path() decorators
|
|
28
|
+
* - Controller class implements the same interface
|
|
29
|
+
* - Controller class has @Controller() decorator
|
|
30
|
+
*
|
|
31
|
+
* Type Parameters:
|
|
32
|
+
* - TApi: The API prototype class type (abstract class with decorators)
|
|
33
|
+
* - TController: The controller class type (must extend TApi)
|
|
34
|
+
*/
|
|
35
|
+
class RESTApiRoutes {
|
|
36
|
+
/**
|
|
37
|
+
* Create a new RESTApiRoutes.
|
|
38
|
+
*
|
|
39
|
+
* @param apiMetaClass - The API interface class with decorators (e.g., SaveApiPrototype)
|
|
40
|
+
* @param controllerClass - The controller class that implements the API (e.g., SaveController)
|
|
41
|
+
*/
|
|
42
|
+
constructor(apiMetaClass, controllerClass) {
|
|
43
|
+
this.apiMetaClass = apiMetaClass;
|
|
44
|
+
this.controllerClass = controllerClass;
|
|
45
|
+
// Validate that apiMetaClass is marked as @ApiInterface
|
|
46
|
+
if (!(0, http_api_1.isApiInterface)(apiMetaClass)) {
|
|
47
|
+
const className = apiMetaClass.name || 'Unknown';
|
|
48
|
+
throw new Error(`Class ${className} must be decorated with @ApiInterface()`);
|
|
49
|
+
}
|
|
50
|
+
// Validate that controllerClass implements the methods from apiMetaClass
|
|
51
|
+
this.validateControllerImplementsApi();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validate that the controller implements all methods from the API interface.
|
|
55
|
+
*/
|
|
56
|
+
validateControllerImplementsApi() {
|
|
57
|
+
const routes = (0, http_api_1.getRoutes)(this.apiMetaClass);
|
|
58
|
+
for (const route of routes) {
|
|
59
|
+
const controllerPrototype = this.controllerClass.prototype;
|
|
60
|
+
if (typeof controllerPrototype[route.methodName] !== 'function') {
|
|
61
|
+
const controllerName = this.controllerClass.name || 'Unknown';
|
|
62
|
+
const apiName = this.apiMetaClass.name || 'Unknown';
|
|
63
|
+
throw new Error(`Controller ${controllerName} must implement method ${route.methodName} from API ${apiName}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Configure routes by reading metadata from the API interface.
|
|
69
|
+
*/
|
|
70
|
+
configure(routeBuilder) {
|
|
71
|
+
const routes = (0, http_api_1.getRoutes)(this.apiMetaClass);
|
|
72
|
+
for (const route of routes) {
|
|
73
|
+
this.registerRoute(routeBuilder, route);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Register a single route with the route builder.
|
|
78
|
+
*/
|
|
79
|
+
registerRoute(routeBuilder, route) {
|
|
80
|
+
if (!route.httpMethod || !route.path) {
|
|
81
|
+
const apiName = this.apiMetaClass.name || 'Unknown';
|
|
82
|
+
throw new Error(`Method ${route.methodName} in ${apiName} must have both @HttpMethod and @Path decorators`);
|
|
83
|
+
}
|
|
84
|
+
// Create typed route handler
|
|
85
|
+
// The handler's return type is inferred from the controller method's return type
|
|
86
|
+
const routeHandler = this.createRouteHandler(route);
|
|
87
|
+
routeBuilder.addRoute({
|
|
88
|
+
method: route.httpMethod,
|
|
89
|
+
path: route.path,
|
|
90
|
+
handler: routeHandler,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Create a typed route handler for a specific route.
|
|
95
|
+
*
|
|
96
|
+
* The handler:
|
|
97
|
+
* 1. Resolves the controller from the DI container
|
|
98
|
+
* 2. Invokes the controller method with extracted parameters
|
|
99
|
+
* 3. Returns the controller method result
|
|
100
|
+
*
|
|
101
|
+
* Type parameter TResult represents the return type of the controller method.
|
|
102
|
+
* At runtime, we can't enforce this statically, but TypeScript will infer it
|
|
103
|
+
* from the method signature on the API interface.
|
|
104
|
+
*/
|
|
105
|
+
createRouteHandler(route) {
|
|
106
|
+
const controllerClass = this.controllerClass;
|
|
107
|
+
return new class extends core_meta_1.RouteHandler {
|
|
108
|
+
async execute(context) {
|
|
109
|
+
const { container, params } = context;
|
|
110
|
+
// Resolve controller instance from DI container
|
|
111
|
+
const controller = container.get(controllerClass);
|
|
112
|
+
// Get the controller method
|
|
113
|
+
const method = controller[route.methodName];
|
|
114
|
+
if (typeof method !== 'function') {
|
|
115
|
+
const controllerName = controllerClass.name || 'Unknown';
|
|
116
|
+
throw new Error(`Method ${route.methodName} not found on controller ${controllerName}`);
|
|
117
|
+
}
|
|
118
|
+
// Invoke the method with parameters and return the result
|
|
119
|
+
// TypeScript trusts that the method returns Promise<TResult> based on
|
|
120
|
+
// the interface definition (e.g., SaveApi.save returns Promise<SaveResponse>)
|
|
121
|
+
const result = await method.apply(controller, params);
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get the API interface class.
|
|
128
|
+
*/
|
|
129
|
+
getApiClass() {
|
|
130
|
+
return this.apiMetaClass;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get the controller class.
|
|
134
|
+
*/
|
|
135
|
+
getControllerClass() {
|
|
136
|
+
return this.controllerClass;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.RESTApiRoutes = RESTApiRoutes;
|
|
140
|
+
//# sourceMappingURL=RESTApiRoutes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RESTApiRoutes.js","sourceRoot":"","sources":["../../../../../packages/http/http-routing/src/RESTApiRoutes.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Metadata keys for server-side routing.
|
|
4
|
+
* These are specific to the routing package (server-side only).
|
|
5
|
+
*/
|
|
6
|
+
export declare const ROUTING_METADATA_KEYS: {
|
|
7
|
+
CONTROLLER: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* @Controller decorator to mark a class as a controller.
|
|
11
|
+
* This is a server-side only decorator.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* @Controller()
|
|
16
|
+
* export class SaveController implements SaveApi {
|
|
17
|
+
* // ...
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function Controller(): ClassDecorator;
|
|
22
|
+
/**
|
|
23
|
+
* Helper function to check if a class is a controller.
|
|
24
|
+
* Server-side only.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isController(controllerClass: any): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Provides a singleton-scoped dependency.
|
|
29
|
+
* When called without arguments, the decorated class binds to itself.
|
|
30
|
+
*
|
|
31
|
+
* Server-side only - registers classes in the DI container.
|
|
32
|
+
*
|
|
33
|
+
* Usage:
|
|
34
|
+
* ```typescript
|
|
35
|
+
* @provideSingleton()
|
|
36
|
+
* @Controller()
|
|
37
|
+
* export class SaveController {
|
|
38
|
+
* // ...
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function provideSingleton(): (target: any) => any;
|
|
43
|
+
/**
|
|
44
|
+
* Provides a transient-scoped dependency (new instance every time).
|
|
45
|
+
* When called without arguments, the decorated class binds to itself.
|
|
46
|
+
*
|
|
47
|
+
* Server-side only - registers classes in the DI container.
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* ```typescript
|
|
51
|
+
* @provideTransient()
|
|
52
|
+
* @Controller()
|
|
53
|
+
* export class TransientController {
|
|
54
|
+
* // ...
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function provideTransient(): (target: any) => any;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ROUTING_METADATA_KEYS = void 0;
|
|
4
|
+
exports.Controller = Controller;
|
|
5
|
+
exports.isController = isController;
|
|
6
|
+
exports.provideSingleton = provideSingleton;
|
|
7
|
+
exports.provideTransient = provideTransient;
|
|
8
|
+
require("reflect-metadata");
|
|
9
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
10
|
+
/**
|
|
11
|
+
* Metadata keys for server-side routing.
|
|
12
|
+
* These are specific to the routing package (server-side only).
|
|
13
|
+
*/
|
|
14
|
+
exports.ROUTING_METADATA_KEYS = {
|
|
15
|
+
CONTROLLER: 'webpieces:controller',
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* @Controller decorator to mark a class as a controller.
|
|
19
|
+
* This is a server-side only decorator.
|
|
20
|
+
*
|
|
21
|
+
* Usage:
|
|
22
|
+
* ```typescript
|
|
23
|
+
* @Controller()
|
|
24
|
+
* export class SaveController implements SaveApi {
|
|
25
|
+
* // ...
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function Controller() {
|
|
30
|
+
return (target) => {
|
|
31
|
+
Reflect.defineMetadata(exports.ROUTING_METADATA_KEYS.CONTROLLER, true, target);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Helper function to check if a class is a controller.
|
|
36
|
+
* Server-side only.
|
|
37
|
+
*/
|
|
38
|
+
function isController(controllerClass) {
|
|
39
|
+
return Reflect.getMetadata(exports.ROUTING_METADATA_KEYS.CONTROLLER, controllerClass) === true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Provides a singleton-scoped dependency.
|
|
43
|
+
* When called without arguments, the decorated class binds to itself.
|
|
44
|
+
*
|
|
45
|
+
* Server-side only - registers classes in the DI container.
|
|
46
|
+
*
|
|
47
|
+
* Usage:
|
|
48
|
+
* ```typescript
|
|
49
|
+
* @provideSingleton()
|
|
50
|
+
* @Controller()
|
|
51
|
+
* export class SaveController {
|
|
52
|
+
* // ...
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function provideSingleton() {
|
|
57
|
+
return (target) => {
|
|
58
|
+
return (0, inversify_binding_decorators_1.fluentProvide)(target).inSingletonScope().done()(target);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Provides a transient-scoped dependency (new instance every time).
|
|
63
|
+
* When called without arguments, the decorated class binds to itself.
|
|
64
|
+
*
|
|
65
|
+
* Server-side only - registers classes in the DI container.
|
|
66
|
+
*
|
|
67
|
+
* Usage:
|
|
68
|
+
* ```typescript
|
|
69
|
+
* @provideTransient()
|
|
70
|
+
* @Controller()
|
|
71
|
+
* export class TransientController {
|
|
72
|
+
* // ...
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function provideTransient() {
|
|
77
|
+
return (target) => {
|
|
78
|
+
return (0, inversify_binding_decorators_1.provide)(target)(target);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../../packages/http/http-routing/src/decorators.ts"],"names":[],"mappings":";;;AAuBA,gCAIC;AAMD,oCAEC;AAiBD,4CAIC;AAiBD,4CAIC;AA7ED,4BAA0B;AAC1B,+EAAsE;AAEtE;;;GAGG;AACU,QAAA,qBAAqB,GAAG;IACnC,UAAU,EAAE,sBAAsB;CACnC,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,SAAgB,UAAU;IACxB,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,OAAO,CAAC,cAAc,CAAC,6BAAqB,CAAC,UAAU,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACzE,CAAC,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAgB,YAAY,CAAC,eAAoB;IAC/C,OAAO,OAAO,CAAC,WAAW,CAAC,6BAAqB,CAAC,UAAU,EAAE,eAAe,CAAC,KAAK,IAAI,CAAC;AACzF,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,gBAAgB;IAC9B,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,OAAO,IAAA,4CAAa,EAAC,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC;IACjE,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,gBAAgB;IAC9B,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,OAAO,IAAA,sCAAO,EAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC,CAAC;AACJ,CAAC"}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ApiInterface, Get, Post, Put, Delete, Patch, Path, getRoutes, isApiInterface, RouteMetadata, METADATA_KEYS, ValidateImplementation, } from '@webpieces/http-api';
|
|
2
|
+
export { Controller, isController, provideSingleton, provideTransient, ROUTING_METADATA_KEYS, } from './decorators';
|
|
3
|
+
export { RESTApiRoutes, ClassType } from './RESTApiRoutes';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RESTApiRoutes = exports.ROUTING_METADATA_KEYS = exports.provideTransient = exports.provideSingleton = exports.isController = exports.Controller = exports.METADATA_KEYS = exports.isApiInterface = exports.getRoutes = exports.Path = exports.Patch = exports.Delete = exports.Put = exports.Post = exports.Get = exports.ApiInterface = void 0;
|
|
4
|
+
// Re-export API decorators from http-api for convenience
|
|
5
|
+
var http_api_1 = require("@webpieces/http-api");
|
|
6
|
+
Object.defineProperty(exports, "ApiInterface", { enumerable: true, get: function () { return http_api_1.ApiInterface; } });
|
|
7
|
+
Object.defineProperty(exports, "Get", { enumerable: true, get: function () { return http_api_1.Get; } });
|
|
8
|
+
Object.defineProperty(exports, "Post", { enumerable: true, get: function () { return http_api_1.Post; } });
|
|
9
|
+
Object.defineProperty(exports, "Put", { enumerable: true, get: function () { return http_api_1.Put; } });
|
|
10
|
+
Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return http_api_1.Delete; } });
|
|
11
|
+
Object.defineProperty(exports, "Patch", { enumerable: true, get: function () { return http_api_1.Patch; } });
|
|
12
|
+
Object.defineProperty(exports, "Path", { enumerable: true, get: function () { return http_api_1.Path; } });
|
|
13
|
+
Object.defineProperty(exports, "getRoutes", { enumerable: true, get: function () { return http_api_1.getRoutes; } });
|
|
14
|
+
Object.defineProperty(exports, "isApiInterface", { enumerable: true, get: function () { return http_api_1.isApiInterface; } });
|
|
15
|
+
Object.defineProperty(exports, "METADATA_KEYS", { enumerable: true, get: function () { return http_api_1.METADATA_KEYS; } });
|
|
16
|
+
// Server-side routing decorators and utilities
|
|
17
|
+
var decorators_1 = require("./decorators");
|
|
18
|
+
Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return decorators_1.Controller; } });
|
|
19
|
+
Object.defineProperty(exports, "isController", { enumerable: true, get: function () { return decorators_1.isController; } });
|
|
20
|
+
Object.defineProperty(exports, "provideSingleton", { enumerable: true, get: function () { return decorators_1.provideSingleton; } });
|
|
21
|
+
Object.defineProperty(exports, "provideTransient", { enumerable: true, get: function () { return decorators_1.provideTransient; } });
|
|
22
|
+
Object.defineProperty(exports, "ROUTING_METADATA_KEYS", { enumerable: true, get: function () { return decorators_1.ROUTING_METADATA_KEYS; } });
|
|
23
|
+
var RESTApiRoutes_1 = require("./RESTApiRoutes");
|
|
24
|
+
Object.defineProperty(exports, "RESTApiRoutes", { enumerable: true, get: function () { return RESTApiRoutes_1.RESTApiRoutes; } });
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/http/http-routing/src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAyD;AACzD,gDAa6B;AAZ3B,wGAAA,YAAY,OAAA;AACZ,+FAAA,GAAG,OAAA;AACH,gGAAA,IAAI,OAAA;AACJ,+FAAA,GAAG,OAAA;AACH,kGAAA,MAAM,OAAA;AACN,iGAAA,KAAK,OAAA;AACL,gGAAA,IAAI,OAAA;AACJ,qGAAA,SAAS,OAAA;AACT,0GAAA,cAAc,OAAA;AAEd,yGAAA,aAAa,OAAA;AAIf,+CAA+C;AAC/C,2CAMsB;AALpB,wGAAA,UAAU,OAAA;AACV,0GAAA,YAAY,OAAA;AACZ,8GAAA,gBAAgB,OAAA;AAChB,8GAAA,gBAAgB,OAAA;AAChB,mHAAA,qBAAqB,OAAA;AAGvB,iDAA2D;AAAlD,8GAAA,aAAa,OAAA"}
|