@opra/common 0.23.2 → 0.24.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 +298 -205
- package/cjs/document/constants.js +4 -2
- package/cjs/document/data-type/api-field.js +3 -3
- package/cjs/document/data-type/complex-type.js +2 -2
- package/cjs/document/data-type/enum-type.js +5 -5
- package/cjs/document/data-type/mapped-type.js +9 -9
- package/cjs/document/data-type/simple-type.js +2 -2
- package/cjs/document/data-type/union-type.js +2 -2
- package/cjs/document/factory/create-document.js +3 -3
- package/cjs/document/factory/factory.js +6 -6
- package/cjs/document/factory/import-resource-class.js +4 -4
- package/cjs/document/factory/import-type-class.js +4 -4
- package/cjs/document/factory/process-resources.js +12 -12
- package/cjs/document/index.js +3 -2
- package/cjs/document/resource/action.js +9 -0
- package/cjs/document/resource/collection-class.js +174 -0
- package/cjs/document/resource/collection-decorator.js +60 -0
- package/cjs/document/resource/collection.js +7 -212
- package/cjs/document/resource/resource-decorator.js +16 -0
- package/cjs/document/resource/resource.js +11 -2
- package/cjs/document/resource/singleton-class.js +53 -0
- package/cjs/document/resource/singleton-decorator.js +53 -0
- package/cjs/document/resource/singleton.js +7 -85
- package/cjs/document/resource/storage-class.js +24 -0
- package/cjs/document/resource/storage-decorator.js +51 -0
- package/cjs/document/resource/storage.js +7 -53
- package/cjs/document/utils/generate-codec.js +5 -5
- package/cjs/exception/resource-errors/resource-not-found.error.js +1 -1
- package/cjs/helpers/mixin-utils.js +2 -2
- package/cjs/schema/opra-schema.ns.js +0 -1
- package/cjs/schema/type-guards.js +3 -3
- package/esm/document/constants.js +3 -1
- package/esm/document/data-type/api-field.js +4 -4
- package/esm/document/data-type/complex-type.js +3 -3
- package/esm/document/data-type/enum-type.js +6 -6
- package/esm/document/data-type/mapped-type.js +10 -10
- package/esm/document/data-type/simple-type.js +3 -3
- package/esm/document/data-type/union-type.js +3 -3
- package/esm/document/factory/create-document.js +3 -3
- package/esm/document/factory/factory.js +7 -7
- package/esm/document/factory/import-resource-class.js +5 -5
- package/esm/document/factory/import-type-class.js +5 -5
- package/esm/document/factory/process-resources.js +9 -9
- package/esm/document/index.js +3 -2
- package/esm/document/resource/action.js +5 -0
- package/esm/document/resource/collection-class.js +169 -0
- package/esm/document/resource/collection-decorator.js +55 -0
- package/esm/document/resource/collection.js +6 -211
- package/esm/document/resource/resource-decorator.js +13 -0
- package/esm/document/resource/resource.js +11 -2
- package/esm/document/resource/singleton-class.js +49 -0
- package/esm/document/resource/singleton-decorator.js +48 -0
- package/esm/document/resource/singleton.js +6 -84
- package/esm/document/resource/storage-class.js +20 -0
- package/esm/document/resource/storage-decorator.js +46 -0
- package/esm/document/resource/storage.js +6 -52
- package/esm/document/utils/generate-codec.js +3 -3
- package/esm/exception/resource-errors/resource-not-found.error.js +1 -1
- package/esm/helpers/mixin-utils.js +2 -2
- package/esm/schema/opra-schema.ns.js +0 -1
- package/esm/schema/type-guards.js +1 -1
- package/package.json +1 -1
- package/types/document/api-document.d.ts +1 -1
- package/types/document/constants.d.ts +3 -1
- package/types/document/data-type/mapped-type.d.ts +2 -2
- package/types/document/factory/factory.d.ts +7 -7
- package/types/document/factory/process-resources.d.ts +2 -2
- package/types/document/index.d.ts +3 -2
- package/types/document/interfaces/{collection-resource.interface.d.ts → collection.interface.d.ts} +1 -1
- package/types/document/interfaces/{singleton-resource.interface.d.ts → singleton.interface.d.ts} +1 -1
- package/types/document/interfaces/{storage-resource.interface.d.ts → storage.interface.d.ts} +1 -1
- package/types/document/resource/action.d.ts +4 -0
- package/types/document/resource/collection-class.d.ts +24 -0
- package/types/document/resource/collection-decorator.d.ts +30 -0
- package/types/document/resource/collection.d.ts +12 -39
- package/types/document/resource/resource-decorator.d.ts +5 -0
- package/types/document/resource/resource.d.ts +8 -0
- package/types/document/resource/singleton-class.d.ts +20 -0
- package/types/document/resource/singleton-decorator.d.ts +25 -0
- package/types/document/resource/singleton.d.ts +10 -29
- package/types/document/resource/storage-class.d.ts +12 -0
- package/types/document/resource/storage-decorator.d.ts +22 -0
- package/types/document/resource/storage.d.ts +9 -20
- package/types/document/utils/generate-codec.d.ts +1 -1
- package/types/helpers/mixin-utils.d.ts +1 -1
- package/types/schema/document.interface.d.ts +1 -1
- package/types/schema/opra-schema.ns.d.ts +0 -1
- package/types/schema/resource/collection.interface.d.ts +18 -19
- package/types/schema/resource/container.interface.d.ts +4 -2
- package/types/schema/resource/resource.interface.d.ts +14 -0
- package/types/schema/resource/singleton.interface.d.ts +8 -8
- package/types/schema/resource/storage.interface.d.ts +7 -8
- package/types/schema/type-guards.d.ts +1 -1
- package/cjs/schema/resource/operation.interface.js +0 -2
- package/esm/schema/resource/operation.interface.js +0 -1
- package/types/schema/resource/operation.interface.d.ts +0 -3
- /package/cjs/document/interfaces/{collection-resource.interface.js → collection.interface.js} +0 -0
- /package/cjs/document/interfaces/{singleton-resource.interface.js → singleton.interface.js} +0 -0
- /package/cjs/document/interfaces/{storage-resource.interface.js → storage.interface.js} +0 -0
- /package/esm/document/interfaces/{collection-resource.interface.js → collection.interface.js} +0 -0
- /package/esm/document/interfaces/{singleton-resource.interface.js → singleton.interface.js} +0 -0
- /package/esm/document/interfaces/{storage-resource.interface.js → storage.interface.js} +0 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.DECORATOR = exports.SOURCE_METADATA = exports.DATATYPE_METADATA = void 0;
|
|
4
|
+
exports.DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
|
|
5
|
+
exports.SOURCE_METADATA = Symbol('SOURCE_METADATA');
|
|
6
|
+
exports.DECORATOR = Symbol('DECORATOR');
|
|
5
7
|
exports.NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
6
8
|
exports.TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
|
|
@@ -15,7 +15,7 @@ exports.ApiField = function (...args) {
|
|
|
15
15
|
return function (target, propertyKey) {
|
|
16
16
|
if (typeof propertyKey !== 'string')
|
|
17
17
|
throw new TypeError(`Symbol properties can't be used as a field`);
|
|
18
|
-
const metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
18
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, target.constructor) || {};
|
|
19
19
|
metadata.kind = index_js_2.OpraSchema.ComplexType.Kind;
|
|
20
20
|
metadata.fields = metadata.fields || {};
|
|
21
21
|
const designType = Reflect.getMetadata('design:type', target, propertyKey);
|
|
@@ -39,13 +39,13 @@ exports.ApiField = function (...args) {
|
|
|
39
39
|
elemMeta.enum = enumObj;
|
|
40
40
|
}
|
|
41
41
|
else {
|
|
42
|
-
const m = Reflect.getOwnMetadata(constants_js_1.
|
|
42
|
+
const m = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, options?.enum);
|
|
43
43
|
if (!index_js_2.OpraSchema.isEnumType(m))
|
|
44
44
|
throw new TypeError(`Invalid "enum" value. Did you forget to set metadata using EnumType() method?`);
|
|
45
45
|
elemMeta.enum = options.enum;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
Reflect.defineMetadata(constants_js_1.
|
|
48
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, (0, index_js_1.omitUndefined)(metadata), target.constructor);
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
// Constructor
|
|
@@ -166,10 +166,10 @@ exports.ComplexType = function (...args) {
|
|
|
166
166
|
const [options] = args;
|
|
167
167
|
return function (target) {
|
|
168
168
|
const name = options?.name || target.name.match(constants_js_1.TYPENAME_PATTERN)?.[1] || target.name;
|
|
169
|
-
let metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
169
|
+
let metadata = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, target);
|
|
170
170
|
if (!metadata) {
|
|
171
171
|
metadata = {};
|
|
172
|
-
Reflect.defineMetadata(constants_js_1.
|
|
172
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, target);
|
|
173
173
|
}
|
|
174
174
|
metadata.kind = index_js_3.OpraSchema.ComplexType.Kind;
|
|
175
175
|
metadata.name = name;
|
|
@@ -44,20 +44,20 @@ exports.EnumType = function (...args) {
|
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
// Injector
|
|
47
|
-
const [
|
|
48
|
-
const values = Array.isArray(
|
|
49
|
-
?
|
|
47
|
+
const [enumSource, options] = args;
|
|
48
|
+
const values = Array.isArray(enumSource)
|
|
49
|
+
? enumSource.reduce((obj, v) => {
|
|
50
50
|
obj[v] = v;
|
|
51
51
|
return obj;
|
|
52
52
|
}, {})
|
|
53
|
-
:
|
|
53
|
+
: enumSource;
|
|
54
54
|
const metadata = {
|
|
55
55
|
kind: index_js_2.OpraSchema.EnumType.Kind,
|
|
56
56
|
values
|
|
57
57
|
};
|
|
58
58
|
if (options)
|
|
59
59
|
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'values']));
|
|
60
|
-
Reflect.defineMetadata(constants_js_1.
|
|
60
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, enumSource);
|
|
61
61
|
return values;
|
|
62
62
|
};
|
|
63
63
|
exports.EnumType.prototype = EnumTypeClass.prototype;
|
|
@@ -48,30 +48,30 @@ exports.MappedType = function (...args) {
|
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
50
|
// MappedType helper
|
|
51
|
-
const [
|
|
51
|
+
const [mappedSource, options] = args;
|
|
52
52
|
const isInheritedPredicate = getIsInheritedPredicateFn(options.pick, options.omit);
|
|
53
53
|
class MappedClass {
|
|
54
54
|
constructor() {
|
|
55
|
-
(0, index_js_1.inheritPropertyInitializers)(this,
|
|
55
|
+
(0, index_js_1.inheritPropertyInitializers)(this, mappedSource, isInheritedPredicate);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
(0, index_js_1.mergePrototype)(MappedClass.prototype,
|
|
58
|
+
(0, index_js_1.mergePrototype)(MappedClass.prototype, mappedSource.prototype);
|
|
59
59
|
// const mappedTypeMetadata: MappedType.TypeMapping[] = [];
|
|
60
|
-
const m = Reflect.getOwnMetadata(constants_js_1.
|
|
60
|
+
const m = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, mappedSource);
|
|
61
61
|
if (!m)
|
|
62
|
-
throw new TypeError(`Class "${
|
|
62
|
+
throw new TypeError(`Class "${mappedSource}" doesn't have datatype metadata information`);
|
|
63
63
|
if (!(m.kind === index_js_2.OpraSchema.ComplexType.Kind))
|
|
64
|
-
throw new TypeError(`Class "${
|
|
64
|
+
throw new TypeError(`Class "${mappedSource}" is not a ${index_js_2.OpraSchema.ComplexType.Kind}`);
|
|
65
65
|
const metadata = {
|
|
66
66
|
kind: 'MappedType',
|
|
67
|
-
type:
|
|
67
|
+
type: mappedSource
|
|
68
68
|
};
|
|
69
69
|
if (options.pick)
|
|
70
70
|
metadata.pick = options.pick;
|
|
71
71
|
if (options.omit)
|
|
72
72
|
metadata.omit = options.omit;
|
|
73
|
-
Reflect.defineMetadata(constants_js_1.
|
|
74
|
-
exports.MappedType._applyMixin(MappedClass,
|
|
73
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, MappedClass);
|
|
74
|
+
exports.MappedType._applyMixin(MappedClass, mappedSource, {
|
|
75
75
|
...options,
|
|
76
76
|
isInheritedPredicate
|
|
77
77
|
});
|
|
@@ -45,12 +45,12 @@ exports.SimpleType = function (...args) {
|
|
|
45
45
|
return function (target) {
|
|
46
46
|
let name = options?.name || target.name.match(constants_js_1.TYPENAME_PATTERN)?.[1] || target.name;
|
|
47
47
|
name = name.charAt(0).toLowerCase() + name.substring(1);
|
|
48
|
-
const metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
48
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, target) || {};
|
|
49
49
|
metadata.kind = index_js_1.OpraSchema.SimpleType.Kind;
|
|
50
50
|
metadata.name = name;
|
|
51
51
|
if (options)
|
|
52
52
|
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name']));
|
|
53
|
-
Reflect.defineMetadata(constants_js_1.
|
|
53
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, target);
|
|
54
54
|
};
|
|
55
55
|
};
|
|
56
56
|
exports.SimpleType.prototype = SimpleTypeClass.prototype;
|
|
@@ -63,9 +63,9 @@ exports.UnionType = function (...args) {
|
|
|
63
63
|
kind: index_js_2.OpraSchema.UnionType.Kind,
|
|
64
64
|
types: []
|
|
65
65
|
};
|
|
66
|
-
Reflect.defineMetadata(constants_js_1.
|
|
66
|
+
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, UnionClass);
|
|
67
67
|
for (const c of clasRefs) {
|
|
68
|
-
const itemMeta = Reflect.getMetadata(constants_js_1.
|
|
68
|
+
const itemMeta = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, c);
|
|
69
69
|
if (!(itemMeta && (itemMeta.kind === index_js_2.OpraSchema.ComplexType.Kind || itemMeta.kind === index_js_2.OpraSchema.UnionType.Kind ||
|
|
70
70
|
itemMeta.kind === index_js_2.OpraSchema.MappedType.Kind)))
|
|
71
71
|
throw new TypeError(`Class "${c.name}" is not a ${index_js_2.OpraSchema.ComplexType.Kind}, ${index_js_2.OpraSchema.UnionType.Kind} or ${index_js_2.OpraSchema.MappedType.Kind}`);
|
|
@@ -30,11 +30,11 @@ async function createDocument(init, options) {
|
|
|
30
30
|
this.curPath.push('resources');
|
|
31
31
|
if (Array.isArray(init.resources)) {
|
|
32
32
|
for (const res of init.resources)
|
|
33
|
-
await this.
|
|
33
|
+
await this.importSourceClass(res);
|
|
34
34
|
}
|
|
35
35
|
else
|
|
36
|
-
this.
|
|
37
|
-
await this.
|
|
36
|
+
this.sourceQueue.setAll(init.resources);
|
|
37
|
+
await this.processSourceQueue();
|
|
38
38
|
this.curPath.pop();
|
|
39
39
|
}
|
|
40
40
|
this.document.types.sort();
|
|
@@ -17,7 +17,7 @@ class DocumentFactory {
|
|
|
17
17
|
constructor() {
|
|
18
18
|
this.document = new api_document_js_1.ApiDocument();
|
|
19
19
|
this.typeQueue = new index_js_1.ResponsiveMap();
|
|
20
|
-
this.
|
|
20
|
+
this.sourceQueue = new index_js_1.ResponsiveMap();
|
|
21
21
|
this.circularRefs = new index_js_1.ResponsiveMap();
|
|
22
22
|
this.curPath = [];
|
|
23
23
|
this.cache = new Map();
|
|
@@ -56,11 +56,11 @@ DocumentFactory.designTypeMap = new Map();
|
|
|
56
56
|
_a.prototype.processTypes = process_types_js_1.processTypes;
|
|
57
57
|
_a.prototype.createDataTypeInstance = process_types_js_1.createDataTypeInstance;
|
|
58
58
|
_a.prototype.addDataType = process_types_js_1.addDataType;
|
|
59
|
-
_a.prototype.
|
|
59
|
+
_a.prototype.importSourceClass = import_resource_class_js_1.importResourceClass;
|
|
60
60
|
_a.prototype.extractSingletonSchema = import_resource_class_js_1.extractSingletonSchema;
|
|
61
61
|
_a.prototype.extractCollectionSchema = import_resource_class_js_1.extractCollectionSchema;
|
|
62
|
-
_a.prototype.
|
|
63
|
-
_a.prototype.
|
|
64
|
-
_a.prototype.
|
|
65
|
-
_a.prototype.
|
|
62
|
+
_a.prototype.processSourceQueue = process_resources_js_1.processSourceQueue;
|
|
63
|
+
_a.prototype.createCollectionSource = process_resources_js_1.createCollectionResource;
|
|
64
|
+
_a.prototype.createSingletonSource = process_resources_js_1.createSingletonResource;
|
|
65
|
+
_a.prototype.createFileSource = process_resources_js_1.createStorageResource;
|
|
66
66
|
})();
|
|
@@ -6,14 +6,14 @@ const index_js_1 = require("../../helpers/index.js");
|
|
|
6
6
|
const index_js_2 = require("../../schema/index.js");
|
|
7
7
|
const constants_js_1 = require("../constants.js");
|
|
8
8
|
async function importResourceClass(thunk) {
|
|
9
|
-
const { document,
|
|
9
|
+
const { document, sourceQueue, cache } = this;
|
|
10
10
|
const controller = await (0, index_js_1.resolveThunk)(thunk);
|
|
11
11
|
const cached = cache.get(controller);
|
|
12
12
|
if (cached)
|
|
13
13
|
return cached;
|
|
14
14
|
const ctor = typeof thunk === 'function' ? thunk : Object.getPrototypeOf(thunk).constructor;
|
|
15
|
-
let metadata = Reflect.getMetadata(constants_js_1.
|
|
16
|
-
if (!metadata && index_js_2.OpraSchema.
|
|
15
|
+
let metadata = Reflect.getMetadata(constants_js_1.SOURCE_METADATA, ctor);
|
|
16
|
+
if (!metadata && index_js_2.OpraSchema.isSource(metadata))
|
|
17
17
|
throw new TypeError(`Class "${ctor.name}" doesn't have a valid Resource metadata`);
|
|
18
18
|
// const controller = typeof thunk === 'function' ? new ctor() : thunk;
|
|
19
19
|
// Clone metadata to prevent changing its contents
|
|
@@ -37,7 +37,7 @@ async function importResourceClass(thunk) {
|
|
|
37
37
|
await this.extractSingletonSchema(schema, ctor, metadata, controller);
|
|
38
38
|
if (index_js_2.OpraSchema.isCollection(schema))
|
|
39
39
|
await this.extractCollectionSchema(schema, ctor, metadata, controller);
|
|
40
|
-
|
|
40
|
+
sourceQueue.set(metadata.name, schema);
|
|
41
41
|
}
|
|
42
42
|
exports.importResourceClass = importResourceClass;
|
|
43
43
|
async function extractSingletonSchema(
|
|
@@ -14,7 +14,7 @@ async function importTypeClass(typeThunk) {
|
|
|
14
14
|
const dt = document.getDataType(thunk, true);
|
|
15
15
|
if (dt && dt.name)
|
|
16
16
|
return dt.name;
|
|
17
|
-
const metadata = Reflect.getMetadata(constants_js_1.
|
|
17
|
+
const metadata = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, thunk);
|
|
18
18
|
if (!(metadata && index_js_2.OpraSchema.isDataType(metadata))) {
|
|
19
19
|
// If thunk is a Type class
|
|
20
20
|
if ((0, index_js_1.isConstructor)(thunk))
|
|
@@ -51,7 +51,7 @@ async function importTypeClass(typeThunk) {
|
|
|
51
51
|
const enumObject = thunk;
|
|
52
52
|
if (index_js_2.OpraSchema.isEnumType(schema)) {
|
|
53
53
|
let baseType;
|
|
54
|
-
if (metadata.base && Reflect.hasMetadata(constants_js_1.
|
|
54
|
+
if (metadata.base && Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, metadata.base)) {
|
|
55
55
|
baseType = await this.importTypeClass(metadata.base);
|
|
56
56
|
}
|
|
57
57
|
schema.base = baseType;
|
|
@@ -66,7 +66,7 @@ async function extractSimpleTypeSchema(target, ctor,
|
|
|
66
66
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
67
67
|
metadata) {
|
|
68
68
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
69
|
-
if (Reflect.hasMetadata(constants_js_1.
|
|
69
|
+
if (Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, baseClass))
|
|
70
70
|
target.base = await this.importTypeClass(baseClass);
|
|
71
71
|
if (typeof ctor.prototype.decode === 'function')
|
|
72
72
|
target.decoder = (0, valgen_1.validator)(metadata.name, ctor.prototype.decode);
|
|
@@ -76,7 +76,7 @@ metadata) {
|
|
|
76
76
|
exports.extractSimpleTypeSchema = extractSimpleTypeSchema;
|
|
77
77
|
async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
78
78
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
79
|
-
if (Reflect.hasMetadata(constants_js_1.
|
|
79
|
+
if (Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, baseClass))
|
|
80
80
|
target.base = await this.importTypeClass(baseClass);
|
|
81
81
|
target.ctor = target.ctor || ctor;
|
|
82
82
|
// Fields
|
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createStorageResource = exports.createSingletonResource = exports.createCollectionResource = exports.processSourceQueue = void 0;
|
|
4
4
|
const index_js_1 = require("../../schema/index.js");
|
|
5
5
|
const collection_js_1 = require("../resource/collection.js");
|
|
6
6
|
const singleton_js_1 = require("../resource/singleton.js");
|
|
7
7
|
const storage_js_1 = require("../resource/storage.js");
|
|
8
|
-
async function
|
|
9
|
-
const { document,
|
|
10
|
-
const
|
|
11
|
-
for (const name of
|
|
12
|
-
const schema =
|
|
8
|
+
async function processSourceQueue() {
|
|
9
|
+
const { document, sourceQueue } = this;
|
|
10
|
+
const sourceNames = Array.from(sourceQueue.keys());
|
|
11
|
+
for (const name of sourceNames) {
|
|
12
|
+
const schema = sourceQueue.get(name);
|
|
13
13
|
if (!schema)
|
|
14
14
|
continue;
|
|
15
15
|
try {
|
|
16
16
|
if (index_js_1.OpraSchema.isCollection(schema)) {
|
|
17
|
-
const resource = await this.
|
|
17
|
+
const resource = await this.createCollectionSource(name, schema);
|
|
18
18
|
document.resources.set(name, resource);
|
|
19
19
|
continue;
|
|
20
20
|
}
|
|
21
21
|
if (index_js_1.OpraSchema.isSingleton(schema)) {
|
|
22
|
-
const resource = await this.
|
|
22
|
+
const resource = await this.createSingletonSource(name, schema);
|
|
23
23
|
document.resources.set(name, resource);
|
|
24
24
|
continue;
|
|
25
25
|
}
|
|
26
26
|
if (index_js_1.OpraSchema.isStorage(schema)) {
|
|
27
|
-
const resource = await this.
|
|
27
|
+
const resource = await this.createFileSource(name, schema);
|
|
28
28
|
document.resources.set(name, resource);
|
|
29
29
|
continue;
|
|
30
30
|
}
|
|
@@ -36,7 +36,7 @@ async function processResourceQueue() {
|
|
|
36
36
|
throw new TypeError(`Invalid Resource schema: ${JSON.stringify(schema).substring(0, 20)}...`);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
exports.
|
|
39
|
+
exports.processSourceQueue = processSourceQueue;
|
|
40
40
|
async function createCollectionResource(name, schema) {
|
|
41
41
|
const { document } = this;
|
|
42
42
|
const dataType = document.getComplexType(schema.type);
|
|
@@ -59,7 +59,7 @@ async function createSingletonResource(name, schema) {
|
|
|
59
59
|
return new singleton_js_1.Singleton(document, initArgs);
|
|
60
60
|
}
|
|
61
61
|
exports.createSingletonResource = createSingletonResource;
|
|
62
|
-
async function
|
|
62
|
+
async function createStorageResource(name, schema) {
|
|
63
63
|
const { document } = this;
|
|
64
64
|
const initArgs = {
|
|
65
65
|
...schema,
|
|
@@ -67,4 +67,4 @@ async function createFileResource(name, schema) {
|
|
|
67
67
|
};
|
|
68
68
|
return new storage_js_1.Storage(document, initArgs);
|
|
69
69
|
}
|
|
70
|
-
exports.
|
|
70
|
+
exports.createStorageResource = createStorageResource;
|
package/cjs/document/index.js
CHANGED
|
@@ -16,5 +16,6 @@ tslib_1.__exportStar(require("./resource/resource.js"), exports);
|
|
|
16
16
|
tslib_1.__exportStar(require("./resource/collection.js"), exports);
|
|
17
17
|
tslib_1.__exportStar(require("./resource/singleton.js"), exports);
|
|
18
18
|
tslib_1.__exportStar(require("./resource/storage.js"), exports);
|
|
19
|
-
tslib_1.__exportStar(require("./interfaces/collection
|
|
20
|
-
tslib_1.__exportStar(require("./interfaces/singleton
|
|
19
|
+
tslib_1.__exportStar(require("./interfaces/collection.interface.js"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./interfaces/singleton.interface.js"), exports);
|
|
21
|
+
tslib_1.__exportStar(require("./interfaces/storage.interface.js"), exports);
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CollectionClass = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const vg = tslib_1.__importStar(require("valgen"));
|
|
6
|
+
const index_js_1 = require("../../exception/index.js");
|
|
7
|
+
const index_js_2 = require("../../filter/index.js");
|
|
8
|
+
const object_utils_js_1 = require("../../helpers/object-utils.js");
|
|
9
|
+
const index_js_3 = require("../../i18n/index.js");
|
|
10
|
+
const index_js_4 = require("../../schema/index.js");
|
|
11
|
+
const simple_type_js_1 = require("../data-type/simple-type.js");
|
|
12
|
+
const generate_codec_js_1 = require("../utils/generate-codec.js");
|
|
13
|
+
const resource_js_1 = require("./resource.js");
|
|
14
|
+
class CollectionClass extends resource_js_1.Resource {
|
|
15
|
+
constructor(document, init) {
|
|
16
|
+
super(document, init);
|
|
17
|
+
this._decoders = {};
|
|
18
|
+
this._encoders = {};
|
|
19
|
+
this.kind = index_js_4.OpraSchema.Collection.Kind;
|
|
20
|
+
this.controller = init.controller;
|
|
21
|
+
this.operations = { ...init.operations };
|
|
22
|
+
const dataType = this.type = init.type;
|
|
23
|
+
// Validate key fields
|
|
24
|
+
this.primaryKey = init.primaryKey
|
|
25
|
+
? (Array.isArray(init.primaryKey) ? init.primaryKey : [init.primaryKey])
|
|
26
|
+
: [];
|
|
27
|
+
if (!this.primaryKey.length)
|
|
28
|
+
throw new TypeError(`You must provide primaryKey for Collection resource ("${this.name}")`);
|
|
29
|
+
this.primaryKey.forEach(f => {
|
|
30
|
+
const field = dataType.getField(f);
|
|
31
|
+
if (!(field?.type instanceof simple_type_js_1.SimpleType))
|
|
32
|
+
throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${field.type.kind}`);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exportSchema() {
|
|
36
|
+
return {
|
|
37
|
+
...super.exportSchema(),
|
|
38
|
+
...(0, object_utils_js_1.omitUndefined)({
|
|
39
|
+
type: this.type.name || 'object',
|
|
40
|
+
operations: this.operations,
|
|
41
|
+
primaryKey: this.primaryKey
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
parseKeyValue(value) {
|
|
46
|
+
if (!this.primaryKey?.length)
|
|
47
|
+
return;
|
|
48
|
+
const dataType = this.type;
|
|
49
|
+
if (this.primaryKey.length > 1) {
|
|
50
|
+
// Build primary key/value mapped object
|
|
51
|
+
const obj = Array.isArray(value)
|
|
52
|
+
? this.primaryKey.reduce((o, k, i) => {
|
|
53
|
+
o[k] = value[i];
|
|
54
|
+
return obj;
|
|
55
|
+
}, {})
|
|
56
|
+
: value;
|
|
57
|
+
// decode values
|
|
58
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
59
|
+
const el = dataType.getField(k);
|
|
60
|
+
if (el.type instanceof simple_type_js_1.SimpleType)
|
|
61
|
+
obj[k] = el.type.decode(v);
|
|
62
|
+
if (obj[k] == null)
|
|
63
|
+
throw new TypeError(`You must provide value of primary field(s) (${k})`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const primaryKey = this.primaryKey[0];
|
|
68
|
+
if (typeof value === 'object')
|
|
69
|
+
value = value[primaryKey];
|
|
70
|
+
const el = dataType.getField(primaryKey);
|
|
71
|
+
let result;
|
|
72
|
+
if (el.type instanceof simple_type_js_1.SimpleType)
|
|
73
|
+
result = el.type.decode(value);
|
|
74
|
+
if (result == null)
|
|
75
|
+
throw new TypeError(`You must provide value of primary field(s) (${primaryKey})`);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
normalizeFieldPath(path) {
|
|
80
|
+
return this.type.normalizeFieldPath(path);
|
|
81
|
+
}
|
|
82
|
+
normalizeSortFields(fields) {
|
|
83
|
+
const normalized = this.type.normalizeFieldPath(fields);
|
|
84
|
+
if (!normalized)
|
|
85
|
+
return;
|
|
86
|
+
const findManyOp = this.operations.findMany;
|
|
87
|
+
const sortFields = findManyOp && findManyOp.sortFields;
|
|
88
|
+
(Array.isArray(normalized) ? normalized : [normalized]).forEach(field => {
|
|
89
|
+
if (!sortFields?.find(x => x === field))
|
|
90
|
+
throw new index_js_1.BadRequestError({
|
|
91
|
+
message: (0, index_js_3.translate)('error:UNACCEPTED_SORT_FIELD', { field }),
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
return normalized;
|
|
95
|
+
}
|
|
96
|
+
normalizeFilter(filter) {
|
|
97
|
+
if (!filter)
|
|
98
|
+
return;
|
|
99
|
+
const ast = typeof filter === 'string' ? index_js_2.OpraFilter.parse(filter) : filter;
|
|
100
|
+
if (ast instanceof index_js_2.OpraFilter.ComparisonExpression) {
|
|
101
|
+
this.normalizeFilter(ast.left);
|
|
102
|
+
if (!(ast.left instanceof index_js_2.OpraFilter.QualifiedIdentifier && ast.left.field))
|
|
103
|
+
throw new TypeError(`Invalid filter query. Left side should be a data field.`);
|
|
104
|
+
// Check if filtering accepted for given field
|
|
105
|
+
const findManyOp = this.operations.findMany;
|
|
106
|
+
const fieldLower = ast.left.value.toLowerCase();
|
|
107
|
+
const filterDef = (findManyOp && findManyOp.filters || [])
|
|
108
|
+
.find(f => f.field.toLowerCase() === fieldLower);
|
|
109
|
+
if (!filterDef) {
|
|
110
|
+
throw new index_js_1.BadRequestError({
|
|
111
|
+
message: (0, index_js_3.translate)('error:UNACCEPTED_FILTER_FIELD', { field: ast.left.value }),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Check if filtering endpoint accepted for given field
|
|
115
|
+
if (!filterDef.operators?.includes(ast.op))
|
|
116
|
+
throw new index_js_1.BadRequestError({
|
|
117
|
+
message: (0, index_js_3.translate)('error:UNACCEPTED_FILTER_OPERATION', { field: ast.left.value }),
|
|
118
|
+
});
|
|
119
|
+
this.normalizeFilter(ast.right);
|
|
120
|
+
return ast;
|
|
121
|
+
}
|
|
122
|
+
if (ast instanceof index_js_2.OpraFilter.LogicalExpression) {
|
|
123
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
124
|
+
return ast;
|
|
125
|
+
}
|
|
126
|
+
if (ast instanceof index_js_2.OpraFilter.ArithmeticExpression) {
|
|
127
|
+
ast.items.forEach(item => this.normalizeFilter(item.expression));
|
|
128
|
+
return ast;
|
|
129
|
+
}
|
|
130
|
+
if (ast instanceof index_js_2.OpraFilter.ArrayExpression) {
|
|
131
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
132
|
+
return ast;
|
|
133
|
+
}
|
|
134
|
+
if (ast instanceof index_js_2.OpraFilter.ParenthesizedExpression) {
|
|
135
|
+
this.normalizeFilter(ast.expression);
|
|
136
|
+
return ast;
|
|
137
|
+
}
|
|
138
|
+
if (ast instanceof index_js_2.OpraFilter.QualifiedIdentifier) {
|
|
139
|
+
const normalizedFieldPath = this.type.normalizeFieldPath(ast.value)?.join('.');
|
|
140
|
+
ast.field = this.type.getField(normalizedFieldPath);
|
|
141
|
+
ast.dataType = ast.field?.type || this.document.getDataType('any');
|
|
142
|
+
ast.value = normalizedFieldPath;
|
|
143
|
+
return ast;
|
|
144
|
+
}
|
|
145
|
+
return ast;
|
|
146
|
+
}
|
|
147
|
+
getDecoder(endpoint) {
|
|
148
|
+
let decoder = this._decoders[endpoint];
|
|
149
|
+
if (decoder)
|
|
150
|
+
return decoder;
|
|
151
|
+
const options = {
|
|
152
|
+
partial: endpoint !== 'create'
|
|
153
|
+
};
|
|
154
|
+
if (endpoint !== 'create')
|
|
155
|
+
options.omit = [...this.primaryKey];
|
|
156
|
+
decoder = (0, generate_codec_js_1.generateCodec)(this.type, 'decode', options);
|
|
157
|
+
this._decoders[endpoint] = decoder;
|
|
158
|
+
return decoder;
|
|
159
|
+
}
|
|
160
|
+
getEncoder(endpoint) {
|
|
161
|
+
let encoder = this._encoders[endpoint];
|
|
162
|
+
if (encoder)
|
|
163
|
+
return encoder;
|
|
164
|
+
const options = {
|
|
165
|
+
partial: true
|
|
166
|
+
};
|
|
167
|
+
encoder = (0, generate_codec_js_1.generateCodec)(this.type, 'encode', options);
|
|
168
|
+
if (endpoint === 'findMany')
|
|
169
|
+
return vg.isArray(encoder);
|
|
170
|
+
this._encoders[endpoint] = encoder;
|
|
171
|
+
return encoder;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.CollectionClass = CollectionClass;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CollectionDecorator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
6
|
+
const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
|
|
7
|
+
const index_js_1 = require("../../schema/index.js");
|
|
8
|
+
const constants_js_1 = require("../constants.js");
|
|
9
|
+
const resource_decorator_js_1 = require("./resource-decorator.js");
|
|
10
|
+
const NAME_PATTERN = /^(.*)(Resource|Collection|Controller)$/;
|
|
11
|
+
const operationProperties = ['create', 'delete', 'deleteMany', 'get', 'findMany', 'update', 'updateMany'];
|
|
12
|
+
function CollectionDecorator(type, options) {
|
|
13
|
+
return function (target) {
|
|
14
|
+
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
15
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target) || {};
|
|
16
|
+
const baseMetadata = Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, Object.getPrototypeOf(target));
|
|
17
|
+
if (baseMetadata) {
|
|
18
|
+
(0, putil_merge_1.default)(metadata, baseMetadata, { deep: true });
|
|
19
|
+
}
|
|
20
|
+
metadata.kind = index_js_1.OpraSchema.Collection.Kind;
|
|
21
|
+
metadata.name = name;
|
|
22
|
+
metadata.type = type;
|
|
23
|
+
// Merge with previous metadata object
|
|
24
|
+
const m = Reflect.getMetadata(constants_js_1.SOURCE_METADATA, target);
|
|
25
|
+
if (m && metadata !== m)
|
|
26
|
+
Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
|
|
27
|
+
// Merge options
|
|
28
|
+
if (options)
|
|
29
|
+
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
|
|
30
|
+
Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, metadata, target);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.CollectionDecorator = CollectionDecorator;
|
|
34
|
+
Object.assign(CollectionDecorator, resource_decorator_js_1.ResourceDecorator);
|
|
35
|
+
CollectionDecorator.Create = createOperationDecorator('create');
|
|
36
|
+
CollectionDecorator.Delete = createOperationDecorator('delete');
|
|
37
|
+
CollectionDecorator.DeleteMany = createOperationDecorator('deleteMany');
|
|
38
|
+
CollectionDecorator.Get = createOperationDecorator('get');
|
|
39
|
+
CollectionDecorator.FindMany = createOperationDecorator('findMany');
|
|
40
|
+
CollectionDecorator.Update = createOperationDecorator('update');
|
|
41
|
+
CollectionDecorator.UpdateMany = createOperationDecorator('updateMany');
|
|
42
|
+
CollectionDecorator.Action = function (options) {
|
|
43
|
+
const oldDecorator = resource_decorator_js_1.ResourceDecorator.Action(options);
|
|
44
|
+
return (target, propertyKey) => {
|
|
45
|
+
if (typeof propertyKey === 'string' && operationProperties.includes(propertyKey))
|
|
46
|
+
throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
|
|
47
|
+
return oldDecorator(target, propertyKey);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
function createOperationDecorator(operation) {
|
|
51
|
+
return (options) => ((target, propertyKey) => {
|
|
52
|
+
if (propertyKey !== operation)
|
|
53
|
+
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
54
|
+
const operationMeta = { ...options };
|
|
55
|
+
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target.constructor) || {});
|
|
56
|
+
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
57
|
+
sourceMetadata.operations[operation] = operationMeta;
|
|
58
|
+
Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, sourceMetadata, target.constructor);
|
|
59
|
+
});
|
|
60
|
+
}
|