@tsed/platform-params 7.0.0-alpha.2 → 7.0.0-beta.1

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