@tsed/schema 6.72.3 → 6.73.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -82,6 +82,21 @@ function getStatusMessage(status) {
82
82
  return HTTP_STATUS_MESSAGES[status];
83
83
  }
84
84
 
85
+ const HTTP_METHODS = ["all", "checkout", "connect", "copy", "delete", "get", "head", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "options", "param", "patch", "post", "propfind", "propatch", "purge", "put", "report", "search", "subscribe", "trace", "unlock", "unsuscribe"];
86
+ exports.OperationMethods = void 0;
87
+
88
+ (function (OperationMethods) {
89
+ OperationMethods["ALL"] = "ALL";
90
+ OperationMethods["GET"] = "GET";
91
+ OperationMethods["POST"] = "POST";
92
+ OperationMethods["PUT"] = "PUT";
93
+ OperationMethods["PATCH"] = "PATCH";
94
+ OperationMethods["HEAD"] = "HEAD";
95
+ OperationMethods["DELETE"] = "DELETE";
96
+ OperationMethods["OPTIONS"] = "OPTIONS";
97
+ OperationMethods["CUSTOM"] = "CUSTOM";
98
+ })(exports.OperationMethods || (exports.OperationMethods = {}));
99
+
85
100
  function _extends() {
86
101
  _extends = Object.assign || function (target) {
87
102
  for (var i = 1; i < arguments.length; i++) {
@@ -2152,10 +2167,10 @@ class JsonEntityStore extends core.Entity {
2152
2167
  return this.from(target, propertyKey, core.descriptorOf(target, propertyKey));
2153
2168
  }
2154
2169
 
2155
- getResponseOptions(contentType = "application/json") {
2170
+ getResponseOptions(status, contentType = "application/json") {
2156
2171
  var _this$operation, _this$operation$getRe;
2157
2172
 
2158
- const media = (_this$operation = this.operation) == null ? void 0 : (_this$operation$getRe = _this$operation.getResponseOf(this.statusCode)) == null ? void 0 : _this$operation$getRe.getMedia(contentType, false);
2173
+ const media = (_this$operation = this.operation) == null ? void 0 : (_this$operation$getRe = _this$operation.getResponseOf(status)) == null ? void 0 : _this$operation$getRe.getMedia(contentType, false);
2159
2174
 
2160
2175
  if (media && media.has("schema")) {
2161
2176
  const schema = media.get("schema");
@@ -2206,7 +2221,11 @@ class JsonEntityStore extends core.Entity {
2206
2221
  if (core.isCollection(type)) {
2207
2222
  this.collectionType = type;
2208
2223
  } else {
2209
- this._type = type;
2224
+ this._type = type; // issue #1534: Enum metadata stored as plain object instead of String (see: https://github.com/tsedio/tsed/issues/1534)
2225
+
2226
+ if (this._type && core.isPlainObject(this._type)) {
2227
+ this._type = String;
2228
+ }
2210
2229
  }
2211
2230
  }
2212
2231
 
@@ -4248,9 +4267,12 @@ function Consumes(...consumes) {
4248
4267
  */
4249
4268
 
4250
4269
  class DecoratorContext extends Map {
4251
- constructor() {
4252
- super(...arguments);
4270
+ constructor(opts = {}) {
4271
+ super();
4253
4272
  this.actions = [];
4273
+ Object.entries(opts).forEach(([key, value]) => {
4274
+ this.set(key, value);
4275
+ });
4254
4276
  }
4255
4277
 
4256
4278
  addAction(cb) {
@@ -4259,7 +4281,7 @@ class DecoratorContext extends Map {
4259
4281
  }
4260
4282
 
4261
4283
  build() {
4262
- const decorator = (...args) => this.onInit(args, decorator);
4284
+ const decorator = (...args) => this.bind(args, decorator);
4263
4285
 
4264
4286
  const wrap = cb => {
4265
4287
  return (...args) => {
@@ -4302,6 +4324,24 @@ class DecoratorContext extends Map {
4302
4324
  return _extends({}, defaultValue || {}, value);
4303
4325
  }
4304
4326
 
4327
+ bind(args, decorator) {
4328
+ this.entity = JsonEntityStore.from(...args);
4329
+ this.beforeInit();
4330
+ this.onInit(args, decorator);
4331
+ this.afterInit();
4332
+ this.clear();
4333
+ }
4334
+
4335
+ onInit(args, decorator) {
4336
+ this.forEach((value, key) => {
4337
+ this.onMapKey(key, value);
4338
+ });
4339
+ }
4340
+
4341
+ onMapKey(key, value) {
4342
+ this.entity.store.set(key, value);
4343
+ }
4344
+
4305
4345
  runActions() {
4306
4346
  this.actions.forEach(action => {
4307
4347
  action(this);
@@ -4309,6 +4349,10 @@ class DecoratorContext extends Map {
4309
4349
  return this;
4310
4350
  }
4311
4351
 
4352
+ beforeInit() {}
4353
+
4354
+ afterInit() {}
4355
+
4312
4356
  }
4313
4357
 
4314
4358
  /**
@@ -4470,9 +4514,9 @@ class ReturnDecoratorContext extends DecoratorContext {
4470
4514
 
4471
4515
  switch (type) {
4472
4516
  case core.DecoratorTypes.METHOD:
4473
- this.store = JsonEntityStore.from(...args);
4517
+ this.entity = JsonEntityStore.from(...args);
4474
4518
 
4475
- if (this.store.operation) {
4519
+ if (this.entity.operation) {
4476
4520
  this.map();
4477
4521
  }
4478
4522
 
@@ -4493,10 +4537,10 @@ class ReturnDecoratorContext extends DecoratorContext {
4493
4537
  map() {
4494
4538
  const model = this.get("model");
4495
4539
  const {
4496
- store,
4540
+ entity,
4497
4541
  decoratorType
4498
4542
  } = this;
4499
- const operation = this.store.operation;
4543
+ const operation = this.entity.operation;
4500
4544
  const currentStatus = this.getStatus();
4501
4545
  const response = operation.ensureResponseOf(currentStatus);
4502
4546
  const contentType = this.getContentType();
@@ -4519,7 +4563,7 @@ class ReturnDecoratorContext extends DecoratorContext {
4519
4563
 
4520
4564
  if (isSuccessStatus(this.get("status")) || currentStatus === "default") {
4521
4565
  if (model) {
4522
- store.type = model;
4566
+ entity.type = model;
4523
4567
  }
4524
4568
  } // additional info for OS2
4525
4569
 
@@ -4690,19 +4734,6 @@ function OperationId(operationId) {
4690
4734
  });
4691
4735
  }
4692
4736
 
4693
- exports.OperationMethods = void 0;
4694
-
4695
- (function (OperationMethods) {
4696
- OperationMethods["ALL"] = "ALL";
4697
- OperationMethods["GET"] = "GET";
4698
- OperationMethods["POST"] = "POST";
4699
- OperationMethods["PUT"] = "PUT";
4700
- OperationMethods["PATCH"] = "PATCH";
4701
- OperationMethods["HEAD"] = "HEAD";
4702
- OperationMethods["DELETE"] = "DELETE";
4703
- OperationMethods["OPTIONS"] = "OPTIONS";
4704
- OperationMethods["CUSTOM"] = "CUSTOM";
4705
- })(exports.OperationMethods || (exports.OperationMethods = {}));
4706
4737
  /**
4707
4738
  * Declare new Operation with his path and http method.
4708
4739
  *
@@ -4723,7 +4754,6 @@ exports.OperationMethods = void 0;
4723
4754
  * @operation
4724
4755
  */
4725
4756
 
4726
-
4727
4757
  function OperationPath(method, path = "/") {
4728
4758
  return JsonEntityFn((store, args) => {
4729
4759
  if (store.decoratorType !== core.DecoratorTypes.METHOD) {
@@ -5231,6 +5261,228 @@ function Location(location, meta) {
5231
5261
  return Returns().Location(location, meta);
5232
5262
  }
5233
5263
 
5264
+ function mapRouteOptions(args) {
5265
+ let method = undefined;
5266
+ let path = undefined;
5267
+ const handlers = args.filter(arg => {
5268
+ if (typeof arg === "string" && HTTP_METHODS.includes(arg.toLowerCase())) {
5269
+ method = arg.toLocaleUpperCase();
5270
+ return false;
5271
+ }
5272
+
5273
+ if (typeof arg === "string" || arg instanceof RegExp) {
5274
+ path = arg || "/";
5275
+ return false;
5276
+ }
5277
+
5278
+ return !!arg;
5279
+ });
5280
+ return {
5281
+ path,
5282
+ method,
5283
+ handlers
5284
+ };
5285
+ }
5286
+
5287
+ class RouteDecoratorContext extends DecoratorContext {
5288
+ constructor() {
5289
+ super(...arguments);
5290
+ this.methods = ["name", "description", "summary", "method", "id", "handlers"];
5291
+ }
5292
+
5293
+ beforeInit() {
5294
+ const path = this.get("path");
5295
+ const method = exports.OperationMethods[this.get("method")] || exports.OperationMethods.CUSTOM;
5296
+ path && this.entity.operation.addOperationPath(method, path);
5297
+ }
5298
+
5299
+ onMapKey(key, value) {
5300
+ switch (key) {
5301
+ case "name":
5302
+ case "id":
5303
+ this.entity.operation.operationId(value);
5304
+ return;
5305
+
5306
+ case "summary":
5307
+ this.entity.operation.summary(value);
5308
+ return;
5309
+
5310
+ case "description":
5311
+ this.entity.operation.description(value);
5312
+ return;
5313
+
5314
+ case "handlers":
5315
+ if (core.isFunction(this.entity.use)) {
5316
+ this.entity.use(value);
5317
+ return;
5318
+ }
5319
+
5320
+ break;
5321
+ }
5322
+
5323
+ return super.onMapKey(key, value);
5324
+ }
5325
+
5326
+ }
5327
+
5328
+ function Route(...args) {
5329
+ const routeOptions = mapRouteOptions(args);
5330
+ const context = new RouteDecoratorContext(routeOptions);
5331
+ return context.build();
5332
+ }
5333
+ /**
5334
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5335
+ *
5336
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5337
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5338
+ * all routes from that point on would require authentication, and automatically load a user.
5339
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5340
+ * to continue matching subsequent routes.
5341
+ * @param path
5342
+ * @param args
5343
+ * @returns {Function}
5344
+ * @decorator
5345
+ * @operation
5346
+ * @httpMethod
5347
+ */
5348
+
5349
+ function All(path = "/", ...args) {
5350
+ return Route(...[exports.OperationMethods.ALL, path].concat(args));
5351
+ }
5352
+ /**
5353
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5354
+ *
5355
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5356
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5357
+ * all routes from that point on would require authentication, and automatically load a user.
5358
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5359
+ * to continue matching subsequent routes.
5360
+ * @param path
5361
+ * @param args
5362
+ * @returns {Function}
5363
+ * @decorator
5364
+ * @operation
5365
+ * @httpMethod
5366
+ */
5367
+
5368
+ function Get(path = "/", ...args) {
5369
+ return Route(...[exports.OperationMethods.GET, path].concat(args));
5370
+ }
5371
+ /**
5372
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5373
+ *
5374
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5375
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5376
+ * all routes from that point on would require authentication, and automatically load a user.
5377
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5378
+ * to continue matching subsequent routes.
5379
+ * @param path
5380
+ * @param args
5381
+ * @returns {Function}
5382
+ * @decorator
5383
+ * @operation
5384
+ * @httpMethod
5385
+ */
5386
+
5387
+ function Post(path = "/", ...args) {
5388
+ return Route(...[exports.OperationMethods.POST, path].concat(args));
5389
+ }
5390
+ /**
5391
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5392
+ *
5393
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5394
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5395
+ * all routes from that point on would require authentication, and automatically load a user.
5396
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5397
+ * to continue matching subsequent routes.
5398
+ * @param path
5399
+ * @param args
5400
+ * @returns {Function}
5401
+ * @decorator
5402
+ * @operation
5403
+ * @httpMethod
5404
+ */
5405
+
5406
+ function Put(path = "/", ...args) {
5407
+ return Route(...[exports.OperationMethods.PUT, path].concat(args));
5408
+ }
5409
+ /**
5410
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5411
+ *
5412
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5413
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5414
+ * all routes from that point on would require authentication, and automatically load a user.
5415
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5416
+ * to continue matching subsequent routes.
5417
+ * @param path
5418
+ * @param args
5419
+ * @returns {Function}
5420
+ * @decorator
5421
+ * @operation
5422
+ * @httpMethod
5423
+ */
5424
+
5425
+ function Delete(path = "/", ...args) {
5426
+ return Route(...[exports.OperationMethods.DELETE, path].concat(args));
5427
+ }
5428
+ /**
5429
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5430
+ *
5431
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5432
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5433
+ * all routes from that point on would require authentication, and automatically load a user.
5434
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5435
+ * to continue matching subsequent routes.
5436
+ * @param path
5437
+ * @param args
5438
+ * @returns {Function}
5439
+ * @decorator
5440
+ * @operation
5441
+ * @httpMethod
5442
+ */
5443
+
5444
+ function Head(path = "/", ...args) {
5445
+ return Route(...[exports.OperationMethods.HEAD, path].concat(args));
5446
+ }
5447
+ /**
5448
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5449
+ *
5450
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5451
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5452
+ * all routes from that point on would require authentication, and automatically load a user.
5453
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5454
+ * to continue matching subsequent routes.
5455
+ * @param path
5456
+ * @param args
5457
+ * @returns {Function}
5458
+ * @decorator
5459
+ * @operation
5460
+ * @httpMethod
5461
+ */
5462
+
5463
+ function Patch(path = "/", ...args) {
5464
+ return Route(...[exports.OperationMethods.PATCH, path].concat(args));
5465
+ }
5466
+ /**
5467
+ * This method is just like the `router.METHOD()` methods, except that it matches all HTTP methods (verbs).
5468
+ *
5469
+ * This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches.
5470
+ * For example, if you placed the following route at the top of all other route definitions, it would require that
5471
+ * all routes from that point on would require authentication, and automatically load a user.
5472
+ * Keep in mind that these callbacks do not have to act as end points; loadUser can perform a task, then call next()
5473
+ * to continue matching subsequent routes.
5474
+ * @param path
5475
+ * @param args
5476
+ * @returns {Function}
5477
+ * @decorator
5478
+ * @operation
5479
+ * @httpMethod
5480
+ */
5481
+
5482
+ function Options(path = "/", ...args) {
5483
+ return Route(...[exports.OperationMethods.OPTIONS, path].concat(args));
5484
+ }
5485
+
5234
5486
  /**
5235
5487
  * Accept unknown properties on the deserialized model.
5236
5488
  *
@@ -7495,6 +7747,7 @@ function JsonEntityComponent(type) {
7495
7747
  }
7496
7748
 
7497
7749
  exports.AdditionalProperties = AdditionalProperties;
7750
+ exports.All = All;
7498
7751
  exports.AllOf = AllOf;
7499
7752
  exports.Allow = Allow;
7500
7753
  exports.Any = Any;
@@ -7510,6 +7763,7 @@ exports.CustomKeys = CustomKeys;
7510
7763
  exports.DateFormat = DateFormat;
7511
7764
  exports.DateTime = DateTime;
7512
7765
  exports.Default = Default;
7766
+ exports.Delete = Delete;
7513
7767
  exports.Deprecated = Deprecated;
7514
7768
  exports.Description = Description;
7515
7769
  exports.Email = Email;
@@ -7522,8 +7776,11 @@ exports.Format = Format;
7522
7776
  exports.ForwardGroups = ForwardGroups;
7523
7777
  exports.GenericOf = GenericOf;
7524
7778
  exports.Generics = Generics;
7779
+ exports.Get = Get;
7525
7780
  exports.Groups = Groups;
7781
+ exports.HTTP_METHODS = HTTP_METHODS;
7526
7782
  exports.HTTP_STATUS_MESSAGES = HTTP_STATUS_MESSAGES;
7783
+ exports.Head = Head;
7527
7784
  exports.Header = Header;
7528
7785
  exports.Hidden = Hidden;
7529
7786
  exports.Ignore = Ignore;
@@ -7560,16 +7817,21 @@ exports.OneOf = OneOf;
7560
7817
  exports.OperationId = OperationId;
7561
7818
  exports.OperationPath = OperationPath;
7562
7819
  exports.Optional = Optional;
7820
+ exports.Options = Options;
7563
7821
  exports.Partial = Partial;
7822
+ exports.Patch = Patch;
7564
7823
  exports.Path = Path;
7565
7824
  exports.Pattern = Pattern;
7825
+ exports.Post = Post;
7566
7826
  exports.Produces = Produces;
7567
7827
  exports.Property = Property;
7828
+ exports.Put = Put;
7568
7829
  exports.ReadOnly = ReadOnly;
7569
7830
  exports.Redirect = Redirect;
7570
7831
  exports.Required = Required;
7571
7832
  exports.RequiredGroups = RequiredGroups;
7572
7833
  exports.Returns = Returns;
7834
+ exports.Route = Route;
7573
7835
  exports.Schema = Schema;
7574
7836
  exports.Security = Security;
7575
7837
  exports.Status = Status;