@opra/common 0.23.2 → 0.24.1
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 +319 -206
- 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 +12 -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 +18 -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 +5 -4
- 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 +8 -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 +18 -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 +3 -2
- 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 +11 -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 +7 -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,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._generateCodec = exports.generateCodec = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const vg = tslib_1.__importStar(require("valgen"));
|
|
6
6
|
const complex_type_js_1 = require("../data-type/complex-type.js");
|
|
@@ -9,10 +9,10 @@ const mapped_type_js_1 = require("../data-type/mapped-type.js");
|
|
|
9
9
|
const simple_type_js_1 = require("../data-type/simple-type.js");
|
|
10
10
|
const union_type_js_1 = require("../data-type/union-type.js");
|
|
11
11
|
function generateCodec(type, codec, options) {
|
|
12
|
-
return
|
|
12
|
+
return _generateCodec(type, codec, options);
|
|
13
13
|
}
|
|
14
14
|
exports.generateCodec = generateCodec;
|
|
15
|
-
function
|
|
15
|
+
function _generateCodec(type, codec, options) {
|
|
16
16
|
const schema = {};
|
|
17
17
|
for (const f of type.fields.values()) {
|
|
18
18
|
let fn;
|
|
@@ -20,7 +20,7 @@ function _generateDecoder(type, codec, options) {
|
|
|
20
20
|
fn = f.type[codec];
|
|
21
21
|
}
|
|
22
22
|
else if (f.type instanceof complex_type_js_1.ComplexType || f.type instanceof mapped_type_js_1.MappedType || f.type instanceof union_type_js_1.UnionType) {
|
|
23
|
-
fn =
|
|
23
|
+
fn = _generateCodec(f.type, codec, options);
|
|
24
24
|
}
|
|
25
25
|
/* istanbul ignore next */
|
|
26
26
|
if (!fn)
|
|
@@ -36,4 +36,4 @@ function _generateDecoder(type, codec, options) {
|
|
|
36
36
|
caseInSensitive: true,
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
-
exports.
|
|
39
|
+
exports._generateCodec = _generateCodec;
|
|
@@ -10,7 +10,7 @@ const opra_exception_js_1 = require("../opra-exception.js");
|
|
|
10
10
|
class ResourceNotFoundError extends opra_exception_js_1.OpraException {
|
|
11
11
|
constructor(resource, keyValue, cause) {
|
|
12
12
|
super({
|
|
13
|
-
message: (0, index_js_1.translate)(`error:RESOURCE_NOT_FOUND`,
|
|
13
|
+
message: (0, index_js_1.translate)(`error:RESOURCE_NOT_FOUND`, `Resource not found`),
|
|
14
14
|
severity: 'error',
|
|
15
15
|
code: 'RESOURCE_NOT_FOUND',
|
|
16
16
|
details: {
|
|
@@ -12,11 +12,11 @@ function mergePrototype(targetProto, baseProto, filter) {
|
|
|
12
12
|
}
|
|
13
13
|
exports.mergePrototype = mergePrototype;
|
|
14
14
|
// noinspection JSUnusedLocalSymbols
|
|
15
|
-
function inheritPropertyInitializers(target,
|
|
15
|
+
function inheritPropertyInitializers(target, clazz,
|
|
16
16
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
17
17
|
isPropertyInherited = (key) => true) {
|
|
18
18
|
try {
|
|
19
|
-
const tempInstance = new
|
|
19
|
+
const tempInstance = new clazz();
|
|
20
20
|
const propertyNames = Object.getOwnPropertyNames(tempInstance);
|
|
21
21
|
propertyNames
|
|
22
22
|
.filter((propertyName) => typeof tempInstance[propertyName] !== 'undefined' &&
|
|
@@ -10,7 +10,6 @@ tslib_1.__exportStar(require("./data-type/mapped-type.interface.js"), exports);
|
|
|
10
10
|
tslib_1.__exportStar(require("./data-type/union-type.interface.js"), exports);
|
|
11
11
|
tslib_1.__exportStar(require("./resource/collection.interface.js"), exports);
|
|
12
12
|
tslib_1.__exportStar(require("./resource/container.interface.js"), exports);
|
|
13
|
-
tslib_1.__exportStar(require("./resource/operation.interface.js"), exports);
|
|
14
13
|
tslib_1.__exportStar(require("./resource/resource.interface.js"), exports);
|
|
15
14
|
tslib_1.__exportStar(require("./resource/singleton.interface.js"), exports);
|
|
16
15
|
tslib_1.__exportStar(require("./resource/storage.interface.js"), exports);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isContainer = exports.isStorage = exports.isSingleton = exports.isCollection = exports.
|
|
3
|
+
exports.isContainer = exports.isStorage = exports.isSingleton = exports.isCollection = exports.isSource = exports.isEnumType = exports.isMappedType = exports.isUnionType = exports.isSimpleType = exports.isComplexType = exports.isDataType = void 0;
|
|
4
4
|
const complex_type_interface_js_1 = require("./data-type/complex-type.interface.js");
|
|
5
5
|
const enum_type_interface_js_1 = require("./data-type/enum-type.interface.js");
|
|
6
6
|
const mapped_type_interface_js_1 = require("./data-type/mapped-type.interface.js");
|
|
@@ -39,13 +39,14 @@ function isEnumType(obj) {
|
|
|
39
39
|
return obj && typeof obj === 'object' && obj.kind === enum_type_interface_js_1.EnumType.Kind;
|
|
40
40
|
}
|
|
41
41
|
exports.isEnumType = isEnumType;
|
|
42
|
-
function
|
|
42
|
+
function isSource(obj) {
|
|
43
43
|
return obj && typeof obj === 'object' &&
|
|
44
44
|
(obj.kind === container_interface_js_1.Container.Kind ||
|
|
45
45
|
obj.kind === collection_interface_js_1.Collection.Kind ||
|
|
46
|
-
obj.kind === singleton_interface_js_1.Singleton.Kind
|
|
46
|
+
obj.kind === singleton_interface_js_1.Singleton.Kind ||
|
|
47
|
+
obj.kind === storage_interface_js_1.Storage.Kind);
|
|
47
48
|
}
|
|
48
|
-
exports.
|
|
49
|
+
exports.isSource = isSource;
|
|
49
50
|
function isCollection(obj) {
|
|
50
51
|
return obj && typeof obj === 'object' && obj.kind === collection_interface_js_1.Collection.Kind;
|
|
51
52
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
|
|
2
|
+
export const RESOURCE_METADATA = Symbol('RESOURCE_METADATA');
|
|
3
|
+
export const DECORATOR = Symbol('DECORATOR');
|
|
2
4
|
export const NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
3
5
|
export const TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { omitUndefined } from '../../helpers/index.js';
|
|
2
2
|
import { OpraSchema } from '../../schema/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { DATATYPE_METADATA } from '../constants.js';
|
|
4
4
|
import { EnumType } from './enum-type.js';
|
|
5
5
|
/**
|
|
6
6
|
* @class ComplexType
|
|
@@ -12,7 +12,7 @@ export const ApiField = function (...args) {
|
|
|
12
12
|
return function (target, propertyKey) {
|
|
13
13
|
if (typeof propertyKey !== 'string')
|
|
14
14
|
throw new TypeError(`Symbol properties can't be used as a field`);
|
|
15
|
-
const metadata = Reflect.getOwnMetadata(
|
|
15
|
+
const metadata = Reflect.getOwnMetadata(DATATYPE_METADATA, target.constructor) || {};
|
|
16
16
|
metadata.kind = OpraSchema.ComplexType.Kind;
|
|
17
17
|
metadata.fields = metadata.fields || {};
|
|
18
18
|
const designType = Reflect.getMetadata('design:type', target, propertyKey);
|
|
@@ -36,13 +36,13 @@ export const ApiField = function (...args) {
|
|
|
36
36
|
elemMeta.enum = enumObj;
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
const m = Reflect.getOwnMetadata(
|
|
39
|
+
const m = Reflect.getOwnMetadata(DATATYPE_METADATA, options?.enum);
|
|
40
40
|
if (!OpraSchema.isEnumType(m))
|
|
41
41
|
throw new TypeError(`Invalid "enum" value. Did you forget to set metadata using EnumType() method?`);
|
|
42
42
|
elemMeta.enum = options.enum;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
Reflect.defineMetadata(
|
|
45
|
+
Reflect.defineMetadata(DATATYPE_METADATA, omitUndefined(metadata), target.constructor);
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
// Constructor
|
|
@@ -4,7 +4,7 @@ import merge from 'putil-merge';
|
|
|
4
4
|
import { omitUndefined, ResponsiveMap } from '../../helpers/index.js';
|
|
5
5
|
import { translate } from '../../i18n/index.js';
|
|
6
6
|
import { OpraSchema } from '../../schema/index.js';
|
|
7
|
-
import {
|
|
7
|
+
import { DATATYPE_METADATA, TYPENAME_PATTERN } from '../constants.js';
|
|
8
8
|
import { ApiField } from './api-field.js';
|
|
9
9
|
import { DataType } from './data-type.js';
|
|
10
10
|
/**
|
|
@@ -162,10 +162,10 @@ export const ComplexType = function (...args) {
|
|
|
162
162
|
const [options] = args;
|
|
163
163
|
return function (target) {
|
|
164
164
|
const name = options?.name || target.name.match(TYPENAME_PATTERN)?.[1] || target.name;
|
|
165
|
-
let metadata = Reflect.getOwnMetadata(
|
|
165
|
+
let metadata = Reflect.getOwnMetadata(DATATYPE_METADATA, target);
|
|
166
166
|
if (!metadata) {
|
|
167
167
|
metadata = {};
|
|
168
|
-
Reflect.defineMetadata(
|
|
168
|
+
Reflect.defineMetadata(DATATYPE_METADATA, metadata, target);
|
|
169
169
|
}
|
|
170
170
|
metadata.kind = OpraSchema.ComplexType.Kind;
|
|
171
171
|
metadata.name = name;
|
|
@@ -4,7 +4,7 @@ import merge from 'putil-merge';
|
|
|
4
4
|
import * as vg from 'valgen';
|
|
5
5
|
import { omitUndefined } from '../../helpers/index.js';
|
|
6
6
|
import { OpraSchema } from '../../schema/index.js';
|
|
7
|
-
import {
|
|
7
|
+
import { DATATYPE_METADATA } from '../constants.js';
|
|
8
8
|
import { DataType } from './data-type.js';
|
|
9
9
|
class EnumTypeClass extends DataType {
|
|
10
10
|
constructor(document, init) {
|
|
@@ -40,20 +40,20 @@ export const EnumType = function (...args) {
|
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
42
|
// Injector
|
|
43
|
-
const [
|
|
44
|
-
const values = Array.isArray(
|
|
45
|
-
?
|
|
43
|
+
const [enumSource, options] = args;
|
|
44
|
+
const values = Array.isArray(enumSource)
|
|
45
|
+
? enumSource.reduce((obj, v) => {
|
|
46
46
|
obj[v] = v;
|
|
47
47
|
return obj;
|
|
48
48
|
}, {})
|
|
49
|
-
:
|
|
49
|
+
: enumSource;
|
|
50
50
|
const metadata = {
|
|
51
51
|
kind: OpraSchema.EnumType.Kind,
|
|
52
52
|
values
|
|
53
53
|
};
|
|
54
54
|
if (options)
|
|
55
55
|
Object.assign(metadata, omit(options, ['kind', 'values']));
|
|
56
|
-
Reflect.defineMetadata(
|
|
56
|
+
Reflect.defineMetadata(DATATYPE_METADATA, metadata, enumSource);
|
|
57
57
|
return values;
|
|
58
58
|
};
|
|
59
59
|
EnumType.prototype = EnumTypeClass.prototype;
|
|
@@ -2,7 +2,7 @@ import 'reflect-metadata';
|
|
|
2
2
|
import merge from 'putil-merge';
|
|
3
3
|
import { inheritPropertyInitializers, mergePrototype, omitUndefined, ResponsiveMap } from '../../helpers/index.js';
|
|
4
4
|
import { OpraSchema } from '../../schema/index.js';
|
|
5
|
-
import {
|
|
5
|
+
import { DATATYPE_METADATA } from '../constants.js';
|
|
6
6
|
import { DataType } from './data-type.js';
|
|
7
7
|
class MappedTypeClass extends DataType {
|
|
8
8
|
constructor(document, init) {
|
|
@@ -44,30 +44,30 @@ export const MappedType = function (...args) {
|
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
// MappedType helper
|
|
47
|
-
const [
|
|
47
|
+
const [mappedSource, options] = args;
|
|
48
48
|
const isInheritedPredicate = getIsInheritedPredicateFn(options.pick, options.omit);
|
|
49
49
|
class MappedClass {
|
|
50
50
|
constructor() {
|
|
51
|
-
inheritPropertyInitializers(this,
|
|
51
|
+
inheritPropertyInitializers(this, mappedSource, isInheritedPredicate);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
mergePrototype(MappedClass.prototype,
|
|
54
|
+
mergePrototype(MappedClass.prototype, mappedSource.prototype);
|
|
55
55
|
// const mappedTypeMetadata: MappedType.TypeMapping[] = [];
|
|
56
|
-
const m = Reflect.getOwnMetadata(
|
|
56
|
+
const m = Reflect.getOwnMetadata(DATATYPE_METADATA, mappedSource);
|
|
57
57
|
if (!m)
|
|
58
|
-
throw new TypeError(`Class "${
|
|
58
|
+
throw new TypeError(`Class "${mappedSource}" doesn't have datatype metadata information`);
|
|
59
59
|
if (!(m.kind === OpraSchema.ComplexType.Kind))
|
|
60
|
-
throw new TypeError(`Class "${
|
|
60
|
+
throw new TypeError(`Class "${mappedSource}" is not a ${OpraSchema.ComplexType.Kind}`);
|
|
61
61
|
const metadata = {
|
|
62
62
|
kind: 'MappedType',
|
|
63
|
-
type:
|
|
63
|
+
type: mappedSource
|
|
64
64
|
};
|
|
65
65
|
if (options.pick)
|
|
66
66
|
metadata.pick = options.pick;
|
|
67
67
|
if (options.omit)
|
|
68
68
|
metadata.omit = options.omit;
|
|
69
|
-
Reflect.defineMetadata(
|
|
70
|
-
MappedType._applyMixin(MappedClass,
|
|
69
|
+
Reflect.defineMetadata(DATATYPE_METADATA, metadata, MappedClass);
|
|
70
|
+
MappedType._applyMixin(MappedClass, mappedSource, {
|
|
71
71
|
...options,
|
|
72
72
|
isInheritedPredicate
|
|
73
73
|
});
|
|
@@ -3,7 +3,7 @@ import omit from 'lodash.omit';
|
|
|
3
3
|
import merge from 'putil-merge';
|
|
4
4
|
import * as vg from 'valgen';
|
|
5
5
|
import { OpraSchema } from '../../schema/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { DATATYPE_METADATA, TYPENAME_PATTERN } from '../constants.js';
|
|
7
7
|
import { DataType } from './data-type.js';
|
|
8
8
|
/**
|
|
9
9
|
* @class SimpleType
|
|
@@ -41,12 +41,12 @@ export const SimpleType = function (...args) {
|
|
|
41
41
|
return function (target) {
|
|
42
42
|
let name = options?.name || target.name.match(TYPENAME_PATTERN)?.[1] || target.name;
|
|
43
43
|
name = name.charAt(0).toLowerCase() + name.substring(1);
|
|
44
|
-
const metadata = Reflect.getOwnMetadata(
|
|
44
|
+
const metadata = Reflect.getOwnMetadata(DATATYPE_METADATA, target) || {};
|
|
45
45
|
metadata.kind = OpraSchema.SimpleType.Kind;
|
|
46
46
|
metadata.name = name;
|
|
47
47
|
if (options)
|
|
48
48
|
Object.assign(metadata, omit(options, ['kind', 'name']));
|
|
49
|
-
Reflect.defineMetadata(
|
|
49
|
+
Reflect.defineMetadata(DATATYPE_METADATA, metadata, target);
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
52
|
SimpleType.prototype = SimpleTypeClass.prototype;
|
|
@@ -2,7 +2,7 @@ import 'reflect-metadata';
|
|
|
2
2
|
import merge from 'putil-merge';
|
|
3
3
|
import { inheritPropertyInitializers, mergePrototype, omitUndefined, ResponsiveMap } from '../../helpers/index.js';
|
|
4
4
|
import { OpraSchema } from '../../schema/index.js';
|
|
5
|
-
import {
|
|
5
|
+
import { DATATYPE_METADATA } from '../constants.js';
|
|
6
6
|
import { ComplexType } from './complex-type.js';
|
|
7
7
|
import { DataType } from './data-type.js';
|
|
8
8
|
import { MappedType } from './mapped-type.js';
|
|
@@ -59,9 +59,9 @@ export const UnionType = function (...args) {
|
|
|
59
59
|
kind: OpraSchema.UnionType.Kind,
|
|
60
60
|
types: []
|
|
61
61
|
};
|
|
62
|
-
Reflect.defineMetadata(
|
|
62
|
+
Reflect.defineMetadata(DATATYPE_METADATA, metadata, UnionClass);
|
|
63
63
|
for (const c of clasRefs) {
|
|
64
|
-
const itemMeta = Reflect.getMetadata(
|
|
64
|
+
const itemMeta = Reflect.getMetadata(DATATYPE_METADATA, c);
|
|
65
65
|
if (!(itemMeta && (itemMeta.kind === OpraSchema.ComplexType.Kind || itemMeta.kind === OpraSchema.UnionType.Kind ||
|
|
66
66
|
itemMeta.kind === OpraSchema.MappedType.Kind)))
|
|
67
67
|
throw new TypeError(`Class "${c.name}" is not a ${OpraSchema.ComplexType.Kind}, ${OpraSchema.UnionType.Kind} or ${OpraSchema.MappedType.Kind}`);
|
|
@@ -27,11 +27,11 @@ export async function createDocument(init, options) {
|
|
|
27
27
|
this.curPath.push('resources');
|
|
28
28
|
if (Array.isArray(init.resources)) {
|
|
29
29
|
for (const res of init.resources)
|
|
30
|
-
await this.
|
|
30
|
+
await this.importSourceClass(res);
|
|
31
31
|
}
|
|
32
32
|
else
|
|
33
|
-
this.
|
|
34
|
-
await this.
|
|
33
|
+
this.sourceQueue.setAll(init.resources);
|
|
34
|
+
await this.processSourceQueue();
|
|
35
35
|
this.curPath.pop();
|
|
36
36
|
}
|
|
37
37
|
this.document.types.sort();
|
|
@@ -5,7 +5,7 @@ import { addReferences } from './add-references.js';
|
|
|
5
5
|
import { createBuiltinTypeDocument, createDocument, createDocumentFromUrl } from './create-document.js';
|
|
6
6
|
import { extractCollectionSchema, extractSingletonSchema, importResourceClass } from './import-resource-class.js';
|
|
7
7
|
import { extractComplexTypeSchema, extractEnumTypeSchema, extractFieldSchema, extractMappedTypeSchema, extractSimpleTypeSchema, extractUnionTypeSchema, importTypeClass } from './import-type-class.js';
|
|
8
|
-
import { createCollectionResource,
|
|
8
|
+
import { createCollectionResource, createSingletonResource, createStorageResource, processSourceQueue } from './process-resources.js';
|
|
9
9
|
import { addDataType, createDataTypeInstance, processTypes } from './process-types.js';
|
|
10
10
|
/**
|
|
11
11
|
* @class DocumentFactory
|
|
@@ -14,7 +14,7 @@ export class DocumentFactory {
|
|
|
14
14
|
constructor() {
|
|
15
15
|
this.document = new ApiDocument();
|
|
16
16
|
this.typeQueue = new ResponsiveMap();
|
|
17
|
-
this.
|
|
17
|
+
this.sourceQueue = new ResponsiveMap();
|
|
18
18
|
this.circularRefs = new ResponsiveMap();
|
|
19
19
|
this.curPath = [];
|
|
20
20
|
this.cache = new Map();
|
|
@@ -52,11 +52,11 @@ DocumentFactory.designTypeMap = new Map();
|
|
|
52
52
|
_a.prototype.processTypes = processTypes;
|
|
53
53
|
_a.prototype.createDataTypeInstance = createDataTypeInstance;
|
|
54
54
|
_a.prototype.addDataType = addDataType;
|
|
55
|
-
_a.prototype.
|
|
55
|
+
_a.prototype.importSourceClass = importResourceClass;
|
|
56
56
|
_a.prototype.extractSingletonSchema = extractSingletonSchema;
|
|
57
57
|
_a.prototype.extractCollectionSchema = extractCollectionSchema;
|
|
58
|
-
_a.prototype.
|
|
59
|
-
_a.prototype.
|
|
60
|
-
_a.prototype.
|
|
61
|
-
_a.prototype.
|
|
58
|
+
_a.prototype.processSourceQueue = processSourceQueue;
|
|
59
|
+
_a.prototype.createCollectionSource = createCollectionResource;
|
|
60
|
+
_a.prototype.createSingletonSource = createSingletonResource;
|
|
61
|
+
_a.prototype.createFileSource = createStorageResource;
|
|
62
62
|
})();
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { cloneObject, resolveClass, resolveThunk } from '../../helpers/index.js';
|
|
3
3
|
import { OpraSchema } from '../../schema/index.js';
|
|
4
|
-
import {
|
|
4
|
+
import { RESOURCE_METADATA } from '../constants.js';
|
|
5
5
|
export async function importResourceClass(thunk) {
|
|
6
|
-
const { document,
|
|
6
|
+
const { document, sourceQueue, cache } = this;
|
|
7
7
|
const controller = await resolveThunk(thunk);
|
|
8
8
|
const cached = cache.get(controller);
|
|
9
9
|
if (cached)
|
|
10
10
|
return cached;
|
|
11
11
|
const ctor = typeof thunk === 'function' ? thunk : Object.getPrototypeOf(thunk).constructor;
|
|
12
|
-
let metadata = Reflect.getMetadata(
|
|
13
|
-
if (!metadata && OpraSchema.
|
|
12
|
+
let metadata = Reflect.getMetadata(RESOURCE_METADATA, ctor);
|
|
13
|
+
if (!metadata && OpraSchema.isSource(metadata))
|
|
14
14
|
throw new TypeError(`Class "${ctor.name}" doesn't have a valid Resource metadata`);
|
|
15
15
|
// const controller = typeof thunk === 'function' ? new ctor() : thunk;
|
|
16
16
|
// Clone metadata to prevent changing its contents
|
|
@@ -34,7 +34,7 @@ export async function importResourceClass(thunk) {
|
|
|
34
34
|
await this.extractSingletonSchema(schema, ctor, metadata, controller);
|
|
35
35
|
if (OpraSchema.isCollection(schema))
|
|
36
36
|
await this.extractCollectionSchema(schema, ctor, metadata, controller);
|
|
37
|
-
|
|
37
|
+
sourceQueue.set(metadata.name, schema);
|
|
38
38
|
}
|
|
39
39
|
export async function extractSingletonSchema(
|
|
40
40
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { validator } from 'valgen';
|
|
2
2
|
import { cloneObject, isConstructor, resolveThunk } from '../../helpers/index.js';
|
|
3
3
|
import { OpraSchema } from '../../schema/index.js';
|
|
4
|
-
import {
|
|
4
|
+
import { DATATYPE_METADATA } from '../constants.js';
|
|
5
5
|
export async function importTypeClass(typeThunk) {
|
|
6
6
|
const { document, typeQueue, cache } = this;
|
|
7
7
|
const thunk = await resolveThunk(typeThunk);
|
|
@@ -11,7 +11,7 @@ export async function importTypeClass(typeThunk) {
|
|
|
11
11
|
const dt = document.getDataType(thunk, true);
|
|
12
12
|
if (dt && dt.name)
|
|
13
13
|
return dt.name;
|
|
14
|
-
const metadata = Reflect.getMetadata(
|
|
14
|
+
const metadata = Reflect.getMetadata(DATATYPE_METADATA, thunk);
|
|
15
15
|
if (!(metadata && OpraSchema.isDataType(metadata))) {
|
|
16
16
|
// If thunk is a Type class
|
|
17
17
|
if (isConstructor(thunk))
|
|
@@ -48,7 +48,7 @@ export async function importTypeClass(typeThunk) {
|
|
|
48
48
|
const enumObject = thunk;
|
|
49
49
|
if (OpraSchema.isEnumType(schema)) {
|
|
50
50
|
let baseType;
|
|
51
|
-
if (metadata.base && Reflect.hasMetadata(
|
|
51
|
+
if (metadata.base && Reflect.hasMetadata(DATATYPE_METADATA, metadata.base)) {
|
|
52
52
|
baseType = await this.importTypeClass(metadata.base);
|
|
53
53
|
}
|
|
54
54
|
schema.base = baseType;
|
|
@@ -62,7 +62,7 @@ export async function extractSimpleTypeSchema(target, ctor,
|
|
|
62
62
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
63
63
|
metadata) {
|
|
64
64
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
65
|
-
if (Reflect.hasMetadata(
|
|
65
|
+
if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
|
|
66
66
|
target.base = await this.importTypeClass(baseClass);
|
|
67
67
|
if (typeof ctor.prototype.decode === 'function')
|
|
68
68
|
target.decoder = validator(metadata.name, ctor.prototype.decode);
|
|
@@ -71,7 +71,7 @@ metadata) {
|
|
|
71
71
|
}
|
|
72
72
|
export async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
73
73
|
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
74
|
-
if (Reflect.hasMetadata(
|
|
74
|
+
if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
|
|
75
75
|
target.base = await this.importTypeClass(baseClass);
|
|
76
76
|
target.ctor = target.ctor || ctor;
|
|
77
77
|
// Fields
|
|
@@ -2,26 +2,26 @@ import { OpraSchema } from '../../schema/index.js';
|
|
|
2
2
|
import { Collection } from '../resource/collection.js';
|
|
3
3
|
import { Singleton } from '../resource/singleton.js';
|
|
4
4
|
import { Storage } from '../resource/storage.js';
|
|
5
|
-
export async function
|
|
6
|
-
const { document,
|
|
7
|
-
const
|
|
8
|
-
for (const name of
|
|
9
|
-
const schema =
|
|
5
|
+
export async function processSourceQueue() {
|
|
6
|
+
const { document, sourceQueue } = this;
|
|
7
|
+
const sourceNames = Array.from(sourceQueue.keys());
|
|
8
|
+
for (const name of sourceNames) {
|
|
9
|
+
const schema = sourceQueue.get(name);
|
|
10
10
|
if (!schema)
|
|
11
11
|
continue;
|
|
12
12
|
try {
|
|
13
13
|
if (OpraSchema.isCollection(schema)) {
|
|
14
|
-
const resource = await this.
|
|
14
|
+
const resource = await this.createCollectionSource(name, schema);
|
|
15
15
|
document.resources.set(name, resource);
|
|
16
16
|
continue;
|
|
17
17
|
}
|
|
18
18
|
if (OpraSchema.isSingleton(schema)) {
|
|
19
|
-
const resource = await this.
|
|
19
|
+
const resource = await this.createSingletonSource(name, schema);
|
|
20
20
|
document.resources.set(name, resource);
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
23
|
if (OpraSchema.isStorage(schema)) {
|
|
24
|
-
const resource = await this.
|
|
24
|
+
const resource = await this.createFileSource(name, schema);
|
|
25
25
|
document.resources.set(name, resource);
|
|
26
26
|
continue;
|
|
27
27
|
}
|
|
@@ -53,7 +53,7 @@ export async function createSingletonResource(name, schema) {
|
|
|
53
53
|
};
|
|
54
54
|
return new Singleton(document, initArgs);
|
|
55
55
|
}
|
|
56
|
-
export async function
|
|
56
|
+
export async function createStorageResource(name, schema) {
|
|
57
57
|
const { document } = this;
|
|
58
58
|
const initArgs = {
|
|
59
59
|
...schema,
|
package/esm/document/index.js
CHANGED
|
@@ -13,5 +13,6 @@ export * from './resource/resource.js';
|
|
|
13
13
|
export * from './resource/collection.js';
|
|
14
14
|
export * from './resource/singleton.js';
|
|
15
15
|
export * from './resource/storage.js';
|
|
16
|
-
export * from './interfaces/collection
|
|
17
|
-
export * from './interfaces/singleton
|
|
16
|
+
export * from './interfaces/collection.interface.js';
|
|
17
|
+
export * from './interfaces/singleton.interface.js';
|
|
18
|
+
export * from './interfaces/storage.interface.js';
|