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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) 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 +46 -0
  4. package/lib/cjs/decorators/bodyParams.js.map +1 -0
  5. package/lib/cjs/decorators/context.js +18 -0
  6. package/lib/cjs/decorators/context.js.map +1 -0
  7. package/lib/cjs/decorators/cookies.js +23 -0
  8. package/lib/cjs/decorators/cookies.js.map +1 -0
  9. package/lib/cjs/decorators/headerParams.js +18 -0
  10. package/lib/cjs/decorators/headerParams.js.map +1 -0
  11. package/lib/cjs/decorators/locals.js +18 -0
  12. package/lib/cjs/decorators/locals.js.map +1 -0
  13. package/lib/cjs/decorators/paramFn.js +17 -0
  14. package/lib/cjs/decorators/paramFn.js.map +1 -0
  15. package/lib/cjs/decorators/pathParams.js +54 -0
  16. package/lib/cjs/decorators/pathParams.js.map +1 -0
  17. package/lib/cjs/decorators/queryParams.js +54 -0
  18. package/lib/cjs/decorators/queryParams.js.map +1 -0
  19. package/lib/cjs/decorators/session.js +18 -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 +50 -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 +32 -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 +6 -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 +40 -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/exports.js +3 -0
  48. package/lib/cjs/exports.js.map +1 -0
  49. package/lib/cjs/index.js +35 -0
  50. package/lib/cjs/index.js.map +1 -0
  51. package/lib/cjs/package.json +3 -0
  52. package/lib/cjs/pipes/DeserializerPipe.js +30 -0
  53. package/lib/cjs/pipes/DeserializerPipe.js.map +1 -0
  54. package/lib/cjs/pipes/ParseExpressionPipe.js +32 -0
  55. package/lib/cjs/pipes/ParseExpressionPipe.js.map +1 -0
  56. package/lib/cjs/pipes/ValidationPipe.js +83 -0
  57. package/lib/cjs/pipes/ValidationPipe.js.map +1 -0
  58. package/lib/cjs/utils/mapParamsOptions.js +30 -0
  59. package/lib/cjs/utils/mapParamsOptions.js.map +1 -0
  60. package/lib/esm/builder/PlatformParams.js +76 -0
  61. package/lib/esm/builder/PlatformParams.js.map +1 -0
  62. package/lib/esm/decorators/bodyParams.js +41 -0
  63. package/lib/esm/decorators/bodyParams.js.map +1 -0
  64. package/lib/esm/decorators/context.js +14 -0
  65. package/lib/esm/decorators/context.js.map +1 -0
  66. package/lib/esm/decorators/cookies.js +18 -0
  67. package/lib/esm/decorators/cookies.js.map +1 -0
  68. package/lib/esm/decorators/headerParams.js +14 -0
  69. package/lib/esm/decorators/headerParams.js.map +1 -0
  70. package/lib/esm/decorators/locals.js +14 -0
  71. package/lib/esm/decorators/locals.js.map +1 -0
  72. package/lib/esm/decorators/paramFn.js +13 -0
  73. package/lib/esm/decorators/paramFn.js.map +1 -0
  74. package/lib/esm/decorators/pathParams.js +49 -0
  75. package/lib/esm/decorators/pathParams.js.map +1 -0
  76. package/lib/esm/decorators/queryParams.js +49 -0
  77. package/lib/esm/decorators/queryParams.js.map +1 -0
  78. package/lib/esm/decorators/session.js +14 -0
  79. package/lib/esm/decorators/session.js.map +1 -0
  80. package/lib/esm/decorators/useDeserialization.js +14 -0
  81. package/lib/esm/decorators/useDeserialization.js.map +1 -0
  82. package/lib/esm/decorators/useParam.js +46 -0
  83. package/lib/esm/decorators/useParam.js.map +1 -0
  84. package/lib/esm/decorators/useParamExpression.js +16 -0
  85. package/lib/esm/decorators/useParamExpression.js.map +1 -0
  86. package/lib/esm/decorators/useParamType.js +28 -0
  87. package/lib/esm/decorators/useParamType.js.map +1 -0
  88. package/lib/esm/decorators/usePipe.js +18 -0
  89. package/lib/esm/decorators/usePipe.js.map +1 -0
  90. package/lib/esm/decorators/useType.js +16 -0
  91. package/lib/esm/decorators/useType.js.map +1 -0
  92. package/lib/esm/decorators/useValidation.js +18 -0
  93. package/lib/esm/decorators/useValidation.js.map +1 -0
  94. package/lib/esm/domain/ParamMetadata.js +2 -0
  95. package/lib/esm/domain/ParamMetadata.js.map +1 -0
  96. package/lib/esm/domain/ParamOptions.js +2 -0
  97. package/lib/esm/domain/ParamOptions.js.map +1 -0
  98. package/lib/esm/domain/ParamTypes.js +37 -0
  99. package/lib/esm/domain/ParamTypes.js.map +1 -0
  100. package/lib/esm/errors/ParamValidationError.js +25 -0
  101. package/lib/esm/errors/ParamValidationError.js.map +1 -0
  102. package/lib/esm/errors/RequiredValidationError.js +28 -0
  103. package/lib/esm/errors/RequiredValidationError.js.map +1 -0
  104. package/lib/esm/errors/ValidationError.js +9 -0
  105. package/lib/esm/errors/ValidationError.js.map +1 -0
  106. package/lib/esm/exports.js +2 -0
  107. package/lib/esm/exports.js.map +1 -0
  108. package/lib/esm/index.js +32 -0
  109. package/lib/esm/index.js.map +1 -0
  110. package/lib/esm/package.json +3 -0
  111. package/lib/esm/pipes/DeserializerPipe.js +27 -0
  112. package/lib/esm/pipes/DeserializerPipe.js.map +1 -0
  113. package/lib/esm/pipes/ParseExpressionPipe.js +29 -0
  114. package/lib/esm/pipes/ParseExpressionPipe.js.map +1 -0
  115. package/lib/esm/pipes/ValidationPipe.js +80 -0
  116. package/lib/esm/pipes/ValidationPipe.js.map +1 -0
  117. package/lib/esm/utils/mapParamsOptions.js +26 -0
  118. package/lib/esm/utils/mapParamsOptions.js.map +1 -0
  119. package/lib/{builder → types/builder}/PlatformParams.d.ts +3 -4
  120. package/lib/{decorators → types/decorators}/bodyParams.d.ts +2 -1
  121. package/lib/{decorators → types/decorators}/context.d.ts +0 -6
  122. package/lib/{decorators → types/decorators}/cookies.d.ts +0 -0
  123. package/lib/{decorators → types/decorators}/headerParams.d.ts +0 -0
  124. package/lib/{decorators → types/decorators}/locals.d.ts +0 -0
  125. package/lib/{decorators → types/decorators}/paramFn.d.ts +2 -2
  126. package/lib/{decorators → types/decorators}/pathParams.d.ts +0 -0
  127. package/lib/{decorators → types/decorators}/queryParams.d.ts +0 -0
  128. package/lib/{decorators → types/decorators}/session.d.ts +0 -0
  129. package/lib/{decorators → types/decorators}/useDeserialization.d.ts +0 -0
  130. package/lib/{decorators → types/decorators}/useParam.d.ts +0 -0
  131. package/lib/{decorators → types/decorators}/useParamExpression.d.ts +0 -0
  132. package/lib/{decorators → types/decorators}/useParamType.d.ts +0 -0
  133. package/lib/{decorators → types/decorators}/usePipe.d.ts +1 -1
  134. package/lib/{decorators → types/decorators}/useType.d.ts +0 -0
  135. package/lib/{decorators → types/decorators}/useValidation.d.ts +0 -0
  136. package/lib/types/domain/ParamMetadata.d.ts +1 -0
  137. package/lib/{domain → types/domain}/ParamOptions.d.ts +0 -0
  138. package/lib/{domain → types/domain}/ParamTypes.d.ts +1 -0
  139. package/lib/{errors → types/errors}/ParamValidationError.d.ts +2 -2
  140. package/lib/{errors → types/errors}/RequiredValidationError.d.ts +2 -3
  141. package/lib/{errors → types/errors}/ValidationError.d.ts +0 -0
  142. package/lib/types/exports.d.ts +1 -0
  143. package/lib/{index.d.ts → types/index.d.ts} +24 -21
  144. package/lib/types/pipes/DeserializerPipe.d.ts +5 -0
  145. package/lib/types/pipes/ParseExpressionPipe.d.ts +6 -0
  146. package/lib/types/pipes/ValidationPipe.d.ts +10 -0
  147. package/lib/{utils → types/utils}/mapParamsOptions.d.ts +0 -0
  148. package/package.json +41 -18
  149. package/readme.md +1 -2
  150. package/tsconfig.compile.esm.json +12 -0
  151. package/lib/domain/ParamMetadata.d.ts +0 -21
  152. package/lib/index.js +0 -844
  153. package/lib/index.js.map +0 -1
  154. package/lib/index.modern.js +0 -825
  155. package/lib/index.modern.js.map +0 -1
  156. package/lib/pipes/DeserializerPipe.d.ts +0 -5
  157. package/lib/pipes/ParseExpressionPipe.d.ts +0 -5
  158. package/lib/pipes/ValidationPipe.d.ts +0 -10
@@ -1,825 +0,0 @@
1
- import { isObject, prototypeOf, ancestorsOf, DecoratorTypes, getValue, nameOf, decoratorTypeOf, useDecorators } from '@tsed/core';
2
- import { __decorate, __metadata } from 'tslib';
3
- import { JsonEntityStore, JsonEntityComponent, getJsonSchema, isParameterType, Name, CollectionOf, Any } from '@tsed/schema';
4
- import { Injectable, Constant, InjectorService, ProviderScope, Inject } from '@tsed/di';
5
- import { deserialize } from '@tsed/json-mapper';
6
- import { BadRequest } from '@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 (!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
- var ParamTypes;
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
- })(ParamTypes || (ParamTypes = {}));
95
-
96
- let ParamMetadata = class ParamMetadata extends 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 === 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 JsonEntityStore.from(prototypeOf(target), propertyKey, index);
136
- }
137
-
138
- static getParams(target, propertyKey) {
139
- const params = [];
140
- const klass = ancestorsOf(target).reverse().find(target => JsonEntityStore.fromMethod(target, propertyKey).children.size);
141
-
142
- if (!klass) {
143
- return [];
144
- }
145
-
146
- JsonEntityStore.fromMethod(klass, propertyKey).children.forEach((param, index) => {
147
- params[+index] = param;
148
- });
149
- return params;
150
- }
151
-
152
- };
153
- ParamMetadata = __decorate([JsonEntityComponent(DecoratorTypes.PARAM), __metadata("design:paramtypes", [Object])], ParamMetadata);
154
-
155
- let ParseExpressionPipe = class ParseExpressionPipe {
156
- transform(scope, param) {
157
- const {
158
- paramType,
159
- type
160
- } = param;
161
- const value = getValue(scope, param.key);
162
-
163
- if ([ParamTypes.QUERY, ParamTypes.PATH].includes(paramType) && value === "" && type !== String) {
164
- return undefined;
165
- }
166
-
167
- return value;
168
- }
169
-
170
- };
171
- ParseExpressionPipe = __decorate([Injectable({
172
- priority: -1000
173
- })], ParseExpressionPipe);
174
-
175
- var DeserializerPipe_1;
176
- let DeserializerPipe = DeserializerPipe_1 = class DeserializerPipe {
177
- transform(value, param) {
178
- return 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
- __decorate([Constant("converter", {}), __metadata("design:type", Object)], DeserializerPipe.prototype, "settings", void 0);
192
-
193
- DeserializerPipe = DeserializerPipe_1 = __decorate([Injectable()], DeserializerPipe);
194
-
195
- class ValidationError extends 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 = 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
- let ValidationPipe = class ValidationPipe {
231
- constructor(injector) {
232
- const provider = injector.getProviders().find(provider => 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 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 === 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 = getJsonSchema(metadata, {
287
- customKeys: true
288
- });
289
- await this.validator.validate(value, {
290
- schema,
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
- ValidationPipe = __decorate([Injectable({
307
- type: "validator"
308
- }), __metadata("design:paramtypes", [InjectorService])], ValidationPipe);
309
-
310
- class ParamValidationError extends 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 BadRequest) {
318
- const name = 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 = 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
- let 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([ParseExpressionPipe, ...param.pipes.sort(sort)].map(map));
349
- return promises.filter(Boolean);
350
- }
351
-
352
- async compile(entity) {
353
- var _this = this;
354
-
355
- const params = ParamMetadata.getParams(entity.target, entity.propertyKey);
356
- const argsPipes = await Promise.all(params.map(async function (param) {
357
- return {
358
- param,
359
- pipes: await _this.getPipes(param)
360
- };
361
- }));
362
- return scope => {
363
- const promises = argsPipes.map(({
364
- param,
365
- pipes
366
- }) => {
367
- return this.getArg(scope, pipes, param);
368
- });
369
- return Promise.all(promises);
370
- };
371
- }
372
-
373
- async getArg(scope, pipes, param) {
374
- return pipes.reduce(async function (value, pipe) {
375
- value = await value;
376
-
377
- try {
378
- return await pipe.transform(value, param);
379
- } catch (er) {
380
- throw ParamValidationError.from(param, er);
381
- }
382
- }, scope);
383
- }
384
-
385
- async compileHandler(metadata) {
386
- var _this2 = this;
387
-
388
- const {
389
- token,
390
- propertyKey,
391
- getCustomArgs
392
- } = metadata;
393
- const provider = this.injector.getProvider(token);
394
- const getArguments = getCustomArgs || (await this.compile(JsonEntityStore.fromMethod(token, propertyKey)));
395
-
396
- if (!provider || !provider.scope || provider.scope === ProviderScope.SINGLETON) {
397
- const instance = await this.injector.invoke(token);
398
- const handler = instance[propertyKey].bind(instance);
399
- return async function (scope) {
400
- const args = await getArguments(scope);
401
- return handler(...args, scope.$ctx);
402
- };
403
- }
404
-
405
- return async function (scope) {
406
- const [instance, args] = await Promise.all([_this2.injector.invoke(token, scope.$ctx.container), getArguments(scope)]);
407
- const handler = instance[propertyKey].bind(instance);
408
- return handler(...args, scope.$ctx);
409
- };
410
- }
411
-
412
- };
413
-
414
- __decorate([Inject(), __metadata("design:type", InjectorService)], PlatformParams.prototype, "injector", void 0);
415
-
416
- PlatformParams = __decorate([Injectable({
417
- scope: ProviderScope.SINGLETON,
418
- imports: []
419
- })], PlatformParams);
420
-
421
- /**
422
- * Get the Param metadata. Use this decorator to compose your own decorator.
423
- *
424
- * @param fn
425
- * @decorator
426
- * @operation
427
- * @input
428
- */
429
-
430
- function ParamFn(fn) {
431
- return (target, propertyKey, index) => {
432
- if (decoratorTypeOf([target, propertyKey, index]) === DecoratorTypes.PARAM) {
433
- fn(ParamMetadata.get(target, propertyKey, index), [target, propertyKey, index]);
434
- }
435
- };
436
- }
437
-
438
- /**
439
- * Register a Pipe to be used with the current decorated param.
440
- *
441
- * @param token
442
- * @param options
443
- * @decorator
444
- * @operation
445
- * @input
446
- * @pipe
447
- */
448
-
449
- function UsePipe(token, options = {}) {
450
- return ParamFn(param => {
451
- param.store.set(token, options);
452
- param.pipes.push(token);
453
- });
454
- }
455
-
456
- /**
457
- * Use JsonMapper to deserialize the data.
458
- *
459
- * @decorator
460
- * @operation
461
- * @input
462
- * @pipe
463
- */
464
-
465
- function UseDeserialization(options = {}) {
466
- return UsePipe(DeserializerPipe, options);
467
- }
468
-
469
- /**
470
- * Get data from a path inside the given object
471
- *
472
- * @param expression
473
- * @decorator
474
- * @operation
475
- * @input
476
- * @pipe
477
- */
478
-
479
- function UseParamExpression(expression) {
480
- return ParamFn(param => {
481
- param.expression = expression || "";
482
- });
483
- }
484
-
485
- /**
486
- * Get the object from request (like body, params, query, etc...).
487
- *
488
- * @decorator
489
- * @operation
490
- * @input
491
- * @pipe
492
- */
493
-
494
- function UseParamType(options) {
495
- return useDecorators(ParamFn(param => {
496
- if (isParameterType(options.paramType)) {
497
- param.parameter.in(options.paramType);
498
- }
499
-
500
- param.paramType = options.paramType;
501
- param.dataPath = options.dataPath;
502
- }), UseParamExpression(options.expression), options.expression && Name(options.expression));
503
- }
504
-
505
- /**
506
- * Set the type of the item colllection.
507
- *
508
- * Prefer @@CollectionOf@@ usage.
509
- *
510
- * @param type
511
- * @decorator
512
- * @operation
513
- * @input
514
- * @alias CollectionOf
515
- */
516
-
517
- function UseType(type) {
518
- return CollectionOf(type);
519
- }
520
-
521
- /**
522
- * Enable validation on the decoratored parameter.
523
- *
524
- * @decorator
525
- * @operation
526
- * @input
527
- * @pipe
528
- */
529
-
530
- function UseValidation() {
531
- return ParamFn(param => {
532
- if (param.type || param.collectionType) {
533
- param.pipes.push(ValidationPipe);
534
- }
535
- });
536
- }
537
-
538
- const _excluded = ["paramType", "useType", "expression", "useValidation", "useConverter"];
539
- /**
540
- * @ignore
541
- * @param options
542
- */
543
-
544
- function mapPipes(options) {
545
- const {
546
- useType,
547
- useValidation,
548
- useConverter
549
- } = options,
550
- props = _objectWithoutPropertiesLoose(options, _excluded);
551
-
552
- return [useType ? UseType(useType) : ParamFn((entity, parameters) => {
553
- if (entity.isCollection && entity.type === Object) {
554
- Any()(...parameters);
555
- }
556
- }), useValidation && UseValidation(), useConverter && UseDeserialization(props)];
557
- }
558
- /**
559
- * Register a new param. It use the paramType to extract value and give it to the next pipe.
560
- *
561
- * Given options allow to enable or disable following pipes:
562
- *
563
- * - useType: Add extra type for the json mapper,
564
- * - expression: Get property from the returned value by the previous pipe.
565
- * - useValidation: Apply validation from the returned value by the previous pipe.
566
- * - useConverter: Apply json mapper from the returned value by the previous pipe.
567
- *
568
- * @param options
569
- * @decorator
570
- * @operation
571
- * @input
572
- * @pipe
573
- */
574
-
575
-
576
- function UseParam(options) {
577
- options = _extends({
578
- dataPath: "$ctx"
579
- }, options);
580
- return useDecorators(UseParamType(options), ...mapPipes(options));
581
- }
582
-
583
- function Context(...args) {
584
- const {
585
- expression,
586
- useType,
587
- useConverter = false,
588
- useValidation = false
589
- } = mapParamsOptions(args);
590
- return UseParam({
591
- paramType: ParamTypes.$CTX,
592
- dataPath: "$ctx",
593
- expression,
594
- useType,
595
- useConverter,
596
- useValidation
597
- });
598
- }
599
-
600
- function BodyParams(...args) {
601
- const {
602
- expression,
603
- useType,
604
- useConverter = true,
605
- useValidation = true
606
- } = mapParamsOptions(args);
607
- return UseParam({
608
- paramType: ParamTypes.BODY,
609
- dataPath: "$ctx.request.body",
610
- expression,
611
- useType,
612
- useConverter,
613
- useValidation
614
- });
615
- }
616
- /**
617
- * RawBodyParams return the value from [request.body](http://expressjs.com/en/4x/api.html#req.body) as a Buffer.
618
- *
619
- * #### Example
620
- *
621
- * ```typescript
622
- * @Controller('/')
623
- * class MyCtrl {
624
- * @Post('/')
625
- * create(@RawBodyParams() body: Buffer) {
626
- * console.log('Entire body', body.toString('utf8'));
627
- * }
628
- * }
629
- * ```
630
- *
631
- * @decorator
632
- * @operation
633
- * @input
634
- */
635
-
636
- function RawBodyParams() {
637
- return useDecorators(UseParam({
638
- paramType: ParamTypes.RAW_BODY,
639
- dataPath: "$ctx.request.rawBody",
640
- useType: Object,
641
- useConverter: false,
642
- useValidation: false
643
- }));
644
- }
645
-
646
- function HeaderParams(...args) {
647
- const {
648
- expression,
649
- useType,
650
- useConverter = false,
651
- useValidation = false
652
- } = mapParamsOptions(args);
653
- return UseParam({
654
- paramType: ParamTypes.HEADER,
655
- dataPath: "$ctx.request.headers",
656
- expression,
657
- useType,
658
- useConverter,
659
- useValidation
660
- });
661
- }
662
-
663
- function CookiesParams(...args) {
664
- const {
665
- expression,
666
- useType,
667
- useConverter = false,
668
- useValidation = false
669
- } = mapParamsOptions(args);
670
- return UseParam({
671
- paramType: ParamTypes.COOKIES,
672
- dataPath: "$ctx.request.cookies",
673
- expression,
674
- useType,
675
- useConverter,
676
- useValidation
677
- });
678
- }
679
- function Cookies(...args) {
680
- // @ts-ignore
681
- return CookiesParams(...args);
682
- }
683
-
684
- function Locals(...args) {
685
- const {
686
- expression,
687
- useType,
688
- useConverter = false,
689
- useValidation = false
690
- } = mapParamsOptions(args);
691
- return UseParam({
692
- paramType: ParamTypes.LOCALS,
693
- dataPath: "$ctx.response.locals",
694
- expression,
695
- useType,
696
- useConverter,
697
- useValidation
698
- });
699
- }
700
-
701
- function PathParams(...args) {
702
- const {
703
- expression,
704
- useType,
705
- useConverter = true,
706
- useValidation = true
707
- } = mapParamsOptions(args);
708
- return UseParam({
709
- paramType: ParamTypes.PATH,
710
- dataPath: "$ctx.request.params",
711
- expression,
712
- useType,
713
- useConverter,
714
- useValidation
715
- });
716
- }
717
- /**
718
- * RawPathParams return the raw value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
719
- *
720
- * Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
721
- *
722
- * #### Example
723
- *
724
- * ```typescript
725
- * @Controller('/')
726
- * class MyCtrl {
727
- * @Get('/')
728
- * get(@RawPathParams() params: string) {
729
- * console.log('Entire params', params);
730
- * }
731
- *
732
- * @Get('/')
733
- * get(@RawPathParams('id') id: string) {
734
- * console.log('ID', id);
735
- * }
736
- * }
737
- * ```
738
- * > For more information on deserialization see [converters](/docs/converters.md) page.
739
- *
740
- * @param expression The path of the property to get.
741
- * @decorator
742
- * @operation
743
- * @input
744
- */
745
-
746
- function RawPathParams(expression) {
747
- return UseParam({
748
- paramType: ParamTypes.PATH,
749
- dataPath: "$ctx.request.params",
750
- expression
751
- });
752
- }
753
-
754
- function QueryParams(...args) {
755
- const {
756
- expression,
757
- useType,
758
- useConverter = true,
759
- useValidation = true
760
- } = mapParamsOptions(args);
761
- return UseParam({
762
- paramType: ParamTypes.QUERY,
763
- dataPath: "$ctx.request.query",
764
- expression,
765
- useType,
766
- useConverter,
767
- useValidation
768
- });
769
- }
770
- /**
771
- * RawQueryParams return the value from [request.query](http://expressjs.com/en/4x/api.html#req.query) object.
772
- *
773
- * Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
774
- *
775
- * #### Example
776
- *
777
- * ```typescript
778
- * @Controller('/')
779
- * class MyCtrl {
780
- * @Get('/')
781
- * get(@RawPathParams() params: any) {
782
- * console.log('Entire params', params);
783
- * }
784
- *
785
- * @Get('/')
786
- * get(@RawPathParams('id') id: string) {
787
- * console.log('ID', id);
788
- * }
789
- * }
790
- * ```
791
- * > For more information on deserialization see [converters](/docs/converters.md) page.
792
- *
793
- * @param expression The path of the property to get.
794
- * @decorator
795
- * @operation
796
- * @input
797
- */
798
-
799
- function RawQueryParams(expression) {
800
- return UseParam({
801
- paramType: ParamTypes.QUERY,
802
- dataPath: "$ctx.request.query",
803
- expression
804
- });
805
- }
806
-
807
- function Session(...args) {
808
- const {
809
- expression,
810
- useType,
811
- useConverter = false,
812
- useValidation = false
813
- } = mapParamsOptions(args);
814
- return UseParam({
815
- paramType: ParamTypes.SESSION,
816
- dataPath: "$ctx.request.session",
817
- expression,
818
- useType,
819
- useConverter,
820
- useValidation
821
- });
822
- }
823
-
824
- export { BodyParams, Context, Cookies, CookiesParams, DeserializerPipe, HeaderParams, Locals, ParamFn, ParamMetadata, ParamTypes, ParamValidationError, ParseExpressionPipe, PathParams, PlatformParams, QueryParams, RawBodyParams, RawPathParams, RawQueryParams, RequiredValidationError, Session, UseDeserialization, UseParam, UseParamExpression, UseParamType, UsePipe, UseType, UseValidation, ValidationError, ValidationPipe, mapParamsOptions };
825
- //# sourceMappingURL=index.modern.js.map