@opra/common 0.18.3 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser.js +1225 -910
- package/cjs/document/api-document.js +55 -38
- package/cjs/document/data-type/api-field.js +5 -5
- package/cjs/document/data-type/builtin/any.type.js +3 -5
- package/cjs/document/data-type/builtin/base64.type.js +5 -28
- package/cjs/document/data-type/builtin/bigint.type.js +7 -25
- package/cjs/document/data-type/builtin/boolean.type.js +5 -18
- package/cjs/document/data-type/builtin/date.type.js +13 -9
- package/cjs/document/data-type/builtin/index.js +3 -1
- package/cjs/document/data-type/builtin/integer.type.js +5 -18
- package/cjs/document/data-type/builtin/number.type.js +5 -23
- package/cjs/document/data-type/builtin/object.type.js +4 -9
- package/cjs/document/data-type/builtin/string.type.js +5 -14
- package/cjs/document/data-type/builtin/time.type.js +11 -23
- package/cjs/document/data-type/builtin/timestamp.type.js +8 -20
- package/cjs/document/data-type/builtin/uuid.type.js +15 -0
- package/cjs/document/data-type/complex-type.js +151 -92
- package/cjs/document/data-type/data-type.js +32 -31
- package/cjs/document/data-type/enum-type.js +49 -43
- package/cjs/document/data-type/mapped-type.js +115 -70
- package/cjs/document/data-type/simple-type.js +46 -45
- package/cjs/document/data-type/union-type.js +69 -36
- package/cjs/document/factory/create-document.js +5 -3
- package/cjs/document/factory/factory.js +23 -20
- package/cjs/document/factory/import-type-class.js +13 -4
- package/cjs/document/resource/collection.js +55 -59
- package/cjs/document/resource/resource.js +10 -21
- package/cjs/document/resource/singleton.js +36 -39
- package/cjs/exception/common-errors/validation.error.js +11 -0
- package/cjs/exception/index.js +2 -1
- package/cjs/exception/{enums/issue-severity.enum.js → issue-severity.enum.js} +1 -1
- package/cjs/exception/opra-exception.js +2 -2
- package/cjs/filter/antlr/OpraFilterLexer.js +1 -1
- package/cjs/filter/antlr/OpraFilterParser.js +1 -1
- package/cjs/filter/errors.js +5 -0
- package/cjs/http/enums/http-headers-codes.enum.js +1 -1
- package/cjs/http/enums/http-status-codes.enum.js +1 -1
- package/cjs/http/http-request-message.js +1 -1
- package/cjs/http/http-response-message.js +1 -1
- package/cjs/schema/data-type/complex-type.interface.js +1 -1
- package/cjs/schema/data-type/enum-type.interface.js +1 -1
- package/cjs/schema/data-type/mapped-type.interface.js +1 -1
- package/cjs/schema/data-type/simple-type.interface.js +1 -1
- package/cjs/schema/data-type/union-type.interface.js +1 -1
- package/cjs/schema/index.js +3 -41
- package/cjs/schema/opra-schema.ns.js +18 -0
- package/cjs/schema/resource/collection.interface.js +1 -1
- package/cjs/schema/resource/container.interface.js +1 -1
- package/cjs/schema/resource/singleton.interface.js +1 -1
- package/esm/document/api-document.js +55 -38
- package/esm/document/data-type/api-field.js +5 -5
- package/esm/document/data-type/builtin/any.type.js +2 -4
- package/esm/document/data-type/builtin/base64.type.js +4 -27
- package/esm/document/data-type/builtin/bigint.type.js +6 -24
- package/esm/document/data-type/builtin/boolean.type.js +4 -17
- package/esm/document/data-type/builtin/date.type.js +12 -8
- package/esm/document/data-type/builtin/index.js +3 -1
- package/esm/document/data-type/builtin/integer.type.js +4 -17
- package/esm/document/data-type/builtin/number.type.js +4 -22
- package/esm/document/data-type/builtin/object.type.js +3 -8
- package/esm/document/data-type/builtin/string.type.js +4 -13
- package/esm/document/data-type/builtin/time.type.js +10 -22
- package/esm/document/data-type/builtin/timestamp.type.js +7 -19
- package/esm/document/data-type/builtin/uuid.type.js +12 -0
- package/esm/document/data-type/complex-type.js +151 -92
- package/esm/document/data-type/data-type.js +31 -31
- package/esm/document/data-type/enum-type.js +49 -43
- package/esm/document/data-type/mapped-type.js +114 -70
- package/esm/document/data-type/simple-type.js +45 -44
- package/esm/document/data-type/union-type.js +68 -36
- package/esm/document/factory/create-document.js +6 -4
- package/esm/document/factory/factory.js +23 -20
- package/esm/document/factory/import-type-class.js +13 -4
- package/esm/document/resource/collection.js +55 -59
- package/esm/document/resource/resource.js +9 -21
- package/esm/document/resource/singleton.js +36 -39
- package/esm/exception/common-errors/validation.error.js +7 -0
- package/esm/exception/index.js +2 -1
- package/esm/exception/opra-exception.js +2 -2
- package/esm/filter/antlr/OpraFilterLexer.js +2 -1
- package/esm/filter/antlr/OpraFilterParser.js +2 -1
- package/esm/filter/errors.js +5 -0
- package/esm/schema/index.js +2 -41
- package/esm/schema/opra-schema.ns.js +15 -0
- package/package.json +6 -4
- package/types/document/api-document.d.ts +3 -2
- package/types/document/data-type/builtin/base64.type.d.ts +0 -4
- package/types/document/data-type/builtin/bigint.type.d.ts +1 -5
- package/types/document/data-type/builtin/boolean.type.d.ts +0 -3
- package/types/document/data-type/builtin/date.type.d.ts +1 -3
- package/types/document/data-type/builtin/index.d.ts +3 -1
- package/types/document/data-type/builtin/integer.type.d.ts +1 -5
- package/types/document/data-type/builtin/number.type.d.ts +0 -4
- package/types/document/data-type/builtin/object.type.d.ts +0 -1
- package/types/document/data-type/builtin/string.type.d.ts +0 -3
- package/types/document/data-type/builtin/time.type.d.ts +1 -6
- package/types/document/data-type/builtin/timestamp.type.d.ts +0 -3
- package/types/document/data-type/builtin/uuid.type.d.ts +2 -0
- package/types/document/data-type/complex-type.d.ts +29 -16
- package/types/document/data-type/data-type.d.ts +19 -15
- package/types/document/data-type/enum-type.d.ts +15 -5
- package/types/document/data-type/mapped-type.d.ts +27 -5
- package/types/document/data-type/simple-type.d.ts +26 -12
- package/types/document/data-type/union-type.d.ts +20 -5
- package/types/document/factory/factory.d.ts +3 -2
- package/types/document/resource/collection.d.ts +11 -9
- package/types/document/resource/resource.d.ts +3 -9
- package/types/document/resource/singleton.d.ts +7 -5
- package/types/exception/common-errors/validation.error.d.ts +4 -0
- package/types/exception/error-issue.d.ts +1 -1
- package/types/exception/index.d.ts +2 -1
- package/types/exception/opra-exception.d.ts +4 -2
- package/types/filter/errors.d.ts +3 -0
- package/types/schema/data-type/enum-type.interface.d.ts +5 -5
- package/types/schema/data-type/simple-type.interface.d.ts +5 -11
- package/types/schema/index.d.ts +2 -51
- package/types/schema/opra-schema.ns.d.ts +15 -0
- package/cjs/document/data-type/builtin/guid.type.js +0 -35
- package/esm/document/data-type/builtin/guid.type.js +0 -32
- package/types/document/data-type/builtin/guid.type.d.ts +0 -7
- /package/esm/exception/{enums/issue-severity.enum.js → issue-severity.enum.js} +0 -0
- /package/types/exception/{enums/issue-severity.enum.d.ts → issue-severity.enum.d.ts} +0 -0
|
@@ -3,12 +3,46 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Singleton = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
6
|
+
const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
|
|
6
7
|
const index_js_1 = require("../../helpers/index.js");
|
|
7
8
|
const index_js_2 = require("../../schema/index.js");
|
|
8
9
|
const constants_js_1 = require("../constants.js");
|
|
9
10
|
const resource_js_1 = require("./resource.js");
|
|
10
11
|
const NESTJS_INJECTABLE_WATERMARK = '__injectable__';
|
|
11
12
|
const NAME_PATTERN = /^(.*)(Resource|Singleton)$/;
|
|
13
|
+
class SingletonClass extends resource_js_1.Resource {
|
|
14
|
+
constructor(document, init) {
|
|
15
|
+
super(document, init);
|
|
16
|
+
this.kind = index_js_2.OpraSchema.Singleton.Kind;
|
|
17
|
+
this.controller = init.controller;
|
|
18
|
+
const operations = this.operations = init.operations || {};
|
|
19
|
+
this.type = init.type;
|
|
20
|
+
if (this.controller) {
|
|
21
|
+
const instance = typeof this.controller == 'function'
|
|
22
|
+
? new this.controller()
|
|
23
|
+
: this.controller;
|
|
24
|
+
for (const operation of Object.values(operations)) {
|
|
25
|
+
if (!operation.handler && operation.handlerName) {
|
|
26
|
+
const fn = instance[operation.handlerName];
|
|
27
|
+
if (!fn)
|
|
28
|
+
throw new TypeError(`No such operation handler (${operation.handlerName}) found`);
|
|
29
|
+
operation.handler = fn.bind(instance);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exportSchema() {
|
|
35
|
+
const out = resource_js_1.Resource.prototype.exportSchema.call(this);
|
|
36
|
+
Object.assign(out, (0, index_js_1.omitUndefined)({
|
|
37
|
+
type: this.type.name,
|
|
38
|
+
operations: this.operations
|
|
39
|
+
}));
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
normalizeFieldPath(path) {
|
|
43
|
+
return this.type.normalizeFieldPath(path);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
12
46
|
exports.Singleton = function (...args) {
|
|
13
47
|
// ClassDecorator
|
|
14
48
|
if (!this) {
|
|
@@ -33,46 +67,9 @@ exports.Singleton = function (...args) {
|
|
|
33
67
|
}
|
|
34
68
|
// Constructor
|
|
35
69
|
const [document, init] = args;
|
|
36
|
-
|
|
37
|
-
resource_js_1.Resource.apply(this, [document, init]);
|
|
38
|
-
};
|
|
39
|
-
const proto = {
|
|
40
|
-
_construct(init) {
|
|
41
|
-
// call super()
|
|
42
|
-
resource_js_1.Resource.prototype._construct.call(this, init);
|
|
43
|
-
const _this = this;
|
|
44
|
-
_this.kind = index_js_2.OpraSchema.Singleton.Kind;
|
|
45
|
-
_this.controller = init.controller;
|
|
46
|
-
const operations = _this.operations = init.operations || {};
|
|
47
|
-
_this.type = init.type;
|
|
48
|
-
if (_this.controller) {
|
|
49
|
-
const instance = typeof _this.controller == 'function'
|
|
50
|
-
? new _this.controller()
|
|
51
|
-
: _this.controller;
|
|
52
|
-
for (const operation of Object.values(operations)) {
|
|
53
|
-
if (!operation.handler && operation.handlerName) {
|
|
54
|
-
const fn = instance[operation.handlerName];
|
|
55
|
-
if (!fn)
|
|
56
|
-
throw new TypeError(`No such operation handler (${operation.handlerName}) found`);
|
|
57
|
-
operation.handler = fn.bind(instance);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
exportSchema() {
|
|
63
|
-
const out = resource_js_1.Resource.prototype.exportSchema.call(this);
|
|
64
|
-
Object.assign(out, (0, index_js_1.omitUndefined)({
|
|
65
|
-
type: this.type.name,
|
|
66
|
-
operations: this.operations
|
|
67
|
-
}));
|
|
68
|
-
return out;
|
|
69
|
-
},
|
|
70
|
-
normalizeFieldPath(path) {
|
|
71
|
-
return this.type.normalizeFieldPath(path);
|
|
72
|
-
},
|
|
70
|
+
(0, putil_merge_1.default)(this, new SingletonClass(document, init), { descriptor: true });
|
|
73
71
|
};
|
|
74
|
-
|
|
75
|
-
Object.setPrototypeOf(exports.Singleton.prototype, resource_js_1.Resource.prototype);
|
|
72
|
+
exports.Singleton.prototype = SingletonClass.prototype;
|
|
76
73
|
function createOperationDecorator(operation) {
|
|
77
74
|
return (options) => ((target, propertyKey) => {
|
|
78
75
|
const metadata = {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationError = void 0;
|
|
4
|
+
const opra_exception_js_1 = require("../opra-exception.js");
|
|
5
|
+
class ValidationError extends opra_exception_js_1.OpraException {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.status = 400;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ValidationError = ValidationError;
|
package/cjs/exception/index.js
CHANGED
|
@@ -4,7 +4,7 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
tslib_1.__exportStar(require("./opra-exception.js"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./error-issue.js"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./wrap-exception.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./
|
|
7
|
+
tslib_1.__exportStar(require("./issue-severity.enum.js"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./http-errors/bad-request.error.js"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./http-errors/failed-dependency.error.js"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./http-errors/forbidden.error.js"), exports);
|
|
@@ -16,3 +16,4 @@ tslib_1.__exportStar(require("./http-errors/unauthorized.error.js"), exports);
|
|
|
16
16
|
tslib_1.__exportStar(require("./http-errors/unprocessable-entity.error.js"), exports);
|
|
17
17
|
tslib_1.__exportStar(require("./resource-errors/resource-conflict.error.js"), exports);
|
|
18
18
|
tslib_1.__exportStar(require("./resource-errors/resource-not-found.error.js"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./common-errors/validation.error.js"), exports);
|
|
@@ -20,4 +20,4 @@ var IssueSeverity;
|
|
|
20
20
|
'may cause it to be performed suboptimally or in a way that is not as desired',
|
|
21
21
|
info: 'The issue has no relation to the degree of success of the action'
|
|
22
22
|
};
|
|
23
|
-
})(IssueSeverity
|
|
23
|
+
})(IssueSeverity || (exports.IssueSeverity = IssueSeverity = {}));
|
|
@@ -10,14 +10,14 @@ class OpraException extends Error {
|
|
|
10
10
|
super('');
|
|
11
11
|
this.status = 500;
|
|
12
12
|
this._initName();
|
|
13
|
+
if (issue instanceof Error)
|
|
14
|
+
cause = issue;
|
|
13
15
|
if (cause) {
|
|
14
16
|
this.cause = cause;
|
|
15
17
|
if (cause.stack)
|
|
16
18
|
this.stack = cause.stack;
|
|
17
19
|
}
|
|
18
20
|
this._init(issue || cause || 'Unknown error');
|
|
19
|
-
if (issue instanceof Error)
|
|
20
|
-
this.stack = issue.stack;
|
|
21
21
|
this.message = index_js_1.i18n.deep(this.issue.message);
|
|
22
22
|
}
|
|
23
23
|
get issue() {
|
|
@@ -36,7 +36,6 @@ class OpraFilterLexer extends antlr4_1.Lexer {
|
|
|
36
36
|
return OpraFilterLexer.__ATN;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
exports.default = OpraFilterLexer;
|
|
40
39
|
OpraFilterLexer.T__0 = 1;
|
|
41
40
|
OpraFilterLexer.T__1 = 2;
|
|
42
41
|
OpraFilterLexer.T__2 = 3;
|
|
@@ -277,3 +276,4 @@ OpraFilterLexer._serializedATN = [4, 0, 43, 426, 6, -1, 2, 0,
|
|
|
277
276
|
268, 273, 279, 282, 286, 291, 293, 301, 303, 306, 311, 319, 321, 326, 328, 335, 337, 341,
|
|
278
277
|
346, 361, 370, 376, 390, 392, 394, 399, 404, 409, 411, 416, 1, 0, 1, 0];
|
|
279
278
|
OpraFilterLexer.DecisionsToDFA = OpraFilterLexer._ATN.decisionToState.map((ds, index) => new antlr4_1.DFA(ds, index));
|
|
279
|
+
exports.default = OpraFilterLexer;
|
|
@@ -768,7 +768,6 @@ class OpraFilterParser extends antlr4_1.Parser {
|
|
|
768
768
|
return OpraFilterParser.__ATN;
|
|
769
769
|
}
|
|
770
770
|
}
|
|
771
|
-
exports.default = OpraFilterParser;
|
|
772
771
|
OpraFilterParser.T__0 = 1;
|
|
773
772
|
OpraFilterParser.T__1 = 2;
|
|
774
773
|
OpraFilterParser.T__2 = 3;
|
|
@@ -913,6 +912,7 @@ OpraFilterParser._serializedATN = [4, 1, 43, 120, 2, 0, 7, 0, 2,
|
|
|
913
912
|
7, 3, 0, 0, 112, 27, 1, 0, 0, 0, 113, 114, 7, 4, 0, 0, 114, 29, 1, 0, 0, 0, 115, 116, 7, 5, 0, 0, 116,
|
|
914
913
|
31, 1, 0, 0, 0, 117, 118, 5, 36, 0, 0, 118, 33, 1, 0, 0, 0, 6, 48, 56, 65, 77, 84, 100];
|
|
915
914
|
OpraFilterParser.DecisionsToDFA = OpraFilterParser._ATN.decisionToState.map((ds, index) => new antlr4_1.DFA(ds, index));
|
|
915
|
+
exports.default = OpraFilterParser;
|
|
916
916
|
class RootContext extends antlr4_1.ParserRuleContext {
|
|
917
917
|
constructor(parser, parent, invokingState) {
|
|
918
918
|
super(parent, invokingState);
|
package/cjs/filter/errors.js
CHANGED
|
@@ -5,6 +5,11 @@ class SyntaxError extends TypeError {
|
|
|
5
5
|
}
|
|
6
6
|
exports.SyntaxError = SyntaxError;
|
|
7
7
|
class ValidationError extends TypeError {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(typeof message === 'string' ? message : message?.message);
|
|
10
|
+
if (typeof message === 'object')
|
|
11
|
+
Object.assign(this, message);
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
exports.ValidationError = ValidationError;
|
|
10
15
|
class FilterParseError extends Error {
|
|
@@ -297,4 +297,4 @@ var HttpStatusCodes;
|
|
|
297
297
|
* Indicates that the client needs to authenticate to gain network access.
|
|
298
298
|
*/
|
|
299
299
|
HttpStatusCodes[HttpStatusCodes["NETWORK_AUTHENTICATION_REQUIRED"] = 511] = "NETWORK_AUTHENTICATION_REQUIRED";
|
|
300
|
-
})(HttpStatusCodes
|
|
300
|
+
})(HttpStatusCodes || (exports.HttpStatusCodes = HttpStatusCodes = {}));
|
|
@@ -22,7 +22,7 @@ var HttpRequestMessage;
|
|
|
22
22
|
return HttpRequestMessageHost.fromStream(readable);
|
|
23
23
|
}
|
|
24
24
|
HttpRequestMessage.fromStream = fromStream;
|
|
25
|
-
})(HttpRequestMessage
|
|
25
|
+
})(HttpRequestMessage || (exports.HttpRequestMessage = HttpRequestMessage = {}));
|
|
26
26
|
const kQuery = Symbol('kQuery');
|
|
27
27
|
const kQueryProxy = Symbol('kQueryProxy');
|
|
28
28
|
const kProtocol = Symbol('kProtocol');
|
|
@@ -28,7 +28,7 @@ var HttpResponseMessage;
|
|
|
28
28
|
return HttpResponseMessageHost.fromStream(readable);
|
|
29
29
|
}
|
|
30
30
|
HttpResponseMessage.fromStream = fromStream;
|
|
31
|
-
})(HttpResponseMessage
|
|
31
|
+
})(HttpResponseMessage || (exports.HttpResponseMessage = HttpResponseMessage = {}));
|
|
32
32
|
/**
|
|
33
33
|
* @class HttpResponseMessageHost
|
|
34
34
|
*/
|
package/cjs/schema/index.js
CHANGED
|
@@ -1,44 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpraSchema = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const mapped_type_interface_js_1 = require("./data-type/mapped-type.interface.js");
|
|
8
|
-
const simple_type_interface_js_1 = require("./data-type/simple-type.interface.js");
|
|
9
|
-
const union_type_interface_js_1 = require("./data-type/union-type.interface.js");
|
|
10
|
-
const collection_interface_js_1 = require("./resource/collection.interface.js");
|
|
11
|
-
const container_interface_js_1 = require("./resource/container.interface.js");
|
|
12
|
-
const singleton_interface_js_1 = require("./resource/singleton.interface.js");
|
|
13
|
-
const type_guards_js_1 = require("./type-guards.js");
|
|
14
|
-
var OpraSchema;
|
|
15
|
-
(function (OpraSchema) {
|
|
16
|
-
// Re-export constants and types
|
|
17
|
-
OpraSchema.SpecVersion = constants_js_1.SpecVersion;
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
-
OpraSchema.ComplexType = complex_type_interface_js_1.ComplexType;
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
-
OpraSchema.EnumType = enum_type_interface_js_1.EnumType;
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
-
OpraSchema.MappedType = mapped_type_interface_js_1.MappedType;
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
25
|
-
OpraSchema.SimpleType = simple_type_interface_js_1.SimpleType;
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
27
|
-
OpraSchema.UnionType = union_type_interface_js_1.UnionType;
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
29
|
-
OpraSchema.Collection = collection_interface_js_1.Collection;
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
31
|
-
OpraSchema.Container = container_interface_js_1.Container;
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
33
|
-
OpraSchema.Singleton = singleton_interface_js_1.Singleton;
|
|
34
|
-
OpraSchema.isDataType = type_guards_js_1.isDataType;
|
|
35
|
-
OpraSchema.isComplexType = type_guards_js_1.isComplexType;
|
|
36
|
-
OpraSchema.isEnumType = type_guards_js_1.isEnumType;
|
|
37
|
-
OpraSchema.isMappedType = type_guards_js_1.isMappedType;
|
|
38
|
-
OpraSchema.isSimpleType = type_guards_js_1.isSimpleType;
|
|
39
|
-
OpraSchema.isUnionType = type_guards_js_1.isUnionType;
|
|
40
|
-
OpraSchema.isResource = type_guards_js_1.isResource;
|
|
41
|
-
OpraSchema.isCollection = type_guards_js_1.isCollection;
|
|
42
|
-
OpraSchema.isContainer = type_guards_js_1.isContainer;
|
|
43
|
-
OpraSchema.isSingleton = type_guards_js_1.isSingleton;
|
|
44
|
-
})(OpraSchema = exports.OpraSchema || (exports.OpraSchema = {}));
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const OpraSchema = tslib_1.__importStar(require("./opra-schema.ns.js"));
|
|
6
|
+
exports.OpraSchema = OpraSchema;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./data-type/complex-type.interface.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./data-type/data-type.interface.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./data-type/enum-type.interface.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./data-type/field.interface.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./data-type/simple-type.interface.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./data-type/mapped-type.interface.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./data-type/union-type.interface.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./resource/collection.interface.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./resource/container.interface.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./resource/endpoint.interface.js"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./resource/resource.interface.js"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./resource/singleton.interface.js"), exports);
|
|
16
|
+
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./document.interface.js"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./type-guards.js"), exports);
|
|
@@ -3,9 +3,9 @@ import { cloneObject, omitUndefined, ResponsiveMap } from '../helpers/index.js';
|
|
|
3
3
|
import { OpraSchema } from '../schema/index.js';
|
|
4
4
|
import { NAMESPACE_PATTERN } from './constants.js';
|
|
5
5
|
import { ComplexType } from './data-type/complex-type.js';
|
|
6
|
-
import { SimpleType } from './data-type/simple-type.js';
|
|
7
6
|
export class ApiDocument {
|
|
8
7
|
constructor() {
|
|
8
|
+
this._designCtorMap = new Map();
|
|
9
9
|
this._typeCache = new ResponsiveMap();
|
|
10
10
|
this._typesCacheByCtor = new Map();
|
|
11
11
|
this.references = new ResponsiveMap();
|
|
@@ -15,59 +15,81 @@ export class ApiDocument {
|
|
|
15
15
|
version: '',
|
|
16
16
|
title: ''
|
|
17
17
|
};
|
|
18
|
+
const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
|
|
19
|
+
const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
|
|
20
|
+
this._designCtorMap.set(String, 'string');
|
|
21
|
+
this._designCtorMap.set(Number, 'number');
|
|
22
|
+
this._designCtorMap.set(Boolean, 'boolean');
|
|
23
|
+
this._designCtorMap.set(Object, 'any');
|
|
24
|
+
this._designCtorMap.set(Date, 'timestamp');
|
|
25
|
+
this._designCtorMap.set(BigIntConstructor, 'bigint');
|
|
26
|
+
this._designCtorMap.set(ArrayBuffer, 'base64');
|
|
27
|
+
this._designCtorMap.set(SharedArrayBuffer, 'base64');
|
|
28
|
+
this._designCtorMap.set(BufferConstructor, 'base64');
|
|
18
29
|
}
|
|
19
|
-
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
getDataType(arg0, silent) {
|
|
20
34
|
let dataType;
|
|
21
|
-
|
|
22
|
-
nameOrCtor = 'any';
|
|
23
|
-
const nameOrCtorName = typeof nameOrCtor === 'function' ? nameOrCtor.name : nameOrCtor;
|
|
35
|
+
const name = typeof arg0 === 'function' ? arg0.name : arg0;
|
|
24
36
|
// Try to get instance from cache
|
|
25
|
-
const t = typeof
|
|
26
|
-
? this._typeCache.get(
|
|
27
|
-
: this._typesCacheByCtor.get(
|
|
37
|
+
const t = typeof arg0 === 'string'
|
|
38
|
+
? this._typeCache.get(arg0)
|
|
39
|
+
: this._typesCacheByCtor.get(arg0);
|
|
28
40
|
if (t)
|
|
29
41
|
return t;
|
|
42
|
+
// If cached as null, it means "not found"
|
|
30
43
|
if (t === null) {
|
|
31
44
|
if (silent)
|
|
32
45
|
return;
|
|
33
|
-
throw new
|
|
46
|
+
throw new Error(`Data type "${name}" does not exists`);
|
|
34
47
|
}
|
|
35
|
-
|
|
36
|
-
|
|
48
|
+
// Convert design ctor to type name (String -> 'string')
|
|
49
|
+
if (typeof arg0 === 'function') {
|
|
50
|
+
const x = this._designCtorMap.get(arg0);
|
|
51
|
+
if (x)
|
|
52
|
+
arg0 = x;
|
|
53
|
+
}
|
|
54
|
+
if (typeof arg0 === 'string') {
|
|
55
|
+
const m = NAMESPACE_PATTERN.exec(arg0);
|
|
37
56
|
if (!m)
|
|
38
|
-
throw new
|
|
57
|
+
throw new TypeError(`Invalid data type name "${name}"`);
|
|
39
58
|
// If given string has namespace pattern (ns:type_name)
|
|
40
59
|
if (m[2]) {
|
|
41
60
|
const ref = this.references.get(m[1]);
|
|
42
61
|
if (!ref) {
|
|
43
62
|
if (silent)
|
|
44
63
|
return;
|
|
45
|
-
throw new
|
|
64
|
+
throw new Error(`No referenced document found for given namespace "${m[1]}"`);
|
|
46
65
|
}
|
|
47
|
-
dataType = ref.getDataType(m[2]);
|
|
48
|
-
this._typeCache.set(nameOrCtor, dataType);
|
|
66
|
+
dataType = ref.getDataType(m[2], silent);
|
|
49
67
|
}
|
|
50
68
|
else {
|
|
51
|
-
const name = m[1];
|
|
52
69
|
// Get instance from own types
|
|
53
|
-
dataType = this.types.get(
|
|
70
|
+
dataType = this.types.get(arg0);
|
|
54
71
|
// if not found, search in references (from last to first)
|
|
55
72
|
if (!dataType) {
|
|
56
73
|
const references = Array.from(this.references.values()).reverse();
|
|
57
74
|
for (const ref of references) {
|
|
58
|
-
dataType = ref.getDataType(name);
|
|
75
|
+
dataType = ref.getDataType(name, true);
|
|
59
76
|
if (dataType)
|
|
60
77
|
break;
|
|
61
78
|
}
|
|
62
79
|
}
|
|
63
|
-
if (dataType)
|
|
64
|
-
this._typeCache.set(dataType.name || name, dataType);
|
|
65
80
|
}
|
|
81
|
+
if (dataType) {
|
|
82
|
+
this._typeCache.set(arg0, dataType);
|
|
83
|
+
return dataType;
|
|
84
|
+
}
|
|
85
|
+
if (!silent)
|
|
86
|
+
throw new NotFoundError(`Data type "${arg0}" does not exists`);
|
|
87
|
+
return;
|
|
66
88
|
}
|
|
67
|
-
|
|
89
|
+
if (typeof arg0 === 'function') {
|
|
68
90
|
const types = Array.from(this.types.values()).reverse();
|
|
69
91
|
for (const dt of types) {
|
|
70
|
-
if (
|
|
92
|
+
if (dt instanceof ComplexType && dt.isTypeOf(arg0)) {
|
|
71
93
|
dataType = dt;
|
|
72
94
|
break;
|
|
73
95
|
}
|
|
@@ -76,27 +98,22 @@ export class ApiDocument {
|
|
|
76
98
|
if (!dataType) {
|
|
77
99
|
const references = Array.from(this.references.values()).reverse();
|
|
78
100
|
for (const ref of references) {
|
|
79
|
-
dataType = ref.getDataType(
|
|
101
|
+
dataType = ref.getDataType(arg0, true);
|
|
80
102
|
if (dataType)
|
|
81
103
|
break;
|
|
82
104
|
}
|
|
83
105
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
if (typeof nameOrCtor === 'string')
|
|
93
|
-
this._typeCache.set(nameOrCtor, null);
|
|
94
|
-
else
|
|
95
|
-
this._typesCacheByCtor.set(nameOrCtor, null);
|
|
96
|
-
}
|
|
97
|
-
if (silent)
|
|
106
|
+
if (dataType)
|
|
107
|
+
this._typesCacheByCtor.set(arg0, dataType);
|
|
108
|
+
if (dataType)
|
|
109
|
+
return dataType;
|
|
110
|
+
if (!silent)
|
|
111
|
+
throw new NotFoundError(`No data type mapping found for class "${name}"`);
|
|
98
112
|
return;
|
|
99
|
-
|
|
113
|
+
}
|
|
114
|
+
/* istanbul ignore next */
|
|
115
|
+
if (!silent)
|
|
116
|
+
throw new TypeError('Invalid argument');
|
|
100
117
|
}
|
|
101
118
|
getComplexType(nameOrCtor, silent) {
|
|
102
119
|
if (nameOrCtor === Object)
|
|
@@ -16,14 +16,15 @@ export const ApiField = function (...args) {
|
|
|
16
16
|
metadata.kind = OpraSchema.ComplexType.Kind;
|
|
17
17
|
metadata.fields = metadata.fields || {};
|
|
18
18
|
const designType = Reflect.getMetadata('design:type', target, propertyKey);
|
|
19
|
-
const isArray = designType === Array;
|
|
20
19
|
const elemMeta = metadata.fields[propertyKey] = {
|
|
21
20
|
...options,
|
|
22
21
|
enum: undefined,
|
|
23
|
-
designType
|
|
22
|
+
designType
|
|
24
23
|
};
|
|
25
|
-
if (designType === Array)
|
|
24
|
+
if (designType === Array) {
|
|
26
25
|
elemMeta.isArray = true;
|
|
26
|
+
delete elemMeta.designType;
|
|
27
|
+
}
|
|
27
28
|
if (options?.enum) {
|
|
28
29
|
elemMeta.type = undefined;
|
|
29
30
|
if (Array.isArray(options.enum)) {
|
|
@@ -66,7 +67,7 @@ export const ApiField = function (...args) {
|
|
|
66
67
|
};
|
|
67
68
|
const proto = {
|
|
68
69
|
exportSchema() {
|
|
69
|
-
|
|
70
|
+
return {
|
|
70
71
|
type: this.type.name ? this.type.name : this.type.exportSchema(),
|
|
71
72
|
description: this.description,
|
|
72
73
|
isArray: this.isArray,
|
|
@@ -74,7 +75,6 @@ const proto = {
|
|
|
74
75
|
fixed: this.fixed,
|
|
75
76
|
required: this.required
|
|
76
77
|
};
|
|
77
|
-
return out;
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
Object.assign(ApiField.prototype, proto);
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
2
|
import { SimpleType } from '../simple-type.js';
|
|
3
|
-
let AnyType = class AnyType {
|
|
3
|
+
export let AnyType = class AnyType {
|
|
4
4
|
};
|
|
5
5
|
AnyType = __decorate([
|
|
6
6
|
SimpleType({
|
|
7
|
-
description: '
|
|
8
|
-
ctor: Object
|
|
7
|
+
description: 'Represents any value'
|
|
9
8
|
})
|
|
10
9
|
], AnyType);
|
|
11
|
-
export { AnyType };
|
|
@@ -1,35 +1,12 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
|
+
import { isBase64 } from 'valgen';
|
|
2
3
|
import { SimpleType } from '../simple-type.js';
|
|
3
|
-
|
|
4
|
-
let Base64Type = class Base64Type {
|
|
5
|
-
decode(v) {
|
|
6
|
-
if (v == null)
|
|
7
|
-
return v;
|
|
8
|
-
if (typeof v === 'string')
|
|
9
|
-
this.validate(v);
|
|
10
|
-
return Buffer.from(v, 'base64').buffer.slice(0);
|
|
11
|
-
}
|
|
12
|
-
encode(v) {
|
|
13
|
-
if (v == null)
|
|
14
|
-
return v;
|
|
15
|
-
if (Buffer.isBuffer(v))
|
|
16
|
-
return v.toString('base64');
|
|
17
|
-
return Buffer.from(v).toString('base64');
|
|
18
|
-
}
|
|
19
|
-
coerce(v) {
|
|
20
|
-
if (v instanceof ArrayBuffer)
|
|
21
|
-
return v;
|
|
22
|
-
return this.decode(v);
|
|
23
|
-
}
|
|
24
|
-
validate(v) {
|
|
25
|
-
if (!(typeof v === 'string' && BASE64_PATTERN.test(v)))
|
|
26
|
-
throw new TypeError(`Invalid base64 value "${String(v).substring(0, 10)}..."`);
|
|
27
|
-
}
|
|
4
|
+
export let Base64Type = class Base64Type {
|
|
28
5
|
};
|
|
29
6
|
Base64Type = __decorate([
|
|
30
7
|
SimpleType({
|
|
31
8
|
description: 'A stream of bytes, base64 encoded',
|
|
32
|
-
|
|
9
|
+
decoder: isBase64(),
|
|
10
|
+
encoder: isBase64(),
|
|
33
11
|
})
|
|
34
12
|
], Base64Type);
|
|
35
|
-
export { Base64Type };
|
|
@@ -1,30 +1,12 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
|
+
import { isBigint, isString, pipe } from 'valgen';
|
|
2
3
|
import { SimpleType } from '../simple-type.js';
|
|
3
|
-
|
|
4
|
-
let BigIntType = class BigIntType {
|
|
5
|
-
decode(v) {
|
|
6
|
-
if (v == null)
|
|
7
|
-
return v;
|
|
8
|
-
return typeof v === 'bigint' ? v : BigInt(v);
|
|
9
|
-
}
|
|
10
|
-
encode(v) {
|
|
11
|
-
if (v == null)
|
|
12
|
-
return v;
|
|
13
|
-
return typeof v === 'bigint' ? String(v) : undefined;
|
|
14
|
-
}
|
|
15
|
-
coerce(v) {
|
|
16
|
-
return this.decode(v);
|
|
17
|
-
}
|
|
18
|
-
validate(v) {
|
|
19
|
-
if (!((typeof v === 'number' && !isNaN(v)) || typeof v === 'bigint'))
|
|
20
|
-
throw new TypeError(`Invalid number value "${v}"`);
|
|
21
|
-
}
|
|
4
|
+
export let BigintType = class BigintType {
|
|
22
5
|
};
|
|
23
|
-
|
|
6
|
+
BigintType = __decorate([
|
|
24
7
|
SimpleType({
|
|
25
|
-
name: 'bigint',
|
|
26
8
|
description: 'BigInt number',
|
|
27
|
-
|
|
9
|
+
decoder: isBigint(),
|
|
10
|
+
encoder: pipe(isBigint(), isString())
|
|
28
11
|
})
|
|
29
|
-
],
|
|
30
|
-
export { BigIntType };
|
|
12
|
+
], BigintType);
|