@opra/common 0.16.2 → 0.17.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/cjs/document/data-type/complex-type.js +69 -56
- package/cjs/document/data-type/simple-type.js +0 -1
- package/cjs/document/resource/collection.js +15 -16
- package/cjs/document/resource/singleton.js +2 -2
- package/cjs/http/http-params.js +10 -11
- package/esm/document/api-document.js +33 -37
- package/esm/document/constants.js +3 -6
- package/esm/document/data-type/api-field.js +12 -15
- package/esm/document/data-type/builtin/any.type.js +5 -8
- package/esm/document/data-type/builtin/base64.type.js +5 -8
- package/esm/document/data-type/builtin/bigint.type.js +5 -8
- package/esm/document/data-type/builtin/boolean.type.js +8 -11
- package/esm/document/data-type/builtin/date.type.js +7 -10
- package/esm/document/data-type/builtin/guid.type.js +9 -12
- package/esm/document/data-type/builtin/index.js +10 -13
- package/esm/document/data-type/builtin/integer.type.js +10 -13
- package/esm/document/data-type/builtin/number.type.js +7 -10
- package/esm/document/data-type/builtin/object.type.js +5 -8
- package/esm/document/data-type/builtin/string.type.js +8 -11
- package/esm/document/data-type/builtin/time.type.js +7 -10
- package/esm/document/data-type/builtin/timestamp.type.js +9 -12
- package/esm/document/data-type/complex-type.js +94 -85
- package/esm/document/data-type/data-type.js +9 -12
- package/esm/document/data-type/enum-type.js +15 -19
- package/esm/document/data-type/mapped-type.js +24 -29
- package/esm/document/data-type/simple-type.js +17 -22
- package/esm/document/data-type/union-type.js +26 -29
- package/esm/document/factory/add-references.js +3 -7
- package/esm/document/factory/create-document.js +10 -16
- package/esm/document/factory/factory.js +33 -37
- package/esm/document/factory/import-resource-class.js +16 -22
- package/esm/document/factory/import-type-class.js +24 -34
- package/esm/document/factory/index.js +1 -4
- package/esm/document/factory/process-resources.js +10 -16
- package/esm/document/factory/process-types.js +31 -37
- package/esm/document/index.js +14 -17
- package/esm/document/resource/collection.js +58 -63
- package/esm/document/resource/resource.js +9 -12
- package/esm/document/resource/singleton.js +27 -31
- package/esm/document/utils/inspect.util.js +4 -7
- package/esm/document/utils/string-compare.util.js +1 -5
- package/esm/exception/enums/issue-severity.enum.js +2 -5
- package/esm/exception/error-issue.js +1 -2
- package/esm/exception/http-errors/bad-request.error.js +4 -8
- package/esm/exception/http-errors/failed-dependency.error.js +4 -8
- package/esm/exception/http-errors/forbidden.error.js +4 -8
- package/esm/exception/http-errors/internal-server.error.js +4 -8
- package/esm/exception/http-errors/method-not-allowed.error.js +4 -8
- package/esm/exception/http-errors/not-acceptable.error.js +4 -8
- package/esm/exception/http-errors/not-found.error.js +4 -8
- package/esm/exception/http-errors/unauthorized.error.js +4 -8
- package/esm/exception/http-errors/unprocessable-entity.error.js +4 -8
- package/esm/exception/index.js +15 -18
- package/esm/exception/opra-exception.js +3 -7
- package/esm/exception/resource-errors/resource-conflict.error.js +4 -8
- package/esm/exception/resource-errors/resource-not-found.error.js +4 -8
- package/esm/exception/wrap-exception.js +20 -24
- package/esm/filter/antlr/OpraFilterLexer.js +6 -9
- package/esm/filter/antlr/OpraFilterListener.js +2 -5
- package/esm/filter/antlr/OpraFilterParser.js +79 -126
- package/esm/filter/antlr/OpraFilterVisitor.js +2 -5
- package/esm/filter/ast/abstract/ast.js +1 -5
- package/esm/filter/ast/abstract/expression.js +2 -6
- package/esm/filter/ast/abstract/literal.js +2 -6
- package/esm/filter/ast/abstract/term.js +2 -6
- package/esm/filter/ast/expressions/arithmetic-expression.js +3 -8
- package/esm/filter/ast/expressions/array-expression.js +2 -6
- package/esm/filter/ast/expressions/comparison-expression.js +2 -6
- package/esm/filter/ast/expressions/logical-expression.js +2 -6
- package/esm/filter/ast/expressions/parentheses-expression.js +2 -6
- package/esm/filter/ast/index.js +16 -19
- package/esm/filter/ast/terms/boolean-literal.js +2 -6
- package/esm/filter/ast/terms/date-literal.js +8 -12
- package/esm/filter/ast/terms/external-constant.js +2 -6
- package/esm/filter/ast/terms/null-literal.js +2 -6
- package/esm/filter/ast/terms/number-literal.js +4 -8
- package/esm/filter/ast/terms/qualified-identifier.js +2 -6
- package/esm/filter/ast/terms/string-literal.js +4 -8
- package/esm/filter/ast/terms/time-literal.js +6 -10
- package/esm/filter/build.js +45 -69
- package/esm/filter/errors.js +3 -9
- package/esm/filter/filter-tree-visitor.js +26 -31
- package/esm/filter/index.js +5 -8
- package/esm/filter/opra-error-listener.js +4 -8
- package/esm/filter/parse.js +14 -19
- package/esm/filter/utils.js +2 -7
- package/esm/helpers/function-utils.js +8 -14
- package/esm/helpers/get-stack-filename.js +1 -5
- package/esm/helpers/index.js +8 -11
- package/esm/helpers/is-url.js +1 -5
- package/esm/helpers/mixin-utils.js +2 -7
- package/esm/helpers/object-utils.js +6 -12
- package/esm/helpers/path-to-object-tree.js +1 -5
- package/esm/helpers/responsive-map.js +1 -5
- package/esm/helpers/type-guards.js +7 -17
- package/esm/http/codecs/boolean-codec.js +1 -5
- package/esm/http/codecs/date-codec.js +1 -5
- package/esm/http/codecs/filter-codec.js +5 -9
- package/esm/http/codecs/integer-codec.js +2 -6
- package/esm/http/codecs/number-codec.js +1 -5
- package/esm/http/codecs/string-codec.js +1 -5
- package/esm/http/enums/http-headers-codes.enum.js +2 -5
- package/esm/http/enums/http-status-codes.enum.js +2 -5
- package/esm/http/enums/http-status-messages.js +1 -4
- package/esm/http/http-headers.js +5 -9
- package/esm/http/http-message.host.js +13 -18
- package/esm/http/http-params.js +30 -36
- package/esm/http/http-request-message.js +18 -22
- package/esm/http/http-response-message.js +23 -28
- package/esm/http/index.js +14 -17
- package/esm/http/interfaces/client-http-headers.interface.js +1 -2
- package/esm/http/interfaces/cookie-options.interface.js +1 -2
- package/esm/http/interfaces/server-http-headers.interface.js +1 -2
- package/esm/i18n/i18n.js +23 -28
- package/esm/i18n/index.js +5 -9
- package/esm/i18n/string-utils.js +2 -7
- package/esm/i18n/translate.js +3 -7
- package/esm/index.js +11 -16
- package/esm/schema/constants.js +1 -4
- package/esm/schema/data-type/complex-type.interface.js +2 -5
- package/esm/schema/data-type/data-type.interface.js +1 -2
- package/esm/schema/data-type/enum-type.interface.js +2 -5
- package/esm/schema/data-type/field.interface.js +1 -2
- package/esm/schema/data-type/mapped-type.interface.js +2 -5
- package/esm/schema/data-type/simple-type.interface.js +2 -5
- package/esm/schema/data-type/union-type.interface.js +2 -5
- package/esm/schema/document.interface.js +1 -2
- package/esm/schema/index.js +31 -34
- package/esm/schema/resource/collection.interface.js +2 -5
- package/esm/schema/resource/container.interface.js +2 -5
- package/esm/schema/resource/endpoint.interface.js +1 -2
- package/esm/schema/resource/resource.interface.js +1 -2
- package/esm/schema/resource/singleton.interface.js +2 -5
- package/esm/schema/type-guards.js +40 -53
- package/esm/types.js +1 -2
- package/esm/url/index.js +5 -8
- package/esm/url/opra-url-path-component.js +3 -7
- package/esm/url/opra-url-path.js +14 -18
- package/esm/url/opra-url.js +15 -19
- package/esm/url/utils/decode-path-component.js +4 -8
- package/esm/url/utils/encode-path-component.js +3 -8
- package/esm/utils/path-utils.js +2 -7
- package/package.json +1 -1
- package/types/document/data-type/complex-type.d.ts +8 -2
- package/types/document/resource/collection.d.ts +5 -3
- package/types/document/resource/singleton.d.ts +3 -1
- package/types/filter/ast/terms/qualified-identifier.d.ts +4 -0
- package/types/http/http-params.d.ts +20 -19
|
@@ -1,29 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const index_js_2 = require("../../schema/index.js");
|
|
6
|
-
const constants_js_1 = require("../constants.js");
|
|
7
|
-
async function importTypeClass(typeThunk) {
|
|
1
|
+
import { cloneObject, isConstructor, resolveThunk } from '../../helpers/index.js';
|
|
2
|
+
import { OpraSchema } from '../../schema/index.js';
|
|
3
|
+
import { METADATA_KEY } from '../constants.js';
|
|
4
|
+
export async function importTypeClass(typeThunk) {
|
|
8
5
|
const { document, typeQueue, cache } = this;
|
|
9
|
-
const thunk = await
|
|
6
|
+
const thunk = await resolveThunk(typeThunk);
|
|
10
7
|
const cached = cache.get(thunk);
|
|
11
8
|
if (cached)
|
|
12
9
|
return cached;
|
|
13
10
|
const dt = document.getDataType(thunk, true);
|
|
14
11
|
if (dt && dt.name)
|
|
15
12
|
return dt.name;
|
|
16
|
-
const metadata = Reflect.getMetadata(
|
|
17
|
-
if (!(metadata &&
|
|
13
|
+
const metadata = Reflect.getMetadata(METADATA_KEY, thunk);
|
|
14
|
+
if (!(metadata && OpraSchema.isDataType(metadata))) {
|
|
18
15
|
// If thunk is a Type class
|
|
19
|
-
if (
|
|
16
|
+
if (isConstructor(thunk))
|
|
20
17
|
throw new TypeError(`Class "${thunk.name}" doesn't have a valid DataType metadata`);
|
|
21
18
|
// If thunk is an Enum object
|
|
22
19
|
throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(thunk).substring(0, 20)}...`);
|
|
23
20
|
}
|
|
24
21
|
// Clone metadata to prevent changing its contents
|
|
25
22
|
const name = metadata.name;
|
|
26
|
-
const schema =
|
|
23
|
+
const schema = cloneObject(metadata);
|
|
27
24
|
const result = name || schema;
|
|
28
25
|
if (name) {
|
|
29
26
|
if (this.document.types.has(name) || typeQueue.has(name))
|
|
@@ -32,15 +29,15 @@ async function importTypeClass(typeThunk) {
|
|
|
32
29
|
typeQueue.set(name, schema);
|
|
33
30
|
}
|
|
34
31
|
// If thunk is a DataType class
|
|
35
|
-
if (
|
|
32
|
+
if (isConstructor(thunk)) {
|
|
36
33
|
const ctor = thunk;
|
|
37
|
-
if (
|
|
34
|
+
if (OpraSchema.isSimpleType(schema))
|
|
38
35
|
await this.extractSimpleTypeSchema(schema, ctor, metadata);
|
|
39
|
-
else if (
|
|
36
|
+
else if (OpraSchema.isComplexType(schema))
|
|
40
37
|
await this.extractComplexTypeSchema(schema, ctor, metadata);
|
|
41
|
-
else if (
|
|
38
|
+
else if (OpraSchema.isMappedType(schema))
|
|
42
39
|
await this.extractMappedTypeSchema(schema, ctor, metadata);
|
|
43
|
-
else if (
|
|
40
|
+
else if (OpraSchema.isUnionType(schema))
|
|
44
41
|
await this.extractUnionTypeSchema(schema, ctor, metadata);
|
|
45
42
|
else
|
|
46
43
|
throw new TypeError(`Class "${ctor.name}" doesn't have a valid DataType metadata`);
|
|
@@ -48,9 +45,9 @@ async function importTypeClass(typeThunk) {
|
|
|
48
45
|
}
|
|
49
46
|
// If thunk is a EnumType object
|
|
50
47
|
const enumObject = thunk;
|
|
51
|
-
if (
|
|
48
|
+
if (OpraSchema.isEnumType(schema)) {
|
|
52
49
|
let baseType;
|
|
53
|
-
if (metadata.base && Reflect.hasMetadata(
|
|
50
|
+
if (metadata.base && Reflect.hasMetadata(METADATA_KEY, metadata.base)) {
|
|
54
51
|
baseType = await this.importTypeClass(metadata.base);
|
|
55
52
|
}
|
|
56
53
|
schema.base = baseType;
|
|
@@ -59,20 +56,18 @@ async function importTypeClass(typeThunk) {
|
|
|
59
56
|
}
|
|
60
57
|
throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(enumObject).substring(0, 20)}...`);
|
|
61
58
|
}
|
|
62
|
-
exports.importTypeClass = importTypeClass;
|
|
63
59
|
// noinspection JSUnusedLocalSymbols
|
|
64
|
-
async function extractSimpleTypeSchema(target, ctor,
|
|
60
|
+
export async function extractSimpleTypeSchema(target, ctor,
|
|
65
61
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
66
62
|
metadata) {
|
|
67
63
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
68
|
-
if (Reflect.hasMetadata(
|
|
64
|
+
if (Reflect.hasMetadata(METADATA_KEY, baseClass))
|
|
69
65
|
target.base = await this.importTypeClass(baseClass);
|
|
70
66
|
target.codec = Object.create(ctor.prototype);
|
|
71
67
|
}
|
|
72
|
-
|
|
73
|
-
async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
68
|
+
export async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
74
69
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
75
|
-
if (Reflect.hasMetadata(
|
|
70
|
+
if (Reflect.hasMetadata(METADATA_KEY, baseClass))
|
|
76
71
|
target.base = await this.importTypeClass(baseClass);
|
|
77
72
|
target.ctor = ctor;
|
|
78
73
|
// Fields
|
|
@@ -109,29 +104,24 @@ async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
|
109
104
|
}
|
|
110
105
|
}
|
|
111
106
|
}
|
|
112
|
-
|
|
113
|
-
async function extractMappedTypeSchema(target, ctor, metadata) {
|
|
107
|
+
export async function extractMappedTypeSchema(target, ctor, metadata) {
|
|
114
108
|
target.type = await this.importTypeClass(metadata.type);
|
|
115
109
|
}
|
|
116
|
-
|
|
117
|
-
async function extractUnionTypeSchema(target, ctor, metadata) {
|
|
110
|
+
export async function extractUnionTypeSchema(target, ctor, metadata) {
|
|
118
111
|
const oldTypes = metadata.types;
|
|
119
112
|
target.types = [];
|
|
120
113
|
for (const type of oldTypes)
|
|
121
114
|
target.types.push(await this.importTypeClass(type));
|
|
122
115
|
}
|
|
123
|
-
exports.extractUnionTypeSchema = extractUnionTypeSchema;
|
|
124
116
|
// noinspection JSUnusedLocalSymbols
|
|
125
|
-
async function extractEnumTypeSchema(
|
|
117
|
+
export async function extractEnumTypeSchema(
|
|
126
118
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
127
119
|
target, enumObject, metadata) {
|
|
128
120
|
// Do nothing. This method is used by external modules for extending the factory
|
|
129
121
|
}
|
|
130
|
-
exports.extractEnumTypeSchema = extractEnumTypeSchema;
|
|
131
122
|
// noinspection JSUnusedLocalSymbols
|
|
132
|
-
async function extractFieldSchema(
|
|
123
|
+
export async function extractFieldSchema(
|
|
133
124
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
134
125
|
target, ctor, metadata, name) {
|
|
135
126
|
// Do nothing. This method is used by external modules for extending the factory
|
|
136
127
|
}
|
|
137
|
-
exports.extractFieldSchema = extractFieldSchema;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const collection_js_1 = require("../resource/collection.js");
|
|
6
|
-
const singleton_js_1 = require("../resource/singleton.js");
|
|
7
|
-
async function processResourceQueue() {
|
|
1
|
+
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
+
import { Collection } from '../resource/collection.js';
|
|
3
|
+
import { Singleton } from '../resource/singleton.js';
|
|
4
|
+
export async function processResourceQueue() {
|
|
8
5
|
const { document, resourceQueue } = this;
|
|
9
6
|
const resourceNames = Array.from(resourceQueue.keys());
|
|
10
7
|
for (const name of resourceNames) {
|
|
@@ -12,12 +9,12 @@ async function processResourceQueue() {
|
|
|
12
9
|
if (!schema)
|
|
13
10
|
continue;
|
|
14
11
|
try {
|
|
15
|
-
if (
|
|
12
|
+
if (OpraSchema.isCollection(schema)) {
|
|
16
13
|
const resource = await this.createCollection(name, schema);
|
|
17
14
|
document.resources.set(name, resource);
|
|
18
15
|
continue;
|
|
19
16
|
}
|
|
20
|
-
if (
|
|
17
|
+
if (OpraSchema.isSingleton(schema)) {
|
|
21
18
|
const resource = await this.createSingleton(name, schema);
|
|
22
19
|
document.resources.set(name, resource);
|
|
23
20
|
continue;
|
|
@@ -30,8 +27,7 @@ async function processResourceQueue() {
|
|
|
30
27
|
throw new TypeError(`Invalid Resource schema: ${JSON.stringify(schema).substring(0, 20)}...`);
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
|
-
|
|
34
|
-
async function createCollection(name, schema) {
|
|
30
|
+
export async function createCollection(name, schema) {
|
|
35
31
|
const { document } = this;
|
|
36
32
|
const dataType = document.getComplexType(schema.type);
|
|
37
33
|
const initArgs = {
|
|
@@ -39,10 +35,9 @@ async function createCollection(name, schema) {
|
|
|
39
35
|
name,
|
|
40
36
|
type: dataType
|
|
41
37
|
};
|
|
42
|
-
return new
|
|
38
|
+
return new Collection(document, initArgs);
|
|
43
39
|
}
|
|
44
|
-
|
|
45
|
-
async function createSingleton(name, schema) {
|
|
40
|
+
export async function createSingleton(name, schema) {
|
|
46
41
|
const { document } = this;
|
|
47
42
|
const dataType = document.getComplexType(schema.type);
|
|
48
43
|
const initArgs = {
|
|
@@ -50,6 +45,5 @@ async function createSingleton(name, schema) {
|
|
|
50
45
|
name,
|
|
51
46
|
type: dataType
|
|
52
47
|
};
|
|
53
|
-
return new
|
|
48
|
+
return new Singleton(document, initArgs);
|
|
54
49
|
}
|
|
55
|
-
exports.createSingleton = createSingleton;
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const simple_type_js_1 = require("../data-type/simple-type.js");
|
|
9
|
-
const union_type_js_1 = require("../data-type/union-type.js");
|
|
10
|
-
async function processTypes() {
|
|
1
|
+
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
+
import { ComplexType } from '../data-type/complex-type.js';
|
|
3
|
+
import { EnumType } from '../data-type/enum-type.js';
|
|
4
|
+
import { MappedType } from '../data-type/mapped-type.js';
|
|
5
|
+
import { SimpleType } from '../data-type/simple-type.js';
|
|
6
|
+
import { UnionType } from '../data-type/union-type.js';
|
|
7
|
+
export async function processTypes() {
|
|
11
8
|
const { document, typeQueue } = this;
|
|
12
9
|
// Create DataType instances
|
|
13
10
|
for (const [name, schema] of typeQueue.entries()) {
|
|
@@ -23,36 +20,34 @@ async function processTypes() {
|
|
|
23
20
|
}
|
|
24
21
|
document.invalidate();
|
|
25
22
|
}
|
|
26
|
-
|
|
27
|
-
function createDataTypeInstance(kind, name) {
|
|
23
|
+
export function createDataTypeInstance(kind, name) {
|
|
28
24
|
const dataType = {
|
|
29
25
|
document: this.document,
|
|
30
26
|
kind,
|
|
31
27
|
name
|
|
32
28
|
};
|
|
33
29
|
switch (kind) {
|
|
34
|
-
case
|
|
35
|
-
Object.setPrototypeOf(dataType,
|
|
30
|
+
case OpraSchema.ComplexType.Kind:
|
|
31
|
+
Object.setPrototypeOf(dataType, ComplexType.prototype);
|
|
36
32
|
break;
|
|
37
|
-
case
|
|
38
|
-
Object.setPrototypeOf(dataType,
|
|
33
|
+
case OpraSchema.EnumType.Kind:
|
|
34
|
+
Object.setPrototypeOf(dataType, EnumType.prototype);
|
|
39
35
|
break;
|
|
40
|
-
case
|
|
41
|
-
Object.setPrototypeOf(dataType,
|
|
36
|
+
case OpraSchema.MappedType.Kind:
|
|
37
|
+
Object.setPrototypeOf(dataType, MappedType.prototype);
|
|
42
38
|
break;
|
|
43
|
-
case
|
|
44
|
-
Object.setPrototypeOf(dataType,
|
|
39
|
+
case OpraSchema.SimpleType.Kind:
|
|
40
|
+
Object.setPrototypeOf(dataType, SimpleType.prototype);
|
|
45
41
|
break;
|
|
46
|
-
case
|
|
47
|
-
Object.setPrototypeOf(dataType,
|
|
42
|
+
case OpraSchema.UnionType.Kind:
|
|
43
|
+
Object.setPrototypeOf(dataType, UnionType.prototype);
|
|
48
44
|
break;
|
|
49
45
|
default:
|
|
50
46
|
throw new TypeError(`Unknown DataType kind (${kind})`);
|
|
51
47
|
}
|
|
52
48
|
return dataType;
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
function addDataType(schemaOrName) {
|
|
50
|
+
export function addDataType(schemaOrName) {
|
|
56
51
|
const { document, typeQueue, circularRefs, curPath } = this;
|
|
57
52
|
const name = typeof schemaOrName === 'string' ? schemaOrName : undefined;
|
|
58
53
|
let schema;
|
|
@@ -74,13 +69,13 @@ function addDataType(schemaOrName) {
|
|
|
74
69
|
try {
|
|
75
70
|
// Init base
|
|
76
71
|
let base;
|
|
77
|
-
if ((
|
|
72
|
+
if ((OpraSchema.isSimpleType(schema) || OpraSchema.isComplexType(schema) || OpraSchema.isEnumType(schema)) && schema.base) {
|
|
78
73
|
curPath.push(typeof schema.base === 'string' ? schema.base : '[base]');
|
|
79
74
|
base = this.addDataType(schema.base);
|
|
80
75
|
curPath.pop();
|
|
81
76
|
}
|
|
82
77
|
// **** Init SimpleType ****
|
|
83
|
-
if (
|
|
78
|
+
if (OpraSchema.isSimpleType(schema)) {
|
|
84
79
|
const initArgs = {
|
|
85
80
|
...schema,
|
|
86
81
|
name,
|
|
@@ -89,13 +84,13 @@ function addDataType(schemaOrName) {
|
|
|
89
84
|
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
90
85
|
if (name)
|
|
91
86
|
curPath.push(name);
|
|
92
|
-
|
|
87
|
+
SimpleType.apply(dataType, [document, initArgs]);
|
|
93
88
|
if (name)
|
|
94
89
|
curPath.pop();
|
|
95
90
|
return dataType;
|
|
96
91
|
}
|
|
97
92
|
// **** Init ComplexType ****
|
|
98
|
-
if (
|
|
93
|
+
if (OpraSchema.isComplexType(schema)) {
|
|
99
94
|
const initArgs = {
|
|
100
95
|
...schema,
|
|
101
96
|
name,
|
|
@@ -104,7 +99,7 @@ function addDataType(schemaOrName) {
|
|
|
104
99
|
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
105
100
|
if (name)
|
|
106
101
|
curPath.push(name);
|
|
107
|
-
|
|
102
|
+
ComplexType.apply(dataType, [document, initArgs]);
|
|
108
103
|
if (name)
|
|
109
104
|
typeQueue.delete(name);
|
|
110
105
|
// process fields
|
|
@@ -126,7 +121,7 @@ function addDataType(schemaOrName) {
|
|
|
126
121
|
return dataType;
|
|
127
122
|
}
|
|
128
123
|
// **** Init EnumType ****
|
|
129
|
-
if (
|
|
124
|
+
if (OpraSchema.isEnumType(schema)) {
|
|
130
125
|
const initArgs = {
|
|
131
126
|
...schema,
|
|
132
127
|
name,
|
|
@@ -135,13 +130,13 @@ function addDataType(schemaOrName) {
|
|
|
135
130
|
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
136
131
|
if (name)
|
|
137
132
|
curPath.push(name);
|
|
138
|
-
|
|
133
|
+
EnumType.apply(dataType, [document, initArgs]);
|
|
139
134
|
if (name)
|
|
140
135
|
curPath.pop();
|
|
141
136
|
return dataType;
|
|
142
137
|
}
|
|
143
138
|
// **** Init UnionType ****
|
|
144
|
-
if (
|
|
139
|
+
if (OpraSchema.isUnionType(schema)) {
|
|
145
140
|
const unionTypes = schema.types.map(t => this.addDataType(t));
|
|
146
141
|
const initArgs = {
|
|
147
142
|
...schema,
|
|
@@ -151,15 +146,15 @@ function addDataType(schemaOrName) {
|
|
|
151
146
|
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
152
147
|
if (name)
|
|
153
148
|
curPath.push(name);
|
|
154
|
-
|
|
149
|
+
UnionType.apply(dataType, [document, initArgs]);
|
|
155
150
|
if (name)
|
|
156
151
|
curPath.pop();
|
|
157
152
|
return dataType;
|
|
158
153
|
}
|
|
159
154
|
// **** Init MappedType ****
|
|
160
|
-
if (
|
|
155
|
+
if (OpraSchema.isMappedType(schema)) {
|
|
161
156
|
const dt = this.addDataType(schema.type);
|
|
162
|
-
if (!(dt instanceof
|
|
157
|
+
if (!(dt instanceof ComplexType))
|
|
163
158
|
throw new TypeError(`MappedType requires a ComplexType`);
|
|
164
159
|
const initArgs = {
|
|
165
160
|
...schema,
|
|
@@ -169,7 +164,7 @@ function addDataType(schemaOrName) {
|
|
|
169
164
|
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
170
165
|
if (name)
|
|
171
166
|
curPath.push(name);
|
|
172
|
-
|
|
167
|
+
MappedType.apply(dataType, [document, initArgs]);
|
|
173
168
|
if (name)
|
|
174
169
|
curPath.pop();
|
|
175
170
|
return dataType;
|
|
@@ -188,4 +183,3 @@ function addDataType(schemaOrName) {
|
|
|
188
183
|
}
|
|
189
184
|
throw new TypeError(`Invalid DataType schema: ${JSON.stringify(schema).substring(0, 20)}...`);
|
|
190
185
|
}
|
|
191
|
-
exports.addDataType = addDataType;
|
package/esm/document/index.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
tslib_1.__exportStar(require("./resource/resource.js"), exports);
|
|
16
|
-
tslib_1.__exportStar(require("./resource/collection.js"), exports);
|
|
17
|
-
tslib_1.__exportStar(require("./resource/singleton.js"), exports);
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './api-document.js';
|
|
4
|
+
export * from './factory/index.js';
|
|
5
|
+
export * from './data-type/data-type.js';
|
|
6
|
+
export * from './data-type/complex-type.js';
|
|
7
|
+
export * from './data-type/api-field.js';
|
|
8
|
+
export * from './data-type/enum-type.js';
|
|
9
|
+
export * from './data-type/mapped-type.js';
|
|
10
|
+
export * from './data-type/simple-type.js';
|
|
11
|
+
export * from './data-type/union-type.js';
|
|
12
|
+
export * from './resource/resource.js';
|
|
13
|
+
export * from './resource/collection.js';
|
|
14
|
+
export * from './resource/singleton.js';
|
|
@@ -1,44 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const index_js_5 = require("../../schema/index.js");
|
|
12
|
-
const constants_js_1 = require("../constants.js");
|
|
13
|
-
const simple_type_js_1 = require("../data-type/simple-type.js");
|
|
14
|
-
const resource_js_1 = require("./resource.js");
|
|
1
|
+
import omit from 'lodash.omit';
|
|
2
|
+
import merge from 'putil-merge';
|
|
3
|
+
import { BadRequestError } from '../../exception/index.js';
|
|
4
|
+
import { ArithmeticExpression, ArrayExpression, ComparisonExpression, LogicalExpression, ParenthesesExpression, QualifiedIdentifier } from '../../filter/index.js';
|
|
5
|
+
import { omitUndefined } from '../../helpers/index.js';
|
|
6
|
+
import { translate } from '../../i18n/index.js';
|
|
7
|
+
import { OpraSchema } from '../../schema/index.js';
|
|
8
|
+
import { METADATA_KEY } from '../constants.js';
|
|
9
|
+
import { SimpleType } from '../data-type/simple-type.js';
|
|
10
|
+
import { Resource } from './resource.js';
|
|
15
11
|
const NESTJS_INJECTABLE_WATERMARK = '__injectable__';
|
|
16
12
|
const NAME_PATTERN = /^(.*)(Resource|Collection)$/;
|
|
17
13
|
/**
|
|
18
14
|
*
|
|
19
15
|
*/
|
|
20
|
-
|
|
16
|
+
export const Collection = function (...args) {
|
|
21
17
|
// ClassDecorator
|
|
22
18
|
if (!this) {
|
|
23
19
|
const [type, options] = args;
|
|
24
20
|
return function (target) {
|
|
25
21
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
26
|
-
const metadata = Reflect.getOwnMetadata(
|
|
27
|
-
const baseMetadata = Reflect.getOwnMetadata(
|
|
22
|
+
const metadata = Reflect.getOwnMetadata(METADATA_KEY, target) || {};
|
|
23
|
+
const baseMetadata = Reflect.getOwnMetadata(METADATA_KEY, Object.getPrototypeOf(target));
|
|
28
24
|
if (baseMetadata) {
|
|
29
|
-
(
|
|
25
|
+
merge(metadata, baseMetadata, { deep: true });
|
|
30
26
|
}
|
|
31
|
-
metadata.kind =
|
|
27
|
+
metadata.kind = OpraSchema.Collection.Kind;
|
|
32
28
|
metadata.name = name;
|
|
33
29
|
metadata.type = type;
|
|
34
30
|
// Merge with previous metadata object
|
|
35
|
-
const m = Reflect.getMetadata(
|
|
31
|
+
const m = Reflect.getMetadata(METADATA_KEY, target);
|
|
36
32
|
if (m && metadata !== m)
|
|
37
|
-
Object.assign(metadata, (
|
|
33
|
+
Object.assign(metadata, omit(m), Object.keys(metadata));
|
|
38
34
|
// Merge options
|
|
39
35
|
if (options)
|
|
40
|
-
Object.assign(metadata, (
|
|
41
|
-
Reflect.defineMetadata(
|
|
36
|
+
Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
|
|
37
|
+
Reflect.defineMetadata(METADATA_KEY, metadata, target);
|
|
42
38
|
/* Define Injectable metadata for NestJS support*/
|
|
43
39
|
Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
|
|
44
40
|
};
|
|
@@ -46,14 +42,14 @@ exports.Collection = function (...args) {
|
|
|
46
42
|
// Constructor
|
|
47
43
|
const [document, init] = args;
|
|
48
44
|
// call super()
|
|
49
|
-
|
|
45
|
+
Resource.apply(this, [document, init]);
|
|
50
46
|
};
|
|
51
47
|
const proto = {
|
|
52
48
|
_construct(init) {
|
|
53
49
|
// call super()
|
|
54
|
-
|
|
50
|
+
Resource.prototype._construct.call(this, init);
|
|
55
51
|
const _this = this;
|
|
56
|
-
_this.kind =
|
|
52
|
+
_this.kind = OpraSchema.Collection.Kind;
|
|
57
53
|
_this.controller = init.controller;
|
|
58
54
|
const operations = _this.operations = init.operations || {};
|
|
59
55
|
const dataType = _this.type = init.type;
|
|
@@ -64,9 +60,9 @@ const proto = {
|
|
|
64
60
|
if (!_this.primaryKey.length)
|
|
65
61
|
throw new TypeError(`You must provide primaryKey for Collection resource ("${_this.name}")`);
|
|
66
62
|
_this.primaryKey.forEach(f => {
|
|
67
|
-
const
|
|
68
|
-
if (!(
|
|
69
|
-
throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${
|
|
63
|
+
const field = dataType.getField(f);
|
|
64
|
+
if (!(field?.type instanceof SimpleType))
|
|
65
|
+
throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${field.type.kind}`);
|
|
70
66
|
});
|
|
71
67
|
if (_this.controller) {
|
|
72
68
|
const instance = typeof _this.controller == 'function'
|
|
@@ -83,8 +79,8 @@ const proto = {
|
|
|
83
79
|
}
|
|
84
80
|
},
|
|
85
81
|
exportSchema() {
|
|
86
|
-
const out =
|
|
87
|
-
Object.assign(out,
|
|
82
|
+
const out = Resource.prototype.exportSchema.call(this);
|
|
83
|
+
Object.assign(out, omitUndefined({
|
|
88
84
|
type: this.type.name,
|
|
89
85
|
operations: this.operations,
|
|
90
86
|
primaryKey: this.primaryKey
|
|
@@ -122,64 +118,63 @@ const proto = {
|
|
|
122
118
|
return result;
|
|
123
119
|
}
|
|
124
120
|
},
|
|
125
|
-
|
|
126
|
-
return this.type.
|
|
121
|
+
normalizeFieldPath(path) {
|
|
122
|
+
return this.type.normalizeFieldPath(path);
|
|
127
123
|
},
|
|
128
124
|
normalizeSortFields(fields) {
|
|
129
|
-
const normalized = this.
|
|
130
|
-
if (!normalized)
|
|
131
|
-
return;
|
|
125
|
+
const normalized = this.type.normalizeFieldPath(fields);
|
|
132
126
|
const findManyEndpoint = this.operations.findMany;
|
|
133
127
|
const sortFields = findManyEndpoint && findManyEndpoint.sortFields;
|
|
134
128
|
normalized.forEach(field => {
|
|
135
129
|
if (!sortFields?.find(x => x === field))
|
|
136
|
-
throw new
|
|
137
|
-
message:
|
|
130
|
+
throw new BadRequestError({
|
|
131
|
+
message: translate('error:UNACCEPTED_SORT_FIELD', { field }, `Field '${field}' is not available for sort operation`),
|
|
138
132
|
});
|
|
139
133
|
});
|
|
140
134
|
return normalized;
|
|
141
135
|
},
|
|
142
|
-
|
|
143
|
-
if (ast instanceof
|
|
144
|
-
this.
|
|
136
|
+
normalizeFilter(ast) {
|
|
137
|
+
if (ast instanceof ComparisonExpression) {
|
|
138
|
+
this.normalizeFilter(ast.left);
|
|
145
139
|
}
|
|
146
|
-
else if (ast instanceof
|
|
147
|
-
ast.items.forEach(item => this.
|
|
140
|
+
else if (ast instanceof LogicalExpression) {
|
|
141
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
148
142
|
}
|
|
149
|
-
else if (ast instanceof
|
|
150
|
-
ast.items.forEach(item => this.
|
|
143
|
+
else if (ast instanceof ArithmeticExpression) {
|
|
144
|
+
ast.items.forEach(item => this.normalizeFilter(item.expression));
|
|
151
145
|
}
|
|
152
|
-
else if (ast instanceof
|
|
153
|
-
ast.items.forEach(item => this.
|
|
146
|
+
else if (ast instanceof ArrayExpression) {
|
|
147
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
154
148
|
}
|
|
155
|
-
else if (ast instanceof
|
|
156
|
-
this.
|
|
149
|
+
else if (ast instanceof ParenthesesExpression) {
|
|
150
|
+
this.normalizeFilter(ast.expression);
|
|
157
151
|
}
|
|
158
|
-
else if (ast instanceof
|
|
159
|
-
|
|
160
|
-
ast.
|
|
152
|
+
else if (ast instanceof QualifiedIdentifier) {
|
|
153
|
+
ast.field = this.type.findField(ast.value);
|
|
154
|
+
ast.dataType = ast.field?.type || this.document.getDataType('any');
|
|
155
|
+
ast.value = this.type.normalizeFieldPath(ast.value);
|
|
161
156
|
}
|
|
162
157
|
return ast;
|
|
163
158
|
}
|
|
164
159
|
};
|
|
165
|
-
Object.assign(
|
|
166
|
-
Object.setPrototypeOf(
|
|
160
|
+
Object.assign(Collection.prototype, proto);
|
|
161
|
+
Object.setPrototypeOf(Collection.prototype, Resource.prototype);
|
|
167
162
|
function createOperationDecorator(operation) {
|
|
168
163
|
return (options) => ((target, propertyKey) => {
|
|
169
164
|
const metadata = {
|
|
170
165
|
...options,
|
|
171
166
|
handlerName: propertyKey
|
|
172
167
|
};
|
|
173
|
-
const resourceMetadata = (Reflect.getOwnMetadata(
|
|
168
|
+
const resourceMetadata = (Reflect.getOwnMetadata(METADATA_KEY, target.constructor) || {});
|
|
174
169
|
resourceMetadata.operations = resourceMetadata.operations || {};
|
|
175
170
|
resourceMetadata.operations[operation] = metadata;
|
|
176
|
-
Reflect.defineMetadata(
|
|
171
|
+
Reflect.defineMetadata(METADATA_KEY, resourceMetadata, target.constructor);
|
|
177
172
|
});
|
|
178
173
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
174
|
+
Collection.Create = createOperationDecorator('create');
|
|
175
|
+
Collection.Delete = createOperationDecorator('delete');
|
|
176
|
+
Collection.DeleteMany = createOperationDecorator('deleteMany');
|
|
177
|
+
Collection.Get = createOperationDecorator('get');
|
|
178
|
+
Collection.FindMany = createOperationDecorator('findMany');
|
|
179
|
+
Collection.Update = createOperationDecorator('update');
|
|
180
|
+
Collection.UpdateMany = createOperationDecorator('updateMany');
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const inspect_util_js_1 = require("../utils/inspect.util.js");
|
|
6
|
-
exports.Resource = function (document, init) {
|
|
7
|
-
if (!(this instanceof exports.Resource)) {
|
|
1
|
+
import { omitUndefined } from '../../helpers/object-utils.js';
|
|
2
|
+
import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../utils/inspect.util.js';
|
|
3
|
+
export const Resource = function (document, init) {
|
|
4
|
+
if (!(this instanceof Resource)) {
|
|
8
5
|
throw new TypeError(`Class constructor must be called with "new" keyword`);
|
|
9
6
|
// noinspection UnreachableCodeJS
|
|
10
7
|
return;
|
|
@@ -22,7 +19,7 @@ const proto = {
|
|
|
22
19
|
// Do nothing
|
|
23
20
|
},
|
|
24
21
|
exportSchema() {
|
|
25
|
-
return
|
|
22
|
+
return omitUndefined({
|
|
26
23
|
kind: this.kind,
|
|
27
24
|
description: this.description
|
|
28
25
|
});
|
|
@@ -30,9 +27,9 @@ const proto = {
|
|
|
30
27
|
toString() {
|
|
31
28
|
return `[${Object.getPrototypeOf(this).constructor.name} ${this.name || '#anonymous'}]`;
|
|
32
29
|
},
|
|
33
|
-
[
|
|
34
|
-
return `[${
|
|
35
|
-
` ${
|
|
30
|
+
[nodeInspectCustom]() {
|
|
31
|
+
return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
|
|
32
|
+
` ${colorFgMagenta + this.name + colorReset}]`;
|
|
36
33
|
}
|
|
37
34
|
};
|
|
38
|
-
Object.assign(
|
|
35
|
+
Object.assign(Resource.prototype, proto);
|