@opra/common 0.23.2 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/browser.js +298 -205
  2. package/cjs/document/constants.js +4 -2
  3. package/cjs/document/data-type/api-field.js +3 -3
  4. package/cjs/document/data-type/complex-type.js +2 -2
  5. package/cjs/document/data-type/enum-type.js +5 -5
  6. package/cjs/document/data-type/mapped-type.js +9 -9
  7. package/cjs/document/data-type/simple-type.js +2 -2
  8. package/cjs/document/data-type/union-type.js +2 -2
  9. package/cjs/document/factory/create-document.js +3 -3
  10. package/cjs/document/factory/factory.js +6 -6
  11. package/cjs/document/factory/import-resource-class.js +4 -4
  12. package/cjs/document/factory/import-type-class.js +4 -4
  13. package/cjs/document/factory/process-resources.js +12 -12
  14. package/cjs/document/index.js +3 -2
  15. package/cjs/document/resource/action.js +9 -0
  16. package/cjs/document/resource/collection-class.js +174 -0
  17. package/cjs/document/resource/collection-decorator.js +60 -0
  18. package/cjs/document/resource/collection.js +7 -212
  19. package/cjs/document/resource/resource-decorator.js +16 -0
  20. package/cjs/document/resource/resource.js +11 -2
  21. package/cjs/document/resource/singleton-class.js +53 -0
  22. package/cjs/document/resource/singleton-decorator.js +53 -0
  23. package/cjs/document/resource/singleton.js +7 -85
  24. package/cjs/document/resource/storage-class.js +24 -0
  25. package/cjs/document/resource/storage-decorator.js +51 -0
  26. package/cjs/document/resource/storage.js +7 -53
  27. package/cjs/document/utils/generate-codec.js +5 -5
  28. package/cjs/exception/resource-errors/resource-not-found.error.js +1 -1
  29. package/cjs/helpers/mixin-utils.js +2 -2
  30. package/cjs/schema/opra-schema.ns.js +0 -1
  31. package/cjs/schema/type-guards.js +3 -3
  32. package/esm/document/constants.js +3 -1
  33. package/esm/document/data-type/api-field.js +4 -4
  34. package/esm/document/data-type/complex-type.js +3 -3
  35. package/esm/document/data-type/enum-type.js +6 -6
  36. package/esm/document/data-type/mapped-type.js +10 -10
  37. package/esm/document/data-type/simple-type.js +3 -3
  38. package/esm/document/data-type/union-type.js +3 -3
  39. package/esm/document/factory/create-document.js +3 -3
  40. package/esm/document/factory/factory.js +7 -7
  41. package/esm/document/factory/import-resource-class.js +5 -5
  42. package/esm/document/factory/import-type-class.js +5 -5
  43. package/esm/document/factory/process-resources.js +9 -9
  44. package/esm/document/index.js +3 -2
  45. package/esm/document/resource/action.js +5 -0
  46. package/esm/document/resource/collection-class.js +169 -0
  47. package/esm/document/resource/collection-decorator.js +55 -0
  48. package/esm/document/resource/collection.js +6 -211
  49. package/esm/document/resource/resource-decorator.js +13 -0
  50. package/esm/document/resource/resource.js +11 -2
  51. package/esm/document/resource/singleton-class.js +49 -0
  52. package/esm/document/resource/singleton-decorator.js +48 -0
  53. package/esm/document/resource/singleton.js +6 -84
  54. package/esm/document/resource/storage-class.js +20 -0
  55. package/esm/document/resource/storage-decorator.js +46 -0
  56. package/esm/document/resource/storage.js +6 -52
  57. package/esm/document/utils/generate-codec.js +3 -3
  58. package/esm/exception/resource-errors/resource-not-found.error.js +1 -1
  59. package/esm/helpers/mixin-utils.js +2 -2
  60. package/esm/schema/opra-schema.ns.js +0 -1
  61. package/esm/schema/type-guards.js +1 -1
  62. package/package.json +1 -1
  63. package/types/document/api-document.d.ts +1 -1
  64. package/types/document/constants.d.ts +3 -1
  65. package/types/document/data-type/mapped-type.d.ts +2 -2
  66. package/types/document/factory/factory.d.ts +7 -7
  67. package/types/document/factory/process-resources.d.ts +2 -2
  68. package/types/document/index.d.ts +3 -2
  69. package/types/document/interfaces/{collection-resource.interface.d.ts → collection.interface.d.ts} +1 -1
  70. package/types/document/interfaces/{singleton-resource.interface.d.ts → singleton.interface.d.ts} +1 -1
  71. package/types/document/interfaces/{storage-resource.interface.d.ts → storage.interface.d.ts} +1 -1
  72. package/types/document/resource/action.d.ts +4 -0
  73. package/types/document/resource/collection-class.d.ts +24 -0
  74. package/types/document/resource/collection-decorator.d.ts +30 -0
  75. package/types/document/resource/collection.d.ts +12 -39
  76. package/types/document/resource/resource-decorator.d.ts +5 -0
  77. package/types/document/resource/resource.d.ts +8 -0
  78. package/types/document/resource/singleton-class.d.ts +20 -0
  79. package/types/document/resource/singleton-decorator.d.ts +25 -0
  80. package/types/document/resource/singleton.d.ts +10 -29
  81. package/types/document/resource/storage-class.d.ts +12 -0
  82. package/types/document/resource/storage-decorator.d.ts +22 -0
  83. package/types/document/resource/storage.d.ts +9 -20
  84. package/types/document/utils/generate-codec.d.ts +1 -1
  85. package/types/helpers/mixin-utils.d.ts +1 -1
  86. package/types/schema/document.interface.d.ts +1 -1
  87. package/types/schema/opra-schema.ns.d.ts +0 -1
  88. package/types/schema/resource/collection.interface.d.ts +18 -19
  89. package/types/schema/resource/container.interface.d.ts +4 -2
  90. package/types/schema/resource/resource.interface.d.ts +14 -0
  91. package/types/schema/resource/singleton.interface.d.ts +8 -8
  92. package/types/schema/resource/storage.interface.d.ts +7 -8
  93. package/types/schema/type-guards.d.ts +1 -1
  94. package/cjs/schema/resource/operation.interface.js +0 -2
  95. package/esm/schema/resource/operation.interface.js +0 -1
  96. package/types/schema/resource/operation.interface.d.ts +0 -3
  97. /package/cjs/document/interfaces/{collection-resource.interface.js → collection.interface.js} +0 -0
  98. /package/cjs/document/interfaces/{singleton-resource.interface.js → singleton.interface.js} +0 -0
  99. /package/cjs/document/interfaces/{storage-resource.interface.js → storage.interface.js} +0 -0
  100. /package/esm/document/interfaces/{collection-resource.interface.js → collection.interface.js} +0 -0
  101. /package/esm/document/interfaces/{singleton-resource.interface.js → singleton.interface.js} +0 -0
  102. /package/esm/document/interfaces/{storage-resource.interface.js → storage.interface.js} +0 -0
@@ -2,179 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Collection = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
5
  const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
7
- const vg = tslib_1.__importStar(require("valgen"));
8
- const index_js_1 = require("../../exception/index.js");
9
- const index_js_2 = require("../../filter/index.js");
10
- const index_js_3 = require("../../helpers/index.js");
11
- const index_js_4 = require("../../i18n/index.js");
12
- const index_js_5 = require("../../schema/index.js");
13
6
  const constants_js_1 = require("../constants.js");
14
- const simple_type_js_1 = require("../data-type/simple-type.js");
15
- const generate_codec_js_1 = require("../utils/generate-codec.js");
16
- const resource_js_1 = require("./resource.js");
17
- const NESTJS_INJECTABLE_WATERMARK = '__injectable__'; // todo, put this in nextjs package wia augmentation
18
- const NAME_PATTERN = /^(.*)(Resource|Collection)$/;
19
- class CollectionClass extends resource_js_1.Resource {
20
- constructor(document, init) {
21
- super(document, init);
22
- this._decoders = {};
23
- this._encoders = {};
24
- this.kind = index_js_5.OpraSchema.Collection.Kind;
25
- this.controller = init.controller;
26
- this.operations = { ...init.operations };
27
- const dataType = this.type = init.type;
28
- // Validate key fields
29
- this.primaryKey = init.primaryKey
30
- ? (Array.isArray(init.primaryKey) ? init.primaryKey : [init.primaryKey])
31
- : [];
32
- if (!this.primaryKey.length)
33
- throw new TypeError(`You must provide primaryKey for Collection resource ("${this.name}")`);
34
- this.primaryKey.forEach(f => {
35
- const field = dataType.getField(f);
36
- if (!(field?.type instanceof simple_type_js_1.SimpleType))
37
- throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${field.type.kind}`);
38
- });
39
- }
40
- exportSchema() {
41
- const out = resource_js_1.Resource.prototype.exportSchema.call(this);
42
- Object.assign(out, (0, index_js_3.omitUndefined)({
43
- type: this.type.name,
44
- operations: this.operations,
45
- primaryKey: this.primaryKey
46
- }));
47
- return out;
48
- }
49
- parseKeyValue(value) {
50
- if (!this.primaryKey?.length)
51
- return;
52
- const dataType = this.type;
53
- if (this.primaryKey.length > 1) {
54
- // Build primary key/value mapped object
55
- const obj = Array.isArray(value)
56
- ? this.primaryKey.reduce((o, k, i) => {
57
- o[k] = value[i];
58
- return obj;
59
- }, {})
60
- : value;
61
- // decode values
62
- for (const [k, v] of Object.entries(obj)) {
63
- const el = dataType.getField(k);
64
- if (el.type instanceof simple_type_js_1.SimpleType)
65
- obj[k] = el.type.decode(v);
66
- if (obj[k] == null)
67
- throw new TypeError(`You must provide value of primary field(s) (${k})`);
68
- }
69
- }
70
- else {
71
- const primaryKey = this.primaryKey[0];
72
- if (typeof value === 'object')
73
- value = value[primaryKey];
74
- const el = dataType.getField(primaryKey);
75
- let result;
76
- if (el.type instanceof simple_type_js_1.SimpleType)
77
- result = el.type.decode(value);
78
- if (result == null)
79
- throw new TypeError(`You must provide value of primary field(s) (${primaryKey})`);
80
- return result;
81
- }
82
- }
83
- normalizeFieldPath(path) {
84
- return this.type.normalizeFieldPath(path);
85
- }
86
- normalizeSortFields(fields) {
87
- const normalized = this.type.normalizeFieldPath(fields);
88
- if (!normalized)
89
- return;
90
- const findManyOp = this.operations.findMany;
91
- const sortFields = findManyOp && findManyOp.sortFields;
92
- (Array.isArray(normalized) ? normalized : [normalized]).forEach(field => {
93
- if (!sortFields?.find(x => x === field))
94
- throw new index_js_1.BadRequestError({
95
- message: (0, index_js_4.translate)('error:UNACCEPTED_SORT_FIELD', { field }),
96
- });
97
- });
98
- return normalized;
99
- }
100
- normalizeFilter(filter) {
101
- if (!filter)
102
- return;
103
- const ast = typeof filter === 'string' ? index_js_2.OpraFilter.parse(filter) : filter;
104
- if (ast instanceof index_js_2.OpraFilter.ComparisonExpression) {
105
- this.normalizeFilter(ast.left);
106
- if (!(ast.left instanceof index_js_2.OpraFilter.QualifiedIdentifier && ast.left.field))
107
- throw new TypeError(`Invalid filter query. Left side should be a data field.`);
108
- // Check if filtering accepted for given field
109
- const findManyOp = this.operations.findMany;
110
- const fieldLower = ast.left.value.toLowerCase();
111
- const filterDef = (findManyOp && findManyOp.filters || [])
112
- .find(f => f.field.toLowerCase() === fieldLower);
113
- if (!filterDef) {
114
- throw new index_js_1.BadRequestError({
115
- message: (0, index_js_4.translate)('error:UNACCEPTED_FILTER_FIELD', { field: ast.left.value }),
116
- });
117
- }
118
- // Check if filtering operation accepted for given field
119
- if (!filterDef.operators?.includes(ast.op))
120
- throw new index_js_1.BadRequestError({
121
- message: (0, index_js_4.translate)('error:UNACCEPTED_FILTER_OPERATION', { field: ast.left.value }),
122
- });
123
- this.normalizeFilter(ast.right);
124
- return ast;
125
- }
126
- if (ast instanceof index_js_2.OpraFilter.LogicalExpression) {
127
- ast.items.forEach(item => this.normalizeFilter(item));
128
- return ast;
129
- }
130
- if (ast instanceof index_js_2.OpraFilter.ArithmeticExpression) {
131
- ast.items.forEach(item => this.normalizeFilter(item.expression));
132
- return ast;
133
- }
134
- if (ast instanceof index_js_2.OpraFilter.ArrayExpression) {
135
- ast.items.forEach(item => this.normalizeFilter(item));
136
- return ast;
137
- }
138
- if (ast instanceof index_js_2.OpraFilter.ParenthesizedExpression) {
139
- this.normalizeFilter(ast.expression);
140
- return ast;
141
- }
142
- if (ast instanceof index_js_2.OpraFilter.QualifiedIdentifier) {
143
- const normalizedFieldPath = this.type.normalizeFieldPath(ast.value)?.join('.');
144
- ast.field = this.type.getField(normalizedFieldPath);
145
- ast.dataType = ast.field?.type || this.document.getDataType('any');
146
- ast.value = normalizedFieldPath;
147
- return ast;
148
- }
149
- return ast;
150
- }
151
- getDecoder(operation) {
152
- let decoder = this._decoders[operation];
153
- if (decoder)
154
- return decoder;
155
- const options = {
156
- partial: operation !== 'create'
157
- };
158
- if (operation !== 'create')
159
- options.omit = [...this.primaryKey];
160
- decoder = (0, generate_codec_js_1.generateCodec)(this.type, 'decode', options);
161
- this._decoders[operation] = decoder;
162
- return decoder;
163
- }
164
- getEncoder(operation) {
165
- let encoder = this._encoders[operation];
166
- if (encoder)
167
- return encoder;
168
- const options = {
169
- partial: true
170
- };
171
- encoder = (0, generate_codec_js_1.generateCodec)(this.type, 'encode', options);
172
- if (operation === 'findMany')
173
- return vg.isArray(encoder);
174
- this._encoders[operation] = encoder;
175
- return encoder;
176
- }
177
- }
7
+ const collection_class_js_1 = require("./collection-class.js");
8
+ const collection_decorator_js_1 = require("./collection-decorator.js");
178
9
  /**
179
10
  *
180
11
  */
@@ -182,48 +13,12 @@ exports.Collection = function (...args) {
182
13
  // ClassDecorator
183
14
  if (!this) {
184
15
  const [type, options] = args;
185
- return function (target) {
186
- const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
187
- const metadata = Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target) || {};
188
- const baseMetadata = Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, Object.getPrototypeOf(target));
189
- if (baseMetadata) {
190
- (0, putil_merge_1.default)(metadata, baseMetadata, { deep: true });
191
- }
192
- metadata.kind = index_js_5.OpraSchema.Collection.Kind;
193
- metadata.name = name;
194
- metadata.type = type;
195
- // Merge with previous metadata object
196
- const m = Reflect.getMetadata(constants_js_1.METADATA_KEY, target);
197
- if (m && metadata !== m)
198
- Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
199
- // Merge options
200
- if (options)
201
- Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
202
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, metadata, target);
203
- /* Define Injectable metadata for NestJS support*/
204
- Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
205
- };
16
+ return exports.Collection[constants_js_1.DECORATOR].call(undefined, type, options);
206
17
  }
207
18
  // Constructor
208
19
  const [document, init] = args;
209
- (0, putil_merge_1.default)(this, new CollectionClass(document, init), { descriptor: true });
20
+ (0, putil_merge_1.default)(this, new collection_class_js_1.CollectionClass(document, init), { descriptor: true });
210
21
  };
211
- exports.Collection.prototype = CollectionClass.prototype;
212
- function createOperationDecorator(operation) {
213
- return (options) => ((target, propertyKey) => {
214
- if (propertyKey !== operation)
215
- throw new TypeError(`Name of the handler name should be '${operation}'`);
216
- const operationMeta = { ...options };
217
- const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target.constructor) || {});
218
- resourceMetadata.operations = resourceMetadata.operations || {};
219
- resourceMetadata.operations[operation] = operationMeta;
220
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, resourceMetadata, target.constructor);
221
- });
222
- }
223
- exports.Collection.Create = createOperationDecorator('create');
224
- exports.Collection.Delete = createOperationDecorator('delete');
225
- exports.Collection.DeleteMany = createOperationDecorator('deleteMany');
226
- exports.Collection.Get = createOperationDecorator('get');
227
- exports.Collection.FindMany = createOperationDecorator('findMany');
228
- exports.Collection.Update = createOperationDecorator('update');
229
- exports.Collection.UpdateMany = createOperationDecorator('updateMany');
22
+ exports.Collection.prototype = collection_class_js_1.CollectionClass.prototype;
23
+ Object.assign(exports.Collection, collection_decorator_js_1.CollectionDecorator);
24
+ exports.Collection[constants_js_1.DECORATOR] = collection_decorator_js_1.CollectionDecorator;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResourceDecorator = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.ResourceDecorator = {};
6
+ exports.ResourceDecorator.Action = function (options) {
7
+ return (target, propertyKey) => {
8
+ if (typeof propertyKey !== 'string')
9
+ throw new TypeError(`This decorator can't be used for Symbol keys'`);
10
+ const actionMeta = { ...options };
11
+ const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target.constructor) || {});
12
+ resourceMetadata.actions = resourceMetadata.actions || {};
13
+ resourceMetadata.actions[propertyKey] = actionMeta;
14
+ Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, resourceMetadata, target.constructor);
15
+ };
16
+ };
@@ -1,20 +1,29 @@
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");
4
5
  const object_utils_js_1 = require("../../helpers/object-utils.js");
5
6
  const inspect_util_js_1 = require("../utils/inspect.util.js");
6
7
  class Resource {
7
8
  constructor(document, init) {
9
+ this.actions = new index_js_1.ResponsiveMap();
8
10
  this.document = document;
9
11
  this.name = init.name;
10
12
  this.description = init.description;
11
13
  this.controller = init.controller;
12
14
  }
13
15
  exportSchema() {
14
- return (0, object_utils_js_1.omitUndefined)({
16
+ const schema = (0, object_utils_js_1.omitUndefined)({
15
17
  kind: this.kind,
16
- description: this.description
18
+ description: this.description,
17
19
  });
20
+ if (this.actions.size) {
21
+ const actions = schema.actions = {};
22
+ for (const [name, r] of this.actions.entries()) {
23
+ actions[name] = r.exportSchema();
24
+ }
25
+ }
26
+ return schema;
18
27
  }
19
28
  toString() {
20
29
  return `[${Object.getPrototypeOf(this).constructor.name} ${this.name || '#anonymous'}]`;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingletonClass = void 0;
4
+ const index_js_1 = require("../../helpers/index.js");
5
+ const index_js_2 = require("../../schema/index.js");
6
+ const generate_codec_js_1 = require("../utils/generate-codec.js");
7
+ const resource_js_1 = require("./resource.js");
8
+ class SingletonClass extends resource_js_1.Resource {
9
+ constructor(document, init) {
10
+ super(document, init);
11
+ this._decoders = {};
12
+ this._encoders = {};
13
+ this.kind = index_js_2.OpraSchema.Singleton.Kind;
14
+ this.controller = init.controller;
15
+ this.operations = { ...init.operations };
16
+ this.type = init.type;
17
+ }
18
+ exportSchema() {
19
+ return {
20
+ ...super.exportSchema(),
21
+ ...(0, index_js_1.omitUndefined)({
22
+ type: this.type.name || 'any',
23
+ operations: this.operations
24
+ })
25
+ };
26
+ }
27
+ normalizeFieldPath(path) {
28
+ return this.type.normalizeFieldPath(path);
29
+ }
30
+ getDecoder(endpoint) {
31
+ let decoder = this._decoders[endpoint];
32
+ if (decoder)
33
+ return decoder;
34
+ const options = {
35
+ partial: endpoint !== 'create'
36
+ };
37
+ decoder = (0, generate_codec_js_1.generateCodec)(this.type, 'decode', options);
38
+ this._decoders[endpoint] = decoder;
39
+ return decoder;
40
+ }
41
+ getEncoder(endpoint) {
42
+ let encoder = this._encoders[endpoint];
43
+ if (encoder)
44
+ return encoder;
45
+ const options = {
46
+ partial: true
47
+ };
48
+ encoder = (0, generate_codec_js_1.generateCodec)(this.type, 'encode', options);
49
+ this._encoders[endpoint] = encoder;
50
+ return encoder;
51
+ }
52
+ }
53
+ exports.SingletonClass = SingletonClass;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingletonDecorator = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
+ const index_js_1 = require("../../schema/index.js");
7
+ const constants_js_1 = require("../constants.js");
8
+ const resource_decorator_js_1 = require("./resource-decorator.js");
9
+ const NAME_PATTERN = /^(.*)(Resource|Singleton|Controller)$/;
10
+ const operationProperties = ['create', 'delete', 'get', 'update'];
11
+ function SingletonDecorator(type, options) {
12
+ return function (target) {
13
+ const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
14
+ const metadata = Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target) || {};
15
+ metadata.kind = index_js_1.OpraSchema.Singleton.Kind;
16
+ metadata.name = name;
17
+ metadata.type = type;
18
+ // Merge with previous metadata object
19
+ const m = Reflect.getMetadata(constants_js_1.SOURCE_METADATA, target);
20
+ if (m && metadata !== m)
21
+ Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
22
+ // Merge options
23
+ if (options)
24
+ Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
25
+ Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, metadata, target);
26
+ };
27
+ }
28
+ exports.SingletonDecorator = SingletonDecorator;
29
+ Object.assign(SingletonDecorator, resource_decorator_js_1.ResourceDecorator);
30
+ SingletonDecorator.Create = createOperationDecorator('create');
31
+ SingletonDecorator.Get = createOperationDecorator('get');
32
+ SingletonDecorator.Delete = createOperationDecorator('delete');
33
+ SingletonDecorator.Update = createOperationDecorator('update');
34
+ SingletonDecorator.Action = function (options) {
35
+ const oldDecorator = resource_decorator_js_1.ResourceDecorator.Action(options);
36
+ const operators = ['create', 'delete', 'get', 'update'];
37
+ return (target, propertyKey) => {
38
+ if (typeof propertyKey === 'string' && operators.includes(propertyKey))
39
+ throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
40
+ return oldDecorator(target, propertyKey);
41
+ };
42
+ };
43
+ function createOperationDecorator(operation) {
44
+ return (options) => ((target, propertyKey) => {
45
+ if (propertyKey !== operation)
46
+ throw new TypeError(`Name of the handler name should be '${operation}'`);
47
+ const operationMeta = { ...options };
48
+ const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target.constructor) || {});
49
+ sourceMetadata.operations = sourceMetadata.operations || {};
50
+ sourceMetadata.operations[operation] = operationMeta;
51
+ Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, sourceMetadata, target.constructor);
52
+ });
53
+ }
@@ -2,98 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Singleton = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
5
  const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
7
- const index_js_1 = require("../../helpers/index.js");
8
- const index_js_2 = require("../../schema/index.js");
9
6
  const constants_js_1 = require("../constants.js");
10
- const generate_codec_js_1 = require("../utils/generate-codec.js");
11
- const resource_js_1 = require("./resource.js");
12
- const NESTJS_INJECTABLE_WATERMARK = '__injectable__'; // todo, put this in nextjs package wia augmentation
13
- const NAME_PATTERN = /^(.*)(Resource|Singleton)$/;
14
- class SingletonClass extends resource_js_1.Resource {
15
- constructor(document, init) {
16
- super(document, init);
17
- this._decoders = {};
18
- this._encoders = {};
19
- this.kind = index_js_2.OpraSchema.Singleton.Kind;
20
- this.controller = init.controller;
21
- this.operations = { ...init.operations };
22
- this.type = init.type;
23
- }
24
- exportSchema() {
25
- const out = resource_js_1.Resource.prototype.exportSchema.call(this);
26
- Object.assign(out, (0, index_js_1.omitUndefined)({
27
- type: this.type.name,
28
- operations: this.operations
29
- }));
30
- return out;
31
- }
32
- normalizeFieldPath(path) {
33
- return this.type.normalizeFieldPath(path);
34
- }
35
- getDecoder(operation) {
36
- let decoder = this._decoders[operation];
37
- if (decoder)
38
- return decoder;
39
- const options = {
40
- partial: operation !== 'create'
41
- };
42
- decoder = (0, generate_codec_js_1.generateCodec)(this.type, 'decode', options);
43
- this._decoders[operation] = decoder;
44
- return decoder;
45
- }
46
- getEncoder(operation) {
47
- let encoder = this._encoders[operation];
48
- if (encoder)
49
- return encoder;
50
- const options = {
51
- partial: true
52
- };
53
- encoder = (0, generate_codec_js_1.generateCodec)(this.type, 'encode', options);
54
- this._encoders[operation] = encoder;
55
- return encoder;
56
- }
57
- }
7
+ const singleton_class_js_1 = require("./singleton-class.js");
8
+ const singleton_decorator_js_1 = require("./singleton-decorator.js");
58
9
  exports.Singleton = function (...args) {
59
10
  // ClassDecorator
60
11
  if (!this) {
61
12
  const [type, options] = args;
62
- return function (target) {
63
- const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
64
- const metadata = Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target) || {};
65
- metadata.kind = index_js_2.OpraSchema.Singleton.Kind;
66
- metadata.name = name;
67
- metadata.type = type;
68
- // Merge with previous metadata object
69
- const m = Reflect.getMetadata(constants_js_1.METADATA_KEY, target);
70
- if (m && metadata !== m)
71
- Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
72
- // Merge options
73
- if (options)
74
- Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
75
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, metadata, target);
76
- /* Define Injectable metadata for NestJS support*/
77
- Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
78
- };
13
+ return exports.Singleton[constants_js_1.DECORATOR].call(undefined, type, options);
79
14
  }
80
15
  // Constructor
81
16
  const [document, init] = args;
82
- (0, putil_merge_1.default)(this, new SingletonClass(document, init), { descriptor: true });
17
+ (0, putil_merge_1.default)(this, new singleton_class_js_1.SingletonClass(document, init), { descriptor: true });
83
18
  };
84
- exports.Singleton.prototype = SingletonClass.prototype;
85
- function createOperationDecorator(operation) {
86
- return (options) => ((target, propertyKey) => {
87
- if (propertyKey !== operation)
88
- throw new TypeError(`Name of the handler name should be '${operation}'`);
89
- const operationMeta = { ...options };
90
- const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target.constructor) || {});
91
- resourceMetadata.operations = resourceMetadata.operations || {};
92
- resourceMetadata.operations[operation] = operationMeta;
93
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, resourceMetadata, target.constructor);
94
- });
95
- }
96
- exports.Singleton.Create = createOperationDecorator('create');
97
- exports.Singleton.Get = createOperationDecorator('get');
98
- exports.Singleton.Delete = createOperationDecorator('delete');
99
- exports.Singleton.Update = createOperationDecorator('update');
19
+ exports.Singleton.prototype = singleton_class_js_1.SingletonClass.prototype;
20
+ Object.assign(exports.Singleton, singleton_decorator_js_1.SingletonDecorator);
21
+ exports.Singleton[constants_js_1.DECORATOR] = singleton_decorator_js_1.SingletonDecorator;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageClass = void 0;
4
+ const index_js_1 = require("../../helpers/index.js");
5
+ const index_js_2 = require("../../schema/index.js");
6
+ const resource_js_1 = require("./resource.js");
7
+ class StorageClass extends resource_js_1.Resource {
8
+ constructor(document, init) {
9
+ super(document, init);
10
+ this.kind = index_js_2.OpraSchema.Storage.Kind;
11
+ this.controller = init.controller;
12
+ this.operations = { ...init.operations };
13
+ }
14
+ exportSchema() {
15
+ return {
16
+ ...super.exportSchema(),
17
+ ...(0, index_js_1.omitUndefined)({
18
+ kind: index_js_2.OpraSchema.Storage.Kind,
19
+ operations: this.operations
20
+ })
21
+ };
22
+ }
23
+ }
24
+ exports.StorageClass = StorageClass;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageDecorator = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
+ const index_js_1 = require("../../schema/index.js");
7
+ const constants_js_1 = require("../constants.js");
8
+ const resource_decorator_js_1 = require("./resource-decorator.js");
9
+ const NAME_PATTERN = /^(.*)(Resource|Storage|Controller)$/;
10
+ const operationProperties = ['delete', 'get', 'post'];
11
+ function StorageDecorator(options) {
12
+ return function (target) {
13
+ const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
14
+ const metadata = Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target) || {};
15
+ metadata.kind = index_js_1.OpraSchema.Storage.Kind;
16
+ metadata.name = name;
17
+ // Merge with previous metadata object
18
+ const m = Reflect.getMetadata(constants_js_1.SOURCE_METADATA, target);
19
+ if (m && metadata !== m)
20
+ Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
21
+ // Merge options
22
+ if (options)
23
+ Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
24
+ Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, metadata, target);
25
+ };
26
+ }
27
+ exports.StorageDecorator = StorageDecorator;
28
+ Object.assign(StorageDecorator, resource_decorator_js_1.ResourceDecorator);
29
+ StorageDecorator.Delete = createOperationDecorator('delete');
30
+ StorageDecorator.Get = createOperationDecorator('get');
31
+ StorageDecorator.Post = createOperationDecorator('post');
32
+ StorageDecorator.Action = function (options) {
33
+ const oldDecorator = resource_decorator_js_1.ResourceDecorator.Action(options);
34
+ const operators = ['delete', 'get', 'post'];
35
+ return (target, propertyKey) => {
36
+ if (typeof propertyKey === 'string' && operators.includes(propertyKey))
37
+ throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
38
+ return oldDecorator(target, propertyKey);
39
+ };
40
+ };
41
+ function createOperationDecorator(operation) {
42
+ return (options) => ((target, propertyKey) => {
43
+ if (propertyKey !== operation)
44
+ throw new TypeError(`Name of the handler name should be '${operation}'`);
45
+ const operationMeta = { ...options };
46
+ const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.SOURCE_METADATA, target.constructor) || {});
47
+ sourceMetadata.operations = sourceMetadata.operations || {};
48
+ sourceMetadata.operations[operation] = operationMeta;
49
+ Reflect.defineMetadata(constants_js_1.SOURCE_METADATA, sourceMetadata, target.constructor);
50
+ });
51
+ }
@@ -2,66 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Storage = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
5
  const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
7
- const index_js_1 = require("../../helpers/index.js");
8
- const index_js_2 = require("../../schema/index.js");
9
6
  const constants_js_1 = require("../constants.js");
10
- const resource_js_1 = require("./resource.js");
11
- const NESTJS_INJECTABLE_WATERMARK = '__injectable__';
12
- const NAME_PATTERN = /^(.*)(Resource)$/;
13
- class StorageClass extends resource_js_1.Resource {
14
- constructor(document, init) {
15
- super(document, init);
16
- this.kind = index_js_2.OpraSchema.Storage.Kind;
17
- this.controller = init.controller;
18
- this.operations = { ...init.operations };
19
- }
20
- exportSchema() {
21
- const out = resource_js_1.Resource.prototype.exportSchema.call(this);
22
- Object.assign(out, (0, index_js_1.omitUndefined)({
23
- operations: this.operations
24
- }));
25
- return out;
26
- }
27
- }
7
+ const storage_class_js_1 = require("./storage-class.js");
8
+ const storage_decorator_js_1 = require("./storage-decorator.js");
28
9
  exports.Storage = function (...args) {
29
10
  // ClassDecorator
30
11
  if (!this) {
31
12
  const [options] = args;
32
- return function (target) {
33
- const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
34
- const metadata = Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target) || {};
35
- metadata.kind = index_js_2.OpraSchema.Storage.Kind;
36
- metadata.name = name;
37
- // Merge with previous metadata object
38
- const m = Reflect.getMetadata(constants_js_1.METADATA_KEY, target);
39
- if (m && metadata !== m)
40
- Object.assign(metadata, (0, lodash_omit_1.default)(m), Object.keys(metadata));
41
- // Merge options
42
- if (options)
43
- Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'name', 'type', 'controller']));
44
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, metadata, target);
45
- /* Define Injectable metadata for NestJS support*/
46
- Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
47
- };
13
+ return exports.Storage[constants_js_1.DECORATOR].call(undefined, options);
48
14
  }
49
15
  // Constructor
50
16
  const [document, init] = args;
51
- (0, putil_merge_1.default)(this, new StorageClass(document, init), { descriptor: true });
17
+ (0, putil_merge_1.default)(this, new storage_class_js_1.StorageClass(document, init), { descriptor: true });
52
18
  };
53
- exports.Storage.prototype = StorageClass.prototype;
54
- function createOperationDecorator(operation) {
55
- return (options) => ((target, propertyKey) => {
56
- if (propertyKey !== operation)
57
- throw new TypeError(`Name of the handler name should be '${operation}'`);
58
- const operationMeta = { ...options };
59
- const resourceMetadata = (Reflect.getOwnMetadata(constants_js_1.METADATA_KEY, target.constructor) || {});
60
- resourceMetadata.operations = resourceMetadata.operations || {};
61
- resourceMetadata.operations[operation] = operationMeta;
62
- Reflect.defineMetadata(constants_js_1.METADATA_KEY, resourceMetadata, target.constructor);
63
- });
64
- }
65
- exports.Storage.Delete = createOperationDecorator('delete');
66
- exports.Storage.Get = createOperationDecorator('get');
67
- exports.Storage.Post = createOperationDecorator('post');
19
+ exports.Storage.prototype = storage_class_js_1.StorageClass.prototype;
20
+ Object.assign(exports.Storage, storage_decorator_js_1.StorageDecorator);
21
+ exports.Storage[constants_js_1.DECORATOR] = storage_decorator_js_1.StorageDecorator;