@webpieces/http-api 0.2.8 → 0.2.10
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 +1 -1
- package/src/decorators.d.ts +2 -1
- package/src/decorators.js +16 -11
- package/src/decorators.js.map +1 -1
- package/src/index.js +2 -1
- package/src/index.js.map +1 -1
package/package.json
CHANGED
package/src/decorators.d.ts
CHANGED
|
@@ -13,11 +13,12 @@ export declare const METADATA_KEYS: {
|
|
|
13
13
|
* Route metadata stored on methods.
|
|
14
14
|
* Used by both server-side routing and client-side HTTP client generation.
|
|
15
15
|
*/
|
|
16
|
-
export
|
|
16
|
+
export declare class RouteMetadata {
|
|
17
17
|
httpMethod: string;
|
|
18
18
|
path: string;
|
|
19
19
|
methodName: string;
|
|
20
20
|
parameterTypes?: any[];
|
|
21
|
+
constructor(httpMethod: string, path: string, methodName: string, parameterTypes?: any[]);
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* Mark a class as an API interface.
|
package/src/decorators.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.METADATA_KEYS = void 0;
|
|
3
|
+
exports.RouteMetadata = exports.METADATA_KEYS = void 0;
|
|
4
4
|
exports.ApiInterface = ApiInterface;
|
|
5
5
|
exports.Get = Get;
|
|
6
6
|
exports.Post = Post;
|
|
@@ -21,6 +21,19 @@ exports.METADATA_KEYS = {
|
|
|
21
21
|
HTTP_METHOD: 'webpieces:http-method',
|
|
22
22
|
PATH: 'webpieces:path',
|
|
23
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* Route metadata stored on methods.
|
|
26
|
+
* Used by both server-side routing and client-side HTTP client generation.
|
|
27
|
+
*/
|
|
28
|
+
class RouteMetadata {
|
|
29
|
+
constructor(httpMethod, path, methodName, parameterTypes) {
|
|
30
|
+
this.httpMethod = httpMethod;
|
|
31
|
+
this.path = path;
|
|
32
|
+
this.methodName = methodName;
|
|
33
|
+
this.parameterTypes = parameterTypes;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.RouteMetadata = RouteMetadata;
|
|
24
37
|
/**
|
|
25
38
|
* Mark a class as an API interface.
|
|
26
39
|
* Similar to Java's JAX-RS interface pattern.
|
|
@@ -63,11 +76,7 @@ function httpMethod(method) {
|
|
|
63
76
|
// Find or create route metadata for this method
|
|
64
77
|
let routeMetadata = existingMetadata.find((r) => r.methodName === propertyKey);
|
|
65
78
|
if (!routeMetadata) {
|
|
66
|
-
routeMetadata =
|
|
67
|
-
httpMethod: '',
|
|
68
|
-
path: '',
|
|
69
|
-
methodName: propertyKey,
|
|
70
|
-
};
|
|
79
|
+
routeMetadata = new RouteMetadata('', '', propertyKey);
|
|
71
80
|
existingMetadata.push(routeMetadata);
|
|
72
81
|
}
|
|
73
82
|
routeMetadata.httpMethod = method;
|
|
@@ -140,11 +149,7 @@ function Path(path) {
|
|
|
140
149
|
// Find or create route metadata for this method
|
|
141
150
|
let routeMetadata = existingMetadata.find((r) => r.methodName === propertyKey);
|
|
142
151
|
if (!routeMetadata) {
|
|
143
|
-
routeMetadata =
|
|
144
|
-
httpMethod: '',
|
|
145
|
-
path: '',
|
|
146
|
-
methodName: propertyKey,
|
|
147
|
-
};
|
|
152
|
+
routeMetadata = new RouteMetadata('', '', propertyKey);
|
|
148
153
|
existingMetadata.push(routeMetadata);
|
|
149
154
|
}
|
|
150
155
|
routeMetadata.path = path;
|
package/src/decorators.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/decorators.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/decorators.ts"],"names":[],"mappings":";;;AAwDA,oCASC;AAwCD,kBAEC;AAMD,oBAEC;AAMD,kBAEC;AAMD,w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CAAC,MAAW,EAAE,WAA4B,EAAE,UAA8B,EAAE,EAAE;QACnF,uDAAuD;QACvD,gDAAgD;QAChD,MAAM,cAAc,GAAG,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC;QAElF,MAAM,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC,qBAAa,CAAC,MAAM,EAAE,cAAc,CAAC,IAAI,EAAE,CAAC;QAEzF,gDAAgD;QAChD,IAAI,aAAa,GAAG,gBAAgB,CAAC,IAAI,CACvC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,WAAW,CACnD,CAAC;QAEF,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,aAAa,GAAG,IAAI,aAAa,CAAC,EAAE,EAAE,EAAE,EAAE,WAAqB,CAAC,CAAC;YACjE,gBAAgB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACvC,CAAC;QAED,aAAa,CAAC,IAAI,GAAG,IAAI,CAAC;QAE1B,OAAO,CAAC,cAAc,CAAC,qBAAa,CAAC,MAAM,EAAE,gBAAgB,EAAE,cAAc,CAAC,CAAC;IACjF,CAAC,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAgB,SAAS,CAAC,QAAa;IACrC,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,qBAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACnE,OAAO,MAAM,IAAI,EAAE,CAAC;AACtB,CAAC;AAED;;;GAGG;AACH,SAAgB,cAAc,CAAC,QAAa;IAC1C,OAAO,OAAO,CAAC,WAAW,CAAC,qBAAa,CAAC,aAAa,EAAE,QAAQ,CAAC,KAAK,IAAI,CAAC;AAC7E,CAAC","sourcesContent":["import 'reflect-metadata';\n\n/**\n * Metadata keys for storing API routing information.\n * These keys are used by both server-side (routing) and client-side (client generation).\n */\nexport const METADATA_KEYS = {\n API_INTERFACE: 'webpieces:api-interface',\n ROUTES: 'webpieces:routes',\n HTTP_METHOD: 'webpieces:http-method',\n PATH: 'webpieces:path',\n};\n\n/**\n * Route metadata stored on methods.\n * Used by both server-side routing and client-side HTTP client generation.\n */\nexport class RouteMetadata {\n httpMethod: string;\n path: string;\n methodName: string;\n parameterTypes?: any[];\n\n constructor(\n httpMethod: string,\n path: string,\n methodName: string,\n parameterTypes?: any[]\n ) {\n this.httpMethod = httpMethod;\n this.path = path;\n this.methodName = methodName;\n this.parameterTypes = parameterTypes;\n }\n}\n\n/**\n * Mark a class as an API interface.\n * Similar to Java's JAX-RS interface pattern.\n *\n * This decorator is used by:\n * - Server: RESTApiRoutes reads it to validate API interfaces\n * - Client: Client generator reads it to identify API interfaces\n *\n * Usage:\n * ```typescript\n * @ApiInterface()\n * abstract class SaveApiPrototype {\n * @Post()\n * @Path('/search/item')\n * save(request: SaveRequest): Promise<SaveResponse> {\n * throw new Error('Must be implemented');\n * }\n * }\n * ```\n */\nexport function ApiInterface(): ClassDecorator {\n return (target: any) => {\n Reflect.defineMetadata(METADATA_KEYS.API_INTERFACE, true, target);\n\n // Initialize routes array if not exists\n if (!Reflect.hasMetadata(METADATA_KEYS.ROUTES, target)) {\n Reflect.defineMetadata(METADATA_KEYS.ROUTES, [], target);\n }\n };\n}\n\n/**\n * Internal helper to mark a method with an HTTP method.\n * Used by @Get, @Post, @Put, @Delete, @Patch decorators.\n */\nfunction httpMethod(method: string): MethodDecorator {\n return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {\n // For static methods, target is the constructor itself\n // For instance methods, target is the prototype\n const metadataTarget = typeof target === 'function' ? target : target.constructor;\n\n const existingMetadata = Reflect.getMetadata(METADATA_KEYS.ROUTES, metadataTarget) || [];\n\n // Find or create route metadata for this method\n let routeMetadata = existingMetadata.find(\n (r: RouteMetadata) => r.methodName === propertyKey\n );\n\n if (!routeMetadata) {\n routeMetadata = new RouteMetadata('', '', propertyKey as string);\n existingMetadata.push(routeMetadata);\n }\n\n routeMetadata.httpMethod = method;\n\n // Get parameter types\n const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey);\n if (paramTypes) {\n routeMetadata.parameterTypes = paramTypes;\n }\n\n Reflect.defineMetadata(METADATA_KEYS.ROUTES, existingMetadata, metadataTarget);\n };\n}\n\n/**\n * @Get decorator for GET requests.\n * Usage: @Get()\n */\nexport function Get(): MethodDecorator {\n return httpMethod('GET');\n}\n\n/**\n * @Post decorator for POST requests.\n * Usage: @Post()\n */\nexport function Post(): MethodDecorator {\n return httpMethod('POST');\n}\n\n/**\n * @Put decorator for PUT requests.\n * Usage: @Put()\n */\nexport function Put(): MethodDecorator {\n return httpMethod('PUT');\n}\n\n/**\n * @Delete decorator for DELETE requests.\n * Usage: @Delete()\n */\nexport function Delete(): MethodDecorator {\n return httpMethod('DELETE');\n}\n\n/**\n * @Patch decorator for PATCH requests.\n * Usage: @Patch()\n */\nexport function Patch(): MethodDecorator {\n return httpMethod('PATCH');\n}\n\n/**\n * @Path decorator to specify the route path.\n * Similar to JAX-RS @Path annotation.\n *\n * This decorator is used by:\n * - Server: To register routes at the specified path\n * - Client: To make HTTP requests to the specified path\n *\n * Usage:\n * ```typescript\n * @Post()\n * @Path('/search/item')\n * save(request: SaveRequest): Promise<SaveResponse> {\n * throw new Error('Must be implemented');\n * }\n * ```\n */\nexport function Path(path: string): MethodDecorator {\n return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {\n // For static methods, target is the constructor itself\n // For instance methods, target is the prototype\n const metadataTarget = typeof target === 'function' ? target : target.constructor;\n\n const existingMetadata = Reflect.getMetadata(METADATA_KEYS.ROUTES, metadataTarget) || [];\n\n // Find or create route metadata for this method\n let routeMetadata = existingMetadata.find(\n (r: RouteMetadata) => r.methodName === propertyKey\n );\n\n if (!routeMetadata) {\n routeMetadata = new RouteMetadata('', '', propertyKey as string);\n existingMetadata.push(routeMetadata);\n }\n\n routeMetadata.path = path;\n\n Reflect.defineMetadata(METADATA_KEYS.ROUTES, existingMetadata, metadataTarget);\n };\n}\n\n/**\n * Helper function to get all routes from an API interface class.\n * Used by both server-side routing and client-side client generation.\n */\nexport function getRoutes(apiClass: any): RouteMetadata[] {\n const routes = Reflect.getMetadata(METADATA_KEYS.ROUTES, apiClass);\n return routes || [];\n}\n\n/**\n * Helper function to check if a class is an API interface.\n * Used by both server-side routing and client-side client generation.\n */\nexport function isApiInterface(apiClass: any): boolean {\n return Reflect.getMetadata(METADATA_KEYS.API_INTERFACE, apiClass) === true;\n}\n"]}
|
package/src/index.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.METADATA_KEYS = exports.isApiInterface = exports.getRoutes = exports.Path = exports.Patch = exports.Delete = exports.Put = exports.Post = exports.Get = exports.ApiInterface = void 0;
|
|
21
|
+
exports.METADATA_KEYS = exports.RouteMetadata = exports.isApiInterface = exports.getRoutes = exports.Path = exports.Patch = exports.Delete = exports.Put = exports.Post = exports.Get = exports.ApiInterface = void 0;
|
|
22
22
|
// API definition decorators
|
|
23
23
|
var decorators_1 = require("./decorators");
|
|
24
24
|
Object.defineProperty(exports, "ApiInterface", { enumerable: true, get: function () { return decorators_1.ApiInterface; } });
|
|
@@ -30,5 +30,6 @@ Object.defineProperty(exports, "Patch", { enumerable: true, get: function () { r
|
|
|
30
30
|
Object.defineProperty(exports, "Path", { enumerable: true, get: function () { return decorators_1.Path; } });
|
|
31
31
|
Object.defineProperty(exports, "getRoutes", { enumerable: true, get: function () { return decorators_1.getRoutes; } });
|
|
32
32
|
Object.defineProperty(exports, "isApiInterface", { enumerable: true, get: function () { return decorators_1.isApiInterface; } });
|
|
33
|
+
Object.defineProperty(exports, "RouteMetadata", { enumerable: true, get: function () { return decorators_1.RouteMetadata; } });
|
|
33
34
|
Object.defineProperty(exports, "METADATA_KEYS", { enumerable: true, get: function () { return decorators_1.METADATA_KEYS; } });
|
|
34
35
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AAEH,4BAA4B;AAC5B,2CAYsB;AAXpB,0GAAA,YAAY,OAAA;AACZ,iGAAA,GAAG,OAAA;AACH,kGAAA,IAAI,OAAA;AACJ,iGAAA,GAAG,OAAA;AACH,oGAAA,MAAM,OAAA;AACN,mGAAA,KAAK,OAAA;AACL,kGAAA,IAAI,OAAA;AACJ,uGAAA,SAAS,OAAA;AACT,4GAAA,cAAc,OAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AAEH,4BAA4B;AAC5B,2CAYsB;AAXpB,0GAAA,YAAY,OAAA;AACZ,iGAAA,GAAG,OAAA;AACH,kGAAA,IAAI,OAAA;AACJ,iGAAA,GAAG,OAAA;AACH,oGAAA,MAAM,OAAA;AACN,mGAAA,KAAK,OAAA;AACL,kGAAA,IAAI,OAAA;AACJ,uGAAA,SAAS,OAAA;AACT,4GAAA,cAAc,OAAA;AACd,2GAAA,aAAa,OAAA;AACb,2GAAA,aAAa,OAAA","sourcesContent":["/**\n * @webpieces/http-api\n *\n * Core HTTP API definition package.\n * Contains decorators and utilities for defining HTTP APIs.\n *\n * This package is used by:\n * - @webpieces/http-routing (server-side): Routes HTTP requests to controllers\n * - @webpieces/http-client (client-side): Generates HTTP clients from API definitions\n *\n * Architecture:\n * ```\n * http-api (defines the contract)\n * ↑\n * ├── http-routing (server: contract → handlers)\n * └── http-client (client: contract → HTTP requests)\n * ```\n */\n\n// API definition decorators\nexport {\n ApiInterface,\n Get,\n Post,\n Put,\n Delete,\n Patch,\n Path,\n getRoutes,\n isApiInterface,\n RouteMetadata,\n METADATA_KEYS,\n} from './decorators';\n\n// Type validators\nexport { ValidateImplementation } from './validators';\n"]}
|