@opra/common 0.25.5 → 0.26.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 +1826 -1338
- package/cjs/document/api-document.js +16 -220
- package/cjs/document/data-type/complex-type-class.js +41 -19
- package/cjs/document/data-type/complex-type.js +1 -1
- package/cjs/document/data-type/data-type.js +2 -1
- package/cjs/document/data-type/enum-type-class.js +15 -12
- package/cjs/document/data-type/enum-type.js +28 -14
- package/cjs/document/data-type/field-class.js +21 -3
- package/cjs/document/data-type/field-decorator.js +0 -19
- package/cjs/document/data-type/mapped-type-class.js +8 -10
- package/cjs/document/data-type/mapped-type.js +1 -1
- package/cjs/document/data-type/simple-type-class.js +17 -8
- package/cjs/document/data-type/simple-type.js +1 -1
- package/cjs/document/data-type/union-type-class.js +15 -13
- package/cjs/document/document-base.js +24 -0
- package/cjs/document/factory/api-document-factory.js +231 -0
- package/cjs/document/factory/type-document-factory.js +324 -0
- package/cjs/document/index.js +7 -1
- package/cjs/document/resource/action-decorator.js +27 -0
- package/cjs/document/resource/collection-class.js +95 -44
- package/cjs/document/resource/collection-decorator.js +182 -0
- package/cjs/document/resource/collection.js +4 -4
- package/cjs/document/resource/container-class.js +82 -0
- package/cjs/document/resource/container-decorator.js +24 -0
- package/cjs/document/resource/container.js +29 -0
- package/cjs/document/resource/crud-resource.js +31 -0
- package/cjs/document/resource/endpoint.js +24 -22
- package/cjs/document/resource/operation-decorator.js +27 -0
- package/cjs/document/resource/parameter.js +51 -0
- package/cjs/document/{decorators/resource.decorator.js → resource/resource-decorator.js} +9 -14
- package/cjs/document/resource/resource.js +20 -5
- package/cjs/document/resource/singleton-class.js +60 -40
- package/cjs/document/resource/singleton-decorator.js +57 -0
- package/cjs/document/resource/singleton.js +4 -4
- package/cjs/document/resource/storage-class.js +11 -17
- package/cjs/document/resource/storage-decorator.js +66 -0
- package/cjs/document/resource/storage.js +3 -3
- package/cjs/document/type-document.js +195 -0
- package/cjs/exception/http-errors/internal-server.error.js +1 -0
- package/cjs/exception/wrap-exception.js +2 -1
- package/cjs/helpers/responsive-map.js +3 -3
- package/cjs/http/opra-url-path.js +2 -1
- package/cjs/schema/document/document-base.interface.js +2 -0
- package/cjs/schema/document/type-document.interface.js +2 -0
- package/cjs/schema/opra-schema.ns.js +3 -1
- package/cjs/schema/type-guards.js +3 -3
- package/esm/document/api-document.js +16 -220
- package/esm/document/data-type/complex-type-class.js +40 -19
- package/esm/document/data-type/complex-type.js +1 -1
- package/esm/document/data-type/data-type.js +2 -1
- package/esm/document/data-type/enum-type-class.js +15 -12
- package/esm/document/data-type/enum-type.js +26 -12
- package/esm/document/data-type/field-class.js +20 -3
- package/esm/document/data-type/field-decorator.js +0 -19
- package/esm/document/data-type/mapped-type-class.js +9 -11
- package/esm/document/data-type/mapped-type.js +1 -1
- package/esm/document/data-type/simple-type-class.js +15 -6
- package/esm/document/data-type/simple-type.js +1 -1
- package/esm/document/data-type/union-type-class.js +11 -9
- package/esm/document/document-base.js +20 -0
- package/esm/document/factory/api-document-factory.js +227 -0
- package/esm/document/factory/type-document-factory.js +320 -0
- package/esm/document/index.js +7 -1
- package/esm/document/resource/action-decorator.js +23 -0
- package/esm/document/resource/collection-class.js +95 -44
- package/esm/document/resource/collection-decorator.js +178 -0
- package/esm/document/resource/collection.js +4 -4
- package/esm/document/resource/container-class.js +78 -0
- package/esm/document/resource/container-decorator.js +20 -0
- package/esm/document/resource/container.js +25 -0
- package/esm/document/resource/crud-resource.js +27 -0
- package/esm/document/resource/endpoint.js +22 -20
- package/esm/document/resource/operation-decorator.js +23 -0
- package/esm/document/resource/parameter.js +46 -0
- package/esm/document/resource/resource-decorator.js +28 -0
- package/esm/document/resource/resource.js +20 -5
- package/esm/document/resource/singleton-class.js +58 -38
- package/esm/document/resource/singleton-decorator.js +53 -0
- package/esm/document/resource/singleton.js +4 -4
- package/esm/document/resource/storage-class.js +9 -15
- package/esm/document/resource/storage-decorator.js +62 -0
- package/esm/document/resource/storage.js +3 -3
- package/esm/document/type-document.js +191 -0
- package/esm/exception/http-errors/internal-server.error.js +1 -0
- package/esm/exception/wrap-exception.js +2 -1
- package/esm/helpers/responsive-map.js +3 -3
- package/esm/http/opra-url-path.js +1 -1
- package/esm/schema/document/document-base.interface.js +1 -0
- package/esm/schema/document/type-document.interface.js +1 -0
- package/esm/schema/opra-schema.ns.js +3 -1
- package/esm/schema/type-guards.js +1 -1
- package/package.json +2 -2
- package/types/document/api-document.d.ts +32 -73
- package/types/document/data-type/complex-type-class.d.ts +5 -5
- package/types/document/data-type/complex-type.d.ts +8 -3
- package/types/document/{decorators → data-type}/complex-type.decorator.d.ts +1 -1
- package/types/document/data-type/data-type.d.ts +11 -1
- package/types/document/data-type/enum-type-class.d.ts +6 -6
- package/types/document/data-type/enum-type.d.ts +12 -6
- package/types/document/data-type/field-class.d.ts +9 -1
- package/types/document/data-type/field-decorator.d.ts +1 -1
- package/types/document/data-type/field.d.ts +4 -3
- package/types/document/data-type/mapped-type-class.d.ts +5 -9
- package/types/document/data-type/mapped-type.d.ts +6 -7
- package/types/document/data-type/simple-type-class.d.ts +6 -3
- package/types/document/{decorators → data-type}/simple-type.decorator.d.ts +1 -1
- package/types/document/data-type/union-type-class.d.ts +2 -8
- package/types/document/data-type/union-type.d.ts +7 -6
- package/types/document/document-base.d.ts +10 -0
- package/types/document/factory/api-document-factory.d.ts +50 -0
- package/types/document/factory/type-document-factory.d.ts +56 -0
- package/types/document/index.d.ts +7 -1
- package/types/document/resource/action-decorator.d.ts +6 -0
- package/types/document/resource/collection-class.d.ts +17 -12
- package/types/document/resource/collection-decorator.d.ts +192 -0
- package/types/document/resource/collection.d.ts +11 -19
- package/types/document/resource/container-class.d.ts +66 -0
- package/types/document/resource/container-decorator.d.ts +37 -0
- package/types/document/resource/container.d.ts +25 -0
- package/types/document/resource/crud-resource.d.ts +16 -0
- package/types/document/resource/endpoint.d.ts +21 -21
- package/types/document/resource/operation-decorator.d.ts +6 -0
- package/types/document/resource/parameter.d.ts +35 -0
- package/types/document/resource/resource-decorator.d.ts +32 -0
- package/types/document/resource/resource.d.ts +15 -13
- package/types/document/resource/singleton-class.d.ts +15 -14
- package/types/document/resource/singleton-decorator.d.ts +125 -0
- package/types/document/resource/singleton.d.ts +11 -16
- package/types/document/resource/storage-class.d.ts +13 -6
- package/types/document/resource/storage-decorator.d.ts +98 -0
- package/types/document/resource/storage.d.ts +9 -12
- package/types/document/type-document.d.ts +68 -0
- package/types/http/opra-url-path.d.ts +1 -1
- package/types/schema/data-type/complex-type.interface.d.ts +1 -1
- package/types/schema/data-type/data-type.interface.d.ts +3 -3
- package/types/schema/data-type/enum-type.interface.d.ts +6 -7
- package/types/schema/data-type/mapped-type.interface.d.ts +4 -3
- package/types/schema/data-type/simple-type.interface.d.ts +3 -3
- package/types/schema/data-type/union-type.interface.d.ts +6 -4
- package/types/schema/document/api-document.interface.d.ts +11 -0
- package/types/schema/document/document-base.interface.d.ts +24 -0
- package/types/schema/document/type-document.interface.d.ts +6 -0
- package/types/schema/opra-schema.ns.d.ts +3 -1
- package/types/schema/resource/collection.interface.d.ts +31 -31
- package/types/schema/resource/container.interface.d.ts +4 -3
- package/types/schema/resource/resource.interface.d.ts +1 -1
- package/types/schema/resource/singleton.interface.d.ts +15 -10
- package/types/schema/resource/storage.interface.d.ts +49 -45
- package/types/schema/type-guards.d.ts +3 -2
- package/cjs/document/decorators/build-operation-decorator.js +0 -27
- package/cjs/document/decorators/collection-decorator.js +0 -30
- package/cjs/document/decorators/singleton.decorator.js +0 -25
- package/cjs/document/decorators/storage.decorator.js +0 -27
- package/cjs/document/factory/add-references.js +0 -20
- package/cjs/document/factory/create-document.js +0 -83
- package/cjs/document/factory/factory.js +0 -66
- package/cjs/document/factory/import-resource-class.js +0 -54
- package/cjs/document/factory/import-type-class.js +0 -146
- package/cjs/document/factory/index.js +0 -4
- package/cjs/document/factory/process-resources.js +0 -70
- package/cjs/document/factory/process-types.js +0 -191
- package/cjs/document/utils/generate-codec.js +0 -39
- package/esm/document/decorators/build-operation-decorator.js +0 -23
- package/esm/document/decorators/collection-decorator.js +0 -26
- package/esm/document/decorators/resource.decorator.js +0 -33
- package/esm/document/decorators/singleton.decorator.js +0 -21
- package/esm/document/decorators/storage.decorator.js +0 -23
- package/esm/document/factory/add-references.js +0 -16
- package/esm/document/factory/create-document.js +0 -77
- package/esm/document/factory/factory.js +0 -62
- package/esm/document/factory/import-resource-class.js +0 -48
- package/esm/document/factory/import-type-class.js +0 -136
- package/esm/document/factory/index.js +0 -1
- package/esm/document/factory/process-resources.js +0 -63
- package/esm/document/factory/process-types.js +0 -185
- package/esm/document/utils/generate-codec.js +0 -33
- package/types/document/decorators/build-operation-decorator.d.ts +0 -13
- package/types/document/decorators/collection-decorator.d.ts +0 -33
- package/types/document/decorators/resource.decorator.d.ts +0 -9
- package/types/document/decorators/singleton.decorator.d.ts +0 -25
- package/types/document/decorators/storage.decorator.d.ts +0 -25
- package/types/document/factory/add-references.d.ts +0 -4
- package/types/document/factory/create-document.d.ts +0 -12
- package/types/document/factory/factory.d.ts +0 -63
- package/types/document/factory/import-resource-class.d.ts +0 -10
- package/types/document/factory/import-type-class.d.ts +0 -17
- package/types/document/factory/index.d.ts +0 -1
- package/types/document/factory/process-resources.d.ts +0 -9
- package/types/document/factory/process-types.d.ts +0 -6
- package/types/document/utils/generate-codec.d.ts +0 -10
- package/types/schema/document.interface.d.ts +0 -34
- /package/cjs/document/{decorators → data-type}/complex-type.decorator.js +0 -0
- /package/cjs/document/{decorators → data-type}/simple-type.decorator.js +0 -0
- /package/cjs/schema/{document.interface.js → document/api-document.interface.js} +0 -0
- /package/esm/document/{decorators → data-type}/complex-type.decorator.js +0 -0
- /package/esm/document/{decorators → data-type}/simple-type.decorator.js +0 -0
- /package/esm/schema/{document.interface.js → document/api-document.interface.js} +0 -0
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractFieldSchema = exports.extractEnumTypeSchema = exports.extractUnionTypeSchema = exports.extractMappedTypeSchema = exports.extractComplexTypeSchema = exports.extractSimpleTypeSchema = exports.importTypeClass = void 0;
|
|
4
|
-
const valgen_1 = require("valgen");
|
|
5
|
-
const index_js_1 = require("../../helpers/index.js");
|
|
6
|
-
const index_js_2 = require("../../schema/index.js");
|
|
7
|
-
const constants_js_1 = require("../constants.js");
|
|
8
|
-
async function importTypeClass(typeThunk) {
|
|
9
|
-
const { document, typeQueue, cache } = this;
|
|
10
|
-
const thunk = await (0, index_js_1.resolveThunk)(typeThunk);
|
|
11
|
-
const cached = cache.get(thunk);
|
|
12
|
-
if (cached)
|
|
13
|
-
return cached;
|
|
14
|
-
const dt = document.getDataType(thunk, true);
|
|
15
|
-
if (dt && dt.name)
|
|
16
|
-
return dt.name;
|
|
17
|
-
const metadata = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, thunk);
|
|
18
|
-
if (!(metadata && index_js_2.OpraSchema.isDataType(metadata))) {
|
|
19
|
-
// If thunk is a Type class
|
|
20
|
-
if ((0, index_js_1.isConstructor)(thunk))
|
|
21
|
-
throw new TypeError(`Class "${thunk.name}" doesn't have a valid DataType metadata`);
|
|
22
|
-
// If thunk is an Enum object
|
|
23
|
-
throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(thunk).substring(0, 20)}...`);
|
|
24
|
-
}
|
|
25
|
-
// Clone metadata to prevent changing its contents
|
|
26
|
-
const name = metadata.name;
|
|
27
|
-
const schema = (0, index_js_1.cloneObject)(metadata);
|
|
28
|
-
const result = name || schema;
|
|
29
|
-
if (name) {
|
|
30
|
-
if (this.document.types.has(name) || typeQueue.has(name))
|
|
31
|
-
throw new TypeError(`Type "${name}" already imported`);
|
|
32
|
-
cache.set(thunk, result);
|
|
33
|
-
typeQueue.set(name, schema);
|
|
34
|
-
}
|
|
35
|
-
// If thunk is a DataType class
|
|
36
|
-
if ((0, index_js_1.isConstructor)(thunk)) {
|
|
37
|
-
const ctor = thunk;
|
|
38
|
-
if (index_js_2.OpraSchema.isSimpleType(schema))
|
|
39
|
-
await this.extractSimpleTypeSchema(schema, ctor, metadata);
|
|
40
|
-
else if (index_js_2.OpraSchema.isComplexType(schema))
|
|
41
|
-
await this.extractComplexTypeSchema(schema, ctor, metadata);
|
|
42
|
-
else if (index_js_2.OpraSchema.isMappedType(schema))
|
|
43
|
-
await this.extractMappedTypeSchema(schema, ctor, metadata);
|
|
44
|
-
else if (index_js_2.OpraSchema.isUnionType(schema))
|
|
45
|
-
await this.extractUnionTypeSchema(schema, ctor, metadata);
|
|
46
|
-
else
|
|
47
|
-
throw new TypeError(`Class "${ctor.name}" doesn't have a valid DataType metadata`);
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
// If thunk is a EnumType object
|
|
51
|
-
const enumObject = thunk;
|
|
52
|
-
if (index_js_2.OpraSchema.isEnumType(schema)) {
|
|
53
|
-
let baseType;
|
|
54
|
-
if (metadata.base && Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, metadata.base)) {
|
|
55
|
-
baseType = await this.importTypeClass(metadata.base);
|
|
56
|
-
}
|
|
57
|
-
schema.base = baseType;
|
|
58
|
-
await this.extractEnumTypeSchema(schema, enumObject, metadata);
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(enumObject).substring(0, 20)}...`);
|
|
62
|
-
}
|
|
63
|
-
exports.importTypeClass = importTypeClass;
|
|
64
|
-
// noinspection JSUnusedLocalSymbols
|
|
65
|
-
async function extractSimpleTypeSchema(target, ctor,
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
67
|
-
metadata) {
|
|
68
|
-
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
69
|
-
if (Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, baseClass))
|
|
70
|
-
target.base = await this.importTypeClass(baseClass);
|
|
71
|
-
if (typeof ctor.prototype.decode === 'function')
|
|
72
|
-
target.decoder = (0, valgen_1.validator)(metadata.name, ctor.prototype.decode);
|
|
73
|
-
if (typeof ctor.prototype.encode === 'function')
|
|
74
|
-
target.encoder = (0, valgen_1.validator)(metadata.name, ctor.prototype.encode);
|
|
75
|
-
}
|
|
76
|
-
exports.extractSimpleTypeSchema = extractSimpleTypeSchema;
|
|
77
|
-
async function extractComplexTypeSchema(target, ctor, metadata) {
|
|
78
|
-
const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
79
|
-
if (Reflect.hasMetadata(constants_js_1.DATATYPE_METADATA, baseClass))
|
|
80
|
-
target.base = await this.importTypeClass(baseClass);
|
|
81
|
-
target.ctor = target.ctor || ctor;
|
|
82
|
-
// Fields
|
|
83
|
-
if (metadata.fields) {
|
|
84
|
-
const fields = target.fields = {};
|
|
85
|
-
for (const [elemName, elemMeta] of Object.entries(metadata.fields)) {
|
|
86
|
-
try {
|
|
87
|
-
const t = await elemMeta.type;
|
|
88
|
-
const type = typeof t === 'function'
|
|
89
|
-
? await this.importTypeClass(t)
|
|
90
|
-
: (t || '');
|
|
91
|
-
const elemSchema = fields[elemName] = {
|
|
92
|
-
...elemMeta,
|
|
93
|
-
type
|
|
94
|
-
};
|
|
95
|
-
if (elemMeta.enum)
|
|
96
|
-
elemSchema.type = await this.importTypeClass(elemMeta.enum);
|
|
97
|
-
if (!elemSchema.type && elemMeta.designType) {
|
|
98
|
-
const mappingType = this.document.getDataType(elemMeta.designType, true);
|
|
99
|
-
if (mappingType)
|
|
100
|
-
elemSchema.type = mappingType.name;
|
|
101
|
-
else
|
|
102
|
-
elemSchema.type = await this.importTypeClass(elemMeta.designType);
|
|
103
|
-
}
|
|
104
|
-
await this.extractFieldSchema(elemSchema, ctor, elemMeta, elemName);
|
|
105
|
-
// Check enum again. External packages may modify enum value
|
|
106
|
-
if (elemMeta.enum)
|
|
107
|
-
elemSchema.type = await this.importTypeClass(elemMeta.enum);
|
|
108
|
-
if (typeof elemSchema.type === 'function')
|
|
109
|
-
elemSchema.type = await this.importTypeClass(elemSchema.type);
|
|
110
|
-
if (elemSchema.isArray && !elemSchema.type)
|
|
111
|
-
throw new TypeError(`"type" must be defined explicitly for array properties`);
|
|
112
|
-
elemSchema.type = elemSchema.type || 'any';
|
|
113
|
-
}
|
|
114
|
-
catch (e) {
|
|
115
|
-
e.message = `Error in class "${ctor.name}.${elemName}". ` + e.message;
|
|
116
|
-
throw e;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
exports.extractComplexTypeSchema = extractComplexTypeSchema;
|
|
122
|
-
async function extractMappedTypeSchema(target, ctor, metadata) {
|
|
123
|
-
target.type = await this.importTypeClass(metadata.type);
|
|
124
|
-
}
|
|
125
|
-
exports.extractMappedTypeSchema = extractMappedTypeSchema;
|
|
126
|
-
async function extractUnionTypeSchema(target, ctor, metadata) {
|
|
127
|
-
const oldTypes = metadata.types;
|
|
128
|
-
target.types = [];
|
|
129
|
-
for (const type of oldTypes)
|
|
130
|
-
target.types.push(await this.importTypeClass(type));
|
|
131
|
-
}
|
|
132
|
-
exports.extractUnionTypeSchema = extractUnionTypeSchema;
|
|
133
|
-
// noinspection JSUnusedLocalSymbols
|
|
134
|
-
async function extractEnumTypeSchema(
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
136
|
-
target, enumObject, metadata) {
|
|
137
|
-
// Do nothing. This method is used by external modules for extending the factory
|
|
138
|
-
}
|
|
139
|
-
exports.extractEnumTypeSchema = extractEnumTypeSchema;
|
|
140
|
-
// noinspection JSUnusedLocalSymbols
|
|
141
|
-
async function extractFieldSchema(
|
|
142
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
143
|
-
target, ctor, metadata, name) {
|
|
144
|
-
// Do nothing. This method is used by external modules for extending the factory
|
|
145
|
-
}
|
|
146
|
-
exports.extractFieldSchema = extractFieldSchema;
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createStorageResource = exports.createSingletonResource = exports.createCollectionResource = exports.processSourceQueue = void 0;
|
|
4
|
-
const index_js_1 = require("../../schema/index.js");
|
|
5
|
-
const collection_js_1 = require("../resource/collection.js");
|
|
6
|
-
const singleton_js_1 = require("../resource/singleton.js");
|
|
7
|
-
const storage_js_1 = require("../resource/storage.js");
|
|
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
|
-
if (!schema)
|
|
14
|
-
continue;
|
|
15
|
-
try {
|
|
16
|
-
if (index_js_1.OpraSchema.isCollection(schema)) {
|
|
17
|
-
const resource = await this.createCollectionSource(name, schema);
|
|
18
|
-
document.resources.set(name, resource);
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
if (index_js_1.OpraSchema.isSingleton(schema)) {
|
|
22
|
-
const resource = await this.createSingletonSource(name, schema);
|
|
23
|
-
document.resources.set(name, resource);
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (index_js_1.OpraSchema.isStorage(schema)) {
|
|
27
|
-
const resource = await this.createFileSource(name, schema);
|
|
28
|
-
document.resources.set(name, resource);
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch (e) {
|
|
33
|
-
e.message = `Error in Resource schema (${name}): ` + e.message;
|
|
34
|
-
throw e;
|
|
35
|
-
}
|
|
36
|
-
throw new TypeError(`Invalid Resource schema: ${JSON.stringify(schema).substring(0, 20)}...`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.processSourceQueue = processSourceQueue;
|
|
40
|
-
async function createCollectionResource(name, schema) {
|
|
41
|
-
const { document } = this;
|
|
42
|
-
const dataType = document.getComplexType(schema.type);
|
|
43
|
-
const initArgs = {
|
|
44
|
-
...schema,
|
|
45
|
-
name,
|
|
46
|
-
type: dataType
|
|
47
|
-
};
|
|
48
|
-
return new collection_js_1.Collection(document, initArgs);
|
|
49
|
-
}
|
|
50
|
-
exports.createCollectionResource = createCollectionResource;
|
|
51
|
-
async function createSingletonResource(name, schema) {
|
|
52
|
-
const { document } = this;
|
|
53
|
-
const dataType = document.getComplexType(schema.type);
|
|
54
|
-
const initArgs = {
|
|
55
|
-
...schema,
|
|
56
|
-
name,
|
|
57
|
-
type: dataType
|
|
58
|
-
};
|
|
59
|
-
return new singleton_js_1.Singleton(document, initArgs);
|
|
60
|
-
}
|
|
61
|
-
exports.createSingletonResource = createSingletonResource;
|
|
62
|
-
async function createStorageResource(name, schema) {
|
|
63
|
-
const { document } = this;
|
|
64
|
-
const initArgs = {
|
|
65
|
-
...schema,
|
|
66
|
-
name
|
|
67
|
-
};
|
|
68
|
-
return new storage_js_1.Storage(document, initArgs);
|
|
69
|
-
}
|
|
70
|
-
exports.createStorageResource = createStorageResource;
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.addDataType = exports.createDataTypeInstance = exports.processTypes = void 0;
|
|
4
|
-
const index_js_1 = require("../../schema/index.js");
|
|
5
|
-
const complex_type_js_1 = require("../data-type/complex-type.js");
|
|
6
|
-
const enum_type_js_1 = require("../data-type/enum-type.js");
|
|
7
|
-
const mapped_type_js_1 = require("../data-type/mapped-type.js");
|
|
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() {
|
|
11
|
-
const { document, typeQueue } = this;
|
|
12
|
-
// Create DataType instances
|
|
13
|
-
for (const [name, schema] of typeQueue.entries()) {
|
|
14
|
-
const dataType = this.createDataTypeInstance(schema.kind, name);
|
|
15
|
-
document.types.set(name, dataType);
|
|
16
|
-
}
|
|
17
|
-
// Process schemas
|
|
18
|
-
const typeNames = Array.from(typeQueue.keys());
|
|
19
|
-
for (const name of typeNames) {
|
|
20
|
-
if (!typeQueue.has(name))
|
|
21
|
-
continue;
|
|
22
|
-
this.addDataType(name);
|
|
23
|
-
}
|
|
24
|
-
document.invalidate();
|
|
25
|
-
}
|
|
26
|
-
exports.processTypes = processTypes;
|
|
27
|
-
function createDataTypeInstance(kind, name) {
|
|
28
|
-
const dataType = {
|
|
29
|
-
document: this.document,
|
|
30
|
-
kind,
|
|
31
|
-
name
|
|
32
|
-
};
|
|
33
|
-
switch (kind) {
|
|
34
|
-
case index_js_1.OpraSchema.ComplexType.Kind:
|
|
35
|
-
Object.setPrototypeOf(dataType, complex_type_js_1.ComplexType.prototype);
|
|
36
|
-
break;
|
|
37
|
-
case index_js_1.OpraSchema.EnumType.Kind:
|
|
38
|
-
Object.setPrototypeOf(dataType, enum_type_js_1.EnumType.prototype);
|
|
39
|
-
break;
|
|
40
|
-
case index_js_1.OpraSchema.MappedType.Kind:
|
|
41
|
-
Object.setPrototypeOf(dataType, mapped_type_js_1.MappedType.prototype);
|
|
42
|
-
break;
|
|
43
|
-
case index_js_1.OpraSchema.SimpleType.Kind:
|
|
44
|
-
Object.setPrototypeOf(dataType, simple_type_js_1.SimpleType.prototype);
|
|
45
|
-
break;
|
|
46
|
-
case index_js_1.OpraSchema.UnionType.Kind:
|
|
47
|
-
Object.setPrototypeOf(dataType, union_type_js_1.UnionType.prototype);
|
|
48
|
-
break;
|
|
49
|
-
default:
|
|
50
|
-
throw new TypeError(`Unknown DataType kind (${kind})`);
|
|
51
|
-
}
|
|
52
|
-
return dataType;
|
|
53
|
-
}
|
|
54
|
-
exports.createDataTypeInstance = createDataTypeInstance;
|
|
55
|
-
function addDataType(schemaOrName) {
|
|
56
|
-
const { document, typeQueue, circularRefs, curPath } = this;
|
|
57
|
-
const name = typeof schemaOrName === 'string' ? schemaOrName : undefined;
|
|
58
|
-
let schema;
|
|
59
|
-
let dataType;
|
|
60
|
-
if (name) {
|
|
61
|
-
// Check if data type exists in document
|
|
62
|
-
dataType = document.getDataType(name);
|
|
63
|
-
// Get schema from stack, it is already done if not exist
|
|
64
|
-
schema = typeQueue.get(name);
|
|
65
|
-
if (!schema)
|
|
66
|
-
return dataType;
|
|
67
|
-
// Detect circular refs
|
|
68
|
-
if (circularRefs.has(name))
|
|
69
|
-
throw new TypeError(`Circular reference detected. ${[...Array.from(circularRefs.keys()), name].join('>')}`);
|
|
70
|
-
circularRefs.set(name, 1);
|
|
71
|
-
}
|
|
72
|
-
else
|
|
73
|
-
schema = schemaOrName;
|
|
74
|
-
try {
|
|
75
|
-
// Init base
|
|
76
|
-
let base;
|
|
77
|
-
if ((index_js_1.OpraSchema.isSimpleType(schema) || index_js_1.OpraSchema.isComplexType(schema) || index_js_1.OpraSchema.isEnumType(schema)) && schema.base) {
|
|
78
|
-
curPath.push(typeof schema.base === 'string' ? schema.base : '[base]');
|
|
79
|
-
base = this.addDataType(schema.base);
|
|
80
|
-
curPath.pop();
|
|
81
|
-
}
|
|
82
|
-
// **** Init SimpleType ****
|
|
83
|
-
if (index_js_1.OpraSchema.isSimpleType(schema)) {
|
|
84
|
-
const initArgs = {
|
|
85
|
-
...schema,
|
|
86
|
-
name,
|
|
87
|
-
base
|
|
88
|
-
};
|
|
89
|
-
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
90
|
-
if (name)
|
|
91
|
-
curPath.push(name);
|
|
92
|
-
simple_type_js_1.SimpleType.apply(dataType, [document, initArgs]);
|
|
93
|
-
if (name)
|
|
94
|
-
curPath.pop();
|
|
95
|
-
return dataType;
|
|
96
|
-
}
|
|
97
|
-
// **** Init ComplexType ****
|
|
98
|
-
if (index_js_1.OpraSchema.isComplexType(schema)) {
|
|
99
|
-
const initArgs = {
|
|
100
|
-
...schema,
|
|
101
|
-
name,
|
|
102
|
-
base
|
|
103
|
-
};
|
|
104
|
-
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
105
|
-
if (name)
|
|
106
|
-
curPath.push(name);
|
|
107
|
-
complex_type_js_1.ComplexType.apply(dataType, [document, initArgs]);
|
|
108
|
-
if (name)
|
|
109
|
-
typeQueue.delete(name);
|
|
110
|
-
// process fields
|
|
111
|
-
if (schema.fields) {
|
|
112
|
-
for (const [elemName, v] of Object.entries(schema.fields)) {
|
|
113
|
-
const elemSchema = typeof v === 'object' ? v : { type: v };
|
|
114
|
-
curPath.push(`${name}.${elemName}[type]`);
|
|
115
|
-
const elemType = this.addDataType(elemSchema.type);
|
|
116
|
-
dataType.addField({
|
|
117
|
-
...elemSchema,
|
|
118
|
-
name: elemName,
|
|
119
|
-
type: elemType
|
|
120
|
-
});
|
|
121
|
-
curPath.pop();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (name)
|
|
125
|
-
curPath.pop();
|
|
126
|
-
return dataType;
|
|
127
|
-
}
|
|
128
|
-
// **** Init EnumType ****
|
|
129
|
-
if (index_js_1.OpraSchema.isEnumType(schema)) {
|
|
130
|
-
const initArgs = {
|
|
131
|
-
...schema,
|
|
132
|
-
name,
|
|
133
|
-
base
|
|
134
|
-
};
|
|
135
|
-
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
136
|
-
if (name)
|
|
137
|
-
curPath.push(name);
|
|
138
|
-
enum_type_js_1.EnumType.apply(dataType, [document, initArgs]);
|
|
139
|
-
if (name)
|
|
140
|
-
curPath.pop();
|
|
141
|
-
return dataType;
|
|
142
|
-
}
|
|
143
|
-
// **** Init UnionType ****
|
|
144
|
-
if (index_js_1.OpraSchema.isUnionType(schema)) {
|
|
145
|
-
const unionTypes = schema.types.map(t => this.addDataType(t));
|
|
146
|
-
const initArgs = {
|
|
147
|
-
...schema,
|
|
148
|
-
name,
|
|
149
|
-
types: unionTypes
|
|
150
|
-
};
|
|
151
|
-
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
152
|
-
if (name)
|
|
153
|
-
curPath.push(name);
|
|
154
|
-
union_type_js_1.UnionType.apply(dataType, [document, initArgs]);
|
|
155
|
-
if (name)
|
|
156
|
-
curPath.pop();
|
|
157
|
-
return dataType;
|
|
158
|
-
}
|
|
159
|
-
// **** Init MappedType ****
|
|
160
|
-
if (index_js_1.OpraSchema.isMappedType(schema)) {
|
|
161
|
-
const dt = this.addDataType(schema.type);
|
|
162
|
-
if (!(dt instanceof complex_type_js_1.ComplexType))
|
|
163
|
-
throw new TypeError(`MappedType requires a ComplexType`);
|
|
164
|
-
const initArgs = {
|
|
165
|
-
...schema,
|
|
166
|
-
name,
|
|
167
|
-
type: dt
|
|
168
|
-
};
|
|
169
|
-
dataType = dataType || this.createDataTypeInstance(schema.kind, name);
|
|
170
|
-
if (name)
|
|
171
|
-
curPath.push(name);
|
|
172
|
-
mapped_type_js_1.MappedType.apply(dataType, [document, initArgs]);
|
|
173
|
-
if (name)
|
|
174
|
-
curPath.pop();
|
|
175
|
-
return dataType;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
catch (e) {
|
|
179
|
-
if (curPath.length)
|
|
180
|
-
e.message = `Error at ${curPath.join('/')}: ` + e.message;
|
|
181
|
-
throw e;
|
|
182
|
-
}
|
|
183
|
-
finally {
|
|
184
|
-
if (name) {
|
|
185
|
-
circularRefs.delete(name);
|
|
186
|
-
typeQueue.delete(name);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
throw new TypeError(`Invalid DataType schema: ${JSON.stringify(schema).substring(0, 20)}...`);
|
|
190
|
-
}
|
|
191
|
-
exports.addDataType = addDataType;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._generateCodec = exports.generateCodec = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const vg = tslib_1.__importStar(require("valgen"));
|
|
6
|
-
const complex_type_js_1 = require("../data-type/complex-type.js");
|
|
7
|
-
const enum_type_js_1 = require("../data-type/enum-type.js");
|
|
8
|
-
const mapped_type_js_1 = require("../data-type/mapped-type.js");
|
|
9
|
-
const simple_type_js_1 = require("../data-type/simple-type.js");
|
|
10
|
-
const union_type_js_1 = require("../data-type/union-type.js");
|
|
11
|
-
function generateCodec(type, codec, options) {
|
|
12
|
-
return _generateCodec(type, codec, options);
|
|
13
|
-
}
|
|
14
|
-
exports.generateCodec = generateCodec;
|
|
15
|
-
function _generateCodec(type, codec, options) {
|
|
16
|
-
const schema = {};
|
|
17
|
-
for (const f of type.fields.values()) {
|
|
18
|
-
let fn;
|
|
19
|
-
if (f.type instanceof simple_type_js_1.SimpleType || f.type instanceof enum_type_js_1.EnumType) {
|
|
20
|
-
fn = f.type[codec];
|
|
21
|
-
}
|
|
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 = _generateCodec(f.type, codec, options);
|
|
24
|
-
}
|
|
25
|
-
/* istanbul ignore next */
|
|
26
|
-
if (!fn)
|
|
27
|
-
throw new TypeError(`Can't generate codec for (${f.type})`);
|
|
28
|
-
if (f.isArray)
|
|
29
|
-
fn = vg.isArray(fn);
|
|
30
|
-
schema[f.name] = !options.partial && f.required ? vg.required(fn) : vg.optional(fn);
|
|
31
|
-
}
|
|
32
|
-
return vg.isObject(schema, {
|
|
33
|
-
ctor: type.ctor,
|
|
34
|
-
additionalFields: type.additionalFields ?? false,
|
|
35
|
-
name: type.name,
|
|
36
|
-
caseInSensitive: true,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
exports._generateCodec = _generateCodec;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { RESOURCE_METADATA } from '../constants.js';
|
|
3
|
-
export function buildOperationDecorator(operation) {
|
|
4
|
-
return (options) => {
|
|
5
|
-
const mergeMetadata = {};
|
|
6
|
-
const decorator = ((target, propertyKey) => {
|
|
7
|
-
if (propertyKey !== operation)
|
|
8
|
-
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
9
|
-
const operationMeta = { ...options };
|
|
10
|
-
const resourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
11
|
-
resourceMetadata.operations = resourceMetadata.operations || {};
|
|
12
|
-
resourceMetadata.operations[operation] = operationMeta;
|
|
13
|
-
Object.assign(operationMeta, mergeMetadata);
|
|
14
|
-
Reflect.defineMetadata(RESOURCE_METADATA, resourceMetadata, target.constructor);
|
|
15
|
-
});
|
|
16
|
-
decorator.Parameter = (name) => {
|
|
17
|
-
mergeMetadata.parameters = mergeMetadata.parameters || {};
|
|
18
|
-
mergeMetadata.parameters[name] = {};
|
|
19
|
-
return decorator;
|
|
20
|
-
};
|
|
21
|
-
return decorator;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
-
import { buildOperationDecorator } from './build-operation-decorator.js';
|
|
3
|
-
import { ResourceDecorator } from './resource.decorator.js';
|
|
4
|
-
const operationProperties = ['create', 'delete', 'deleteMany', 'get', 'findMany', 'update', 'updateMany'];
|
|
5
|
-
export function CollectionDecorator(type, options) {
|
|
6
|
-
return ResourceDecorator(OpraSchema.Collection.Kind, { ...options, type });
|
|
7
|
-
}
|
|
8
|
-
const operationDecorators = {
|
|
9
|
-
Create: buildOperationDecorator('create'),
|
|
10
|
-
Delete: buildOperationDecorator('delete'),
|
|
11
|
-
DeleteMany: buildOperationDecorator('deleteMany'),
|
|
12
|
-
Get: buildOperationDecorator('get'),
|
|
13
|
-
FindMany: buildOperationDecorator('findMany'),
|
|
14
|
-
Update: buildOperationDecorator('update'),
|
|
15
|
-
UpdateMany: buildOperationDecorator('updateMany')
|
|
16
|
-
};
|
|
17
|
-
Object.assign(CollectionDecorator, ResourceDecorator);
|
|
18
|
-
Object.assign(CollectionDecorator, operationDecorators);
|
|
19
|
-
CollectionDecorator.Action = function (options) {
|
|
20
|
-
const oldDecorator = ResourceDecorator.Action(options);
|
|
21
|
-
return (target, propertyKey) => {
|
|
22
|
-
if (typeof propertyKey === 'string' && operationProperties.includes(propertyKey))
|
|
23
|
-
throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
|
|
24
|
-
return oldDecorator(target, propertyKey);
|
|
25
|
-
};
|
|
26
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import omit from 'lodash.omit';
|
|
2
|
-
import merge from 'putil-merge';
|
|
3
|
-
import { RESOURCE_METADATA } from '../constants.js';
|
|
4
|
-
export function ResourceDecorator(kind, options) {
|
|
5
|
-
const namePattern = new RegExp(`^(.*)(${kind}|Resource|Controller)$`);
|
|
6
|
-
return function (target) {
|
|
7
|
-
const name = options?.name || namePattern.exec(target.name)?.[1] || target.name;
|
|
8
|
-
const metadata = { kind, name };
|
|
9
|
-
const baseMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, Object.getPrototypeOf(target));
|
|
10
|
-
if (baseMetadata)
|
|
11
|
-
merge(metadata, baseMetadata, { deep: true });
|
|
12
|
-
const oldMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target);
|
|
13
|
-
if (oldMetadata)
|
|
14
|
-
merge(metadata, oldMetadata, { deep: true });
|
|
15
|
-
merge(metadata, {
|
|
16
|
-
kind,
|
|
17
|
-
name,
|
|
18
|
-
...omit(options, ['kind', 'name', 'controller'])
|
|
19
|
-
}, { deep: true });
|
|
20
|
-
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
ResourceDecorator.Action = function (options) {
|
|
24
|
-
return (target, propertyKey) => {
|
|
25
|
-
if (typeof propertyKey !== 'string')
|
|
26
|
-
throw new TypeError(`This decorator can't be used for Symbol keys'`);
|
|
27
|
-
const actionMeta = { ...options };
|
|
28
|
-
const resourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
29
|
-
resourceMetadata.actions = resourceMetadata.actions || {};
|
|
30
|
-
resourceMetadata.actions[propertyKey] = actionMeta;
|
|
31
|
-
Reflect.defineMetadata(RESOURCE_METADATA, resourceMetadata, target.constructor);
|
|
32
|
-
};
|
|
33
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
-
import { buildOperationDecorator } from './build-operation-decorator.js';
|
|
3
|
-
import { ResourceDecorator } from './resource.decorator.js';
|
|
4
|
-
const operationProperties = ['create', 'delete', 'get', 'update'];
|
|
5
|
-
export function SingletonDecorator(type, options) {
|
|
6
|
-
return ResourceDecorator(OpraSchema.Singleton.Kind, { ...options, type });
|
|
7
|
-
}
|
|
8
|
-
Object.assign(SingletonDecorator, ResourceDecorator);
|
|
9
|
-
SingletonDecorator.Create = buildOperationDecorator('create');
|
|
10
|
-
SingletonDecorator.Get = buildOperationDecorator('get');
|
|
11
|
-
SingletonDecorator.Delete = buildOperationDecorator('delete');
|
|
12
|
-
SingletonDecorator.Update = buildOperationDecorator('update');
|
|
13
|
-
SingletonDecorator.Action = function (options) {
|
|
14
|
-
const oldDecorator = ResourceDecorator.Action(options);
|
|
15
|
-
const operators = ['create', 'delete', 'get', 'update'];
|
|
16
|
-
return (target, propertyKey) => {
|
|
17
|
-
if (typeof propertyKey === 'string' && operators.includes(propertyKey))
|
|
18
|
-
throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
|
|
19
|
-
return oldDecorator(target, propertyKey);
|
|
20
|
-
};
|
|
21
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
-
import { buildOperationDecorator } from './build-operation-decorator.js';
|
|
3
|
-
import { ResourceDecorator } from './resource.decorator.js';
|
|
4
|
-
const operationProperties = ['delete', 'get', 'post'];
|
|
5
|
-
export function StorageDecorator(options) {
|
|
6
|
-
return ResourceDecorator(OpraSchema.Storage.Kind, options);
|
|
7
|
-
}
|
|
8
|
-
const operationDecorators = {
|
|
9
|
-
Delete: buildOperationDecorator('delete'),
|
|
10
|
-
Get: buildOperationDecorator('get'),
|
|
11
|
-
Post: buildOperationDecorator('post')
|
|
12
|
-
};
|
|
13
|
-
Object.assign(StorageDecorator, ResourceDecorator);
|
|
14
|
-
Object.assign(StorageDecorator, operationDecorators);
|
|
15
|
-
StorageDecorator.Action = function (options) {
|
|
16
|
-
const oldDecorator = ResourceDecorator.Action(options);
|
|
17
|
-
const operators = ['delete', 'get', 'post'];
|
|
18
|
-
return (target, propertyKey) => {
|
|
19
|
-
if (typeof propertyKey === 'string' && operators.includes(propertyKey))
|
|
20
|
-
throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
|
|
21
|
-
return oldDecorator(target, propertyKey);
|
|
22
|
-
};
|
|
23
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ApiDocument } from '../api-document.js';
|
|
2
|
-
export async function addReferences(references) {
|
|
3
|
-
const { document } = this;
|
|
4
|
-
for (const [ns, r] of Object.entries(references)) {
|
|
5
|
-
if (typeof r === 'string') {
|
|
6
|
-
document.references.set(ns, await this.createDocumentFromUrl(r));
|
|
7
|
-
}
|
|
8
|
-
else if (r instanceof ApiDocument)
|
|
9
|
-
document.references.set(ns, r);
|
|
10
|
-
else if (typeof r === 'object') {
|
|
11
|
-
document.references.set(ns, await this.createDocument(r));
|
|
12
|
-
}
|
|
13
|
-
else
|
|
14
|
-
throw new TypeError(`Invalid document reference (${ns}) in schema`);
|
|
15
|
-
}
|
|
16
|
-
}
|