@opra/common 0.31.6 → 0.31.8

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/browser.js CHANGED
@@ -616,224 +616,6 @@ var FailedDependencyError = class extends OpraException {
616
616
  }
617
617
  };
618
618
 
619
- // ../../build/common/esm/exception/http-errors/forbidden.error.js
620
- var ForbiddenError = class extends OpraException {
621
- static {
622
- __name(this, "ForbiddenError");
623
- }
624
- constructor() {
625
- super(...arguments);
626
- this.status = 403;
627
- }
628
- init(issue) {
629
- super.init({
630
- message: translate("error:FORBIDDEN", "You are not authorized to perform this action"),
631
- code: "FORBIDDEN",
632
- ...issue
633
- });
634
- }
635
- };
636
-
637
- // ../../build/common/esm/exception/http-errors/internal-server.error.js
638
- var InternalServerError = class extends OpraException {
639
- static {
640
- __name(this, "InternalServerError");
641
- }
642
- constructor() {
643
- super(...arguments);
644
- this.status = 500;
645
- }
646
- init(issue) {
647
- super.init({
648
- message: translate("error:INTERNAL_SERVER_ERROR", "Internal server error"),
649
- code: "INTERNAL_SERVER_ERROR",
650
- severity: "fatal",
651
- ...issue
652
- });
653
- }
654
- };
655
-
656
- // ../../build/common/esm/exception/http-errors/method-not-allowed.error.js
657
- var MethodNotAllowedError = class extends OpraException {
658
- static {
659
- __name(this, "MethodNotAllowedError");
660
- }
661
- constructor() {
662
- super(...arguments);
663
- this.status = 405;
664
- }
665
- init(issue) {
666
- super.init({
667
- message: translate("error:METHOD_NOT_ALLOWED", "Method not allowed"),
668
- code: "METHOD_NOT_ALLOWED",
669
- ...issue
670
- });
671
- }
672
- };
673
-
674
- // ../../build/common/esm/exception/http-errors/not-acceptable.error.js
675
- var NotAcceptableError = class extends OpraException {
676
- static {
677
- __name(this, "NotAcceptableError");
678
- }
679
- constructor() {
680
- super(...arguments);
681
- this.status = 406;
682
- }
683
- init(issue) {
684
- super.init({
685
- message: translate("error:NOT_ACCEPTABLE", "Not Acceptable"),
686
- code: "NOT_ACCEPTABLE",
687
- ...issue
688
- });
689
- }
690
- };
691
-
692
- // ../../build/common/esm/exception/http-errors/not-found.error.js
693
- var NotFoundError = class extends OpraException {
694
- static {
695
- __name(this, "NotFoundError");
696
- }
697
- constructor() {
698
- super(...arguments);
699
- this.status = 404;
700
- }
701
- init(issue) {
702
- super.init({
703
- message: translate("error:NOT_FOUND", "Not found"),
704
- code: "NOT_FOUND",
705
- ...issue
706
- });
707
- }
708
- };
709
-
710
- // ../../build/common/esm/exception/http-errors/unauthorized.error.js
711
- var UnauthorizedError = class extends OpraException {
712
- static {
713
- __name(this, "UnauthorizedError");
714
- }
715
- constructor() {
716
- super(...arguments);
717
- this.status = 401;
718
- }
719
- init(issue) {
720
- super.init({
721
- message: translate("error:UNAUTHORIZED", "You have not been authenticated to perform this action"),
722
- code: "UNAUTHORIZED",
723
- ...issue
724
- });
725
- }
726
- };
727
-
728
- // ../../build/common/esm/exception/http-errors/unprocessable-entity.error.js
729
- var UnprocessableEntityError = class extends OpraException {
730
- static {
731
- __name(this, "UnprocessableEntityError");
732
- }
733
- constructor() {
734
- super(...arguments);
735
- this.status = 422;
736
- }
737
- init(issue) {
738
- super.init({
739
- message: translate("error:UNPROCESSABLE_ENTITY", "Unprocessable entity"),
740
- severity: "error",
741
- code: "UNPROCESSABLE_ENTITY",
742
- ...issue
743
- });
744
- }
745
- };
746
-
747
- // ../../build/common/esm/exception/wrap-exception.js
748
- function wrapException(error) {
749
- if (error instanceof OpraException)
750
- return error;
751
- let status = 500;
752
- if (typeof error.status === "number")
753
- status = error.status;
754
- else if (typeof error.getStatus === "function")
755
- status = error.getStatus();
756
- switch (status) {
757
- case 400:
758
- return new BadRequestError(error);
759
- case 401:
760
- return new UnauthorizedError(error);
761
- case 403:
762
- return new ForbiddenError(error);
763
- case 404:
764
- return new NotFoundError(error);
765
- case 405:
766
- return new MethodNotAllowedError(error);
767
- case 406:
768
- return new NotAcceptableError(error);
769
- case 422:
770
- return new UnprocessableEntityError(error);
771
- case 424:
772
- return new FailedDependencyError(error);
773
- default:
774
- return new InternalServerError(error);
775
- }
776
- }
777
- __name(wrapException, "wrapException");
778
-
779
- // ../../build/common/esm/exception/issue-severity.enum.js
780
- var IssueSeverity;
781
- (function(IssueSeverity2) {
782
- let Enum;
783
- (function(Enum2) {
784
- Enum2["fatal"] = "fatal";
785
- Enum2["error"] = "error";
786
- Enum2["warning"] = "warning";
787
- Enum2["info"] = "info";
788
- })(Enum = IssueSeverity2.Enum || (IssueSeverity2.Enum = {}));
789
- IssueSeverity2.name = "IssueSeverity";
790
- IssueSeverity2.description = "Severity of the issue";
791
- IssueSeverity2.Keys = Object.keys(IssueSeverity2);
792
- IssueSeverity2.descriptions = {
793
- fatal: "The issue caused the action to fail and no further checking could be performed",
794
- error: "The issue is sufficiently important to cause the action to fail",
795
- warning: "The issue is not important enough to cause the action to fail but may cause it to be performed suboptimally or in a way that is not as desired",
796
- info: "The issue has no relation to the degree of success of the action"
797
- };
798
- })(IssueSeverity || (IssueSeverity = {}));
799
-
800
- // ../../build/common/esm/exception/http-errors/conflict.error.js
801
- var ConflictError = class extends OpraException {
802
- static {
803
- __name(this, "ConflictError");
804
- }
805
- constructor() {
806
- super(...arguments);
807
- this.status = 409;
808
- }
809
- init(issue) {
810
- super.init({
811
- message: translate("error:CONFLICT", "Conflict"),
812
- code: "CONFLICT",
813
- ...issue
814
- });
815
- }
816
- };
817
-
818
- // ../../build/common/esm/exception/resource-errors/resource-conflict.error.js
819
- var ResourceConflictError = class extends OpraException {
820
- static {
821
- __name(this, "ResourceConflictError");
822
- }
823
- constructor(resource, fields, cause) {
824
- super({
825
- message: translate(`error:RESOURCE_CONFLICT`, { resource, fields }, `There is already an other {{resource}} resource with same field values ({{fields}})`),
826
- severity: "error",
827
- code: "RESOURCE_CONFLICT",
828
- details: {
829
- resource,
830
- location: fields
831
- }
832
- }, cause);
833
- this.status = 409;
834
- }
835
- };
836
-
837
619
  // ../../build/common/esm/http/opra-url.js
838
620
  import { splitString as splitString3, tokenize as tokenize3 } from "fast-tokenizer";
839
621
 
@@ -1546,6 +1328,224 @@ var HttpStatusMessages = {
1546
1328
  "511": "Network Authentication Required"
1547
1329
  };
1548
1330
 
1331
+ // ../../build/common/esm/exception/http-errors/forbidden.error.js
1332
+ var ForbiddenError = class extends OpraException {
1333
+ static {
1334
+ __name(this, "ForbiddenError");
1335
+ }
1336
+ constructor() {
1337
+ super(...arguments);
1338
+ this.status = HttpStatusCodes.FORBIDDEN;
1339
+ }
1340
+ init(issue) {
1341
+ super.init({
1342
+ message: translate("error:FORBIDDEN", "You are not authorized to perform this action"),
1343
+ code: "FORBIDDEN",
1344
+ ...issue
1345
+ });
1346
+ }
1347
+ };
1348
+
1349
+ // ../../build/common/esm/exception/http-errors/internal-server.error.js
1350
+ var InternalServerError = class extends OpraException {
1351
+ static {
1352
+ __name(this, "InternalServerError");
1353
+ }
1354
+ constructor() {
1355
+ super(...arguments);
1356
+ this.status = 500;
1357
+ }
1358
+ init(issue) {
1359
+ super.init({
1360
+ message: translate("error:INTERNAL_SERVER_ERROR", "Internal server error"),
1361
+ code: "INTERNAL_SERVER_ERROR",
1362
+ severity: "fatal",
1363
+ ...issue
1364
+ });
1365
+ }
1366
+ };
1367
+
1368
+ // ../../build/common/esm/exception/http-errors/method-not-allowed.error.js
1369
+ var MethodNotAllowedError = class extends OpraException {
1370
+ static {
1371
+ __name(this, "MethodNotAllowedError");
1372
+ }
1373
+ constructor() {
1374
+ super(...arguments);
1375
+ this.status = 405;
1376
+ }
1377
+ init(issue) {
1378
+ super.init({
1379
+ message: translate("error:METHOD_NOT_ALLOWED", "Method not allowed"),
1380
+ code: "METHOD_NOT_ALLOWED",
1381
+ ...issue
1382
+ });
1383
+ }
1384
+ };
1385
+
1386
+ // ../../build/common/esm/exception/http-errors/not-acceptable.error.js
1387
+ var NotAcceptableError = class extends OpraException {
1388
+ static {
1389
+ __name(this, "NotAcceptableError");
1390
+ }
1391
+ constructor() {
1392
+ super(...arguments);
1393
+ this.status = 406;
1394
+ }
1395
+ init(issue) {
1396
+ super.init({
1397
+ message: translate("error:NOT_ACCEPTABLE", "Not Acceptable"),
1398
+ code: "NOT_ACCEPTABLE",
1399
+ ...issue
1400
+ });
1401
+ }
1402
+ };
1403
+
1404
+ // ../../build/common/esm/exception/http-errors/not-found.error.js
1405
+ var NotFoundError = class extends OpraException {
1406
+ static {
1407
+ __name(this, "NotFoundError");
1408
+ }
1409
+ constructor() {
1410
+ super(...arguments);
1411
+ this.status = 404;
1412
+ }
1413
+ init(issue) {
1414
+ super.init({
1415
+ message: translate("error:NOT_FOUND", "Not found"),
1416
+ code: "NOT_FOUND",
1417
+ ...issue
1418
+ });
1419
+ }
1420
+ };
1421
+
1422
+ // ../../build/common/esm/exception/http-errors/unauthorized.error.js
1423
+ var UnauthorizedError = class extends OpraException {
1424
+ static {
1425
+ __name(this, "UnauthorizedError");
1426
+ }
1427
+ constructor() {
1428
+ super(...arguments);
1429
+ this.status = 401;
1430
+ }
1431
+ init(issue) {
1432
+ super.init({
1433
+ message: translate("error:UNAUTHORIZED", "You have not been authenticated to perform this action"),
1434
+ code: "UNAUTHORIZED",
1435
+ ...issue
1436
+ });
1437
+ }
1438
+ };
1439
+
1440
+ // ../../build/common/esm/exception/http-errors/unprocessable-entity.error.js
1441
+ var UnprocessableEntityError = class extends OpraException {
1442
+ static {
1443
+ __name(this, "UnprocessableEntityError");
1444
+ }
1445
+ constructor() {
1446
+ super(...arguments);
1447
+ this.status = 422;
1448
+ }
1449
+ init(issue) {
1450
+ super.init({
1451
+ message: translate("error:UNPROCESSABLE_ENTITY", "Unprocessable entity"),
1452
+ severity: "error",
1453
+ code: "UNPROCESSABLE_ENTITY",
1454
+ ...issue
1455
+ });
1456
+ }
1457
+ };
1458
+
1459
+ // ../../build/common/esm/exception/wrap-exception.js
1460
+ function wrapException(error) {
1461
+ if (error instanceof OpraException)
1462
+ return error;
1463
+ let status = 500;
1464
+ if (typeof error.status === "number")
1465
+ status = error.status;
1466
+ else if (typeof error.getStatus === "function")
1467
+ status = error.getStatus();
1468
+ switch (status) {
1469
+ case 400:
1470
+ return new BadRequestError(error);
1471
+ case 401:
1472
+ return new UnauthorizedError(error);
1473
+ case 403:
1474
+ return new ForbiddenError(error);
1475
+ case 404:
1476
+ return new NotFoundError(error);
1477
+ case 405:
1478
+ return new MethodNotAllowedError(error);
1479
+ case 406:
1480
+ return new NotAcceptableError(error);
1481
+ case 422:
1482
+ return new UnprocessableEntityError(error);
1483
+ case 424:
1484
+ return new FailedDependencyError(error);
1485
+ default:
1486
+ return new InternalServerError(error);
1487
+ }
1488
+ }
1489
+ __name(wrapException, "wrapException");
1490
+
1491
+ // ../../build/common/esm/exception/issue-severity.enum.js
1492
+ var IssueSeverity;
1493
+ (function(IssueSeverity2) {
1494
+ let Enum;
1495
+ (function(Enum2) {
1496
+ Enum2["fatal"] = "fatal";
1497
+ Enum2["error"] = "error";
1498
+ Enum2["warning"] = "warning";
1499
+ Enum2["info"] = "info";
1500
+ })(Enum = IssueSeverity2.Enum || (IssueSeverity2.Enum = {}));
1501
+ IssueSeverity2.name = "IssueSeverity";
1502
+ IssueSeverity2.description = "Severity of the issue";
1503
+ IssueSeverity2.Keys = Object.keys(IssueSeverity2);
1504
+ IssueSeverity2.descriptions = {
1505
+ fatal: "The issue caused the action to fail and no further checking could be performed",
1506
+ error: "The issue is sufficiently important to cause the action to fail",
1507
+ warning: "The issue is not important enough to cause the action to fail but may cause it to be performed suboptimally or in a way that is not as desired",
1508
+ info: "The issue has no relation to the degree of success of the action"
1509
+ };
1510
+ })(IssueSeverity || (IssueSeverity = {}));
1511
+
1512
+ // ../../build/common/esm/exception/http-errors/conflict.error.js
1513
+ var ConflictError = class extends OpraException {
1514
+ static {
1515
+ __name(this, "ConflictError");
1516
+ }
1517
+ constructor() {
1518
+ super(...arguments);
1519
+ this.status = 409;
1520
+ }
1521
+ init(issue) {
1522
+ super.init({
1523
+ message: translate("error:CONFLICT", "Conflict"),
1524
+ code: "CONFLICT",
1525
+ ...issue
1526
+ });
1527
+ }
1528
+ };
1529
+
1530
+ // ../../build/common/esm/exception/resource-errors/resource-conflict.error.js
1531
+ var ResourceConflictError = class extends OpraException {
1532
+ static {
1533
+ __name(this, "ResourceConflictError");
1534
+ }
1535
+ constructor(resource, fields, cause) {
1536
+ super({
1537
+ message: translate(`error:RESOURCE_CONFLICT`, { resource, fields }, `There is already an other {{resource}} resource with same field values ({{fields}})`),
1538
+ severity: "error",
1539
+ code: "RESOURCE_CONFLICT",
1540
+ details: {
1541
+ resource,
1542
+ location: fields
1543
+ }
1544
+ }, cause);
1545
+ this.status = 409;
1546
+ }
1547
+ };
1548
+
1549
1549
  // ../../build/common/esm/exception/resource-errors/resource-not-found.error.js
1550
1550
  var ResourceNotFoundError = class extends OpraException {
1551
1551
  static {
@@ -3085,21 +3085,29 @@ var MappedTypeClass = class extends ComplexTypeClass {
3085
3085
  const own = this.own;
3086
3086
  own.pick = init.pick;
3087
3087
  own.omit = init.omit;
3088
+ own.partial = init.partial;
3088
3089
  this.kind = opra_schema_ns_exports.MappedType.Kind;
3089
3090
  this.pick = own.pick;
3090
3091
  this.omit = own.omit;
3092
+ this.partial = own.partial;
3091
3093
  const isInheritedPredicate = getIsInheritedPredicateFn(init.pick, init.omit);
3092
3094
  for (const fieldName of this.fields.keys()) {
3093
3095
  if (!isInheritedPredicate(fieldName)) {
3094
3096
  this.fields.delete(fieldName);
3095
3097
  }
3098
+ if (this.partial === true || Array.isArray(this.partial) && this.partial.includes(fieldName)) {
3099
+ const f = this.fields.get(fieldName);
3100
+ if (f)
3101
+ f.required = false;
3102
+ }
3096
3103
  }
3097
3104
  }
3098
3105
  exportSchema() {
3099
3106
  const out = super.exportSchema();
3100
3107
  Object.assign(out, omitUndefined({
3101
3108
  pick: this.own.pick,
3102
- omit: this.own.omit
3109
+ omit: this.own.omit,
3110
+ partial: this.own.partial
3103
3111
  }));
3104
3112
  return out;
3105
3113
  }
@@ -3126,6 +3134,10 @@ function OmitType(classRef, keys) {
3126
3134
  return MappedType2(classRef, { omit: keys });
3127
3135
  }
3128
3136
  __name(OmitType, "OmitType");
3137
+ function PartialType(classRef, keys) {
3138
+ return MappedType2(classRef, { partial: keys || true });
3139
+ }
3140
+ __name(PartialType, "PartialType");
3129
3141
  var MappedType2 = /* @__PURE__ */ __name(function(...args) {
3130
3142
  if (this) {
3131
3143
  const [document, init] = args;
@@ -3146,7 +3158,7 @@ var MappedType2 = /* @__PURE__ */ __name(function(...args) {
3146
3158
  const m = Reflect.getOwnMetadata(DATATYPE_METADATA, mappedSource);
3147
3159
  if (!m)
3148
3160
  throw new TypeError(`Class "${mappedSource}" doesn't have datatype metadata information`);
3149
- if (!(m.kind === opra_schema_ns_exports.ComplexType.Kind))
3161
+ if (!(m.kind === opra_schema_ns_exports.ComplexType.Kind || m.kind === opra_schema_ns_exports.MappedType.Kind || m.kind === opra_schema_ns_exports.MixinType.Kind))
3150
3162
  throw new TypeError(`Class "${mappedSource}" is not a ${opra_schema_ns_exports.ComplexType.Kind}`);
3151
3163
  const metadata = {
3152
3164
  kind: "MappedType",
@@ -3156,6 +3168,8 @@ var MappedType2 = /* @__PURE__ */ __name(function(...args) {
3156
3168
  metadata.pick = options.pick;
3157
3169
  if (options.omit)
3158
3170
  metadata.omit = options.omit;
3171
+ if (options.partial)
3172
+ metadata.partial = options.partial;
3159
3173
  Reflect.defineMetadata(DATATYPE_METADATA, metadata, MappedClass);
3160
3174
  MappedType2._applyMixin(MappedClass, mappedSource, {
3161
3175
  ...options,
@@ -11256,8 +11270,13 @@ var CollectionClass = class extends CrudResource {
11256
11270
  }, {}) : value;
11257
11271
  for (const [k, v] of Object.entries(obj)) {
11258
11272
  const el = dataType.getField(k);
11259
- if (el.type instanceof SimpleType2)
11260
- obj[k] = el.type.decode(v);
11273
+ if (el.type instanceof SimpleType2) {
11274
+ try {
11275
+ obj[k] = el.type.decode(v, { coerce: true });
11276
+ } catch (e) {
11277
+ e.message = `The key value for field "${el.name}" is not valid. ` + e.message;
11278
+ }
11279
+ }
11261
11280
  if (obj[k] == null)
11262
11281
  throw new TypeError(`You must provide value of primary field(s) (${k})`);
11263
11282
  }
@@ -11267,8 +11286,13 @@ var CollectionClass = class extends CrudResource {
11267
11286
  value = value[primaryKey];
11268
11287
  const el = dataType.getField(primaryKey);
11269
11288
  let result;
11270
- if (value != null && el.type instanceof SimpleType2)
11271
- result = el.type.decode(value);
11289
+ if (value != null && el.type instanceof SimpleType2) {
11290
+ try {
11291
+ result = el.type.decode(value, { coerce: true });
11292
+ } catch (e) {
11293
+ e.message = `The key value is not valid. ` + e.message;
11294
+ }
11295
+ }
11272
11296
  if (result == null)
11273
11297
  throw new TypeError(`You must provide value of primary field(s) (${primaryKey})`);
11274
11298
  return result;
@@ -12150,6 +12174,7 @@ export {
12150
12174
  OpraURLPath,
12151
12175
  OpraURLPathComponent,
12152
12176
  Parameter,
12177
+ PartialType,
12153
12178
  PickType,
12154
12179
  RESOURCE_METADATA,
12155
12180
  Resource,
@@ -11,14 +11,21 @@ class MappedTypeClass extends complex_type_class_js_1.ComplexTypeClass {
11
11
  const own = this.own;
12
12
  own.pick = init.pick;
13
13
  own.omit = init.omit;
14
+ own.partial = init.partial;
14
15
  this.kind = index_js_2.OpraSchema.MappedType.Kind;
15
16
  this.pick = own.pick;
16
17
  this.omit = own.omit;
18
+ this.partial = own.partial;
17
19
  const isInheritedPredicate = getIsInheritedPredicateFn(init.pick, init.omit);
18
20
  for (const fieldName of this.fields.keys()) {
19
21
  if (!isInheritedPredicate(fieldName)) {
20
22
  this.fields.delete(fieldName);
21
23
  }
24
+ if (this.partial === true || (Array.isArray(this.partial) && this.partial.includes(fieldName))) {
25
+ const f = this.fields.get(fieldName);
26
+ if (f)
27
+ f.required = false;
28
+ }
22
29
  }
23
30
  }
24
31
  exportSchema() {
@@ -26,6 +33,7 @@ class MappedTypeClass extends complex_type_class_js_1.ComplexTypeClass {
26
33
  Object.assign(out, (0, index_js_1.omitUndefined)({
27
34
  pick: this.own.pick,
28
35
  omit: this.own.omit,
36
+ partial: this.own.partial
29
37
  }));
30
38
  return out;
31
39
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MappedType = exports.OmitType = exports.PickType = void 0;
3
+ exports.MappedType = exports.PartialType = exports.OmitType = exports.PickType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  require("reflect-metadata");
6
6
  const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
@@ -22,6 +22,13 @@ function OmitType(classRef, keys) {
22
22
  return (0, exports.MappedType)(classRef, { omit: keys });
23
23
  }
24
24
  exports.OmitType = OmitType;
25
+ /**
26
+ *
27
+ */
28
+ function PartialType(classRef, keys) {
29
+ return (0, exports.MappedType)(classRef, { partial: keys || true });
30
+ }
31
+ exports.PartialType = PartialType;
25
32
  /**
26
33
  * @class MappedType
27
34
  */
@@ -45,7 +52,7 @@ exports.MappedType = function (...args) {
45
52
  const m = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, mappedSource);
46
53
  if (!m)
47
54
  throw new TypeError(`Class "${mappedSource}" doesn't have datatype metadata information`);
48
- if (!(m.kind === index_js_2.OpraSchema.ComplexType.Kind))
55
+ if (!(m.kind === index_js_2.OpraSchema.ComplexType.Kind || m.kind === index_js_2.OpraSchema.MappedType.Kind || m.kind === index_js_2.OpraSchema.MixinType.Kind))
49
56
  throw new TypeError(`Class "${mappedSource}" is not a ${index_js_2.OpraSchema.ComplexType.Kind}`);
50
57
  const metadata = {
51
58
  kind: 'MappedType',
@@ -55,6 +62,8 @@ exports.MappedType = function (...args) {
55
62
  metadata.pick = options.pick;
56
63
  if (options.omit)
57
64
  metadata.omit = options.omit;
65
+ if (options.partial)
66
+ metadata.partial = options.partial;
58
67
  Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, MappedClass);
59
68
  exports.MappedType._applyMixin(MappedClass, mappedSource, {
60
69
  ...options,
@@ -152,8 +152,14 @@ class CollectionClass extends crud_resource_js_1.CrudResource {
152
152
  // decode values
153
153
  for (const [k, v] of Object.entries(obj)) {
154
154
  const el = dataType.getField(k);
155
- if (el.type instanceof simple_type_js_1.SimpleType)
156
- obj[k] = el.type.decode(v);
155
+ if (el.type instanceof simple_type_js_1.SimpleType) {
156
+ try {
157
+ obj[k] = el.type.decode(v, { coerce: true });
158
+ }
159
+ catch (e) {
160
+ e.message = `The key value for field "${el.name}" is not valid. ` + e.message;
161
+ }
162
+ }
157
163
  if (obj[k] == null)
158
164
  throw new TypeError(`You must provide value of primary field(s) (${k})`);
159
165
  }
@@ -164,8 +170,14 @@ class CollectionClass extends crud_resource_js_1.CrudResource {
164
170
  value = value[primaryKey];
165
171
  const el = dataType.getField(primaryKey);
166
172
  let result;
167
- if (value != null && el.type instanceof simple_type_js_1.SimpleType)
168
- result = el.type.decode(value);
173
+ if (value != null && el.type instanceof simple_type_js_1.SimpleType) {
174
+ try {
175
+ result = el.type.decode(value, { coerce: true });
176
+ }
177
+ catch (e) {
178
+ e.message = `The key value is not valid. ` + e.message;
179
+ }
180
+ }
169
181
  if (result == null)
170
182
  throw new TypeError(`You must provide value of primary field(s) (${primaryKey})`);
171
183
  return result;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ForbiddenError = void 0;
4
- const index_js_1 = require("../../i18n/index.js");
4
+ const index_js_1 = require("../../http/index.js");
5
+ const index_js_2 = require("../../i18n/index.js");
5
6
  const opra_exception_js_1 = require("../opra-exception.js");
6
7
  /**
7
8
  * 403 Forbidden
@@ -12,11 +13,11 @@ const opra_exception_js_1 = require("../opra-exception.js");
12
13
  class ForbiddenError extends opra_exception_js_1.OpraException {
13
14
  constructor() {
14
15
  super(...arguments);
15
- this.status = 403;
16
+ this.status = index_js_1.HttpStatusCodes.FORBIDDEN;
16
17
  }
17
18
  init(issue) {
18
19
  super.init({
19
- message: (0, index_js_1.translate)('error:FORBIDDEN', 'You are not authorized to perform this action'),
20
+ message: (0, index_js_2.translate)('error:FORBIDDEN', 'You are not authorized to perform this action'),
20
21
  code: 'FORBIDDEN',
21
22
  ...issue
22
23
  });
@@ -8,14 +8,21 @@ export class MappedTypeClass extends ComplexTypeClass {
8
8
  const own = this.own;
9
9
  own.pick = init.pick;
10
10
  own.omit = init.omit;
11
+ own.partial = init.partial;
11
12
  this.kind = OpraSchema.MappedType.Kind;
12
13
  this.pick = own.pick;
13
14
  this.omit = own.omit;
15
+ this.partial = own.partial;
14
16
  const isInheritedPredicate = getIsInheritedPredicateFn(init.pick, init.omit);
15
17
  for (const fieldName of this.fields.keys()) {
16
18
  if (!isInheritedPredicate(fieldName)) {
17
19
  this.fields.delete(fieldName);
18
20
  }
21
+ if (this.partial === true || (Array.isArray(this.partial) && this.partial.includes(fieldName))) {
22
+ const f = this.fields.get(fieldName);
23
+ if (f)
24
+ f.required = false;
25
+ }
19
26
  }
20
27
  }
21
28
  exportSchema() {
@@ -23,6 +30,7 @@ export class MappedTypeClass extends ComplexTypeClass {
23
30
  Object.assign(out, omitUndefined({
24
31
  pick: this.own.pick,
25
32
  omit: this.own.omit,
33
+ partial: this.own.partial
26
34
  }));
27
35
  return out;
28
36
  }
@@ -16,6 +16,12 @@ export function PickType(classRef, keys) {
16
16
  export function OmitType(classRef, keys) {
17
17
  return MappedType(classRef, { omit: keys });
18
18
  }
19
+ /**
20
+ *
21
+ */
22
+ export function PartialType(classRef, keys) {
23
+ return MappedType(classRef, { partial: keys || true });
24
+ }
19
25
  /**
20
26
  * @class MappedType
21
27
  */
@@ -39,7 +45,7 @@ export const MappedType = function (...args) {
39
45
  const m = Reflect.getOwnMetadata(DATATYPE_METADATA, mappedSource);
40
46
  if (!m)
41
47
  throw new TypeError(`Class "${mappedSource}" doesn't have datatype metadata information`);
42
- if (!(m.kind === OpraSchema.ComplexType.Kind))
48
+ if (!(m.kind === OpraSchema.ComplexType.Kind || m.kind === OpraSchema.MappedType.Kind || m.kind === OpraSchema.MixinType.Kind))
43
49
  throw new TypeError(`Class "${mappedSource}" is not a ${OpraSchema.ComplexType.Kind}`);
44
50
  const metadata = {
45
51
  kind: 'MappedType',
@@ -49,6 +55,8 @@ export const MappedType = function (...args) {
49
55
  metadata.pick = options.pick;
50
56
  if (options.omit)
51
57
  metadata.omit = options.omit;
58
+ if (options.partial)
59
+ metadata.partial = options.partial;
52
60
  Reflect.defineMetadata(DATATYPE_METADATA, metadata, MappedClass);
53
61
  MappedType._applyMixin(MappedClass, mappedSource, {
54
62
  ...options,
@@ -148,8 +148,14 @@ export class CollectionClass extends CrudResource {
148
148
  // decode values
149
149
  for (const [k, v] of Object.entries(obj)) {
150
150
  const el = dataType.getField(k);
151
- if (el.type instanceof SimpleType)
152
- obj[k] = el.type.decode(v);
151
+ if (el.type instanceof SimpleType) {
152
+ try {
153
+ obj[k] = el.type.decode(v, { coerce: true });
154
+ }
155
+ catch (e) {
156
+ e.message = `The key value for field "${el.name}" is not valid. ` + e.message;
157
+ }
158
+ }
153
159
  if (obj[k] == null)
154
160
  throw new TypeError(`You must provide value of primary field(s) (${k})`);
155
161
  }
@@ -160,8 +166,14 @@ export class CollectionClass extends CrudResource {
160
166
  value = value[primaryKey];
161
167
  const el = dataType.getField(primaryKey);
162
168
  let result;
163
- if (value != null && el.type instanceof SimpleType)
164
- result = el.type.decode(value);
169
+ if (value != null && el.type instanceof SimpleType) {
170
+ try {
171
+ result = el.type.decode(value, { coerce: true });
172
+ }
173
+ catch (e) {
174
+ e.message = `The key value is not valid. ` + e.message;
175
+ }
176
+ }
165
177
  if (result == null)
166
178
  throw new TypeError(`You must provide value of primary field(s) (${primaryKey})`);
167
179
  return result;
@@ -1,3 +1,4 @@
1
+ import { HttpStatusCodes } from '../../http/index.js';
1
2
  import { translate } from '../../i18n/index.js';
2
3
  import { OpraException } from '../opra-exception.js';
3
4
  /**
@@ -9,7 +10,7 @@ import { OpraException } from '../opra-exception.js';
9
10
  export class ForbiddenError extends OpraException {
10
11
  constructor() {
11
12
  super(...arguments);
12
- this.status = 403;
13
+ this.status = HttpStatusCodes.FORBIDDEN;
13
14
  }
14
15
  init(issue) {
15
16
  super.init({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "0.31.6",
3
+ "version": "0.31.8",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -46,7 +46,7 @@
46
46
  "putil-varhelpers": "^1.6.5",
47
47
  "reflect-metadata": "^0.1.13",
48
48
  "uid": "^2.0.1",
49
- "valgen": "^4.2.3"
49
+ "valgen": "^4.2.4"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@browsery/fs": "^0.4.0",
@@ -11,6 +11,7 @@ export declare class MappedTypeClass extends ComplexTypeClass {
11
11
  readonly base: ComplexType | MixinType | MappedType;
12
12
  readonly omit?: Field.Name[];
13
13
  readonly pick?: Field.Name[];
14
+ readonly partial?: Field.Name[] | boolean;
14
15
  constructor(document: ApiDocument, init: MappedType.InitArguments);
15
16
  exportSchema(): any;
16
17
  }
@@ -12,6 +12,10 @@ export declare function PickType<T extends any[], I1, S1, K extends keyof I1>(cl
12
12
  *
13
13
  */
14
14
  export declare function OmitType<T extends any[], I1, S1, K extends keyof I1>(classRef: Class<T, I1, S1>, keys: readonly K[]): Class<T, Omit<I1, K>> & Omit<Pick<S1, keyof typeof classRef>, 'prototype' | 'constructor'>;
15
+ /**
16
+ *
17
+ */
18
+ export declare function PartialType<T extends any[], I1, S1, K extends keyof I1>(classRef: Class<T, I1, S1>, keys?: readonly K[]): Class<T, Omit<I1, K>> & Omit<Pick<S1, keyof typeof classRef>, 'prototype' | 'constructor'>;
15
19
  /**
16
20
  * Type definition of MappedType constructor type
17
21
  * @type MappedTypeConstructor
@@ -19,9 +23,10 @@ export declare function OmitType<T extends any[], I1, S1, K extends keyof I1>(cl
19
23
  export interface MappedTypeConstructor {
20
24
  prototype: MappedType;
21
25
  new (document: ApiDocument, init: MappedType.InitArguments): MappedType;
22
- <T extends any[], I1, S1, PickKey extends keyof I1, OmitKey extends keyof I1>(resource: Class<T, I1, S1>, options: {
26
+ <T extends any[], I1, S1, PickKey extends keyof I1, OmitKey extends keyof I1, PartialKey extends keyof I1>(resource: Class<T, I1, S1>, options: {
23
27
  pickKeys?: readonly PickKey[];
24
28
  omitKeys?: readonly OmitKey[];
29
+ partialKeys?: readonly PartialKey[];
25
30
  }): Class<T, Omit<Pick<I1, PickKey>, OmitKey>>;
26
31
  _applyMixin(targetType: Type, sourceType: Type, options: MappedType.Options<any> & {
27
32
  isInheritedPredicate: (fieldName: string) => boolean;
@@ -41,16 +46,17 @@ export interface MappedType extends MappedTypeClass {
41
46
  * @namespace MappedType
42
47
  */
43
48
  export declare namespace MappedType {
44
- interface InitArguments extends ComplexType.InitArguments, Pick<OpraSchema.MappedType, 'pick' | 'omit'> {
49
+ interface InitArguments extends ComplexType.InitArguments, Pick<OpraSchema.MappedType, 'pick' | 'omit' | 'partial'> {
45
50
  }
46
- interface Metadata extends StrictOmit<ComplexType.Metadata, 'kind' | 'base' | 'name'>, Pick<OpraSchema.MappedType, 'pick' | 'omit'> {
51
+ interface Metadata extends StrictOmit<ComplexType.Metadata, 'kind' | 'base' | 'name'>, Pick<OpraSchema.MappedType, 'pick' | 'omit' | 'partial'> {
47
52
  kind: OpraSchema.MappedType.Kind;
48
53
  base: Type;
49
54
  }
50
- interface OwnProperties extends ComplexType.OwnProperties, Pick<OpraSchema.MappedType, 'pick' | 'omit'> {
55
+ interface OwnProperties extends ComplexType.OwnProperties, Pick<OpraSchema.MappedType, 'pick' | 'omit' | 'partial'> {
51
56
  }
52
57
  interface Options<T, K = keyof T> {
53
58
  pick?: readonly K[];
54
59
  omit?: readonly K[];
60
+ partial?: readonly K[] | boolean;
55
61
  }
56
62
  }
@@ -1,3 +1,4 @@
1
+ import { HttpStatusCodes } from '../../http/index.js';
1
2
  import { ErrorIssue } from '../error-issue.js';
2
3
  import { OpraException } from '../opra-exception.js';
3
4
  /**
@@ -7,6 +8,6 @@ import { OpraException } from '../opra-exception.js';
7
8
  * the client's identity is known to the server.
8
9
  */
9
10
  export declare class ForbiddenError extends OpraException {
10
- status: number;
11
+ status: HttpStatusCodes;
11
12
  protected init(issue: Partial<ErrorIssue>): void;
12
13
  }
@@ -5,6 +5,7 @@ export interface MappedType extends StrictOmit<ComplexType, 'kind'> {
5
5
  kind: MappedType.Kind;
6
6
  omit?: Field.Name[];
7
7
  pick?: Field.Name[];
8
+ partial?: Field.Name[] | boolean;
8
9
  }
9
10
  export declare namespace MappedType {
10
11
  const Kind = "MappedType";