@webpieces/http-api 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 +25 -0
- package/src/decorators.d.ts +95 -0
- package/src/decorators.js +169 -0
- package/src/decorators.js.map +1 -0
- package/src/index.d.ts +20 -0
- package/src/index.js +34 -0
- package/src/index.js.map +1 -0
- package/src/validators.d.ts +41 -0
- package/src/validators.js +3 -0
- package/src/validators.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @webpieces/http-api
|
|
2
|
+
|
|
3
|
+
> HTTP API decorators for defining REST APIs
|
|
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-api
|
|
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,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webpieces/http-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "HTTP API decorators for defining REST APIs",
|
|
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-api"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"webpieces",
|
|
17
|
+
"http",
|
|
18
|
+
"api",
|
|
19
|
+
"decorators"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {}
|
|
25
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Metadata keys for storing API routing information.
|
|
4
|
+
* These keys are used by both server-side (routing) and client-side (client generation).
|
|
5
|
+
*/
|
|
6
|
+
export declare const METADATA_KEYS: {
|
|
7
|
+
API_INTERFACE: string;
|
|
8
|
+
ROUTES: string;
|
|
9
|
+
HTTP_METHOD: string;
|
|
10
|
+
PATH: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Route metadata stored on methods.
|
|
14
|
+
* Used by both server-side routing and client-side HTTP client generation.
|
|
15
|
+
*/
|
|
16
|
+
export interface RouteMetadata {
|
|
17
|
+
httpMethod: string;
|
|
18
|
+
path: string;
|
|
19
|
+
methodName: string;
|
|
20
|
+
parameterTypes?: any[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Mark a class as an API interface.
|
|
24
|
+
* Similar to Java's JAX-RS interface pattern.
|
|
25
|
+
*
|
|
26
|
+
* This decorator is used by:
|
|
27
|
+
* - Server: RESTApiRoutes reads it to validate API interfaces
|
|
28
|
+
* - Client: Client generator reads it to identify API interfaces
|
|
29
|
+
*
|
|
30
|
+
* Usage:
|
|
31
|
+
* ```typescript
|
|
32
|
+
* @ApiInterface()
|
|
33
|
+
* abstract class SaveApiPrototype {
|
|
34
|
+
* @Post()
|
|
35
|
+
* @Path('/search/item')
|
|
36
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
37
|
+
* throw new Error('Must be implemented');
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function ApiInterface(): ClassDecorator;
|
|
43
|
+
/**
|
|
44
|
+
* @Get decorator for GET requests.
|
|
45
|
+
* Usage: @Get()
|
|
46
|
+
*/
|
|
47
|
+
export declare function Get(): MethodDecorator;
|
|
48
|
+
/**
|
|
49
|
+
* @Post decorator for POST requests.
|
|
50
|
+
* Usage: @Post()
|
|
51
|
+
*/
|
|
52
|
+
export declare function Post(): MethodDecorator;
|
|
53
|
+
/**
|
|
54
|
+
* @Put decorator for PUT requests.
|
|
55
|
+
* Usage: @Put()
|
|
56
|
+
*/
|
|
57
|
+
export declare function Put(): MethodDecorator;
|
|
58
|
+
/**
|
|
59
|
+
* @Delete decorator for DELETE requests.
|
|
60
|
+
* Usage: @Delete()
|
|
61
|
+
*/
|
|
62
|
+
export declare function Delete(): MethodDecorator;
|
|
63
|
+
/**
|
|
64
|
+
* @Patch decorator for PATCH requests.
|
|
65
|
+
* Usage: @Patch()
|
|
66
|
+
*/
|
|
67
|
+
export declare function Patch(): MethodDecorator;
|
|
68
|
+
/**
|
|
69
|
+
* @Path decorator to specify the route path.
|
|
70
|
+
* Similar to JAX-RS @Path annotation.
|
|
71
|
+
*
|
|
72
|
+
* This decorator is used by:
|
|
73
|
+
* - Server: To register routes at the specified path
|
|
74
|
+
* - Client: To make HTTP requests to the specified path
|
|
75
|
+
*
|
|
76
|
+
* Usage:
|
|
77
|
+
* ```typescript
|
|
78
|
+
* @Post()
|
|
79
|
+
* @Path('/search/item')
|
|
80
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
81
|
+
* throw new Error('Must be implemented');
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function Path(path: string): MethodDecorator;
|
|
86
|
+
/**
|
|
87
|
+
* Helper function to get all routes from an API interface class.
|
|
88
|
+
* Used by both server-side routing and client-side client generation.
|
|
89
|
+
*/
|
|
90
|
+
export declare function getRoutes(apiClass: any): RouteMetadata[];
|
|
91
|
+
/**
|
|
92
|
+
* Helper function to check if a class is an API interface.
|
|
93
|
+
* Used by both server-side routing and client-side client generation.
|
|
94
|
+
*/
|
|
95
|
+
export declare function isApiInterface(apiClass: any): boolean;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.METADATA_KEYS = void 0;
|
|
4
|
+
exports.ApiInterface = ApiInterface;
|
|
5
|
+
exports.Get = Get;
|
|
6
|
+
exports.Post = Post;
|
|
7
|
+
exports.Put = Put;
|
|
8
|
+
exports.Delete = Delete;
|
|
9
|
+
exports.Patch = Patch;
|
|
10
|
+
exports.Path = Path;
|
|
11
|
+
exports.getRoutes = getRoutes;
|
|
12
|
+
exports.isApiInterface = isApiInterface;
|
|
13
|
+
require("reflect-metadata");
|
|
14
|
+
/**
|
|
15
|
+
* Metadata keys for storing API routing information.
|
|
16
|
+
* These keys are used by both server-side (routing) and client-side (client generation).
|
|
17
|
+
*/
|
|
18
|
+
exports.METADATA_KEYS = {
|
|
19
|
+
API_INTERFACE: 'webpieces:api-interface',
|
|
20
|
+
ROUTES: 'webpieces:routes',
|
|
21
|
+
HTTP_METHOD: 'webpieces:http-method',
|
|
22
|
+
PATH: 'webpieces:path',
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Mark a class as an API interface.
|
|
26
|
+
* Similar to Java's JAX-RS interface pattern.
|
|
27
|
+
*
|
|
28
|
+
* This decorator is used by:
|
|
29
|
+
* - Server: RESTApiRoutes reads it to validate API interfaces
|
|
30
|
+
* - Client: Client generator reads it to identify API interfaces
|
|
31
|
+
*
|
|
32
|
+
* Usage:
|
|
33
|
+
* ```typescript
|
|
34
|
+
* @ApiInterface()
|
|
35
|
+
* abstract class SaveApiPrototype {
|
|
36
|
+
* @Post()
|
|
37
|
+
* @Path('/search/item')
|
|
38
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
39
|
+
* throw new Error('Must be implemented');
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function ApiInterface() {
|
|
45
|
+
return (target) => {
|
|
46
|
+
Reflect.defineMetadata(exports.METADATA_KEYS.API_INTERFACE, true, target);
|
|
47
|
+
// Initialize routes array if not exists
|
|
48
|
+
if (!Reflect.hasMetadata(exports.METADATA_KEYS.ROUTES, target)) {
|
|
49
|
+
Reflect.defineMetadata(exports.METADATA_KEYS.ROUTES, [], target);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Internal helper to mark a method with an HTTP method.
|
|
55
|
+
* Used by @Get, @Post, @Put, @Delete, @Patch decorators.
|
|
56
|
+
*/
|
|
57
|
+
function httpMethod(method) {
|
|
58
|
+
return (target, propertyKey, descriptor) => {
|
|
59
|
+
// For static methods, target is the constructor itself
|
|
60
|
+
// For instance methods, target is the prototype
|
|
61
|
+
const metadataTarget = typeof target === 'function' ? target : target.constructor;
|
|
62
|
+
const existingMetadata = Reflect.getMetadata(exports.METADATA_KEYS.ROUTES, metadataTarget) || [];
|
|
63
|
+
// Find or create route metadata for this method
|
|
64
|
+
let routeMetadata = existingMetadata.find((r) => r.methodName === propertyKey);
|
|
65
|
+
if (!routeMetadata) {
|
|
66
|
+
routeMetadata = {
|
|
67
|
+
httpMethod: '',
|
|
68
|
+
path: '',
|
|
69
|
+
methodName: propertyKey,
|
|
70
|
+
};
|
|
71
|
+
existingMetadata.push(routeMetadata);
|
|
72
|
+
}
|
|
73
|
+
routeMetadata.httpMethod = method;
|
|
74
|
+
// Get parameter types
|
|
75
|
+
const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey);
|
|
76
|
+
if (paramTypes) {
|
|
77
|
+
routeMetadata.parameterTypes = paramTypes;
|
|
78
|
+
}
|
|
79
|
+
Reflect.defineMetadata(exports.METADATA_KEYS.ROUTES, existingMetadata, metadataTarget);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @Get decorator for GET requests.
|
|
84
|
+
* Usage: @Get()
|
|
85
|
+
*/
|
|
86
|
+
function Get() {
|
|
87
|
+
return httpMethod('GET');
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @Post decorator for POST requests.
|
|
91
|
+
* Usage: @Post()
|
|
92
|
+
*/
|
|
93
|
+
function Post() {
|
|
94
|
+
return httpMethod('POST');
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @Put decorator for PUT requests.
|
|
98
|
+
* Usage: @Put()
|
|
99
|
+
*/
|
|
100
|
+
function Put() {
|
|
101
|
+
return httpMethod('PUT');
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @Delete decorator for DELETE requests.
|
|
105
|
+
* Usage: @Delete()
|
|
106
|
+
*/
|
|
107
|
+
function Delete() {
|
|
108
|
+
return httpMethod('DELETE');
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @Patch decorator for PATCH requests.
|
|
112
|
+
* Usage: @Patch()
|
|
113
|
+
*/
|
|
114
|
+
function Patch() {
|
|
115
|
+
return httpMethod('PATCH');
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @Path decorator to specify the route path.
|
|
119
|
+
* Similar to JAX-RS @Path annotation.
|
|
120
|
+
*
|
|
121
|
+
* This decorator is used by:
|
|
122
|
+
* - Server: To register routes at the specified path
|
|
123
|
+
* - Client: To make HTTP requests to the specified path
|
|
124
|
+
*
|
|
125
|
+
* Usage:
|
|
126
|
+
* ```typescript
|
|
127
|
+
* @Post()
|
|
128
|
+
* @Path('/search/item')
|
|
129
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
130
|
+
* throw new Error('Must be implemented');
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function Path(path) {
|
|
135
|
+
return (target, propertyKey, descriptor) => {
|
|
136
|
+
// For static methods, target is the constructor itself
|
|
137
|
+
// For instance methods, target is the prototype
|
|
138
|
+
const metadataTarget = typeof target === 'function' ? target : target.constructor;
|
|
139
|
+
const existingMetadata = Reflect.getMetadata(exports.METADATA_KEYS.ROUTES, metadataTarget) || [];
|
|
140
|
+
// Find or create route metadata for this method
|
|
141
|
+
let routeMetadata = existingMetadata.find((r) => r.methodName === propertyKey);
|
|
142
|
+
if (!routeMetadata) {
|
|
143
|
+
routeMetadata = {
|
|
144
|
+
httpMethod: '',
|
|
145
|
+
path: '',
|
|
146
|
+
methodName: propertyKey,
|
|
147
|
+
};
|
|
148
|
+
existingMetadata.push(routeMetadata);
|
|
149
|
+
}
|
|
150
|
+
routeMetadata.path = path;
|
|
151
|
+
Reflect.defineMetadata(exports.METADATA_KEYS.ROUTES, existingMetadata, metadataTarget);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Helper function to get all routes from an API interface class.
|
|
156
|
+
* Used by both server-side routing and client-side client generation.
|
|
157
|
+
*/
|
|
158
|
+
function getRoutes(apiClass) {
|
|
159
|
+
const routes = Reflect.getMetadata(exports.METADATA_KEYS.ROUTES, apiClass);
|
|
160
|
+
return routes || [];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Helper function to check if a class is an API interface.
|
|
164
|
+
* Used by both server-side routing and client-side client generation.
|
|
165
|
+
*/
|
|
166
|
+
function isApiInterface(apiClass) {
|
|
167
|
+
return Reflect.getMetadata(exports.METADATA_KEYS.API_INTERFACE, apiClass) === true;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/decorators.ts"],"names":[],"mappings":";;;AA4CA,oCASC;AA4CD,kBAEC;AAMD,oBAEC;AAMD,kBAEC;AAMD,wBAEC;AAMD,sBAEC;AAmBD,oBA0BC;AAMD,8BAGC;AAMD,wCAEC;AAjMD,4BAA0B;AAE1B;;;GAGG;AACU,QAAA,aAAa,GAAG;IAC3B,aAAa,EAAE,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CAAC,UAAU,KAAK,WAAW,CACnD,CAAC;QAEF,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,aAAa,GAAG;gBACd,UAAU,EAAE,EAAE;gBACd,IAAI,EAAE,EAAE;gBACR,UAAU,EAAE,WAAqB;aAClC,CAAC;YACF,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"}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @webpieces/http-api
|
|
3
|
+
*
|
|
4
|
+
* Core HTTP API definition package.
|
|
5
|
+
* Contains decorators and utilities for defining HTTP APIs.
|
|
6
|
+
*
|
|
7
|
+
* This package is used by:
|
|
8
|
+
* - @webpieces/http-routing (server-side): Routes HTTP requests to controllers
|
|
9
|
+
* - @webpieces/http-client (client-side): Generates HTTP clients from API definitions
|
|
10
|
+
*
|
|
11
|
+
* Architecture:
|
|
12
|
+
* ```
|
|
13
|
+
* http-api (defines the contract)
|
|
14
|
+
* ↑
|
|
15
|
+
* ├── http-routing (server: contract → handlers)
|
|
16
|
+
* └── http-client (client: contract → HTTP requests)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export { ApiInterface, Get, Post, Put, Delete, Patch, Path, getRoutes, isApiInterface, RouteMetadata, METADATA_KEYS, } from './decorators';
|
|
20
|
+
export { ValidateImplementation } from './validators';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @webpieces/http-api
|
|
4
|
+
*
|
|
5
|
+
* Core HTTP API definition package.
|
|
6
|
+
* Contains decorators and utilities for defining HTTP APIs.
|
|
7
|
+
*
|
|
8
|
+
* This package is used by:
|
|
9
|
+
* - @webpieces/http-routing (server-side): Routes HTTP requests to controllers
|
|
10
|
+
* - @webpieces/http-client (client-side): Generates HTTP clients from API definitions
|
|
11
|
+
*
|
|
12
|
+
* Architecture:
|
|
13
|
+
* ```
|
|
14
|
+
* http-api (defines the contract)
|
|
15
|
+
* ↑
|
|
16
|
+
* ├── http-routing (server: contract → handlers)
|
|
17
|
+
* └── http-client (client: contract → HTTP requests)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
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;
|
|
22
|
+
// API definition decorators
|
|
23
|
+
var decorators_1 = require("./decorators");
|
|
24
|
+
Object.defineProperty(exports, "ApiInterface", { enumerable: true, get: function () { return decorators_1.ApiInterface; } });
|
|
25
|
+
Object.defineProperty(exports, "Get", { enumerable: true, get: function () { return decorators_1.Get; } });
|
|
26
|
+
Object.defineProperty(exports, "Post", { enumerable: true, get: function () { return decorators_1.Post; } });
|
|
27
|
+
Object.defineProperty(exports, "Put", { enumerable: true, get: function () { return decorators_1.Put; } });
|
|
28
|
+
Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return decorators_1.Delete; } });
|
|
29
|
+
Object.defineProperty(exports, "Patch", { enumerable: true, get: function () { return decorators_1.Patch; } });
|
|
30
|
+
Object.defineProperty(exports, "Path", { enumerable: true, get: function () { return decorators_1.Path; } });
|
|
31
|
+
Object.defineProperty(exports, "getRoutes", { enumerable: true, get: function () { return decorators_1.getRoutes; } });
|
|
32
|
+
Object.defineProperty(exports, "isApiInterface", { enumerable: true, get: function () { return decorators_1.isApiInterface; } });
|
|
33
|
+
Object.defineProperty(exports, "METADATA_KEYS", { enumerable: true, get: function () { return decorators_1.METADATA_KEYS; } });
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +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;AAEd,2GAAA,aAAa,OAAA"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level validator to ensure a class implements all methods from an interface.
|
|
3
|
+
*
|
|
4
|
+
* This validator provides compile-time verification that an implementation
|
|
5
|
+
* (e.g., controller, client, mock) fully implements an API interface.
|
|
6
|
+
*
|
|
7
|
+
* Usage in server-side controllers:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* export class SaveController extends SaveApiPrototype implements SaveApi {
|
|
10
|
+
* // Compile-time check: ensures all SaveApi methods are implemented
|
|
11
|
+
* private readonly __validator!: ValidateImplementation<SaveController, SaveApi>;
|
|
12
|
+
*
|
|
13
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
14
|
+
* // implementation
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Usage in client-side implementations:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* export class MockSaveClient implements SaveApi {
|
|
22
|
+
* private readonly __validator!: ValidateImplementation<MockSaveClient, SaveApi>;
|
|
23
|
+
*
|
|
24
|
+
* save(request: SaveRequest): Promise<SaveResponse> {
|
|
25
|
+
* // mock implementation
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Benefits:
|
|
31
|
+
* - Compile error if any interface method is missing
|
|
32
|
+
* - Compile error if method signatures don't match
|
|
33
|
+
* - Works with controllers, clients, mocks, stubs, etc.
|
|
34
|
+
* - Type-safe contract enforcement
|
|
35
|
+
*
|
|
36
|
+
* Note: The `!` assertion is safe because this field is never accessed at runtime.
|
|
37
|
+
* It only exists for compile-time type checking.
|
|
38
|
+
*/
|
|
39
|
+
export type ValidateImplementation<TImpl, TInterface> = {
|
|
40
|
+
[K in keyof TInterface]: K extends keyof TImpl ? TImpl[K] extends TInterface[K] ? TInterface[K] : never : never;
|
|
41
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../../../../packages/http/http-api/src/validators.ts"],"names":[],"mappings":""}
|