@opra/common 0.24.0 → 0.24.2
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 +48 -28
- package/cjs/document/constants.js +2 -2
- package/cjs/document/factory/import-resource-class.js +1 -1
- package/cjs/document/resource/action.js +3 -0
- package/cjs/document/resource/collection-decorator.js +6 -6
- package/cjs/document/resource/resource-decorator.js +2 -2
- package/cjs/document/resource/resource.js +14 -7
- package/cjs/document/resource/singleton-decorator.js +5 -5
- package/cjs/document/resource/storage-decorator.js +5 -5
- package/cjs/schema/type-guards.js +2 -1
- package/esm/document/constants.js +1 -1
- package/esm/document/factory/import-resource-class.js +2 -2
- package/esm/document/resource/action.js +3 -0
- package/esm/document/resource/collection-decorator.js +7 -7
- package/esm/document/resource/resource-decorator.js +3 -3
- package/esm/document/resource/resource.js +14 -7
- package/esm/document/resource/singleton-decorator.js +6 -6
- package/esm/document/resource/storage-decorator.js +6 -6
- package/esm/schema/type-guards.js +2 -1
- package/package.json +1 -1
- package/types/document/constants.d.ts +1 -1
- package/types/document/resource/action.d.ts +7 -0
- package/types/document/resource/resource.d.ts +1 -2
package/browser.js
CHANGED
|
@@ -15,7 +15,7 @@ import "reflect-metadata";
|
|
|
15
15
|
|
|
16
16
|
// ../../build/common/esm/document/constants.js
|
|
17
17
|
var DATATYPE_METADATA = Symbol("DATATYPE_METADATA");
|
|
18
|
-
var
|
|
18
|
+
var RESOURCE_METADATA = Symbol("RESOURCE_METADATA");
|
|
19
19
|
var DECORATOR = Symbol("DECORATOR");
|
|
20
20
|
var NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
21
21
|
var TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
|
|
@@ -987,7 +987,7 @@ function isEnumType(obj) {
|
|
|
987
987
|
}
|
|
988
988
|
__name(isEnumType, "isEnumType");
|
|
989
989
|
function isSource(obj) {
|
|
990
|
-
return obj && typeof obj === "object" && (obj.kind === Container.Kind || obj.kind === Collection.Kind || obj.kind === Singleton.Kind);
|
|
990
|
+
return obj && typeof obj === "object" && (obj.kind === Container.Kind || obj.kind === Collection.Kind || obj.kind === Singleton.Kind || obj.kind === Storage.Kind);
|
|
991
991
|
}
|
|
992
992
|
__name(isSource, "isSource");
|
|
993
993
|
function isCollection(obj) {
|
|
@@ -1949,7 +1949,7 @@ async function importResourceClass(thunk) {
|
|
|
1949
1949
|
if (cached)
|
|
1950
1950
|
return cached;
|
|
1951
1951
|
const ctor = typeof thunk === "function" ? thunk : Object.getPrototypeOf(thunk).constructor;
|
|
1952
|
-
let metadata = Reflect.getMetadata(
|
|
1952
|
+
let metadata = Reflect.getMetadata(RESOURCE_METADATA, ctor);
|
|
1953
1953
|
if (!metadata && opra_schema_ns_exports.isSource(metadata))
|
|
1954
1954
|
throw new TypeError(`Class "${ctor.name}" doesn't have a valid Resource metadata`);
|
|
1955
1955
|
metadata = cloneObject(metadata);
|
|
@@ -9814,29 +9814,49 @@ function _generateCodec(type, codec, options) {
|
|
|
9814
9814
|
}
|
|
9815
9815
|
__name(_generateCodec, "_generateCodec");
|
|
9816
9816
|
|
|
9817
|
+
// ../../build/common/esm/document/resource/action.js
|
|
9818
|
+
var Action = class {
|
|
9819
|
+
static {
|
|
9820
|
+
__name(this, "Action");
|
|
9821
|
+
}
|
|
9822
|
+
constructor(init) {
|
|
9823
|
+
this.name = init.name;
|
|
9824
|
+
}
|
|
9825
|
+
exportSchema() {
|
|
9826
|
+
return {};
|
|
9827
|
+
}
|
|
9828
|
+
};
|
|
9829
|
+
|
|
9817
9830
|
// ../../build/common/esm/document/resource/resource.js
|
|
9818
9831
|
var Resource = class {
|
|
9819
9832
|
static {
|
|
9820
9833
|
__name(this, "Resource");
|
|
9821
9834
|
}
|
|
9822
9835
|
constructor(document, init) {
|
|
9823
|
-
this.actions =
|
|
9836
|
+
this.actions = {};
|
|
9824
9837
|
this.document = document;
|
|
9825
9838
|
this.name = init.name;
|
|
9826
9839
|
this.description = init.description;
|
|
9827
9840
|
this.controller = init.controller;
|
|
9841
|
+
if (init.actions) {
|
|
9842
|
+
for (const [name, meta] of Object.entries(init.actions)) {
|
|
9843
|
+
this.actions[name.toLowerCase()] = new Action({ ...meta, name });
|
|
9844
|
+
}
|
|
9845
|
+
}
|
|
9828
9846
|
}
|
|
9829
9847
|
exportSchema() {
|
|
9830
9848
|
const schema = omitUndefined({
|
|
9831
9849
|
kind: this.kind,
|
|
9832
9850
|
description: this.description
|
|
9833
9851
|
});
|
|
9834
|
-
|
|
9835
|
-
|
|
9836
|
-
|
|
9837
|
-
|
|
9838
|
-
|
|
9852
|
+
const actions = {};
|
|
9853
|
+
let i = 0;
|
|
9854
|
+
for (const action of Object.values(this.actions)) {
|
|
9855
|
+
actions[action.name] = action.exportSchema();
|
|
9856
|
+
i++;
|
|
9839
9857
|
}
|
|
9858
|
+
if (i)
|
|
9859
|
+
schema.actions = actions;
|
|
9840
9860
|
return schema;
|
|
9841
9861
|
}
|
|
9842
9862
|
toString() {
|
|
@@ -10012,10 +10032,10 @@ ResourceDecorator.Action = function(options) {
|
|
|
10012
10032
|
if (typeof propertyKey !== "string")
|
|
10013
10033
|
throw new TypeError(`This decorator can't be used for Symbol keys'`);
|
|
10014
10034
|
const actionMeta = { ...options };
|
|
10015
|
-
const resourceMetadata = Reflect.getOwnMetadata(
|
|
10035
|
+
const resourceMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {};
|
|
10016
10036
|
resourceMetadata.actions = resourceMetadata.actions || {};
|
|
10017
10037
|
resourceMetadata.actions[propertyKey] = actionMeta;
|
|
10018
|
-
Reflect.defineMetadata(
|
|
10038
|
+
Reflect.defineMetadata(RESOURCE_METADATA, resourceMetadata, target.constructor);
|
|
10019
10039
|
};
|
|
10020
10040
|
};
|
|
10021
10041
|
|
|
@@ -10025,20 +10045,20 @@ var operationProperties = ["create", "delete", "deleteMany", "get", "findMany",
|
|
|
10025
10045
|
function CollectionDecorator(type, options) {
|
|
10026
10046
|
return function(target) {
|
|
10027
10047
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
10028
|
-
const metadata = Reflect.getOwnMetadata(
|
|
10029
|
-
const baseMetadata = Reflect.getOwnMetadata(
|
|
10048
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
10049
|
+
const baseMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, Object.getPrototypeOf(target));
|
|
10030
10050
|
if (baseMetadata) {
|
|
10031
10051
|
merge7(metadata, baseMetadata, { deep: true });
|
|
10032
10052
|
}
|
|
10033
10053
|
metadata.kind = opra_schema_ns_exports.Collection.Kind;
|
|
10034
10054
|
metadata.name = name;
|
|
10035
10055
|
metadata.type = type;
|
|
10036
|
-
const m = Reflect.getMetadata(
|
|
10056
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
10037
10057
|
if (m && metadata !== m)
|
|
10038
10058
|
Object.assign(metadata, omit4(m), Object.keys(metadata));
|
|
10039
10059
|
if (options)
|
|
10040
10060
|
Object.assign(metadata, omit4(options, ["kind", "name", "type", "controller"]));
|
|
10041
|
-
Reflect.defineMetadata(
|
|
10061
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
10042
10062
|
};
|
|
10043
10063
|
}
|
|
10044
10064
|
__name(CollectionDecorator, "CollectionDecorator");
|
|
@@ -10063,10 +10083,10 @@ function createOperationDecorator(operation) {
|
|
|
10063
10083
|
if (propertyKey !== operation)
|
|
10064
10084
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
10065
10085
|
const operationMeta = { ...options };
|
|
10066
|
-
const sourceMetadata = Reflect.getOwnMetadata(
|
|
10086
|
+
const sourceMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {};
|
|
10067
10087
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
10068
10088
|
sourceMetadata.operations[operation] = operationMeta;
|
|
10069
|
-
Reflect.defineMetadata(
|
|
10089
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
10070
10090
|
};
|
|
10071
10091
|
}
|
|
10072
10092
|
__name(createOperationDecorator, "createOperationDecorator");
|
|
@@ -10143,16 +10163,16 @@ var NAME_PATTERN2 = /^(.*)(Resource|Singleton|Controller)$/;
|
|
|
10143
10163
|
function SingletonDecorator(type, options) {
|
|
10144
10164
|
return function(target) {
|
|
10145
10165
|
const name = options?.name || target.name.match(NAME_PATTERN2)?.[1] || target.name;
|
|
10146
|
-
const metadata = Reflect.getOwnMetadata(
|
|
10166
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
10147
10167
|
metadata.kind = opra_schema_ns_exports.Singleton.Kind;
|
|
10148
10168
|
metadata.name = name;
|
|
10149
10169
|
metadata.type = type;
|
|
10150
|
-
const m = Reflect.getMetadata(
|
|
10170
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
10151
10171
|
if (m && metadata !== m)
|
|
10152
10172
|
Object.assign(metadata, omit5(m), Object.keys(metadata));
|
|
10153
10173
|
if (options)
|
|
10154
10174
|
Object.assign(metadata, omit5(options, ["kind", "name", "type", "controller"]));
|
|
10155
|
-
Reflect.defineMetadata(
|
|
10175
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
10156
10176
|
};
|
|
10157
10177
|
}
|
|
10158
10178
|
__name(SingletonDecorator, "SingletonDecorator");
|
|
@@ -10175,10 +10195,10 @@ function createOperationDecorator2(operation) {
|
|
|
10175
10195
|
if (propertyKey !== operation)
|
|
10176
10196
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
10177
10197
|
const operationMeta = { ...options };
|
|
10178
|
-
const sourceMetadata = Reflect.getOwnMetadata(
|
|
10198
|
+
const sourceMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {};
|
|
10179
10199
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
10180
10200
|
sourceMetadata.operations[operation] = operationMeta;
|
|
10181
|
-
Reflect.defineMetadata(
|
|
10201
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
10182
10202
|
};
|
|
10183
10203
|
}
|
|
10184
10204
|
__name(createOperationDecorator2, "createOperationDecorator");
|
|
@@ -10227,15 +10247,15 @@ var NAME_PATTERN3 = /^(.*)(Resource|Storage|Controller)$/;
|
|
|
10227
10247
|
function StorageDecorator(options) {
|
|
10228
10248
|
return function(target) {
|
|
10229
10249
|
const name = options?.name || target.name.match(NAME_PATTERN3)?.[1] || target.name;
|
|
10230
|
-
const metadata = Reflect.getOwnMetadata(
|
|
10250
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
10231
10251
|
metadata.kind = opra_schema_ns_exports.Storage.Kind;
|
|
10232
10252
|
metadata.name = name;
|
|
10233
|
-
const m = Reflect.getMetadata(
|
|
10253
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
10234
10254
|
if (m && metadata !== m)
|
|
10235
10255
|
Object.assign(metadata, omit6(m), Object.keys(metadata));
|
|
10236
10256
|
if (options)
|
|
10237
10257
|
Object.assign(metadata, omit6(options, ["kind", "name", "type", "controller"]));
|
|
10238
|
-
Reflect.defineMetadata(
|
|
10258
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
10239
10259
|
};
|
|
10240
10260
|
}
|
|
10241
10261
|
__name(StorageDecorator, "StorageDecorator");
|
|
@@ -10257,10 +10277,10 @@ function createOperationDecorator3(operation) {
|
|
|
10257
10277
|
if (propertyKey !== operation)
|
|
10258
10278
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
10259
10279
|
const operationMeta = { ...options };
|
|
10260
|
-
const sourceMetadata = Reflect.getOwnMetadata(
|
|
10280
|
+
const sourceMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {};
|
|
10261
10281
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
10262
10282
|
sourceMetadata.operations[operation] = operationMeta;
|
|
10263
|
-
Reflect.defineMetadata(
|
|
10283
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
10264
10284
|
};
|
|
10265
10285
|
}
|
|
10266
10286
|
__name(createOperationDecorator3, "createOperationDecorator");
|
|
@@ -11304,11 +11324,11 @@ export {
|
|
|
11304
11324
|
OpraURL,
|
|
11305
11325
|
OpraURLPath,
|
|
11306
11326
|
PickType,
|
|
11327
|
+
RESOURCE_METADATA,
|
|
11307
11328
|
Resource,
|
|
11308
11329
|
ResourceConflictError,
|
|
11309
11330
|
ResourceNotFoundError,
|
|
11310
11331
|
ResponsiveMap,
|
|
11311
|
-
SOURCE_METADATA,
|
|
11312
11332
|
SimpleType2 as SimpleType,
|
|
11313
11333
|
Singleton2 as Singleton,
|
|
11314
11334
|
Storage2 as Storage,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.DECORATOR = exports.
|
|
3
|
+
exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.DECORATOR = exports.RESOURCE_METADATA = exports.DATATYPE_METADATA = void 0;
|
|
4
4
|
exports.DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
|
|
5
|
-
exports.
|
|
5
|
+
exports.RESOURCE_METADATA = Symbol('RESOURCE_METADATA');
|
|
6
6
|
exports.DECORATOR = Symbol('DECORATOR');
|
|
7
7
|
exports.NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
8
8
|
exports.TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
|
|
@@ -12,7 +12,7 @@ async function importResourceClass(thunk) {
|
|
|
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.
|
|
15
|
+
let metadata = Reflect.getMetadata(constants_js_1.RESOURCE_METADATA, ctor);
|
|
16
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;
|
|
@@ -12,8 +12,8 @@ const operationProperties = ['create', 'delete', 'deleteMany', 'get', 'findMany'
|
|
|
12
12
|
function CollectionDecorator(type, options) {
|
|
13
13
|
return function (target) {
|
|
14
14
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
15
|
-
const metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
16
|
-
const baseMetadata = Reflect.getOwnMetadata(constants_js_1.
|
|
15
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target) || {};
|
|
16
|
+
const baseMetadata = Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, Object.getPrototypeOf(target));
|
|
17
17
|
if (baseMetadata) {
|
|
18
18
|
(0, putil_merge_1.default)(metadata, baseMetadata, { deep: true });
|
|
19
19
|
}
|
|
@@ -21,13 +21,13 @@ function CollectionDecorator(type, options) {
|
|
|
21
21
|
metadata.name = name;
|
|
22
22
|
metadata.type = type;
|
|
23
23
|
// Merge with previous metadata object
|
|
24
|
-
const m = Reflect.getMetadata(constants_js_1.
|
|
24
|
+
const m = Reflect.getMetadata(constants_js_1.RESOURCE_METADATA, target);
|
|
25
25
|
if (m && metadata !== m)
|
|
26
26
|
Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
|
|
27
27
|
// Merge options
|
|
28
28
|
if (options)
|
|
29
29
|
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
|
|
30
|
-
Reflect.defineMetadata(constants_js_1.
|
|
30
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, metadata, target);
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
exports.CollectionDecorator = CollectionDecorator;
|
|
@@ -52,9 +52,9 @@ function createOperationDecorator(operation) {
|
|
|
52
52
|
if (propertyKey !== operation)
|
|
53
53
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
54
54
|
const operationMeta = { ...options };
|
|
55
|
-
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.
|
|
55
|
+
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target.constructor) || {});
|
|
56
56
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
57
57
|
sourceMetadata.operations[operation] = operationMeta;
|
|
58
|
-
Reflect.defineMetadata(constants_js_1.
|
|
58
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -8,9 +8,9 @@ exports.ResourceDecorator.Action = function (options) {
|
|
|
8
8
|
if (typeof propertyKey !== 'string')
|
|
9
9
|
throw new TypeError(`This decorator can't be used for Symbol keys'`);
|
|
10
10
|
const actionMeta = { ...options };
|
|
11
|
-
const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.
|
|
11
|
+
const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target.constructor) || {});
|
|
12
12
|
resourceMetadata.actions = resourceMetadata.actions || {};
|
|
13
13
|
resourceMetadata.actions[propertyKey] = actionMeta;
|
|
14
|
-
Reflect.defineMetadata(constants_js_1.
|
|
14
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, resourceMetadata, target.constructor);
|
|
15
15
|
};
|
|
16
16
|
};
|
|
@@ -1,28 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Resource = void 0;
|
|
4
|
-
const index_js_1 = require("../../helpers/index.js");
|
|
5
4
|
const object_utils_js_1 = require("../../helpers/object-utils.js");
|
|
6
5
|
const inspect_util_js_1 = require("../utils/inspect.util.js");
|
|
6
|
+
const action_js_1 = require("./action.js");
|
|
7
7
|
class Resource {
|
|
8
8
|
constructor(document, init) {
|
|
9
|
-
this.actions =
|
|
9
|
+
this.actions = {};
|
|
10
10
|
this.document = document;
|
|
11
11
|
this.name = init.name;
|
|
12
12
|
this.description = init.description;
|
|
13
13
|
this.controller = init.controller;
|
|
14
|
+
if (init.actions) {
|
|
15
|
+
for (const [name, meta] of Object.entries(init.actions)) {
|
|
16
|
+
this.actions[name.toLowerCase()] = new action_js_1.Action({ ...meta, name });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
exportSchema() {
|
|
16
21
|
const schema = (0, object_utils_js_1.omitUndefined)({
|
|
17
22
|
kind: this.kind,
|
|
18
23
|
description: this.description,
|
|
19
24
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const actions = {};
|
|
26
|
+
let i = 0;
|
|
27
|
+
for (const action of Object.values(this.actions)) {
|
|
28
|
+
actions[action.name] = action.exportSchema();
|
|
29
|
+
i++;
|
|
25
30
|
}
|
|
31
|
+
if (i)
|
|
32
|
+
schema.actions = actions;
|
|
26
33
|
return schema;
|
|
27
34
|
}
|
|
28
35
|
toString() {
|
|
@@ -11,18 +11,18 @@ const operationProperties = ['create', 'delete', 'get', 'update'];
|
|
|
11
11
|
function SingletonDecorator(type, options) {
|
|
12
12
|
return function (target) {
|
|
13
13
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
14
|
-
const metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
14
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target) || {};
|
|
15
15
|
metadata.kind = index_js_1.OpraSchema.Singleton.Kind;
|
|
16
16
|
metadata.name = name;
|
|
17
17
|
metadata.type = type;
|
|
18
18
|
// Merge with previous metadata object
|
|
19
|
-
const m = Reflect.getMetadata(constants_js_1.
|
|
19
|
+
const m = Reflect.getMetadata(constants_js_1.RESOURCE_METADATA, target);
|
|
20
20
|
if (m && metadata !== m)
|
|
21
21
|
Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
|
|
22
22
|
// Merge options
|
|
23
23
|
if (options)
|
|
24
24
|
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
|
|
25
|
-
Reflect.defineMetadata(constants_js_1.
|
|
25
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, metadata, target);
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
exports.SingletonDecorator = SingletonDecorator;
|
|
@@ -45,9 +45,9 @@ function createOperationDecorator(operation) {
|
|
|
45
45
|
if (propertyKey !== operation)
|
|
46
46
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
47
47
|
const operationMeta = { ...options };
|
|
48
|
-
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.
|
|
48
|
+
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target.constructor) || {});
|
|
49
49
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
50
50
|
sourceMetadata.operations[operation] = operationMeta;
|
|
51
|
-
Reflect.defineMetadata(constants_js_1.
|
|
51
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
52
52
|
});
|
|
53
53
|
}
|
|
@@ -11,17 +11,17 @@ const operationProperties = ['delete', 'get', 'post'];
|
|
|
11
11
|
function StorageDecorator(options) {
|
|
12
12
|
return function (target) {
|
|
13
13
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
14
|
-
const metadata = Reflect.getOwnMetadata(constants_js_1.
|
|
14
|
+
const metadata = Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target) || {};
|
|
15
15
|
metadata.kind = index_js_1.OpraSchema.Storage.Kind;
|
|
16
16
|
metadata.name = name;
|
|
17
17
|
// Merge with previous metadata object
|
|
18
|
-
const m = Reflect.getMetadata(constants_js_1.
|
|
18
|
+
const m = Reflect.getMetadata(constants_js_1.RESOURCE_METADATA, target);
|
|
19
19
|
if (m && metadata !== m)
|
|
20
20
|
Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
|
|
21
21
|
// Merge options
|
|
22
22
|
if (options)
|
|
23
23
|
Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
|
|
24
|
-
Reflect.defineMetadata(constants_js_1.
|
|
24
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, metadata, target);
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
exports.StorageDecorator = StorageDecorator;
|
|
@@ -43,9 +43,9 @@ function createOperationDecorator(operation) {
|
|
|
43
43
|
if (propertyKey !== operation)
|
|
44
44
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
45
45
|
const operationMeta = { ...options };
|
|
46
|
-
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.
|
|
46
|
+
const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RESOURCE_METADATA, target.constructor) || {});
|
|
47
47
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
48
48
|
sourceMetadata.operations[operation] = operationMeta;
|
|
49
|
-
Reflect.defineMetadata(constants_js_1.
|
|
49
|
+
Reflect.defineMetadata(constants_js_1.RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
50
50
|
});
|
|
51
51
|
}
|
|
@@ -43,7 +43,8 @@ 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
49
|
exports.isSource = isSource;
|
|
49
50
|
function isCollection(obj) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
|
|
2
|
-
export const
|
|
2
|
+
export const RESOURCE_METADATA = Symbol('RESOURCE_METADATA');
|
|
3
3
|
export const DECORATOR = Symbol('DECORATOR');
|
|
4
4
|
export const NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
5
5
|
export const TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
|
|
@@ -1,7 +1,7 @@
|
|
|
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
6
|
const { document, sourceQueue, cache } = this;
|
|
7
7
|
const controller = await resolveThunk(thunk);
|
|
@@ -9,7 +9,7 @@ export async function importResourceClass(thunk) {
|
|
|
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(
|
|
12
|
+
let metadata = Reflect.getMetadata(RESOURCE_METADATA, ctor);
|
|
13
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;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import omit from 'lodash.omit';
|
|
2
2
|
import merge from 'putil-merge';
|
|
3
3
|
import { OpraSchema } from '../../schema/index.js';
|
|
4
|
-
import {
|
|
4
|
+
import { RESOURCE_METADATA } from '../constants.js';
|
|
5
5
|
import { ResourceDecorator } from './resource-decorator.js';
|
|
6
6
|
const NAME_PATTERN = /^(.*)(Resource|Collection|Controller)$/;
|
|
7
7
|
const operationProperties = ['create', 'delete', 'deleteMany', 'get', 'findMany', 'update', 'updateMany'];
|
|
8
8
|
export function CollectionDecorator(type, options) {
|
|
9
9
|
return function (target) {
|
|
10
10
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
11
|
-
const metadata = Reflect.getOwnMetadata(
|
|
12
|
-
const baseMetadata = Reflect.getOwnMetadata(
|
|
11
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
12
|
+
const baseMetadata = Reflect.getOwnMetadata(RESOURCE_METADATA, Object.getPrototypeOf(target));
|
|
13
13
|
if (baseMetadata) {
|
|
14
14
|
merge(metadata, baseMetadata, { deep: true });
|
|
15
15
|
}
|
|
@@ -17,13 +17,13 @@ export function CollectionDecorator(type, options) {
|
|
|
17
17
|
metadata.name = name;
|
|
18
18
|
metadata.type = type;
|
|
19
19
|
// Merge with previous metadata object
|
|
20
|
-
const m = Reflect.getMetadata(
|
|
20
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
21
21
|
if (m && metadata !== m)
|
|
22
22
|
Object.assign(metadata, omit(m), Object.keys(metadata));
|
|
23
23
|
// Merge options
|
|
24
24
|
if (options)
|
|
25
25
|
Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
|
|
26
|
-
Reflect.defineMetadata(
|
|
26
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
Object.assign(CollectionDecorator, ResourceDecorator);
|
|
@@ -47,9 +47,9 @@ function createOperationDecorator(operation) {
|
|
|
47
47
|
if (propertyKey !== operation)
|
|
48
48
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
49
49
|
const operationMeta = { ...options };
|
|
50
|
-
const sourceMetadata = (Reflect.getOwnMetadata(
|
|
50
|
+
const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
51
51
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
52
52
|
sourceMetadata.operations[operation] = operationMeta;
|
|
53
|
-
Reflect.defineMetadata(
|
|
53
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
54
54
|
});
|
|
55
55
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RESOURCE_METADATA } from '../constants.js';
|
|
2
2
|
export const ResourceDecorator = {};
|
|
3
3
|
ResourceDecorator.Action = function (options) {
|
|
4
4
|
return (target, propertyKey) => {
|
|
5
5
|
if (typeof propertyKey !== 'string')
|
|
6
6
|
throw new TypeError(`This decorator can't be used for Symbol keys'`);
|
|
7
7
|
const actionMeta = { ...options };
|
|
8
|
-
const resourceMetadata = (Reflect.getOwnMetadata(
|
|
8
|
+
const resourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
9
9
|
resourceMetadata.actions = resourceMetadata.actions || {};
|
|
10
10
|
resourceMetadata.actions[propertyKey] = actionMeta;
|
|
11
|
-
Reflect.defineMetadata(
|
|
11
|
+
Reflect.defineMetadata(RESOURCE_METADATA, resourceMetadata, target.constructor);
|
|
12
12
|
};
|
|
13
13
|
};
|
|
@@ -1,25 +1,32 @@
|
|
|
1
|
-
import { ResponsiveMap } from '../../helpers/index.js';
|
|
2
1
|
import { omitUndefined } from '../../helpers/object-utils.js';
|
|
3
2
|
import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../utils/inspect.util.js';
|
|
3
|
+
import { Action } from './action.js';
|
|
4
4
|
export class Resource {
|
|
5
5
|
constructor(document, init) {
|
|
6
|
-
this.actions =
|
|
6
|
+
this.actions = {};
|
|
7
7
|
this.document = document;
|
|
8
8
|
this.name = init.name;
|
|
9
9
|
this.description = init.description;
|
|
10
10
|
this.controller = init.controller;
|
|
11
|
+
if (init.actions) {
|
|
12
|
+
for (const [name, meta] of Object.entries(init.actions)) {
|
|
13
|
+
this.actions[name.toLowerCase()] = new Action({ ...meta, name });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
11
16
|
}
|
|
12
17
|
exportSchema() {
|
|
13
18
|
const schema = omitUndefined({
|
|
14
19
|
kind: this.kind,
|
|
15
20
|
description: this.description,
|
|
16
21
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
const actions = {};
|
|
23
|
+
let i = 0;
|
|
24
|
+
for (const action of Object.values(this.actions)) {
|
|
25
|
+
actions[action.name] = action.exportSchema();
|
|
26
|
+
i++;
|
|
22
27
|
}
|
|
28
|
+
if (i)
|
|
29
|
+
schema.actions = actions;
|
|
23
30
|
return schema;
|
|
24
31
|
}
|
|
25
32
|
toString() {
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import omit from 'lodash.omit';
|
|
2
2
|
import { OpraSchema } from '../../schema/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { RESOURCE_METADATA } from '../constants.js';
|
|
4
4
|
import { ResourceDecorator } from './resource-decorator.js';
|
|
5
5
|
const NAME_PATTERN = /^(.*)(Resource|Singleton|Controller)$/;
|
|
6
6
|
const operationProperties = ['create', 'delete', 'get', 'update'];
|
|
7
7
|
export function SingletonDecorator(type, options) {
|
|
8
8
|
return function (target) {
|
|
9
9
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
10
|
-
const metadata = Reflect.getOwnMetadata(
|
|
10
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
11
11
|
metadata.kind = OpraSchema.Singleton.Kind;
|
|
12
12
|
metadata.name = name;
|
|
13
13
|
metadata.type = type;
|
|
14
14
|
// Merge with previous metadata object
|
|
15
|
-
const m = Reflect.getMetadata(
|
|
15
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
16
16
|
if (m && metadata !== m)
|
|
17
17
|
Object.assign(metadata, omit(m), Object.keys(metadata));
|
|
18
18
|
// Merge options
|
|
19
19
|
if (options)
|
|
20
20
|
Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
|
|
21
|
-
Reflect.defineMetadata(
|
|
21
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
Object.assign(SingletonDecorator, ResourceDecorator);
|
|
@@ -40,9 +40,9 @@ function createOperationDecorator(operation) {
|
|
|
40
40
|
if (propertyKey !== operation)
|
|
41
41
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
42
42
|
const operationMeta = { ...options };
|
|
43
|
-
const sourceMetadata = (Reflect.getOwnMetadata(
|
|
43
|
+
const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
44
44
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
45
45
|
sourceMetadata.operations[operation] = operationMeta;
|
|
46
|
-
Reflect.defineMetadata(
|
|
46
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
47
47
|
});
|
|
48
48
|
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import omit from 'lodash.omit';
|
|
2
2
|
import { OpraSchema } from '../../schema/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { RESOURCE_METADATA } from '../constants.js';
|
|
4
4
|
import { ResourceDecorator } from './resource-decorator.js';
|
|
5
5
|
const NAME_PATTERN = /^(.*)(Resource|Storage|Controller)$/;
|
|
6
6
|
const operationProperties = ['delete', 'get', 'post'];
|
|
7
7
|
export function StorageDecorator(options) {
|
|
8
8
|
return function (target) {
|
|
9
9
|
const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
|
|
10
|
-
const metadata = Reflect.getOwnMetadata(
|
|
10
|
+
const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
|
|
11
11
|
metadata.kind = OpraSchema.Storage.Kind;
|
|
12
12
|
metadata.name = name;
|
|
13
13
|
// Merge with previous metadata object
|
|
14
|
-
const m = Reflect.getMetadata(
|
|
14
|
+
const m = Reflect.getMetadata(RESOURCE_METADATA, target);
|
|
15
15
|
if (m && metadata !== m)
|
|
16
16
|
Object.assign(metadata, omit(m), Object.keys(metadata));
|
|
17
17
|
// Merge options
|
|
18
18
|
if (options)
|
|
19
19
|
Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
|
|
20
|
-
Reflect.defineMetadata(
|
|
20
|
+
Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
Object.assign(StorageDecorator, ResourceDecorator);
|
|
@@ -38,9 +38,9 @@ function createOperationDecorator(operation) {
|
|
|
38
38
|
if (propertyKey !== operation)
|
|
39
39
|
throw new TypeError(`Name of the handler name should be '${operation}'`);
|
|
40
40
|
const operationMeta = { ...options };
|
|
41
|
-
const sourceMetadata = (Reflect.getOwnMetadata(
|
|
41
|
+
const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
|
|
42
42
|
sourceMetadata.operations = sourceMetadata.operations || {};
|
|
43
43
|
sourceMetadata.operations[operation] = operationMeta;
|
|
44
|
-
Reflect.defineMetadata(
|
|
44
|
+
Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
|
|
45
45
|
});
|
|
46
46
|
}
|
|
@@ -34,7 +34,8 @@ export function isSource(obj) {
|
|
|
34
34
|
return obj && typeof obj === 'object' &&
|
|
35
35
|
(obj.kind === Container.Kind ||
|
|
36
36
|
obj.kind === Collection.Kind ||
|
|
37
|
-
obj.kind === Singleton.Kind
|
|
37
|
+
obj.kind === Singleton.Kind ||
|
|
38
|
+
obj.kind === Storage.Kind);
|
|
38
39
|
}
|
|
39
40
|
export function isCollection(obj) {
|
|
40
41
|
return obj && typeof obj === 'object' && obj.kind === Collection.Kind;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const DATATYPE_METADATA: unique symbol;
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const RESOURCE_METADATA: unique symbol;
|
|
3
3
|
export declare const DECORATOR: unique symbol;
|
|
4
4
|
export declare const NAMESPACE_PATTERN: RegExp;
|
|
5
5
|
export declare const TYPENAME_PATTERN: RegExp;
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { OpraSchema } from '../../schema/index.js';
|
|
2
|
+
export declare namespace Action {
|
|
3
|
+
interface InitArguments extends OpraSchema.Action {
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
2
7
|
export declare class Action {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
constructor(init: Action.InitArguments);
|
|
3
10
|
exportSchema(): OpraSchema.Action;
|
|
4
11
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { StrictOmit, Type } from 'ts-gems';
|
|
2
|
-
import { ResponsiveMap } from '../../helpers/index.js';
|
|
3
2
|
import { OpraSchema } from '../../schema/index.js';
|
|
4
3
|
import type { ApiDocument } from '../api-document.js';
|
|
5
4
|
import { nodeInspectCustom } from '../utils/inspect.util.js';
|
|
@@ -24,7 +23,7 @@ export declare abstract class Resource {
|
|
|
24
23
|
readonly description?: string;
|
|
25
24
|
readonly controller?: object | Type;
|
|
26
25
|
abstract readonly operations: Record<string, any>;
|
|
27
|
-
readonly actions:
|
|
26
|
+
readonly actions: Record<string, Action>;
|
|
28
27
|
protected constructor(document: ApiDocument, init: Resource.InitArguments);
|
|
29
28
|
exportSchema(): OpraSchema.ResourceBase;
|
|
30
29
|
toString(): string;
|