@tsed/platform-params 6.87.1 → 6.89.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.
Files changed (174) hide show
  1. package/lib/{builder → cjs/builder}/PlatformParams.d.ts +0 -0
  2. package/lib/cjs/builder/PlatformParams.js +79 -0
  3. package/lib/cjs/builder/PlatformParams.js.map +1 -0
  4. package/lib/{decorators → cjs/decorators}/bodyParams.d.ts +0 -0
  5. package/lib/cjs/decorators/bodyParams.js +53 -0
  6. package/lib/cjs/decorators/bodyParams.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/context.d.ts +0 -0
  8. package/lib/cjs/decorators/context.js +19 -0
  9. package/lib/cjs/decorators/context.js.map +1 -0
  10. package/lib/{decorators → cjs/decorators}/cookies.d.ts +0 -0
  11. package/lib/cjs/decorators/cookies.js +24 -0
  12. package/lib/cjs/decorators/cookies.js.map +1 -0
  13. package/lib/{decorators → cjs/decorators}/headerParams.d.ts +0 -0
  14. package/lib/cjs/decorators/headerParams.js +19 -0
  15. package/lib/cjs/decorators/headerParams.js.map +1 -0
  16. package/lib/{decorators → cjs/decorators}/locals.d.ts +0 -0
  17. package/lib/cjs/decorators/locals.js +19 -0
  18. package/lib/cjs/decorators/locals.js.map +1 -0
  19. package/lib/{decorators → cjs/decorators}/paramFn.d.ts +0 -0
  20. package/lib/cjs/decorators/paramFn.js +22 -0
  21. package/lib/cjs/decorators/paramFn.js.map +1 -0
  22. package/lib/{decorators → cjs/decorators}/pathParams.d.ts +0 -0
  23. package/lib/cjs/decorators/pathParams.js +55 -0
  24. package/lib/cjs/decorators/pathParams.js.map +1 -0
  25. package/lib/{decorators → cjs/decorators}/queryParams.d.ts +0 -0
  26. package/lib/cjs/decorators/queryParams.js +55 -0
  27. package/lib/cjs/decorators/queryParams.js.map +1 -0
  28. package/lib/{decorators → cjs/decorators}/session.d.ts +0 -0
  29. package/lib/cjs/decorators/session.js +19 -0
  30. package/lib/cjs/decorators/session.js.map +1 -0
  31. package/lib/{decorators → cjs/decorators}/useDeserialization.d.ts +0 -0
  32. package/lib/cjs/decorators/useDeserialization.js +18 -0
  33. package/lib/cjs/decorators/useDeserialization.js.map +1 -0
  34. package/lib/{decorators → cjs/decorators}/useParam.d.ts +0 -0
  35. package/lib/cjs/decorators/useParam.js +38 -0
  36. package/lib/cjs/decorators/useParam.js.map +1 -0
  37. package/lib/{decorators → cjs/decorators}/useParamExpression.d.ts +0 -0
  38. package/lib/cjs/decorators/useParamExpression.js +20 -0
  39. package/lib/cjs/decorators/useParamExpression.js.map +1 -0
  40. package/lib/{decorators → cjs/decorators}/useParamType.d.ts +0 -0
  41. package/lib/cjs/decorators/useParamType.js +26 -0
  42. package/lib/cjs/decorators/useParamType.js.map +1 -0
  43. package/lib/{decorators → cjs/decorators}/usePipe.d.ts +0 -0
  44. package/lib/cjs/decorators/usePipe.js +22 -0
  45. package/lib/cjs/decorators/usePipe.js.map +1 -0
  46. package/lib/{decorators → cjs/decorators}/useType.d.ts +0 -0
  47. package/lib/cjs/decorators/useType.js +20 -0
  48. package/lib/cjs/decorators/useType.js.map +1 -0
  49. package/lib/{decorators → cjs/decorators}/useValidation.d.ts +0 -0
  50. package/lib/cjs/decorators/useValidation.js +22 -0
  51. package/lib/cjs/decorators/useValidation.js.map +1 -0
  52. package/lib/{domain → cjs/domain}/ParamMetadata.d.ts +0 -0
  53. package/lib/cjs/domain/ParamMetadata.js +34 -0
  54. package/lib/cjs/domain/ParamMetadata.js.map +1 -0
  55. package/lib/{domain → cjs/domain}/ParamOptions.d.ts +0 -0
  56. package/lib/cjs/domain/ParamOptions.js +3 -0
  57. package/lib/cjs/domain/ParamOptions.js.map +1 -0
  58. package/lib/{domain → cjs/domain}/ParamTypes.d.ts +0 -0
  59. package/lib/cjs/domain/ParamTypes.js +30 -0
  60. package/lib/cjs/domain/ParamTypes.js.map +1 -0
  61. package/lib/{errors → cjs/errors}/ParamValidationError.d.ts +0 -0
  62. package/lib/cjs/errors/ParamValidationError.js +29 -0
  63. package/lib/cjs/errors/ParamValidationError.js.map +1 -0
  64. package/lib/{errors → cjs/errors}/RequiredValidationError.d.ts +0 -1
  65. package/lib/cjs/errors/RequiredValidationError.js +32 -0
  66. package/lib/cjs/errors/RequiredValidationError.js.map +1 -0
  67. package/lib/{errors → cjs/errors}/ValidationError.d.ts +0 -0
  68. package/lib/cjs/errors/ValidationError.js +13 -0
  69. package/lib/cjs/errors/ValidationError.js.map +1 -0
  70. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  71. package/lib/cjs/index.js +37 -0
  72. package/lib/cjs/index.js.map +1 -0
  73. package/lib/{pipes → cjs/pipes}/DeserializerPipe.d.ts +0 -0
  74. package/lib/cjs/pipes/DeserializerPipe.js +30 -0
  75. package/lib/cjs/pipes/DeserializerPipe.js.map +1 -0
  76. package/lib/{pipes → cjs/pipes}/ParseExpressionPipe.d.ts +0 -0
  77. package/lib/cjs/pipes/ParseExpressionPipe.js +24 -0
  78. package/lib/cjs/pipes/ParseExpressionPipe.js.map +1 -0
  79. package/lib/{pipes → cjs/pipes}/ValidationPipe.d.ts +0 -0
  80. package/lib/cjs/pipes/ValidationPipe.js +80 -0
  81. package/lib/cjs/pipes/ValidationPipe.js.map +1 -0
  82. package/lib/{utils → cjs/utils}/mapParamsOptions.d.ts +0 -0
  83. package/lib/cjs/utils/mapParamsOptions.js +30 -0
  84. package/lib/cjs/utils/mapParamsOptions.js.map +1 -0
  85. package/lib/esm/builder/PlatformParams.js +76 -0
  86. package/lib/esm/builder/PlatformParams.js.map +1 -0
  87. package/lib/esm/decorators/bodyParams.js +48 -0
  88. package/lib/esm/decorators/bodyParams.js.map +1 -0
  89. package/lib/esm/decorators/context.js +15 -0
  90. package/lib/esm/decorators/context.js.map +1 -0
  91. package/lib/esm/decorators/cookies.js +19 -0
  92. package/lib/esm/decorators/cookies.js.map +1 -0
  93. package/lib/esm/decorators/headerParams.js +15 -0
  94. package/lib/esm/decorators/headerParams.js.map +1 -0
  95. package/lib/esm/decorators/locals.js +15 -0
  96. package/lib/esm/decorators/locals.js.map +1 -0
  97. package/lib/esm/decorators/paramFn.js +18 -0
  98. package/lib/esm/decorators/paramFn.js.map +1 -0
  99. package/lib/esm/decorators/pathParams.js +50 -0
  100. package/lib/esm/decorators/pathParams.js.map +1 -0
  101. package/lib/esm/decorators/queryParams.js +50 -0
  102. package/lib/esm/decorators/queryParams.js.map +1 -0
  103. package/lib/esm/decorators/session.js +15 -0
  104. package/lib/esm/decorators/session.js.map +1 -0
  105. package/lib/esm/decorators/useDeserialization.js +14 -0
  106. package/lib/esm/decorators/useDeserialization.js.map +1 -0
  107. package/lib/esm/decorators/useParam.js +34 -0
  108. package/lib/esm/decorators/useParam.js.map +1 -0
  109. package/lib/esm/decorators/useParamExpression.js +16 -0
  110. package/lib/esm/decorators/useParamExpression.js.map +1 -0
  111. package/lib/esm/decorators/useParamType.js +22 -0
  112. package/lib/esm/decorators/useParamType.js.map +1 -0
  113. package/lib/esm/decorators/usePipe.js +18 -0
  114. package/lib/esm/decorators/usePipe.js.map +1 -0
  115. package/lib/esm/decorators/useType.js +16 -0
  116. package/lib/esm/decorators/useType.js.map +1 -0
  117. package/lib/esm/decorators/useValidation.js +18 -0
  118. package/lib/esm/decorators/useValidation.js.map +1 -0
  119. package/lib/esm/domain/ParamMetadata.js +31 -0
  120. package/lib/esm/domain/ParamMetadata.js.map +1 -0
  121. package/lib/esm/domain/ParamOptions.js +2 -0
  122. package/lib/esm/domain/ParamOptions.js.map +1 -0
  123. package/lib/esm/domain/ParamTypes.js +27 -0
  124. package/lib/esm/domain/ParamTypes.js.map +1 -0
  125. package/lib/esm/errors/ParamValidationError.js +25 -0
  126. package/lib/esm/errors/ParamValidationError.js.map +1 -0
  127. package/lib/esm/errors/RequiredValidationError.js +28 -0
  128. package/lib/esm/errors/RequiredValidationError.js.map +1 -0
  129. package/lib/esm/errors/ValidationError.js +9 -0
  130. package/lib/esm/errors/ValidationError.js.map +1 -0
  131. package/lib/esm/index.js +34 -0
  132. package/lib/esm/index.js.map +1 -0
  133. package/lib/esm/pipes/DeserializerPipe.js +27 -0
  134. package/lib/esm/pipes/DeserializerPipe.js.map +1 -0
  135. package/lib/esm/pipes/ParseExpressionPipe.js +21 -0
  136. package/lib/esm/pipes/ParseExpressionPipe.js.map +1 -0
  137. package/lib/esm/pipes/ValidationPipe.js +77 -0
  138. package/lib/esm/pipes/ValidationPipe.js.map +1 -0
  139. package/lib/esm/utils/mapParamsOptions.js +26 -0
  140. package/lib/esm/utils/mapParamsOptions.js.map +1 -0
  141. package/lib/types/builder/PlatformParams.d.ts +23 -0
  142. package/lib/types/decorators/bodyParams.d.ts +64 -0
  143. package/lib/types/decorators/context.d.ts +59 -0
  144. package/lib/types/decorators/cookies.d.ts +81 -0
  145. package/lib/types/decorators/headerParams.d.ts +33 -0
  146. package/lib/types/decorators/locals.d.ts +42 -0
  147. package/lib/types/decorators/paramFn.d.ts +11 -0
  148. package/lib/types/decorators/pathParams.d.ts +68 -0
  149. package/lib/types/decorators/queryParams.d.ts +73 -0
  150. package/lib/types/decorators/session.d.ts +36 -0
  151. package/lib/types/decorators/useDeserialization.d.ts +9 -0
  152. package/lib/types/decorators/useParam.d.ts +22 -0
  153. package/lib/types/decorators/useParamExpression.d.ts +10 -0
  154. package/lib/types/decorators/useParamType.d.ts +10 -0
  155. package/lib/types/decorators/usePipe.d.ts +13 -0
  156. package/lib/types/decorators/useType.d.ts +13 -0
  157. package/lib/types/decorators/useValidation.d.ts +9 -0
  158. package/lib/types/domain/ParamMetadata.d.ts +16 -0
  159. package/lib/types/domain/ParamOptions.d.ts +10 -0
  160. package/lib/types/domain/ParamTypes.d.ts +24 -0
  161. package/lib/types/errors/ParamValidationError.d.ts +8 -0
  162. package/lib/types/errors/RequiredValidationError.d.ts +6 -0
  163. package/lib/types/errors/ValidationError.d.ts +6 -0
  164. package/lib/types/index.d.ts +28 -0
  165. package/lib/types/pipes/DeserializerPipe.d.ts +5 -0
  166. package/lib/types/pipes/ParseExpressionPipe.d.ts +5 -0
  167. package/lib/types/pipes/ValidationPipe.d.ts +10 -0
  168. package/lib/types/utils/mapParamsOptions.d.ts +5 -0
  169. package/package.json +20 -16
  170. package/tsconfig.compile.esm.json +23 -0
  171. package/lib/index.js +0 -807
  172. package/lib/index.js.map +0 -1
  173. package/lib/index.modern.js +0 -788
  174. package/lib/index.modern.js.map +0 -1
package/lib/index.js DELETED
@@ -1,807 +0,0 @@
1
- var core = require('@tsed/core');
2
- var tslib = require('tslib');
3
- var schema = require('@tsed/schema');
4
- var di = require('@tsed/di');
5
- var jsonMapper = require('@tsed/json-mapper');
6
- var exceptions = require('@tsed/exceptions');
7
-
8
- function _extends() {
9
- _extends = Object.assign || function (target) {
10
- for (var i = 1; i < arguments.length; i++) {
11
- var source = arguments[i];
12
-
13
- for (var key in source) {
14
- if (Object.prototype.hasOwnProperty.call(source, key)) {
15
- target[key] = source[key];
16
- }
17
- }
18
- }
19
-
20
- return target;
21
- };
22
-
23
- return _extends.apply(this, arguments);
24
- }
25
-
26
- function _objectWithoutPropertiesLoose(source, excluded) {
27
- if (source == null) return {};
28
- var target = {};
29
- var sourceKeys = Object.keys(source);
30
- var key, i;
31
-
32
- for (i = 0; i < sourceKeys.length; i++) {
33
- key = sourceKeys[i];
34
- if (excluded.indexOf(key) >= 0) continue;
35
- target[key] = source[key];
36
- }
37
-
38
- return target;
39
- }
40
-
41
- /**
42
- * @ignore
43
- */
44
-
45
- function mapParamsOptions(args) {
46
- if (args.length === 1) {
47
- if (typeof args[0] === "string") {
48
- return {
49
- expression: args[0]
50
- };
51
- }
52
-
53
- if (!core.isObject(args[0])) {
54
- return {
55
- useType: args[0]
56
- };
57
- }
58
-
59
- return _extends({}, args[0]);
60
- }
61
-
62
- return {
63
- expression: args[0],
64
- useType: args[1]
65
- };
66
- }
67
-
68
- exports.ParamTypes = void 0;
69
-
70
- (function (ParamTypes) {
71
- ParamTypes["$CTX"] = "$CTX";
72
- ParamTypes["BODY"] = "BODY";
73
- ParamTypes["PATH"] = "PATH";
74
- ParamTypes["QUERY"] = "QUERY";
75
- ParamTypes["HEADER"] = "HEADER";
76
- ParamTypes["RAW_BODY"] = "RAW_BODY";
77
- ParamTypes["COOKIES"] = "COOKIES";
78
- ParamTypes["LOCALS"] = "LOCALS";
79
- ParamTypes["SESSION"] = "SESSION";
80
- ParamTypes["FILES"] = "FILES"; /// NOT STD
81
-
82
- ParamTypes["NEXT_FN"] = "NEXT_FN";
83
- ParamTypes["ERR"] = "ERR";
84
- ParamTypes["REQUEST"] = "REQUEST";
85
- ParamTypes["PLATFORM_REQUEST"] = "PLATFORM_REQUEST";
86
- ParamTypes["NODE_REQUEST"] = "NODE_REQUEST";
87
- ParamTypes["RESPONSE"] = "RESPONSE";
88
- ParamTypes["PLATFORM_RESPONSE"] = "PLATFORM_RESPONSE";
89
- ParamTypes["NODE_RESPONSE"] = "NODE_RESPONSE";
90
- ParamTypes["RESPONSE_DATA"] = "RESPONSE_DATA";
91
- ParamTypes["ENDPOINT_INFO"] = "ENDPOINT_INFO";
92
- ParamTypes["RES"] = "RES";
93
- ParamTypes["REQ"] = "REQ";
94
- })(exports.ParamTypes || (exports.ParamTypes = {}));
95
-
96
- exports.ParamMetadata = class ParamMetadata extends schema.JsonParameterStore {
97
- constructor(options) {
98
- super(options);
99
- this.paramType = "$ctx";
100
- this.pipes = [];
101
- this.paramType = options.paramType || this.paramType;
102
- this.expression = options.expression || this.expression;
103
- this.dataPath = options.dataPath || this.dataPath;
104
- this.pipes = options.pipes || [];
105
- }
106
-
107
- get key() {
108
- let {
109
- expression,
110
- paramType,
111
- dataPath
112
- } = this;
113
-
114
- if (expression && paramType === exports.ParamTypes.HEADER) {
115
- expression = String(expression).toLowerCase();
116
- }
117
-
118
- return [dataPath, expression].filter(Boolean).join(".");
119
- }
120
-
121
- static get(target, propertyKey, index) {
122
- return schema.JsonEntityStore.from(core.prototypeOf(target), propertyKey, index);
123
- }
124
-
125
- };
126
- exports.ParamMetadata = tslib.__decorate([schema.JsonEntityComponent(core.DecoratorTypes.PARAM), tslib.__metadata("design:paramtypes", [Object])], exports.ParamMetadata);
127
-
128
- exports.ParseExpressionPipe = class ParseExpressionPipe {
129
- transform(scope, param) {
130
- const {
131
- paramType,
132
- type
133
- } = param;
134
- const value = core.getValue(scope, param.key);
135
-
136
- if ([exports.ParamTypes.QUERY, exports.ParamTypes.PATH].includes(paramType) && value === "" && type !== String) {
137
- return undefined;
138
- }
139
-
140
- return value;
141
- }
142
-
143
- };
144
- exports.ParseExpressionPipe = tslib.__decorate([di.Injectable({
145
- priority: -1000
146
- })], exports.ParseExpressionPipe);
147
-
148
- var DeserializerPipe_1;
149
- exports.DeserializerPipe = DeserializerPipe_1 = class DeserializerPipe {
150
- transform(value, param) {
151
- return jsonMapper.deserialize(value, _extends({
152
- useAlias: true,
153
- additionalProperties: this.settings.additionalProperties === "accept",
154
- type: param.type,
155
- collectionType: param.collectionType,
156
- groups: param.parameter.groups,
157
- genericTypes: param.nestedGenerics[0],
158
- nestedGenerics: param.nestedGenerics
159
- }, param.store.get(DeserializerPipe_1) || {}));
160
- }
161
-
162
- };
163
-
164
- tslib.__decorate([di.Constant("converter", {}), tslib.__metadata("design:type", Object)], exports.DeserializerPipe.prototype, "settings", void 0);
165
-
166
- exports.DeserializerPipe = DeserializerPipe_1 = tslib.__decorate([di.Injectable()], exports.DeserializerPipe);
167
-
168
- class ValidationError extends exceptions.BadRequest {
169
- constructor(message, errors = []) {
170
- super(message);
171
- this.name = "VALIDATION_ERROR";
172
- this.errors = errors;
173
- }
174
-
175
- }
176
-
177
- class RequiredValidationError extends ValidationError {
178
- constructor() {
179
- super(...arguments);
180
- this.name = "REQUIRED_VALIDATION_ERROR";
181
- }
182
-
183
- static from(metadata) {
184
- const name = core.nameOf(metadata.paramType);
185
- const expression = metadata.expression;
186
- const type = name.toLowerCase().replace(/parse|params|filter/gi, "");
187
- const message = `It should have required parameter '${expression}'`;
188
- const errors = [{
189
- dataPath: "",
190
- keyword: "required",
191
- message,
192
- modelName: type,
193
- params: {
194
- missingProperty: expression
195
- },
196
- schemaPath: "#/required"
197
- }];
198
- return new RequiredValidationError(message, errors);
199
- }
200
-
201
- }
202
-
203
- exports.ValidationPipe = class ValidationPipe {
204
- constructor(injector) {
205
- const provider = injector.getProviders().find(provider => core.nameOf(provider.token) === "AjvService");
206
-
207
- if (provider) {
208
- this.validator = injector.invoke(provider.token);
209
- }
210
- }
211
-
212
- coerceTypes(value, metadata) {
213
- if (value === undefined) {
214
- return value;
215
- }
216
-
217
- if (value === "null") {
218
- return null;
219
- }
220
-
221
- if (metadata.isPrimitive) {
222
- try {
223
- return jsonMapper.deserialize(value, {
224
- type: metadata.type
225
- });
226
- } catch (er) {
227
- return value;
228
- }
229
- }
230
-
231
- if (metadata.isArray) {
232
- return [].concat(value);
233
- }
234
-
235
- return value;
236
- }
237
-
238
- skip(value, metadata) {
239
- return metadata.paramType === exports.ParamTypes.PATH && !metadata.isPrimitive;
240
- }
241
-
242
- async transform(value, metadata) {
243
- if (!this.validator) {
244
- this.checkIsRequired(value, metadata);
245
- return value;
246
- }
247
-
248
- if (this.skip(value, metadata)) {
249
- return value;
250
- }
251
-
252
- value = this.coerceTypes(value, metadata);
253
- this.checkIsRequired(value, metadata);
254
-
255
- if (value === undefined) {
256
- return value;
257
- }
258
-
259
- const schema$1 = schema.getJsonSchema(metadata, {
260
- customKeys: true
261
- });
262
- await this.validator.validate(value, {
263
- schema: schema$1,
264
- type: metadata.isClass ? metadata.type : undefined,
265
- collectionType: metadata.collectionType
266
- });
267
- return value;
268
- }
269
-
270
- checkIsRequired(value, metadata) {
271
- if (metadata.isRequired(value)) {
272
- throw RequiredValidationError.from(metadata);
273
- }
274
-
275
- return true;
276
- }
277
-
278
- };
279
- exports.ValidationPipe = tslib.__decorate([di.Injectable({
280
- type: "validator"
281
- }), tslib.__metadata("design:paramtypes", [di.InjectorService])], exports.ValidationPipe);
282
-
283
- class ParamValidationError extends exceptions.BadRequest {
284
- constructor() {
285
- super(...arguments);
286
- this.name = "PARAM_VALIDATION_ERROR";
287
- }
288
-
289
- static from(metadata, origin = {}) {
290
- if (origin instanceof ValidationError || origin instanceof exceptions.BadRequest) {
291
- const name = core.nameOf(metadata.paramType).toLowerCase().replace(/parse|params|filter/gi, "");
292
- const expression = metadata.expression;
293
- const message = `Bad request on parameter "request.${name}${expression ? "." + expression : ""}".\n${origin.message}`.trim();
294
- const error = new ParamValidationError(message);
295
- error.dataPath = String(metadata.expression) || "";
296
- error.requestType = core.nameOf(metadata.paramType);
297
- error.origin = origin.origin || origin;
298
- return error;
299
- }
300
-
301
- return origin;
302
- }
303
-
304
- }
305
-
306
- /**
307
- * Platform Params abstraction layer.
308
- * @platform
309
- */
310
-
311
- exports.PlatformParams = class PlatformParams {
312
- async getPipes(param) {
313
- const get = pipe => {
314
- return this.injector.getProvider(pipe).priority || 0;
315
- };
316
-
317
- const sort = (p1, p2) => get(p1) < get(p2) ? -1 : get(p1) > get(p2) ? 1 : 0;
318
-
319
- const map = token => this.injector.invoke(token);
320
-
321
- const promises = await Promise.all([exports.ParseExpressionPipe, ...param.pipes.sort(sort)].map(map));
322
- return promises.filter(Boolean);
323
- }
324
-
325
- async compile(entity) {
326
- const params = schema.JsonParameterStore.getParams(entity.target, entity.propertyKey);
327
- const argsPipes = await Promise.all(params.map(async param => {
328
- return {
329
- param,
330
- pipes: await this.getPipes(param)
331
- };
332
- }));
333
- return scope => {
334
- const promises = argsPipes.map(({
335
- param,
336
- pipes
337
- }) => {
338
- return this.getArg(scope, pipes, param);
339
- });
340
- return Promise.all(promises);
341
- };
342
- }
343
-
344
- async getArg(scope, pipes, param) {
345
- return pipes.reduce(async (value, pipe) => {
346
- value = await value;
347
-
348
- try {
349
- return await pipe.transform(value, param);
350
- } catch (er) {
351
- throw ParamValidationError.from(param, er);
352
- }
353
- }, scope);
354
- }
355
-
356
- async compileHandler(metadata) {
357
- const {
358
- token,
359
- propertyKey,
360
- getCustomArgs
361
- } = metadata;
362
- const provider = this.injector.getProvider(token);
363
- const getArguments = getCustomArgs || (await this.compile(schema.JsonEntityStore.fromMethod(token, propertyKey)));
364
-
365
- if (!provider || !provider.scope || provider.scope === di.ProviderScope.SINGLETON) {
366
- const instance = await this.injector.invoke(token);
367
- const handler = instance[propertyKey].bind(instance);
368
- return async scope => {
369
- const args = await getArguments(scope);
370
- return handler(...args, scope.$ctx);
371
- };
372
- }
373
-
374
- return async scope => {
375
- const [instance, args] = await Promise.all([this.injector.invoke(token, scope.$ctx.container), getArguments(scope)]);
376
- const handler = instance[propertyKey].bind(instance);
377
- return handler(...args, scope.$ctx);
378
- };
379
- }
380
-
381
- };
382
-
383
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", di.InjectorService)], exports.PlatformParams.prototype, "injector", void 0);
384
-
385
- exports.PlatformParams = tslib.__decorate([di.Injectable({
386
- scope: di.ProviderScope.SINGLETON,
387
- imports: []
388
- })], exports.PlatformParams);
389
-
390
- /**
391
- * Get the Param metadata. Use this decorator to compose your own decorator.
392
- *
393
- * @param fn
394
- * @decorator
395
- * @operation
396
- * @input
397
- */
398
-
399
- function ParamFn(fn) {
400
- return (target, propertyKey, index) => {
401
- if (core.decoratorTypeOf([target, propertyKey, index]) === core.DecoratorTypes.PARAM) {
402
- fn(exports.ParamMetadata.get(target, propertyKey, index), [target, propertyKey, index]);
403
- }
404
- };
405
- }
406
-
407
- /**
408
- * Register a Pipe to be used with the current decorated param.
409
- *
410
- * @param token
411
- * @param options
412
- * @decorator
413
- * @operation
414
- * @input
415
- * @pipe
416
- */
417
-
418
- function UsePipe(token, options = {}) {
419
- return ParamFn(param => {
420
- param.store.set(token, options);
421
- param.pipes.push(token);
422
- });
423
- }
424
-
425
- /**
426
- * Use JsonMapper to deserialize the data.
427
- *
428
- * @decorator
429
- * @operation
430
- * @input
431
- * @pipe
432
- */
433
-
434
- function UseDeserialization(options = {}) {
435
- return UsePipe(exports.DeserializerPipe, options);
436
- }
437
-
438
- /**
439
- * Get data from a path inside the given object
440
- *
441
- * @param expression
442
- * @decorator
443
- * @operation
444
- * @input
445
- * @pipe
446
- */
447
-
448
- function UseParamExpression(expression) {
449
- return ParamFn(param => {
450
- param.expression = expression || "";
451
- });
452
- }
453
-
454
- /**
455
- * Get the object from request (like body, params, query, etc...).
456
- *
457
- * @decorator
458
- * @operation
459
- * @input
460
- * @pipe
461
- */
462
-
463
- function UseParamType(options) {
464
- return core.useDecorators(ParamFn(param => {
465
- if (schema.isParameterType(options.paramType)) {
466
- param.parameter.in(options.paramType);
467
- }
468
-
469
- param.paramType = options.paramType;
470
- param.dataPath = options.dataPath;
471
- }), UseParamExpression(options.expression), options.expression && schema.Name(options.expression));
472
- }
473
-
474
- /**
475
- * Set the type of the item colllection.
476
- *
477
- * Prefer @@CollectionOf@@ usage.
478
- *
479
- * @param type
480
- * @decorator
481
- * @operation
482
- * @input
483
- * @alias CollectionOf
484
- */
485
-
486
- function UseType(type) {
487
- return schema.CollectionOf(type);
488
- }
489
-
490
- /**
491
- * Enable validation on the decoratored parameter.
492
- *
493
- * @decorator
494
- * @operation
495
- * @input
496
- * @pipe
497
- */
498
-
499
- function UseValidation() {
500
- return ParamFn(param => {
501
- if (param.type || param.collectionType) {
502
- param.pipes.push(exports.ValidationPipe);
503
- }
504
- });
505
- }
506
-
507
- const _excluded = ["paramType", "useType", "expression", "useValidation", "useConverter"];
508
- /**
509
- * @ignore
510
- * @param options
511
- */
512
-
513
- function mapPipes(options) {
514
- const {
515
- useType,
516
- useValidation,
517
- useConverter
518
- } = options,
519
- props = _objectWithoutPropertiesLoose(options, _excluded);
520
-
521
- return [useType ? UseType(useType) : ParamFn((entity, parameters) => {
522
- if (entity.isCollection && entity.type === Object) {
523
- schema.Any()(...parameters);
524
- }
525
- }), useValidation && UseValidation(), useConverter && UseDeserialization(props)];
526
- }
527
-
528
- function UseParam(...args) {
529
- const options = _extends({
530
- dataPath: "$ctx"
531
- }, (core.isString(args[0]) ? args[1] : args[0]) || {}, {
532
- paramType: core.isString(args[0]) ? args[0] : args[0].paramType
533
- });
534
-
535
- return core.useDecorators(UseParamType(options), ...mapPipes(options));
536
- }
537
-
538
- function Context(...args) {
539
- const {
540
- expression,
541
- useType,
542
- useConverter = false,
543
- useValidation = false
544
- } = mapParamsOptions(args);
545
- return UseParam({
546
- paramType: exports.ParamTypes.$CTX,
547
- dataPath: "$ctx",
548
- expression,
549
- useType,
550
- useConverter,
551
- useValidation
552
- });
553
- }
554
-
555
- function BodyParams(...args) {
556
- const {
557
- expression,
558
- useType,
559
- useConverter = true,
560
- useValidation = true
561
- } = mapParamsOptions(args);
562
- return UseParam({
563
- paramType: exports.ParamTypes.BODY,
564
- dataPath: "$ctx.request.body",
565
- expression,
566
- useType,
567
- useConverter,
568
- useValidation
569
- });
570
- }
571
- /**
572
- * RawBodyParams return the value from [request.body](http://expressjs.com/en/4x/api.html#req.body) as a Buffer.
573
- *
574
- * #### Example
575
- *
576
- * ```typescript
577
- * @Controller('/')
578
- * class MyCtrl {
579
- * @Post('/')
580
- * create(@RawBodyParams() body: Buffer) {
581
- * console.log('Entire body', body.toString("utf8"));
582
- * }
583
- * }
584
- * ```
585
- *
586
- * @decorator
587
- * @operation
588
- * @input
589
- */
590
-
591
- function RawBodyParams() {
592
- return core.useDecorators(UseParam({
593
- paramType: exports.ParamTypes.RAW_BODY,
594
- dataPath: "$ctx.request.rawBody",
595
- useType: String,
596
- useConverter: false,
597
- useValidation: false
598
- }), schema.JsonEntityFn(entity => {
599
- var _entity$parent$operat;
600
-
601
- (_entity$parent$operat = entity.parent.operation) == null ? void 0 : _entity$parent$operat.consumes(["*/*"]);
602
- }));
603
- }
604
-
605
- function HeaderParams(...args) {
606
- const {
607
- expression,
608
- useType,
609
- useConverter = false,
610
- useValidation = false
611
- } = mapParamsOptions(args);
612
- return UseParam({
613
- paramType: exports.ParamTypes.HEADER,
614
- dataPath: "$ctx.request.headers",
615
- expression,
616
- useType,
617
- useConverter,
618
- useValidation
619
- });
620
- }
621
-
622
- function CookiesParams(...args) {
623
- const {
624
- expression,
625
- useType,
626
- useConverter = false,
627
- useValidation = false
628
- } = mapParamsOptions(args);
629
- return UseParam({
630
- paramType: exports.ParamTypes.COOKIES,
631
- dataPath: "$ctx.request.cookies",
632
- expression,
633
- useType,
634
- useConverter,
635
- useValidation
636
- });
637
- }
638
- function Cookies(...args) {
639
- // @ts-ignore
640
- return CookiesParams(...args);
641
- }
642
-
643
- function Locals(...args) {
644
- const {
645
- expression,
646
- useType,
647
- useConverter = false,
648
- useValidation = false
649
- } = mapParamsOptions(args);
650
- return UseParam({
651
- paramType: exports.ParamTypes.LOCALS,
652
- dataPath: "$ctx.response.locals",
653
- expression,
654
- useType,
655
- useConverter,
656
- useValidation
657
- });
658
- }
659
-
660
- function PathParams(...args) {
661
- const {
662
- expression,
663
- useType,
664
- useConverter = true,
665
- useValidation = true
666
- } = mapParamsOptions(args);
667
- return UseParam({
668
- paramType: exports.ParamTypes.PATH,
669
- dataPath: "$ctx.request.params",
670
- expression,
671
- useType,
672
- useConverter,
673
- useValidation
674
- });
675
- }
676
- /**
677
- * RawPathParams return the raw value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
678
- *
679
- * Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
680
- *
681
- * #### Example
682
- *
683
- * ```typescript
684
- * @Controller('/')
685
- * class MyCtrl {
686
- * @Get('/')
687
- * get(@RawPathParams() params: string) {
688
- * console.log('Entire params', params);
689
- * }
690
- *
691
- * @Get('/')
692
- * get(@RawPathParams('id') id: string) {
693
- * console.log('ID', id);
694
- * }
695
- * }
696
- * ```
697
- * > For more information on deserialization see [converters](/docs/converters.md) page.
698
- *
699
- * @param expression The path of the property to get.
700
- * @decorator
701
- * @operation
702
- * @input
703
- */
704
-
705
- function RawPathParams(expression) {
706
- return UseParam({
707
- paramType: exports.ParamTypes.PATH,
708
- dataPath: "$ctx.request.params",
709
- expression
710
- });
711
- }
712
-
713
- function QueryParams(...args) {
714
- const {
715
- expression,
716
- useType,
717
- useConverter = true,
718
- useValidation = true
719
- } = mapParamsOptions(args);
720
- return UseParam({
721
- paramType: exports.ParamTypes.QUERY,
722
- dataPath: "$ctx.request.query",
723
- expression,
724
- useType,
725
- useConverter,
726
- useValidation
727
- });
728
- }
729
- /**
730
- * RawQueryParams return the value from [request.query](http://expressjs.com/en/4x/api.html#req.query) object.
731
- *
732
- * Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
733
- *
734
- * #### Example
735
- *
736
- * ```typescript
737
- * @Controller('/')
738
- * class MyCtrl {
739
- * @Get('/')
740
- * get(@RawPathParams() params: any) {
741
- * console.log('Entire params', params);
742
- * }
743
- *
744
- * @Get('/')
745
- * get(@RawPathParams('id') id: string) {
746
- * console.log('ID', id);
747
- * }
748
- * }
749
- * ```
750
- * > For more information on deserialization see [converters](/docs/converters.md) page.
751
- *
752
- * @param expression The path of the property to get.
753
- * @decorator
754
- * @operation
755
- * @input
756
- */
757
-
758
- function RawQueryParams(expression) {
759
- return UseParam({
760
- paramType: exports.ParamTypes.QUERY,
761
- dataPath: "$ctx.request.query",
762
- expression
763
- });
764
- }
765
-
766
- function Session(...args) {
767
- const {
768
- expression,
769
- useType,
770
- useConverter = false,
771
- useValidation = false
772
- } = mapParamsOptions(args);
773
- return UseParam({
774
- paramType: exports.ParamTypes.SESSION,
775
- dataPath: "$ctx.request.session",
776
- expression,
777
- useType,
778
- useConverter,
779
- useValidation
780
- });
781
- }
782
-
783
- exports.BodyParams = BodyParams;
784
- exports.Context = Context;
785
- exports.Cookies = Cookies;
786
- exports.CookiesParams = CookiesParams;
787
- exports.HeaderParams = HeaderParams;
788
- exports.Locals = Locals;
789
- exports.ParamFn = ParamFn;
790
- exports.ParamValidationError = ParamValidationError;
791
- exports.PathParams = PathParams;
792
- exports.QueryParams = QueryParams;
793
- exports.RawBodyParams = RawBodyParams;
794
- exports.RawPathParams = RawPathParams;
795
- exports.RawQueryParams = RawQueryParams;
796
- exports.RequiredValidationError = RequiredValidationError;
797
- exports.Session = Session;
798
- exports.UseDeserialization = UseDeserialization;
799
- exports.UseParam = UseParam;
800
- exports.UseParamExpression = UseParamExpression;
801
- exports.UseParamType = UseParamType;
802
- exports.UsePipe = UsePipe;
803
- exports.UseType = UseType;
804
- exports.UseValidation = UseValidation;
805
- exports.ValidationError = ValidationError;
806
- exports.mapParamsOptions = mapParamsOptions;
807
- //# sourceMappingURL=index.js.map