@opra/common 0.23.2 → 0.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/browser.js +319 -206
  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 +12 -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 +18 -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 +5 -4
  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 +8 -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 +18 -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 +3 -2
  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 +11 -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 +7 -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
@@ -1,95 +1,17 @@
1
- import omit from 'lodash.omit';
2
1
  import merge from 'putil-merge';
3
- import { omitUndefined } from '../../helpers/index.js';
4
- import { OpraSchema } from '../../schema/index.js';
5
- import { METADATA_KEY } from '../constants.js';
6
- import { generateCodec } from '../utils/generate-codec.js';
7
- import { Resource } from './resource.js';
8
- const NESTJS_INJECTABLE_WATERMARK = '__injectable__'; // todo, put this in nextjs package wia augmentation
9
- const NAME_PATTERN = /^(.*)(Resource|Singleton)$/;
10
- class SingletonClass extends Resource {
11
- constructor(document, init) {
12
- super(document, init);
13
- this._decoders = {};
14
- this._encoders = {};
15
- this.kind = OpraSchema.Singleton.Kind;
16
- this.controller = init.controller;
17
- this.operations = { ...init.operations };
18
- this.type = init.type;
19
- }
20
- exportSchema() {
21
- const out = Resource.prototype.exportSchema.call(this);
22
- Object.assign(out, omitUndefined({
23
- type: this.type.name,
24
- operations: this.operations
25
- }));
26
- return out;
27
- }
28
- normalizeFieldPath(path) {
29
- return this.type.normalizeFieldPath(path);
30
- }
31
- getDecoder(operation) {
32
- let decoder = this._decoders[operation];
33
- if (decoder)
34
- return decoder;
35
- const options = {
36
- partial: operation !== 'create'
37
- };
38
- decoder = generateCodec(this.type, 'decode', options);
39
- this._decoders[operation] = decoder;
40
- return decoder;
41
- }
42
- getEncoder(operation) {
43
- let encoder = this._encoders[operation];
44
- if (encoder)
45
- return encoder;
46
- const options = {
47
- partial: true
48
- };
49
- encoder = generateCodec(this.type, 'encode', options);
50
- this._encoders[operation] = encoder;
51
- return encoder;
52
- }
53
- }
2
+ import { DECORATOR } from '../constants.js';
3
+ import { SingletonClass } from './singleton-class.js';
4
+ import { SingletonDecorator } from './singleton-decorator.js';
54
5
  export const Singleton = function (...args) {
55
6
  // ClassDecorator
56
7
  if (!this) {
57
8
  const [type, options] = args;
58
- return function (target) {
59
- const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
60
- const metadata = Reflect.getOwnMetadata(METADATA_KEY, target) || {};
61
- metadata.kind = OpraSchema.Singleton.Kind;
62
- metadata.name = name;
63
- metadata.type = type;
64
- // Merge with previous metadata object
65
- const m = Reflect.getMetadata(METADATA_KEY, target);
66
- if (m && metadata !== m)
67
- Object.assign(metadata, omit(m), Object.keys(metadata));
68
- // Merge options
69
- if (options)
70
- Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
71
- Reflect.defineMetadata(METADATA_KEY, metadata, target);
72
- /* Define Injectable metadata for NestJS support*/
73
- Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
74
- };
9
+ return Singleton[DECORATOR].call(undefined, type, options);
75
10
  }
76
11
  // Constructor
77
12
  const [document, init] = args;
78
13
  merge(this, new SingletonClass(document, init), { descriptor: true });
79
14
  };
80
15
  Singleton.prototype = SingletonClass.prototype;
81
- function createOperationDecorator(operation) {
82
- return (options) => ((target, propertyKey) => {
83
- if (propertyKey !== operation)
84
- throw new TypeError(`Name of the handler name should be '${operation}'`);
85
- const operationMeta = { ...options };
86
- const resourceMetadata = (Reflect.getOwnMetadata(METADATA_KEY, target.constructor) || {});
87
- resourceMetadata.operations = resourceMetadata.operations || {};
88
- resourceMetadata.operations[operation] = operationMeta;
89
- Reflect.defineMetadata(METADATA_KEY, resourceMetadata, target.constructor);
90
- });
91
- }
92
- Singleton.Create = createOperationDecorator('create');
93
- Singleton.Get = createOperationDecorator('get');
94
- Singleton.Delete = createOperationDecorator('delete');
95
- Singleton.Update = createOperationDecorator('update');
16
+ Object.assign(Singleton, SingletonDecorator);
17
+ Singleton[DECORATOR] = SingletonDecorator;
@@ -0,0 +1,20 @@
1
+ import { omitUndefined } from '../../helpers/index.js';
2
+ import { OpraSchema } from '../../schema/index.js';
3
+ import { Resource } from './resource.js';
4
+ export class StorageClass extends Resource {
5
+ constructor(document, init) {
6
+ super(document, init);
7
+ this.kind = OpraSchema.Storage.Kind;
8
+ this.controller = init.controller;
9
+ this.operations = { ...init.operations };
10
+ }
11
+ exportSchema() {
12
+ return {
13
+ ...super.exportSchema(),
14
+ ...omitUndefined({
15
+ kind: OpraSchema.Storage.Kind,
16
+ operations: this.operations
17
+ })
18
+ };
19
+ }
20
+ }
@@ -0,0 +1,46 @@
1
+ import omit from 'lodash.omit';
2
+ import { OpraSchema } from '../../schema/index.js';
3
+ import { RESOURCE_METADATA } from '../constants.js';
4
+ import { ResourceDecorator } from './resource-decorator.js';
5
+ const NAME_PATTERN = /^(.*)(Resource|Storage|Controller)$/;
6
+ const operationProperties = ['delete', 'get', 'post'];
7
+ export function StorageDecorator(options) {
8
+ return function (target) {
9
+ const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
10
+ const metadata = Reflect.getOwnMetadata(RESOURCE_METADATA, target) || {};
11
+ metadata.kind = OpraSchema.Storage.Kind;
12
+ metadata.name = name;
13
+ // Merge with previous metadata object
14
+ const m = Reflect.getMetadata(RESOURCE_METADATA, target);
15
+ if (m && metadata !== m)
16
+ Object.assign(metadata, omit(m), Object.keys(metadata));
17
+ // Merge options
18
+ if (options)
19
+ Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
20
+ Reflect.defineMetadata(RESOURCE_METADATA, metadata, target);
21
+ };
22
+ }
23
+ Object.assign(StorageDecorator, ResourceDecorator);
24
+ StorageDecorator.Delete = createOperationDecorator('delete');
25
+ StorageDecorator.Get = createOperationDecorator('get');
26
+ StorageDecorator.Post = createOperationDecorator('post');
27
+ StorageDecorator.Action = function (options) {
28
+ const oldDecorator = ResourceDecorator.Action(options);
29
+ const operators = ['delete', 'get', 'post'];
30
+ return (target, propertyKey) => {
31
+ if (typeof propertyKey === 'string' && operators.includes(propertyKey))
32
+ throw new TypeError(`The "${propertyKey}" property is reserved for "${propertyKey}" operations and cannot be used as an action'`);
33
+ return oldDecorator(target, propertyKey);
34
+ };
35
+ };
36
+ function createOperationDecorator(operation) {
37
+ return (options) => ((target, propertyKey) => {
38
+ if (propertyKey !== operation)
39
+ throw new TypeError(`Name of the handler name should be '${operation}'`);
40
+ const operationMeta = { ...options };
41
+ const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
42
+ sourceMetadata.operations = sourceMetadata.operations || {};
43
+ sourceMetadata.operations[operation] = operationMeta;
44
+ Reflect.defineMetadata(RESOURCE_METADATA, sourceMetadata, target.constructor);
45
+ });
46
+ }
@@ -1,63 +1,17 @@
1
- import omit from 'lodash.omit';
2
1
  import merge from 'putil-merge';
3
- import { omitUndefined } from '../../helpers/index.js';
4
- import { OpraSchema } from '../../schema/index.js';
5
- import { METADATA_KEY } from '../constants.js';
6
- import { Resource } from './resource.js';
7
- const NESTJS_INJECTABLE_WATERMARK = '__injectable__';
8
- const NAME_PATTERN = /^(.*)(Resource)$/;
9
- class StorageClass extends Resource {
10
- constructor(document, init) {
11
- super(document, init);
12
- this.kind = OpraSchema.Storage.Kind;
13
- this.controller = init.controller;
14
- this.operations = { ...init.operations };
15
- }
16
- exportSchema() {
17
- const out = Resource.prototype.exportSchema.call(this);
18
- Object.assign(out, omitUndefined({
19
- operations: this.operations
20
- }));
21
- return out;
22
- }
23
- }
2
+ import { DECORATOR } from '../constants.js';
3
+ import { StorageClass } from './storage-class.js';
4
+ import { StorageDecorator } from './storage-decorator.js';
24
5
  export const Storage = function (...args) {
25
6
  // ClassDecorator
26
7
  if (!this) {
27
8
  const [options] = args;
28
- return function (target) {
29
- const name = options?.name || target.name.match(NAME_PATTERN)?.[1] || target.name;
30
- const metadata = Reflect.getOwnMetadata(METADATA_KEY, target) || {};
31
- metadata.kind = OpraSchema.Storage.Kind;
32
- metadata.name = name;
33
- // Merge with previous metadata object
34
- const m = Reflect.getMetadata(METADATA_KEY, target);
35
- if (m && metadata !== m)
36
- Object.assign(metadata, omit(m), Object.keys(metadata));
37
- // Merge options
38
- if (options)
39
- Object.assign(metadata, omit(options, ['kind', 'name', 'type', 'controller']));
40
- Reflect.defineMetadata(METADATA_KEY, metadata, target);
41
- /* Define Injectable metadata for NestJS support*/
42
- Reflect.defineMetadata(NESTJS_INJECTABLE_WATERMARK, true, target);
43
- };
9
+ return Storage[DECORATOR].call(undefined, options);
44
10
  }
45
11
  // Constructor
46
12
  const [document, init] = args;
47
13
  merge(this, new StorageClass(document, init), { descriptor: true });
48
14
  };
49
15
  Storage.prototype = StorageClass.prototype;
50
- function createOperationDecorator(operation) {
51
- return (options) => ((target, propertyKey) => {
52
- if (propertyKey !== operation)
53
- throw new TypeError(`Name of the handler name should be '${operation}'`);
54
- const operationMeta = { ...options };
55
- const resourceMetadata = (Reflect.getOwnMetadata(METADATA_KEY, target.constructor) || {});
56
- resourceMetadata.operations = resourceMetadata.operations || {};
57
- resourceMetadata.operations[operation] = operationMeta;
58
- Reflect.defineMetadata(METADATA_KEY, resourceMetadata, target.constructor);
59
- });
60
- }
61
- Storage.Delete = createOperationDecorator('delete');
62
- Storage.Get = createOperationDecorator('get');
63
- Storage.Post = createOperationDecorator('post');
16
+ Object.assign(Storage, StorageDecorator);
17
+ Storage[DECORATOR] = StorageDecorator;
@@ -5,9 +5,9 @@ import { MappedType } from '../data-type/mapped-type.js';
5
5
  import { SimpleType } from '../data-type/simple-type.js';
6
6
  import { UnionType } from '../data-type/union-type.js';
7
7
  export function generateCodec(type, codec, options) {
8
- return _generateDecoder(type, codec, options);
8
+ return _generateCodec(type, codec, options);
9
9
  }
10
- export function _generateDecoder(type, codec, options) {
10
+ export function _generateCodec(type, codec, options) {
11
11
  const schema = {};
12
12
  for (const f of type.fields.values()) {
13
13
  let fn;
@@ -15,7 +15,7 @@ export function _generateDecoder(type, codec, options) {
15
15
  fn = f.type[codec];
16
16
  }
17
17
  else if (f.type instanceof ComplexType || f.type instanceof MappedType || f.type instanceof UnionType) {
18
- fn = _generateDecoder(f.type, codec, options);
18
+ fn = _generateCodec(f.type, codec, options);
19
19
  }
20
20
  /* istanbul ignore next */
21
21
  if (!fn)
@@ -7,7 +7,7 @@ import { OpraException } from '../opra-exception.js';
7
7
  export class ResourceNotFoundError extends OpraException {
8
8
  constructor(resource, keyValue, cause) {
9
9
  super({
10
- message: translate(`error:RESOURCE_NOT_FOUND`, { resource: resource + (keyValue ? '@' + keyValue : '') }, `The resource '{{resource}}' not found`),
10
+ message: translate(`error:RESOURCE_NOT_FOUND`, `Resource not found`),
11
11
  severity: 'error',
12
12
  code: 'RESOURCE_NOT_FOUND',
13
13
  details: {
@@ -8,11 +8,11 @@ export function mergePrototype(targetProto, baseProto, filter) {
8
8
  }
9
9
  }
10
10
  // noinspection JSUnusedLocalSymbols
11
- export function inheritPropertyInitializers(target, sourceClass,
11
+ export function inheritPropertyInitializers(target, clazz,
12
12
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
13
  isPropertyInherited = (key) => true) {
14
14
  try {
15
- const tempInstance = new sourceClass();
15
+ const tempInstance = new clazz();
16
16
  const propertyNames = Object.getOwnPropertyNames(tempInstance);
17
17
  propertyNames
18
18
  .filter((propertyName) => typeof tempInstance[propertyName] !== 'undefined' &&
@@ -7,7 +7,6 @@ export * from './data-type/mapped-type.interface.js';
7
7
  export * from './data-type/union-type.interface.js';
8
8
  export * from './resource/collection.interface.js';
9
9
  export * from './resource/container.interface.js';
10
- export * from './resource/operation.interface.js';
11
10
  export * from './resource/resource.interface.js';
12
11
  export * from './resource/singleton.interface.js';
13
12
  export * from './resource/storage.interface.js';
@@ -30,11 +30,12 @@ export function isMappedType(obj) {
30
30
  export function isEnumType(obj) {
31
31
  return obj && typeof obj === 'object' && obj.kind === EnumType.Kind;
32
32
  }
33
- export function isResource(obj) {
33
+ 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,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "0.23.2",
3
+ "version": "0.24.1",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -4,7 +4,7 @@ import { OpraSchema } from '../schema/index.js';
4
4
  import { ComplexType } from './data-type/complex-type.js';
5
5
  import type { DataType } from './data-type/data-type.js';
6
6
  import { SimpleType } from './data-type/simple-type.js';
7
- import type { Collection } from './resource/collection.js';
7
+ import { Collection } from './resource/collection.js';
8
8
  import { Resource } from './resource/resource.js';
9
9
  import { Singleton } from './resource/singleton.js';
10
10
  import { Storage } from './resource/storage.js';
@@ -1,3 +1,5 @@
1
- export declare const METADATA_KEY: unique symbol;
1
+ export declare const DATATYPE_METADATA: unique symbol;
2
+ export declare const RESOURCE_METADATA: unique symbol;
3
+ export declare const DECORATOR: unique symbol;
2
4
  export declare const NAMESPACE_PATTERN: RegExp;
3
5
  export declare const TYPENAME_PATTERN: RegExp;
@@ -49,11 +49,11 @@ export interface MappedType extends MappedTypeClass {
49
49
  export interface MappedTypeConstructor {
50
50
  prototype: MappedType;
51
51
  new (document: ApiDocument, init: MappedType.InitArguments): MappedType;
52
- <T extends any[], I1, S1, PickKey extends keyof I1, OmitKey extends keyof I1>(source: Class<T, I1, S1>, options: {
52
+ <T extends any[], I1, S1, PickKey extends keyof I1, OmitKey extends keyof I1>(resource: Class<T, I1, S1>, options: {
53
53
  pickKeys?: readonly PickKey[];
54
54
  omitKeys?: readonly OmitKey[];
55
55
  }): Class<T, Omit<Pick<I1, PickKey>, OmitKey>>;
56
- _applyMixin(target: Type, source: Type, options: MappedType.Options<any> & {
56
+ _applyMixin(targetType: Type, sourceType: Type, options: MappedType.Options<any> & {
57
57
  isInheritedPredicate: (fieldName: string) => boolean;
58
58
  }): void;
59
59
  }
@@ -7,7 +7,7 @@ import { addReferences } from './add-references.js';
7
7
  import { createBuiltinTypeDocument, createDocument, createDocumentFromUrl } from './create-document.js';
8
8
  import { extractCollectionSchema, extractSingletonSchema, importResourceClass } from './import-resource-class.js';
9
9
  import { extractComplexTypeSchema, extractEnumTypeSchema, extractFieldSchema, extractMappedTypeSchema, extractSimpleTypeSchema, extractUnionTypeSchema, importTypeClass } from './import-type-class.js';
10
- import { createCollectionResource, createFileResource, createSingletonResource, processResourceQueue } from './process-resources.js';
10
+ import { createCollectionResource, createSingletonResource, createStorageResource, processSourceQueue } from './process-resources.js';
11
11
  import { addDataType, createDataTypeInstance, processTypes } from './process-types.js';
12
12
  /**
13
13
  * @namespace DocumentFactory
@@ -26,7 +26,7 @@ export declare class DocumentFactory {
26
26
  static designTypeMap: Map<Function | Type<any>, string>;
27
27
  protected document: ApiDocument;
28
28
  protected typeQueue: ResponsiveMap<OpraSchema.DataType>;
29
- protected resourceQueue: ResponsiveMap<OpraSchema.Resource>;
29
+ protected sourceQueue: ResponsiveMap<OpraSchema.Resource>;
30
30
  protected circularRefs: ResponsiveMap<any>;
31
31
  protected curPath: string[];
32
32
  protected cache: Map<any, any>;
@@ -44,13 +44,13 @@ export declare class DocumentFactory {
44
44
  protected processTypes: typeof processTypes;
45
45
  protected createDataTypeInstance: typeof createDataTypeInstance;
46
46
  protected addDataType: typeof addDataType;
47
- protected importResourceClass: typeof importResourceClass;
47
+ protected importSourceClass: typeof importResourceClass;
48
48
  protected extractSingletonSchema: typeof extractSingletonSchema;
49
49
  protected extractCollectionSchema: typeof extractCollectionSchema;
50
- protected processResourceQueue: typeof processResourceQueue;
51
- protected createCollectionResource: typeof createCollectionResource;
52
- protected createSingletonResource: typeof createSingletonResource;
53
- protected createFileResource: typeof createFileResource;
50
+ protected processSourceQueue: typeof processSourceQueue;
51
+ protected createCollectionSource: typeof createCollectionResource;
52
+ protected createSingletonSource: typeof createSingletonResource;
53
+ protected createFileSource: typeof createStorageResource;
54
54
  /**
55
55
  * Creates ApiDocument instance from given schema object
56
56
  * @param init
@@ -3,7 +3,7 @@ import { Collection } from '../resource/collection.js';
3
3
  import { Singleton } from '../resource/singleton.js';
4
4
  import { Storage } from '../resource/storage.js';
5
5
  import type { DocumentFactory } from './factory.js';
6
- export declare function processResourceQueue(this: DocumentFactory): Promise<void>;
6
+ export declare function processSourceQueue(this: DocumentFactory): Promise<void>;
7
7
  export declare function createCollectionResource(this: DocumentFactory, name: string, schema: OpraSchema.Collection): Promise<Collection>;
8
8
  export declare function createSingletonResource(this: DocumentFactory, name: string, schema: OpraSchema.Singleton): Promise<Singleton>;
9
- export declare function createFileResource(this: DocumentFactory, name: string, schema: OpraSchema.Storage): Promise<Storage>;
9
+ export declare function createStorageResource(this: DocumentFactory, name: string, schema: OpraSchema.Storage): Promise<Storage>;
@@ -13,5 +13,6 @@ export * from './resource/resource.js';
13
13
  export * from './resource/collection.js';
14
14
  export * from './resource/singleton.js';
15
15
  export * from './resource/storage.js';
16
- export * from './interfaces/collection-resource.interface.js';
17
- export * from './interfaces/singleton-resource.interface.js';
16
+ export * from './interfaces/collection.interface.js';
17
+ export * from './interfaces/singleton.interface.js';
18
+ export * from './interfaces/storage.interface.js';
@@ -1,5 +1,5 @@
1
1
  import { PartialOutput } from '../../types.js';
2
- export interface CollectionResource<T> {
2
+ export interface ICollection<T> {
3
3
  create?(...args: any[]): Promise<PartialOutput<T>>;
4
4
  delete?(...args: any[]): Promise<number> | undefined;
5
5
  deleteMany?(...args: any[]): Promise<number> | undefined;
@@ -1,5 +1,5 @@
1
1
  import { PartialOutput } from '../../types.js';
2
- export interface SingletonResource<T> {
2
+ export interface ISingleton<T> {
3
3
  create?(...args: any[]): Promise<PartialOutput<T>>;
4
4
  delete?(...args: any[]): Promise<number> | undefined;
5
5
  get?(...args: any[]): Promise<PartialOutput<T> | undefined>;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { Readable } from 'stream';
4
- export interface StorageResource {
4
+ export interface IStorage {
5
5
  delete?(...args: any[]): Promise<number | undefined>;
6
6
  get?(...args: any[]): Promise<Buffer | Readable | undefined>;
7
7
  post?(...args: any[]): Promise<void>;
@@ -0,0 +1,11 @@
1
+ import { OpraSchema } from '../../schema/index.js';
2
+ export declare namespace Action {
3
+ interface InitArguments extends OpraSchema.Action {
4
+ name: string;
5
+ }
6
+ }
7
+ export declare class Action {
8
+ readonly name: string;
9
+ constructor(init: Action.InitArguments);
10
+ exportSchema(): OpraSchema.Action;
11
+ }
@@ -0,0 +1,24 @@
1
+ import * as vg from 'valgen';
2
+ import { OpraFilter } from '../../filter/index.js';
3
+ import { OpraSchema } from '../../schema/index.js';
4
+ import type { ApiDocument } from '../api-document.js';
5
+ import { ComplexType } from '../data-type/complex-type.js';
6
+ import type { Collection } from './collection.js';
7
+ import { Resource } from './resource.js';
8
+ export declare class CollectionClass extends Resource {
9
+ private _decoders;
10
+ private _encoders;
11
+ readonly type: ComplexType;
12
+ readonly kind = "Collection";
13
+ readonly operations: OpraSchema.Collection.Operations;
14
+ readonly controller?: object;
15
+ readonly primaryKey: string[];
16
+ constructor(document: ApiDocument, init: Collection.InitArguments);
17
+ exportSchema(): OpraSchema.Collection;
18
+ parseKeyValue(value: any): any;
19
+ normalizeFieldPath(path: string | string[]): string[] | undefined;
20
+ normalizeSortFields(this: Collection, fields: string | string[]): string[] | undefined;
21
+ normalizeFilter(filter: string | OpraFilter.Expression): OpraFilter.Expression | undefined;
22
+ getDecoder(endpoint: keyof OpraSchema.Collection.Operations): vg.Validator<any, any>;
23
+ getEncoder(endpoint: keyof OpraSchema.Collection.Operations): vg.Validator<any, any>;
24
+ }
@@ -0,0 +1,30 @@
1
+ import { StrictOmit, Type } from 'ts-gems';
2
+ import type { Collection } from './collection.js';
3
+ import { Resource } from './resource.js';
4
+ import { ResourceDecorator } from './resource-decorator.js';
5
+ type ErrorMessage<T, Error> = [T] extends [never] ? Error : T;
6
+ declare const operationProperties: readonly ["create", "delete", "deleteMany", "get", "findMany", "update", "updateMany"];
7
+ type OperationProperties = typeof operationProperties[number];
8
+ export interface CollectionDecorator extends StrictOmit<ResourceDecorator, 'Action'> {
9
+ <T>(type: Type<T> | string, options?: Collection.DecoratorOptions<T>): ClassDecorator;
10
+ Action: (options?: Resource.ActionOptions) => (<T, K extends keyof T>(target: T, propertyKey: ErrorMessage<Exclude<K, OperationProperties>, `'${string & K}' property is reserved for operation endpoints and can not be used for actions`>) => void);
11
+ Create: (options?: Collection.CreateEndpointOptions) => ((target: Object, propertyKey: 'create') => void);
12
+ Delete: (options?: Collection.DeleteEndpointOptions) => ((target: Object, propertyKey: 'delete') => void);
13
+ DeleteMany: (options?: Collection.DeleteManyEndpointOptions) => ((target: Object, propertyKey: 'deleteMany') => void);
14
+ Get: (options?: Collection.GetEndpointOptions) => ((target: Object, propertyKey: 'get') => void);
15
+ FindMany: (options?: Collection.FindManyEndpointOptions) => ((target: Object, propertyKey: 'findMany') => void);
16
+ Update: (options?: Collection.UpdateEndpointOptions) => ((target: Object, propertyKey: 'update') => void);
17
+ UpdateMany: (options?: Collection.UpdateManyEndpointOptions) => ((target: Object, propertyKey: 'updateMany') => void);
18
+ }
19
+ export declare function CollectionDecorator(type: Type | string, options?: Collection.DecoratorOptions): ClassDecorator;
20
+ export declare namespace CollectionDecorator {
21
+ var Create: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
22
+ var Delete: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
23
+ var DeleteMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
24
+ var Get: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
25
+ var FindMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
26
+ var Update: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
27
+ var UpdateMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
28
+ var Action: (options: any) => PropertyDecorator;
29
+ }
30
+ export {};
@@ -1,10 +1,10 @@
1
- import { StrictOmit, Type } from 'ts-gems';
2
- import * as vg from 'valgen';
3
- import { OpraFilter } from '../../filter/index.js';
1
+ import { StrictOmit } from 'ts-gems';
4
2
  import { OpraSchema } from '../../schema/index.js';
5
3
  import type { TypeThunkAsync } from '../../types.js';
6
4
  import type { ApiDocument } from '../api-document.js';
7
5
  import { ComplexType } from '../data-type/complex-type.js';
6
+ import { CollectionClass } from './collection-class.js';
7
+ import { CollectionDecorator } from './collection-decorator.js';
8
8
  import { Resource } from './resource.js';
9
9
  export declare namespace Collection {
10
10
  interface InitArguments extends Resource.InitArguments, StrictOmit<OpraSchema.Collection, 'kind' | 'type'> {
@@ -13,8 +13,7 @@ export declare namespace Collection {
13
13
  interface DecoratorOptions<T = any> extends Resource.DecoratorOptions {
14
14
  primaryKey?: keyof T | (keyof T)[];
15
15
  }
16
- interface Metadata extends StrictOmit<OpraSchema.Collection, 'type'> {
17
- name: string;
16
+ interface Metadata extends StrictOmit<Resource.Metadata, 'kind'>, StrictOmit<OpraSchema.Collection, 'type'> {
18
17
  type: TypeThunkAsync | string;
19
18
  }
20
19
  namespace Create {
@@ -31,42 +30,17 @@ export declare namespace Collection {
31
30
  }
32
31
  namespace UpdateMany {
33
32
  }
34
- type CreateOperationOptions = OpraSchema.Collection.CreateOperation;
35
- type DeleteOperationOptions = OpraSchema.Collection.DeleteOperation;
36
- type DeleteManyOperationOptions = OpraSchema.Collection.DeleteManyOperation;
37
- type FindManyOperationOptions = OpraSchema.Collection.FindManyOperation;
38
- type GetOperationOptions = OpraSchema.Collection.GetOperation;
39
- type UpdateOperationOptions = OpraSchema.Collection.UpdateOperation;
40
- type UpdateManyOperationOptions = OpraSchema.Collection.UpdateManyOperation;
33
+ type CreateEndpointOptions = OpraSchema.Collection.CreateEndpoint;
34
+ type DeleteEndpointOptions = OpraSchema.Collection.DeleteEndpoint;
35
+ type DeleteManyEndpointOptions = OpraSchema.Collection.DeleteManyEndpoint;
36
+ type FindManyEndpointOptions = OpraSchema.Collection.FindManyEndpoint;
37
+ type GetEndpointOptions = OpraSchema.Collection.GetEndpoint;
38
+ type UpdateEndpointOptions = OpraSchema.Collection.UpdateEndpoint;
39
+ type UpdateManyEndpointOptions = OpraSchema.Collection.UpdateManyEndpoint;
41
40
  }
42
- declare class CollectionClass extends Resource {
43
- private _decoders;
44
- private _encoders;
45
- readonly type: ComplexType;
46
- readonly kind = "Collection";
47
- readonly operations: OpraSchema.Collection.Operations;
48
- readonly controller?: object;
49
- readonly primaryKey: string[];
50
- constructor(document: ApiDocument, init: Collection.InitArguments);
51
- exportSchema(this: Collection): OpraSchema.Collection;
52
- parseKeyValue(this: Collection, value: any): any;
53
- normalizeFieldPath(this: Collection, path: string | string[]): string[] | undefined;
54
- normalizeSortFields(this: Collection, fields: string | string[]): string[] | undefined;
55
- normalizeFilter(filter: string | OpraFilter.Expression): OpraFilter.Expression | undefined;
56
- getDecoder(operation: keyof OpraSchema.Collection.Operations): vg.Validator<any, any>;
57
- getEncoder(operation: keyof OpraSchema.Collection.Operations): vg.Validator<any, any>;
58
- }
59
- export interface CollectionConstructor {
41
+ export interface CollectionConstructor extends CollectionDecorator {
60
42
  prototype: Collection;
61
43
  new (document: ApiDocument, init: Collection.InitArguments): Collection;
62
- <T>(type: Type<T> | string, options?: Collection.DecoratorOptions<T>): ClassDecorator;
63
- Create: (options?: Collection.CreateOperationOptions) => ((target: Object, propertyKey: 'create') => void);
64
- Delete: (options?: Collection.DeleteOperationOptions) => ((target: Object, propertyKey: 'delete') => void);
65
- DeleteMany: (options?: Collection.DeleteManyOperationOptions) => ((target: Object, propertyKey: 'deleteMany') => void);
66
- Get: (options?: Collection.GetOperationOptions) => ((target: Object, propertyKey: 'get') => void);
67
- FindMany: (options?: Collection.FindManyOperationOptions) => ((target: Object, propertyKey: 'findMany') => void);
68
- Update: (options?: Collection.UpdateOperationOptions) => ((target: Object, propertyKey: 'update') => void);
69
- UpdateMany: (options?: Collection.UpdateManyOperationOptions) => ((target: Object, propertyKey: 'updateMany') => void);
70
44
  }
71
45
  export interface Collection extends CollectionClass {
72
46
  }
@@ -74,4 +48,3 @@ export interface Collection extends CollectionClass {
74
48
  *
75
49
  */
76
50
  export declare const Collection: CollectionConstructor;
77
- export {};
@@ -0,0 +1,5 @@
1
+ import type { Resource } from './resource.js';
2
+ export interface ResourceDecorator {
3
+ Action: (options?: Resource.ActionOptions) => PropertyDecorator;
4
+ }
5
+ export declare const ResourceDecorator: ResourceDecorator;
@@ -2,6 +2,7 @@ import { StrictOmit, Type } from 'ts-gems';
2
2
  import { OpraSchema } from '../../schema/index.js';
3
3
  import type { ApiDocument } from '../api-document.js';
4
4
  import { nodeInspectCustom } from '../utils/inspect.util.js';
5
+ import { Action } from './action.js';
5
6
  export declare namespace Resource {
6
7
  interface InitArguments extends StrictOmit<OpraSchema.ResourceBase, 'kind'> {
7
8
  name: string;
@@ -9,6 +10,11 @@ export declare namespace Resource {
9
10
  }
10
11
  interface DecoratorOptions extends Partial<Pick<InitArguments, 'name' | 'description'>> {
11
12
  }
13
+ interface Metadata extends OpraSchema.ResourceBase {
14
+ name: string;
15
+ }
16
+ interface ActionOptions {
17
+ }
12
18
  }
13
19
  export declare abstract class Resource {
14
20
  readonly document: ApiDocument;
@@ -17,6 +23,7 @@ export declare abstract class Resource {
17
23
  readonly description?: string;
18
24
  readonly controller?: object | Type;
19
25
  abstract readonly operations: Record<string, any>;
26
+ readonly actions: Record<string, Action>;
20
27
  protected constructor(document: ApiDocument, init: Resource.InitArguments);
21
28
  exportSchema(): OpraSchema.ResourceBase;
22
29
  toString(): string;