@e22m4u/ts-rest-router 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. package/.c8rc +9 -0
  2. package/.commitlintrc +5 -0
  3. package/.editorconfig +13 -0
  4. package/.husky/commit-msg +1 -0
  5. package/.husky/pre-commit +6 -0
  6. package/.mocharc.json +5 -0
  7. package/.prettierrc +7 -0
  8. package/LICENSE +21 -0
  9. package/README-ru.md +41 -0
  10. package/README.md +41 -0
  11. package/build-cjs.js +16 -0
  12. package/dist/cjs/index.cjs +692 -0
  13. package/dist/esm/controller-registry.d.ts +65 -0
  14. package/dist/esm/controller-registry.js +281 -0
  15. package/dist/esm/controller-registry.spec.d.ts +1 -0
  16. package/dist/esm/controller-registry.spec.js +719 -0
  17. package/dist/esm/debuggable-service.d.ts +18 -0
  18. package/dist/esm/debuggable-service.js +23 -0
  19. package/dist/esm/debuggable-service.spec.d.ts +1 -0
  20. package/dist/esm/debuggable-service.spec.js +16 -0
  21. package/dist/esm/decorators/action/action-decorator.d.ts +53 -0
  22. package/dist/esm/decorators/action/action-decorator.js +66 -0
  23. package/dist/esm/decorators/action/action-decorator.spec.d.ts +1 -0
  24. package/dist/esm/decorators/action/action-decorator.spec.js +59 -0
  25. package/dist/esm/decorators/action/action-metadata.d.ts +23 -0
  26. package/dist/esm/decorators/action/action-metadata.js +5 -0
  27. package/dist/esm/decorators/action/action-reflector.d.ts +22 -0
  28. package/dist/esm/decorators/action/action-reflector.js +29 -0
  29. package/dist/esm/decorators/action/action-reflector.spec.d.ts +1 -0
  30. package/dist/esm/decorators/action/action-reflector.spec.js +84 -0
  31. package/dist/esm/decorators/action/index.d.ts +3 -0
  32. package/dist/esm/decorators/action/index.js +3 -0
  33. package/dist/esm/decorators/controller/controller-decorator.d.ts +13 -0
  34. package/dist/esm/decorators/controller/controller-decorator.js +20 -0
  35. package/dist/esm/decorators/controller/controller-decorator.spec.d.ts +1 -0
  36. package/dist/esm/decorators/controller/controller-decorator.spec.js +53 -0
  37. package/dist/esm/decorators/controller/controller-metadata.d.ts +17 -0
  38. package/dist/esm/decorators/controller/controller-metadata.js +5 -0
  39. package/dist/esm/decorators/controller/controller-reflector.d.ts +20 -0
  40. package/dist/esm/decorators/controller/controller-reflector.js +24 -0
  41. package/dist/esm/decorators/controller/controller-reflector.spec.d.ts +1 -0
  42. package/dist/esm/decorators/controller/controller-reflector.spec.js +45 -0
  43. package/dist/esm/decorators/controller/index.d.ts +3 -0
  44. package/dist/esm/decorators/controller/index.js +3 -0
  45. package/dist/esm/decorators/index.d.ts +4 -0
  46. package/dist/esm/decorators/index.js +4 -0
  47. package/dist/esm/decorators/request-context/index.d.ts +3 -0
  48. package/dist/esm/decorators/request-context/index.js +3 -0
  49. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +17 -0
  50. package/dist/esm/decorators/request-context/request-context-decorator.js +32 -0
  51. package/dist/esm/decorators/request-context/request-context-decorator.spec.d.ts +1 -0
  52. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +59 -0
  53. package/dist/esm/decorators/request-context/request-context-metadata.d.ts +17 -0
  54. package/dist/esm/decorators/request-context/request-context-metadata.js +5 -0
  55. package/dist/esm/decorators/request-context/request-context-reflector.d.ts +24 -0
  56. package/dist/esm/decorators/request-context/request-context-reflector.js +31 -0
  57. package/dist/esm/decorators/request-context/request-context-reflector.spec.d.ts +1 -0
  58. package/dist/esm/decorators/request-context/request-context-reflector.spec.js +59 -0
  59. package/dist/esm/decorators/request-data/index.d.ts +3 -0
  60. package/dist/esm/decorators/request-data/index.js +3 -0
  61. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +28 -0
  62. package/dist/esm/decorators/request-data/request-data-decorator.js +84 -0
  63. package/dist/esm/decorators/request-data/request-data-decorator.spec.d.ts +1 -0
  64. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +534 -0
  65. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +29 -0
  66. package/dist/esm/decorators/request-data/request-data-metadata.js +16 -0
  67. package/dist/esm/decorators/request-data/request-data-reflector.d.ts +24 -0
  68. package/dist/esm/decorators/request-data/request-data-reflector.js +31 -0
  69. package/dist/esm/decorators/request-data/request-data-reflector.spec.d.ts +1 -0
  70. package/dist/esm/decorators/request-data/request-data-reflector.spec.js +60 -0
  71. package/dist/esm/errors/index.d.ts +1 -0
  72. package/dist/esm/errors/index.js +1 -0
  73. package/dist/esm/errors/not-a-controller-error.d.ts +12 -0
  74. package/dist/esm/errors/not-a-controller-error.js +14 -0
  75. package/dist/esm/index.d.ts +5 -0
  76. package/dist/esm/index.js +5 -0
  77. package/dist/esm/rest-router.d.ts +19 -0
  78. package/dist/esm/rest-router.js +24 -0
  79. package/dist/esm/types.d.ts +57 -0
  80. package/dist/esm/types.js +2 -0
  81. package/dist/esm/utils/capitalize.d.ts +6 -0
  82. package/dist/esm/utils/capitalize.js +8 -0
  83. package/dist/esm/utils/capitalize.spec.d.ts +1 -0
  84. package/dist/esm/utils/capitalize.spec.js +8 -0
  85. package/dist/esm/utils/create-debugger.d.ts +11 -0
  86. package/dist/esm/utils/create-debugger.js +15 -0
  87. package/dist/esm/utils/create-debugger.spec.d.ts +1 -0
  88. package/dist/esm/utils/create-debugger.spec.js +8 -0
  89. package/dist/esm/utils/create-error.d.ts +10 -0
  90. package/dist/esm/utils/create-error.js +13 -0
  91. package/dist/esm/utils/create-error.spec.d.ts +1 -0
  92. package/dist/esm/utils/create-error.spec.js +8 -0
  93. package/dist/esm/utils/index.d.ts +4 -0
  94. package/dist/esm/utils/index.js +4 -0
  95. package/dist/esm/utils/to-camel-case.d.ts +6 -0
  96. package/dist/esm/utils/to-camel-case.js +11 -0
  97. package/dist/esm/utils/to-camel-case.spec.d.ts +1 -0
  98. package/dist/esm/utils/to-camel-case.spec.js +10 -0
  99. package/dist/tsconfig.tsbuildinfo +1 -0
  100. package/eslint.config.js +43 -0
  101. package/package.json +74 -0
  102. package/src/controller-registry.spec.ts +592 -0
  103. package/src/controller-registry.ts +355 -0
  104. package/src/debuggable-service.spec.ts +18 -0
  105. package/src/debuggable-service.ts +27 -0
  106. package/src/decorators/action/action-decorator.spec.ts +42 -0
  107. package/src/decorators/action/action-decorator.ts +100 -0
  108. package/src/decorators/action/action-metadata.ts +28 -0
  109. package/src/decorators/action/action-reflector.spec.ts +84 -0
  110. package/src/decorators/action/action-reflector.ts +38 -0
  111. package/src/decorators/action/index.ts +3 -0
  112. package/src/decorators/controller/controller-decorator.spec.ts +41 -0
  113. package/src/decorators/controller/controller-decorator.ts +29 -0
  114. package/src/decorators/controller/controller-metadata.ts +21 -0
  115. package/src/decorators/controller/controller-reflector.spec.ts +45 -0
  116. package/src/decorators/controller/controller-reflector.ts +28 -0
  117. package/src/decorators/controller/index.ts +3 -0
  118. package/src/decorators/index.ts +4 -0
  119. package/src/decorators/request-context/index.ts +3 -0
  120. package/src/decorators/request-context/request-context-decorator.spec.ts +41 -0
  121. package/src/decorators/request-context/request-context-decorator.ts +57 -0
  122. package/src/decorators/request-context/request-context-metadata.ts +21 -0
  123. package/src/decorators/request-context/request-context-reflector.spec.ts +77 -0
  124. package/src/decorators/request-context/request-context-reflector.ts +57 -0
  125. package/src/decorators/request-data/index.ts +3 -0
  126. package/src/decorators/request-data/request-data-decorator.spec.ts +477 -0
  127. package/src/decorators/request-data/request-data-decorator.ts +106 -0
  128. package/src/decorators/request-data/request-data-metadata.ts +34 -0
  129. package/src/decorators/request-data/request-data-reflector.spec.ts +78 -0
  130. package/src/decorators/request-data/request-data-reflector.ts +57 -0
  131. package/src/errors/index.ts +1 -0
  132. package/src/errors/not-a-controller-error.ts +15 -0
  133. package/src/index.ts +5 -0
  134. package/src/rest-router.ts +31 -0
  135. package/src/types.ts +59 -0
  136. package/src/utils/capitalize.spec.ts +9 -0
  137. package/src/utils/capitalize.ts +8 -0
  138. package/src/utils/create-debugger.spec.ts +9 -0
  139. package/src/utils/create-debugger.ts +21 -0
  140. package/src/utils/create-error.spec.ts +9 -0
  141. package/src/utils/create-error.ts +19 -0
  142. package/src/utils/index.ts +4 -0
  143. package/src/utils/to-camel-case.spec.ts +11 -0
  144. package/src/utils/to-camel-case.ts +11 -0
  145. package/tsconfig.json +17 -0
@@ -0,0 +1,692 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // dist/esm/index.js
32
+ var esm_exports = {};
33
+ __export(esm_exports, {
34
+ ACTIONS_METADATA_KEY: () => ACTIONS_METADATA_KEY,
35
+ ActionReflector: () => ActionReflector,
36
+ CONTROLLER_METADATA_KEY: () => CONTROLLER_METADATA_KEY,
37
+ ControllerReflector: () => ControllerReflector,
38
+ ControllerRegistry: () => ControllerRegistry,
39
+ NotAControllerError: () => NotAControllerError,
40
+ REQUEST_CONTEXT_METADATA_KEY: () => REQUEST_CONTEXT_METADATA_KEY,
41
+ REQUEST_DATA_METADATA_KEY: () => REQUEST_DATA_METADATA_KEY,
42
+ RequestContextReflector: () => RequestContextReflector,
43
+ RequestDataReflector: () => RequestDataReflector,
44
+ RequestDataSource: () => RequestDataSource,
45
+ RestRouter: () => RestRouter,
46
+ action: () => action,
47
+ body: () => body,
48
+ bodyParam: () => bodyParam,
49
+ capitalize: () => capitalize,
50
+ controller: () => controller,
51
+ cookie: () => cookie,
52
+ cookies: () => cookies,
53
+ createDebugger: () => createDebugger,
54
+ createError: () => createError,
55
+ del: () => del,
56
+ get: () => get,
57
+ header: () => header,
58
+ headers: () => headers,
59
+ param: () => param,
60
+ params: () => params,
61
+ patch: () => patch,
62
+ post: () => post,
63
+ put: () => put,
64
+ queries: () => queries,
65
+ query: () => query,
66
+ request: () => request,
67
+ requestContext: () => requestContext,
68
+ requestData: () => requestData,
69
+ response: () => response,
70
+ toCamelCase: () => toCamelCase
71
+ });
72
+ module.exports = __toCommonJS(esm_exports);
73
+
74
+ // dist/esm/utils/capitalize.js
75
+ function capitalize(input) {
76
+ return input.charAt(0).toUpperCase() + input.slice(1);
77
+ }
78
+ __name(capitalize, "capitalize");
79
+
80
+ // dist/esm/utils/create-error.js
81
+ var import_js_format = require("@e22m4u/js-format");
82
+ function createError(errorCtor, message, ...args) {
83
+ const interpolatedMessage = (0, import_js_format.format)(message, ...args);
84
+ return new errorCtor(interpolatedMessage);
85
+ }
86
+ __name(createError, "createError");
87
+
88
+ // dist/esm/utils/to-camel-case.js
89
+ function toCamelCase(input) {
90
+ return input.replace(/(^\w|[A-Z]|\b\w)/g, (c) => c.toUpperCase()).replace(/\W+/g, "").replace(/(^\w)/g, (c) => c.toLowerCase());
91
+ }
92
+ __name(toCamelCase, "toCamelCase");
93
+
94
+ // dist/esm/utils/create-debugger.js
95
+ var import_debug = __toESM(require("debug"), 1);
96
+ var import_js_format2 = require("@e22m4u/js-format");
97
+ function createDebugger(name) {
98
+ const debug = (0, import_debug.default)(`tsRestRouter:${name}`);
99
+ return function(message, ...args) {
100
+ const interpolatedMessage = (0, import_js_format2.format)(message, ...args);
101
+ return debug(interpolatedMessage);
102
+ };
103
+ }
104
+ __name(createDebugger, "createDebugger");
105
+
106
+ // dist/esm/rest-router.js
107
+ var import_js_trie_router3 = require("@e22m4u/js-trie-router");
108
+
109
+ // dist/esm/debuggable-service.js
110
+ var import_js_service = require("@e22m4u/js-service");
111
+ var _DebuggableService = class _DebuggableService extends import_js_service.Service {
112
+ /**
113
+ * Debug.
114
+ */
115
+ debug;
116
+ /**
117
+ * Constructor.
118
+ *
119
+ * @param container
120
+ */
121
+ constructor(container) {
122
+ super(container);
123
+ const serviceName = toCamelCase(this.constructor.name);
124
+ this.debug = createDebugger(serviceName);
125
+ this.debug("%v is created.", this.constructor);
126
+ }
127
+ };
128
+ __name(_DebuggableService, "DebuggableService");
129
+ var DebuggableService = _DebuggableService;
130
+
131
+ // dist/esm/controller-registry.js
132
+ var import_js_format4 = require("@e22m4u/js-format");
133
+ var import_js_trie_router2 = require("@e22m4u/js-trie-router");
134
+ var import_ts_data_schema2 = require("@e22m4u/ts-data-schema");
135
+ var import_ts_data_schema3 = require("@e22m4u/ts-data-schema");
136
+
137
+ // dist/esm/decorators/action/action-metadata.js
138
+ var import_ts_reflector = require("@e22m4u/ts-reflector");
139
+ var ACTIONS_METADATA_KEY = new import_ts_reflector.MetadataKey("actionsMetadataKey");
140
+
141
+ // dist/esm/decorators/action/action-reflector.js
142
+ var import_ts_reflector2 = require("@e22m4u/ts-reflector");
143
+ var _ActionReflector = class _ActionReflector {
144
+ /**
145
+ * Set metadata.
146
+ *
147
+ * @param metadata
148
+ * @param target
149
+ * @param propertyKey
150
+ */
151
+ static setMetadata(metadata, target, propertyKey) {
152
+ const oldMap = import_ts_reflector2.Reflector.getOwnMetadata(ACTIONS_METADATA_KEY, target);
153
+ const newMap = new Map(oldMap);
154
+ newMap.set(propertyKey, metadata);
155
+ import_ts_reflector2.Reflector.defineMetadata(ACTIONS_METADATA_KEY, newMap, target);
156
+ }
157
+ /**
158
+ * Get metadata.
159
+ *
160
+ * @param target
161
+ */
162
+ static getMetadata(target) {
163
+ const metadata = import_ts_reflector2.Reflector.getOwnMetadata(ACTIONS_METADATA_KEY, target);
164
+ return metadata ?? /* @__PURE__ */ new Map();
165
+ }
166
+ };
167
+ __name(_ActionReflector, "ActionReflector");
168
+ var ActionReflector = _ActionReflector;
169
+
170
+ // dist/esm/decorators/action/action-decorator.js
171
+ var import_js_trie_router = require("@e22m4u/js-trie-router");
172
+ var import_ts_reflector3 = require("@e22m4u/ts-reflector");
173
+ var import_ts_reflector4 = require("@e22m4u/ts-reflector");
174
+ function action(options) {
175
+ return function(target, propertyKey, descriptor) {
176
+ const decoratorType = (0, import_ts_reflector4.getDecoratorTargetType)(target, propertyKey, descriptor);
177
+ if (decoratorType !== import_ts_reflector3.DecoratorTargetType.INSTANCE_METHOD)
178
+ throw new Error("@action decorator is only supported on an instance method.");
179
+ const metadata = {
180
+ ...options,
181
+ propertyKey
182
+ };
183
+ ActionReflector.setMetadata(metadata, target.constructor, propertyKey);
184
+ };
185
+ }
186
+ __name(action, "action");
187
+ var get = /* @__PURE__ */ __name((path, options) => {
188
+ return action({ ...options, path, method: import_js_trie_router.HttpMethod.GET });
189
+ }, "get");
190
+ var post = /* @__PURE__ */ __name((path, options) => {
191
+ return action({ ...options, path, method: import_js_trie_router.HttpMethod.POST });
192
+ }, "post");
193
+ var put = /* @__PURE__ */ __name((path, options) => {
194
+ return action({ ...options, path, method: import_js_trie_router.HttpMethod.PUT });
195
+ }, "put");
196
+ var patch = /* @__PURE__ */ __name((path, options) => {
197
+ return action({ ...options, path, method: import_js_trie_router.HttpMethod.PATCH });
198
+ }, "patch");
199
+ var del = /* @__PURE__ */ __name((path, options) => {
200
+ return action({ ...options, path, method: import_js_trie_router.HttpMethod.DELETE });
201
+ }, "del");
202
+
203
+ // dist/esm/decorators/controller/controller-metadata.js
204
+ var import_ts_reflector5 = require("@e22m4u/ts-reflector");
205
+ var CONTROLLER_METADATA_KEY = new import_ts_reflector5.MetadataKey("controllerMetadataKey");
206
+
207
+ // dist/esm/decorators/controller/controller-decorator.js
208
+ var import_ts_reflector7 = require("@e22m4u/ts-reflector");
209
+ var import_ts_reflector8 = require("@e22m4u/ts-reflector");
210
+
211
+ // dist/esm/decorators/controller/controller-reflector.js
212
+ var import_ts_reflector6 = require("@e22m4u/ts-reflector");
213
+ var _ControllerReflector = class _ControllerReflector {
214
+ /**
215
+ * Set metadata.
216
+ *
217
+ * @param metadata
218
+ * @param target
219
+ */
220
+ static setMetadata(metadata, target) {
221
+ return import_ts_reflector6.Reflector.defineMetadata(CONTROLLER_METADATA_KEY, metadata, target);
222
+ }
223
+ /**
224
+ * Get metadata.
225
+ *
226
+ * @param target
227
+ */
228
+ static getMetadata(target) {
229
+ return import_ts_reflector6.Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, target);
230
+ }
231
+ };
232
+ __name(_ControllerReflector, "ControllerReflector");
233
+ var ControllerReflector = _ControllerReflector;
234
+
235
+ // dist/esm/decorators/controller/controller-decorator.js
236
+ function controller(options) {
237
+ return function(target) {
238
+ const decoratorType = (0, import_ts_reflector8.getDecoratorTargetType)(target);
239
+ if (decoratorType !== import_ts_reflector7.DecoratorTargetType.CONSTRUCTOR)
240
+ throw new Error("@controller decorator is only supported on a class.");
241
+ const metadata = {
242
+ ...options,
243
+ className: target.name
244
+ };
245
+ ControllerReflector.setMetadata(metadata, target);
246
+ };
247
+ }
248
+ __name(controller, "controller");
249
+
250
+ // dist/esm/decorators/request-data/request-data-metadata.js
251
+ var import_ts_reflector9 = require("@e22m4u/ts-reflector");
252
+ var RequestDataSource;
253
+ (function(RequestDataSource2) {
254
+ RequestDataSource2["PARAMS"] = "params";
255
+ RequestDataSource2["QUERY"] = "query";
256
+ RequestDataSource2["HEADERS"] = "headers";
257
+ RequestDataSource2["COOKIE"] = "cookie";
258
+ RequestDataSource2["BODY"] = "body";
259
+ })(RequestDataSource || (RequestDataSource = {}));
260
+ var REQUEST_DATA_METADATA_KEY = new import_ts_reflector9.MetadataKey("requestDataMetadataKey");
261
+
262
+ // dist/esm/decorators/request-data/request-data-decorator.js
263
+ var import_ts_data_schema = require("@e22m4u/ts-data-schema");
264
+ var import_ts_reflector11 = require("@e22m4u/ts-reflector");
265
+ var import_ts_reflector12 = require("@e22m4u/ts-reflector");
266
+
267
+ // dist/esm/decorators/request-data/request-data-reflector.js
268
+ var import_ts_reflector10 = require("@e22m4u/ts-reflector");
269
+ var _RequestDataReflector = class _RequestDataReflector {
270
+ /**
271
+ * Set metadata.
272
+ *
273
+ * @param metadata
274
+ * @param target
275
+ * @param index
276
+ * @param propertyKey
277
+ */
278
+ static setMetadata(metadata, target, index, propertyKey) {
279
+ const oldMap = import_ts_reflector10.Reflector.getOwnMetadata(REQUEST_DATA_METADATA_KEY, target, propertyKey);
280
+ const newMap = new Map(oldMap);
281
+ newMap.set(index, metadata);
282
+ import_ts_reflector10.Reflector.defineMetadata(REQUEST_DATA_METADATA_KEY, newMap, target, propertyKey);
283
+ }
284
+ /**
285
+ * Get metadata.
286
+ *
287
+ * @param target
288
+ * @param propertyKey
289
+ */
290
+ static getMetadata(target, propertyKey) {
291
+ const metadata = import_ts_reflector10.Reflector.getOwnMetadata(REQUEST_DATA_METADATA_KEY, target, propertyKey);
292
+ return metadata ?? /* @__PURE__ */ new Map();
293
+ }
294
+ };
295
+ __name(_RequestDataReflector, "RequestDataReflector");
296
+ var RequestDataReflector = _RequestDataReflector;
297
+
298
+ // dist/esm/decorators/request-data/request-data-decorator.js
299
+ function requestData(metadata) {
300
+ return function(target, propertyKey, indexOrDescriptor) {
301
+ const decoratorType = (0, import_ts_reflector12.getDecoratorTargetType)(target, propertyKey, indexOrDescriptor);
302
+ if (decoratorType !== import_ts_reflector11.DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
303
+ throw new Error("@requestData decorator is only supported on an instance method parameter.");
304
+ RequestDataReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
305
+ };
306
+ }
307
+ __name(requestData, "requestData");
308
+ function createDataDecorator(source) {
309
+ return function() {
310
+ const schema = { type: import_ts_data_schema.DataType.OBJECT };
311
+ return requestData({ schema, source });
312
+ };
313
+ }
314
+ __name(createDataDecorator, "createDataDecorator");
315
+ function createPropertyDecorator(source) {
316
+ return function(propertyKey, schemaOrType) {
317
+ const properties = {};
318
+ const rootSchema = { type: import_ts_data_schema.DataType.OBJECT };
319
+ if (typeof schemaOrType === "object") {
320
+ properties[propertyKey] = schemaOrType;
321
+ rootSchema.properties = properties;
322
+ } else if (typeof schemaOrType === "string") {
323
+ properties[propertyKey] = { type: schemaOrType };
324
+ rootSchema.properties = properties;
325
+ }
326
+ return requestData({
327
+ source,
328
+ schema: rootSchema,
329
+ property: propertyKey
330
+ });
331
+ };
332
+ }
333
+ __name(createPropertyDecorator, "createPropertyDecorator");
334
+ var params = createDataDecorator(RequestDataSource.PARAMS);
335
+ var param = createPropertyDecorator(RequestDataSource.PARAMS);
336
+ var queries = createDataDecorator(RequestDataSource.QUERY);
337
+ var query = createPropertyDecorator(RequestDataSource.QUERY);
338
+ var headers = createDataDecorator(RequestDataSource.HEADERS);
339
+ var header = createPropertyDecorator(RequestDataSource.HEADERS);
340
+ var cookies = createDataDecorator(RequestDataSource.COOKIE);
341
+ var cookie = createPropertyDecorator(RequestDataSource.COOKIE);
342
+ var bodyParam = createPropertyDecorator(RequestDataSource.BODY);
343
+ function body(schemaOrType) {
344
+ let schema;
345
+ if (typeof schemaOrType === "object") {
346
+ schema = schemaOrType;
347
+ } else if (typeof schemaOrType === "string") {
348
+ schema = { type: schemaOrType };
349
+ } else {
350
+ schema = { type: import_ts_data_schema.DataType.ANY };
351
+ }
352
+ return requestData({ schema, source: RequestDataSource.BODY });
353
+ }
354
+ __name(body, "body");
355
+
356
+ // dist/esm/decorators/request-context/request-context-metadata.js
357
+ var import_ts_reflector13 = require("@e22m4u/ts-reflector");
358
+ var REQUEST_CONTEXT_METADATA_KEY = new import_ts_reflector13.MetadataKey("requestContextMetadataKey");
359
+
360
+ // dist/esm/decorators/request-context/request-context-decorator.js
361
+ var import_ts_reflector15 = require("@e22m4u/ts-reflector");
362
+ var import_ts_reflector16 = require("@e22m4u/ts-reflector");
363
+
364
+ // dist/esm/decorators/request-context/request-context-reflector.js
365
+ var import_ts_reflector14 = require("@e22m4u/ts-reflector");
366
+ var _RequestContextReflector = class _RequestContextReflector {
367
+ /**
368
+ * Set metadata.
369
+ *
370
+ * @param metadata
371
+ * @param target
372
+ * @param index
373
+ * @param propertyKey
374
+ */
375
+ static setMetadata(metadata, target, index, propertyKey) {
376
+ const oldMap = import_ts_reflector14.Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
377
+ const newMap = new Map(oldMap);
378
+ newMap.set(index, metadata);
379
+ import_ts_reflector14.Reflector.defineMetadata(REQUEST_CONTEXT_METADATA_KEY, newMap, target, propertyKey);
380
+ }
381
+ /**
382
+ * Get metadata.
383
+ *
384
+ * @param target
385
+ * @param propertyKey
386
+ */
387
+ static getMetadata(target, propertyKey) {
388
+ const metadata = import_ts_reflector14.Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
389
+ return metadata ?? /* @__PURE__ */ new Map();
390
+ }
391
+ };
392
+ __name(_RequestContextReflector, "RequestContextReflector");
393
+ var RequestContextReflector = _RequestContextReflector;
394
+
395
+ // dist/esm/decorators/request-context/request-context-decorator.js
396
+ function requestContext(propertyOrMetadata) {
397
+ return function(target, propertyKey, indexOrDescriptor) {
398
+ const decoratorType = (0, import_ts_reflector16.getDecoratorTargetType)(target, propertyKey, indexOrDescriptor);
399
+ if (decoratorType !== import_ts_reflector15.DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
400
+ throw new Error("@requestContext decorator is only supported on an instance method parameter.");
401
+ const metadata = typeof propertyOrMetadata !== "object" ? { property: propertyOrMetadata } : propertyOrMetadata;
402
+ RequestContextReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
403
+ };
404
+ }
405
+ __name(requestContext, "requestContext");
406
+ function request() {
407
+ return requestContext("req");
408
+ }
409
+ __name(request, "request");
410
+ function response() {
411
+ return requestContext("res");
412
+ }
413
+ __name(response, "response");
414
+
415
+ // dist/esm/errors/not-a-controller-error.js
416
+ var import_js_format3 = require("@e22m4u/js-format");
417
+ var _NotAControllerError = class _NotAControllerError extends import_js_format3.Errorf {
418
+ /**
419
+ * Constructor.
420
+ *
421
+ * @param value
422
+ */
423
+ constructor(value) {
424
+ super("%v is not a controller, do use @controller decorator on it.", value);
425
+ }
426
+ };
427
+ __name(_NotAControllerError, "NotAControllerError");
428
+ var NotAControllerError = _NotAControllerError;
429
+
430
+ // dist/esm/controller-registry.js
431
+ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
432
+ /**
433
+ * Controllers.
434
+ */
435
+ controllers = /* @__PURE__ */ new Set();
436
+ /**
437
+ * Add controller.
438
+ *
439
+ * @param ctor
440
+ * @param options
441
+ */
442
+ addController(ctor, options) {
443
+ if (this.hasController(ctor))
444
+ throw new import_js_format4.Errorf("The controller %v is already registered.");
445
+ const controllerMd = ControllerReflector.getMetadata(ctor);
446
+ if (!controllerMd)
447
+ throw new NotAControllerError(ctor);
448
+ this.debug("Adding controller %s.", ctor.name);
449
+ const pathPrefix = this.getPathPrefixByControllerMetadata(controllerMd, options);
450
+ this.debug("Path prefix is %v.", pathPrefix);
451
+ const preHandlers = this.getPreHandlersByControllerMetadata(controllerMd, options);
452
+ this.debug("%v total pre-handlers found.", preHandlers.length);
453
+ const postHandlers = this.getPostHandlersByControllerMetadata(controllerMd, options);
454
+ this.debug("%v total post-handlers found.", postHandlers.length);
455
+ const actionsMd = ActionReflector.getMetadata(ctor);
456
+ this.debug("%v actions found.", actionsMd.size);
457
+ const router = this.getService(import_js_trie_router2.TrieRouter);
458
+ actionsMd.forEach((actionMd, actionName) => {
459
+ this.debug("Adding route for %s.%s.", ctor.name, actionName);
460
+ this.debug("Route path is %v.", actionMd.path);
461
+ const prefixedRoutePath = `${pathPrefix}/${actionMd.path}`.replace(/\/\//g, "/");
462
+ this.debug("Prefixed route path is %v.", prefixedRoutePath);
463
+ const actionPreHandlers = Array.isArray(actionMd.before) ? actionMd.before : actionMd.before ? [actionMd.before] : [];
464
+ this.debug("%v action pre-handlers found.", actionPreHandlers.length);
465
+ const mergedPreHandlers = [...preHandlers, ...actionPreHandlers];
466
+ const actionPostHandlers = Array.isArray(actionMd.after) ? actionMd.after : actionMd.after ? [actionMd.after] : [];
467
+ this.debug("%v action post-handlers found.", actionPostHandlers.length);
468
+ const mergedPostHandlers = [...postHandlers, ...actionPostHandlers];
469
+ const routeHandler = this.createRouteHandler(ctor, actionName);
470
+ router.defineRoute({
471
+ method: actionMd.method,
472
+ path: prefixedRoutePath,
473
+ preHandler: mergedPreHandlers,
474
+ handler: routeHandler,
475
+ postHandler: mergedPostHandlers
476
+ });
477
+ this.debug("Route %s %v is added.", actionMd.method.toUpperCase(), prefixedRoutePath);
478
+ });
479
+ this.controllers.add(ctor);
480
+ return this;
481
+ }
482
+ /**
483
+ * Has controller.
484
+ *
485
+ * @param ctor
486
+ */
487
+ hasController(ctor) {
488
+ return this.controllers.has(ctor);
489
+ }
490
+ /**
491
+ * Get path prefix by controller metadata.
492
+ *
493
+ * @param controllerMd
494
+ * @param options
495
+ */
496
+ getPathPrefixByControllerMetadata(controllerMd, options) {
497
+ const rootPathPrefix = (options == null ? void 0 : options.pathPrefix) || "";
498
+ this.debug("Root path prefix is %v.", rootPathPrefix);
499
+ const controllerPathPrefix = controllerMd.path || "";
500
+ this.debug("Controller path prefix is %v.", controllerPathPrefix);
501
+ const mergedPathPrefix = `/${rootPathPrefix}/${controllerPathPrefix}`.replace(/\/\//g, "/").replace(/\/$/, "");
502
+ this.debug("Merged path prefix is %v.", mergedPathPrefix);
503
+ return mergedPathPrefix;
504
+ }
505
+ /**
506
+ * Get pre-handlers by controller metadata.
507
+ *
508
+ * @param controllerMd
509
+ * @param options
510
+ */
511
+ getPreHandlersByControllerMetadata(controllerMd, options) {
512
+ let rootPreHandlers = [];
513
+ if (options == null ? void 0 : options.before)
514
+ rootPreHandlers = Array.isArray(options == null ? void 0 : options.before) ? options.before : [options.before];
515
+ this.debug("%v root pre-handlers found.", rootPreHandlers.length);
516
+ let ctlPreHandlers = [];
517
+ if (controllerMd.before)
518
+ ctlPreHandlers = Array.isArray(controllerMd.before) ? controllerMd.before : [controllerMd.before];
519
+ this.debug("%v controller pre-handlers found.", ctlPreHandlers.length);
520
+ const mergedPreHandlers = [...rootPreHandlers, ...ctlPreHandlers];
521
+ this.debug("%v merged pre-handlers.", mergedPreHandlers.length);
522
+ return mergedPreHandlers;
523
+ }
524
+ /**
525
+ * Get post-handlers by controller metadata.
526
+ *
527
+ * @param controllerMd
528
+ * @param options
529
+ */
530
+ getPostHandlersByControllerMetadata(controllerMd, options) {
531
+ let rootPostHandlers = [];
532
+ if (options == null ? void 0 : options.after)
533
+ rootPostHandlers = Array.isArray(options.after) ? options.after : [options.after];
534
+ this.debug("%v root post-handlers found.", rootPostHandlers.length);
535
+ let ctlPostHandlers = [];
536
+ if (controllerMd.after)
537
+ ctlPostHandlers = Array.isArray(controllerMd.after) ? controllerMd.after : [controllerMd.after];
538
+ this.debug("%v controller post-handlers found.", ctlPostHandlers.length);
539
+ const mergedPostHandlers = [...rootPostHandlers, ...ctlPostHandlers];
540
+ this.debug("%v merged post-handlers.", mergedPostHandlers.length);
541
+ return mergedPostHandlers;
542
+ }
543
+ /**
544
+ * Create route handler.
545
+ *
546
+ * @param controllerCtor
547
+ * @param actionName
548
+ * @protected
549
+ */
550
+ createRouteHandler(controllerCtor, actionName) {
551
+ this.debug("Creating route handler for %s.%s.", controllerCtor.name, actionName);
552
+ const requestContextMetadataMap = RequestContextReflector.getMetadata(controllerCtor, actionName);
553
+ const requestDataMetadataMap = RequestDataReflector.getMetadata(controllerCtor, actionName);
554
+ const argsNumber = controllerCtor.prototype[actionName].length;
555
+ const dataTypeCaster = this.getService(import_ts_data_schema3.DataTypeCaster);
556
+ const dataValidator = this.getService(import_ts_data_schema2.DataValidator);
557
+ return (requestContext2) => {
558
+ this.debug("Executing route handler for %s.%s.", controllerCtor.name, actionName);
559
+ const args = Array(argsNumber).map((value, index) => {
560
+ if (value != null)
561
+ return value;
562
+ const requestContextMd = requestContextMetadataMap.get(index);
563
+ if (requestContextMd != null) {
564
+ this.debug("Argument %v has request context metadata.", index);
565
+ if (requestContextMd.property == null) {
566
+ this.debug("Request context property is not specified.");
567
+ this.debug("Argument %v is set to %v.", index, requestContext2);
568
+ return requestContext2;
569
+ }
570
+ const propName = requestContextMd.property;
571
+ const propValue = requestContext2[propName];
572
+ this.debug("Request context property is %v.", propName);
573
+ this.debug("Argument %v is set to %v.", index, propValue);
574
+ return propValue;
575
+ } else {
576
+ this.debug("No RequestContextMetadata specified for %v argument.", index);
577
+ }
578
+ const requestDataMd = requestDataMetadataMap.get(index);
579
+ if (requestDataMd != null) {
580
+ this.debug("Argument %v has request data metadata.", index);
581
+ let data;
582
+ switch (requestDataMd.source) {
583
+ case RequestDataSource.PARAMS:
584
+ data = requestContext2.params;
585
+ break;
586
+ case RequestDataSource.QUERY:
587
+ data = requestContext2.query;
588
+ break;
589
+ case RequestDataSource.HEADERS:
590
+ data = requestContext2.headers;
591
+ break;
592
+ case RequestDataSource.COOKIE:
593
+ data = requestContext2.cookie;
594
+ break;
595
+ case RequestDataSource.BODY:
596
+ data = requestContext2.body;
597
+ break;
598
+ }
599
+ this.debug("Request data source is %v.", requestDataMd.source);
600
+ if (requestDataMd.schema) {
601
+ data = dataTypeCaster.cast(data, requestDataMd.schema, {
602
+ noTypeCastError: true,
603
+ sourcePath: requestDataMd.source
604
+ });
605
+ this.debug("Data type casting is passed.");
606
+ dataValidator.validate(data, requestDataMd.schema, requestDataMd.source);
607
+ this.debug("Data validation is passed.");
608
+ }
609
+ if (requestDataMd.property == null) {
610
+ this.debug("Request data property is not specified.");
611
+ this.debug("Argument %v is set to %v.", index, data);
612
+ return data;
613
+ }
614
+ const dataAsObject = data;
615
+ const propName = requestDataMd.property;
616
+ const propValue = dataAsObject[propName];
617
+ this.debug("Request data property is %v.", propName);
618
+ this.debug("Argument %v is set to %v.", index, propValue);
619
+ return propValue;
620
+ } else {
621
+ this.debug("No RequestDataMetadata specified for %v argument.", index);
622
+ }
623
+ });
624
+ const controller2 = this.getService(controllerCtor);
625
+ return controller2[actionName](...args);
626
+ };
627
+ }
628
+ };
629
+ __name(_ControllerRegistry, "ControllerRegistry");
630
+ var ControllerRegistry = _ControllerRegistry;
631
+
632
+ // dist/esm/rest-router.js
633
+ var _RestRouter = class _RestRouter extends DebuggableService {
634
+ /**
635
+ * Request listener.
636
+ */
637
+ get requestListener() {
638
+ return this.getService(import_js_trie_router3.TrieRouter).requestListener;
639
+ }
640
+ /**
641
+ * Add controller.
642
+ *
643
+ * @param ctor
644
+ * @param options
645
+ */
646
+ addController(ctor, options) {
647
+ this.getService(ControllerRegistry).addController(ctor, options);
648
+ return this;
649
+ }
650
+ };
651
+ __name(_RestRouter, "RestRouter");
652
+ var RestRouter = _RestRouter;
653
+ // Annotate the CommonJS export names for ESM import in node:
654
+ 0 && (module.exports = {
655
+ ACTIONS_METADATA_KEY,
656
+ ActionReflector,
657
+ CONTROLLER_METADATA_KEY,
658
+ ControllerReflector,
659
+ ControllerRegistry,
660
+ NotAControllerError,
661
+ REQUEST_CONTEXT_METADATA_KEY,
662
+ REQUEST_DATA_METADATA_KEY,
663
+ RequestContextReflector,
664
+ RequestDataReflector,
665
+ RequestDataSource,
666
+ RestRouter,
667
+ action,
668
+ body,
669
+ bodyParam,
670
+ capitalize,
671
+ controller,
672
+ cookie,
673
+ cookies,
674
+ createDebugger,
675
+ createError,
676
+ del,
677
+ get,
678
+ header,
679
+ headers,
680
+ param,
681
+ params,
682
+ patch,
683
+ post,
684
+ put,
685
+ queries,
686
+ query,
687
+ request,
688
+ requestContext,
689
+ requestData,
690
+ response,
691
+ toCamelCase
692
+ });