@opra/common 0.25.5 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/browser.js +1826 -1338
  2. package/cjs/document/api-document.js +16 -220
  3. package/cjs/document/data-type/complex-type-class.js +41 -19
  4. package/cjs/document/data-type/complex-type.js +1 -1
  5. package/cjs/document/data-type/data-type.js +2 -1
  6. package/cjs/document/data-type/enum-type-class.js +15 -12
  7. package/cjs/document/data-type/enum-type.js +28 -14
  8. package/cjs/document/data-type/field-class.js +21 -3
  9. package/cjs/document/data-type/field-decorator.js +0 -19
  10. package/cjs/document/data-type/mapped-type-class.js +8 -10
  11. package/cjs/document/data-type/mapped-type.js +1 -1
  12. package/cjs/document/data-type/simple-type-class.js +17 -8
  13. package/cjs/document/data-type/simple-type.js +1 -1
  14. package/cjs/document/data-type/union-type-class.js +15 -13
  15. package/cjs/document/document-base.js +24 -0
  16. package/cjs/document/factory/api-document-factory.js +231 -0
  17. package/cjs/document/factory/type-document-factory.js +324 -0
  18. package/cjs/document/index.js +7 -1
  19. package/cjs/document/resource/action-decorator.js +27 -0
  20. package/cjs/document/resource/collection-class.js +95 -44
  21. package/cjs/document/resource/collection-decorator.js +182 -0
  22. package/cjs/document/resource/collection.js +4 -4
  23. package/cjs/document/resource/container-class.js +82 -0
  24. package/cjs/document/resource/container-decorator.js +24 -0
  25. package/cjs/document/resource/container.js +29 -0
  26. package/cjs/document/resource/crud-resource.js +31 -0
  27. package/cjs/document/resource/endpoint.js +24 -22
  28. package/cjs/document/resource/operation-decorator.js +27 -0
  29. package/cjs/document/resource/parameter.js +51 -0
  30. package/cjs/document/{decorators/resource.decorator.js → resource/resource-decorator.js} +9 -14
  31. package/cjs/document/resource/resource.js +20 -5
  32. package/cjs/document/resource/singleton-class.js +60 -40
  33. package/cjs/document/resource/singleton-decorator.js +57 -0
  34. package/cjs/document/resource/singleton.js +4 -4
  35. package/cjs/document/resource/storage-class.js +11 -17
  36. package/cjs/document/resource/storage-decorator.js +66 -0
  37. package/cjs/document/resource/storage.js +3 -3
  38. package/cjs/document/type-document.js +195 -0
  39. package/cjs/exception/http-errors/internal-server.error.js +1 -0
  40. package/cjs/exception/wrap-exception.js +2 -1
  41. package/cjs/helpers/responsive-map.js +3 -3
  42. package/cjs/http/opra-url-path.js +2 -1
  43. package/cjs/schema/document/document-base.interface.js +2 -0
  44. package/cjs/schema/document/type-document.interface.js +2 -0
  45. package/cjs/schema/opra-schema.ns.js +3 -1
  46. package/cjs/schema/type-guards.js +3 -3
  47. package/esm/document/api-document.js +16 -220
  48. package/esm/document/data-type/complex-type-class.js +40 -19
  49. package/esm/document/data-type/complex-type.js +1 -1
  50. package/esm/document/data-type/data-type.js +2 -1
  51. package/esm/document/data-type/enum-type-class.js +15 -12
  52. package/esm/document/data-type/enum-type.js +26 -12
  53. package/esm/document/data-type/field-class.js +20 -3
  54. package/esm/document/data-type/field-decorator.js +0 -19
  55. package/esm/document/data-type/mapped-type-class.js +9 -11
  56. package/esm/document/data-type/mapped-type.js +1 -1
  57. package/esm/document/data-type/simple-type-class.js +15 -6
  58. package/esm/document/data-type/simple-type.js +1 -1
  59. package/esm/document/data-type/union-type-class.js +11 -9
  60. package/esm/document/document-base.js +20 -0
  61. package/esm/document/factory/api-document-factory.js +227 -0
  62. package/esm/document/factory/type-document-factory.js +320 -0
  63. package/esm/document/index.js +7 -1
  64. package/esm/document/resource/action-decorator.js +23 -0
  65. package/esm/document/resource/collection-class.js +95 -44
  66. package/esm/document/resource/collection-decorator.js +178 -0
  67. package/esm/document/resource/collection.js +4 -4
  68. package/esm/document/resource/container-class.js +78 -0
  69. package/esm/document/resource/container-decorator.js +20 -0
  70. package/esm/document/resource/container.js +25 -0
  71. package/esm/document/resource/crud-resource.js +27 -0
  72. package/esm/document/resource/endpoint.js +22 -20
  73. package/esm/document/resource/operation-decorator.js +23 -0
  74. package/esm/document/resource/parameter.js +46 -0
  75. package/esm/document/resource/resource-decorator.js +28 -0
  76. package/esm/document/resource/resource.js +20 -5
  77. package/esm/document/resource/singleton-class.js +58 -38
  78. package/esm/document/resource/singleton-decorator.js +53 -0
  79. package/esm/document/resource/singleton.js +4 -4
  80. package/esm/document/resource/storage-class.js +9 -15
  81. package/esm/document/resource/storage-decorator.js +62 -0
  82. package/esm/document/resource/storage.js +3 -3
  83. package/esm/document/type-document.js +191 -0
  84. package/esm/exception/http-errors/internal-server.error.js +1 -0
  85. package/esm/exception/wrap-exception.js +2 -1
  86. package/esm/helpers/responsive-map.js +3 -3
  87. package/esm/http/opra-url-path.js +1 -1
  88. package/esm/schema/document/document-base.interface.js +1 -0
  89. package/esm/schema/document/type-document.interface.js +1 -0
  90. package/esm/schema/opra-schema.ns.js +3 -1
  91. package/esm/schema/type-guards.js +1 -1
  92. package/package.json +2 -2
  93. package/types/document/api-document.d.ts +32 -73
  94. package/types/document/data-type/complex-type-class.d.ts +5 -5
  95. package/types/document/data-type/complex-type.d.ts +8 -3
  96. package/types/document/{decorators → data-type}/complex-type.decorator.d.ts +1 -1
  97. package/types/document/data-type/data-type.d.ts +11 -1
  98. package/types/document/data-type/enum-type-class.d.ts +6 -6
  99. package/types/document/data-type/enum-type.d.ts +12 -6
  100. package/types/document/data-type/field-class.d.ts +9 -1
  101. package/types/document/data-type/field-decorator.d.ts +1 -1
  102. package/types/document/data-type/field.d.ts +4 -3
  103. package/types/document/data-type/mapped-type-class.d.ts +5 -9
  104. package/types/document/data-type/mapped-type.d.ts +6 -7
  105. package/types/document/data-type/simple-type-class.d.ts +6 -3
  106. package/types/document/{decorators → data-type}/simple-type.decorator.d.ts +1 -1
  107. package/types/document/data-type/union-type-class.d.ts +2 -8
  108. package/types/document/data-type/union-type.d.ts +7 -6
  109. package/types/document/document-base.d.ts +10 -0
  110. package/types/document/factory/api-document-factory.d.ts +50 -0
  111. package/types/document/factory/type-document-factory.d.ts +56 -0
  112. package/types/document/index.d.ts +7 -1
  113. package/types/document/resource/action-decorator.d.ts +6 -0
  114. package/types/document/resource/collection-class.d.ts +17 -12
  115. package/types/document/resource/collection-decorator.d.ts +192 -0
  116. package/types/document/resource/collection.d.ts +11 -19
  117. package/types/document/resource/container-class.d.ts +66 -0
  118. package/types/document/resource/container-decorator.d.ts +37 -0
  119. package/types/document/resource/container.d.ts +25 -0
  120. package/types/document/resource/crud-resource.d.ts +16 -0
  121. package/types/document/resource/endpoint.d.ts +21 -21
  122. package/types/document/resource/operation-decorator.d.ts +6 -0
  123. package/types/document/resource/parameter.d.ts +35 -0
  124. package/types/document/resource/resource-decorator.d.ts +32 -0
  125. package/types/document/resource/resource.d.ts +15 -13
  126. package/types/document/resource/singleton-class.d.ts +15 -14
  127. package/types/document/resource/singleton-decorator.d.ts +125 -0
  128. package/types/document/resource/singleton.d.ts +11 -16
  129. package/types/document/resource/storage-class.d.ts +13 -6
  130. package/types/document/resource/storage-decorator.d.ts +98 -0
  131. package/types/document/resource/storage.d.ts +9 -12
  132. package/types/document/type-document.d.ts +68 -0
  133. package/types/http/opra-url-path.d.ts +1 -1
  134. package/types/schema/data-type/complex-type.interface.d.ts +1 -1
  135. package/types/schema/data-type/data-type.interface.d.ts +3 -3
  136. package/types/schema/data-type/enum-type.interface.d.ts +6 -7
  137. package/types/schema/data-type/mapped-type.interface.d.ts +4 -3
  138. package/types/schema/data-type/simple-type.interface.d.ts +3 -3
  139. package/types/schema/data-type/union-type.interface.d.ts +6 -4
  140. package/types/schema/document/api-document.interface.d.ts +11 -0
  141. package/types/schema/document/document-base.interface.d.ts +24 -0
  142. package/types/schema/document/type-document.interface.d.ts +6 -0
  143. package/types/schema/opra-schema.ns.d.ts +3 -1
  144. package/types/schema/resource/collection.interface.d.ts +31 -31
  145. package/types/schema/resource/container.interface.d.ts +4 -3
  146. package/types/schema/resource/resource.interface.d.ts +1 -1
  147. package/types/schema/resource/singleton.interface.d.ts +15 -10
  148. package/types/schema/resource/storage.interface.d.ts +49 -45
  149. package/types/schema/type-guards.d.ts +3 -2
  150. package/cjs/document/decorators/build-operation-decorator.js +0 -27
  151. package/cjs/document/decorators/collection-decorator.js +0 -30
  152. package/cjs/document/decorators/singleton.decorator.js +0 -25
  153. package/cjs/document/decorators/storage.decorator.js +0 -27
  154. package/cjs/document/factory/add-references.js +0 -20
  155. package/cjs/document/factory/create-document.js +0 -83
  156. package/cjs/document/factory/factory.js +0 -66
  157. package/cjs/document/factory/import-resource-class.js +0 -54
  158. package/cjs/document/factory/import-type-class.js +0 -146
  159. package/cjs/document/factory/index.js +0 -4
  160. package/cjs/document/factory/process-resources.js +0 -70
  161. package/cjs/document/factory/process-types.js +0 -191
  162. package/cjs/document/utils/generate-codec.js +0 -39
  163. package/esm/document/decorators/build-operation-decorator.js +0 -23
  164. package/esm/document/decorators/collection-decorator.js +0 -26
  165. package/esm/document/decorators/resource.decorator.js +0 -33
  166. package/esm/document/decorators/singleton.decorator.js +0 -21
  167. package/esm/document/decorators/storage.decorator.js +0 -23
  168. package/esm/document/factory/add-references.js +0 -16
  169. package/esm/document/factory/create-document.js +0 -77
  170. package/esm/document/factory/factory.js +0 -62
  171. package/esm/document/factory/import-resource-class.js +0 -48
  172. package/esm/document/factory/import-type-class.js +0 -136
  173. package/esm/document/factory/index.js +0 -1
  174. package/esm/document/factory/process-resources.js +0 -63
  175. package/esm/document/factory/process-types.js +0 -185
  176. package/esm/document/utils/generate-codec.js +0 -33
  177. package/types/document/decorators/build-operation-decorator.d.ts +0 -13
  178. package/types/document/decorators/collection-decorator.d.ts +0 -33
  179. package/types/document/decorators/resource.decorator.d.ts +0 -9
  180. package/types/document/decorators/singleton.decorator.d.ts +0 -25
  181. package/types/document/decorators/storage.decorator.d.ts +0 -25
  182. package/types/document/factory/add-references.d.ts +0 -4
  183. package/types/document/factory/create-document.d.ts +0 -12
  184. package/types/document/factory/factory.d.ts +0 -63
  185. package/types/document/factory/import-resource-class.d.ts +0 -10
  186. package/types/document/factory/import-type-class.d.ts +0 -17
  187. package/types/document/factory/index.d.ts +0 -1
  188. package/types/document/factory/process-resources.d.ts +0 -9
  189. package/types/document/factory/process-types.d.ts +0 -6
  190. package/types/document/utils/generate-codec.d.ts +0 -10
  191. package/types/schema/document.interface.d.ts +0 -34
  192. /package/cjs/document/{decorators → data-type}/complex-type.decorator.js +0 -0
  193. /package/cjs/document/{decorators → data-type}/simple-type.decorator.js +0 -0
  194. /package/cjs/schema/{document.interface.js → document/api-document.interface.js} +0 -0
  195. /package/esm/document/{decorators → data-type}/complex-type.decorator.js +0 -0
  196. /package/esm/document/{decorators → data-type}/simple-type.decorator.js +0 -0
  197. /package/esm/schema/{document.interface.js → document/api-document.interface.js} +0 -0
@@ -0,0 +1,53 @@
1
+ import { OpraSchema } from '../../schema/index.js';
2
+ import { createActionDecorator } from './action-decorator.js';
3
+ import { CollectionDecorator } from './collection-decorator.js';
4
+ import { ResourceDecorator } from './resource-decorator.js';
5
+ const operationProperties = ['create', 'delete', 'get', 'update'];
6
+ export function SingletonDecorator(type, options) {
7
+ return ResourceDecorator(OpraSchema.Singleton.Kind, { ...options, type });
8
+ }
9
+ Object.assign(SingletonDecorator, ResourceDecorator);
10
+ /*
11
+ * Action PropertyDecorator
12
+ */
13
+ (function (SingletonDecorator) {
14
+ /**
15
+ * Action PropertyDecorator
16
+ */
17
+ function Action(options) {
18
+ const list = [];
19
+ return createActionDecorator(options, operationProperties, list);
20
+ }
21
+ SingletonDecorator.Action = Action;
22
+ function Create(options) {
23
+ return CollectionDecorator.Create(options);
24
+ }
25
+ SingletonDecorator.Create = Create;
26
+ })(SingletonDecorator || (SingletonDecorator = {}));
27
+ /*
28
+ * Get PropertyDecorator
29
+ */
30
+ (function (SingletonDecorator) {
31
+ function Get(options) {
32
+ return CollectionDecorator.Get(options);
33
+ }
34
+ SingletonDecorator.Get = Get;
35
+ })(SingletonDecorator || (SingletonDecorator = {}));
36
+ /*
37
+ * Delete PropertyDecorator
38
+ */
39
+ (function (SingletonDecorator) {
40
+ function Delete(options) {
41
+ return CollectionDecorator.Delete(options);
42
+ }
43
+ SingletonDecorator.Delete = Delete;
44
+ })(SingletonDecorator || (SingletonDecorator = {}));
45
+ /*
46
+ * Update PropertyDecorator
47
+ */
48
+ (function (SingletonDecorator) {
49
+ function Update(options) {
50
+ return CollectionDecorator.Update(options);
51
+ }
52
+ SingletonDecorator.Update = Update;
53
+ })(SingletonDecorator || (SingletonDecorator = {}));
@@ -1,20 +1,20 @@
1
1
  import merge from 'putil-merge';
2
2
  import { DECORATOR } from '../constants.js';
3
- import { SingletonDecorator } from '../decorators/singleton.decorator.js';
4
3
  import { SingletonClass } from './singleton-class.js';
4
+ import { SingletonDecorator } from './singleton-decorator.js';
5
5
  /**
6
6
  * @class Singleton
7
7
  * @decorator Singleton
8
8
  */
9
9
  export const Singleton = function (...args) {
10
- // ClassDecorator
10
+ // Decorator
11
11
  if (!this) {
12
12
  const [type, options] = args;
13
13
  return Singleton[DECORATOR].call(undefined, type, options);
14
14
  }
15
15
  // Constructor
16
- const [document, init] = args;
17
- merge(this, new SingletonClass(document, init), { descriptor: true });
16
+ const [parent, init] = args;
17
+ merge(this, new SingletonClass(parent, init), { descriptor: true });
18
18
  };
19
19
  Singleton.prototype = SingletonClass.prototype;
20
20
  Object.assign(Singleton, SingletonDecorator);
@@ -1,20 +1,14 @@
1
- import { omitUndefined } from '../../helpers/index.js';
2
1
  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);
2
+ import { CrudResource } from './crud-resource.js';
3
+ export class StorageClass extends CrudResource {
4
+ constructor(parent, init) {
5
+ super(parent, init);
7
6
  this.kind = OpraSchema.Storage.Kind;
8
- this.controller = init.controller;
9
- this.operations = { ...init.operations };
10
7
  }
11
- exportSchema() {
12
- return {
13
- ...super.exportSchema(),
14
- ...omitUndefined({
15
- kind: OpraSchema.Storage.Kind,
16
- operations: this.operations
17
- })
18
- };
8
+ getOperation(name) {
9
+ return super.getOperation(name);
10
+ }
11
+ exportSchema(options) {
12
+ return super.exportSchema(options);
19
13
  }
20
14
  }
@@ -0,0 +1,62 @@
1
+ import { OpraSchema } from '../../schema/index.js';
2
+ import { createActionDecorator } from './action-decorator.js';
3
+ import { createOperationDecorator } from './operation-decorator.js';
4
+ import { ResourceDecorator } from './resource-decorator.js';
5
+ const operationProperties = ['delete', 'get', 'post'];
6
+ export function StorageDecorator(options) {
7
+ return ResourceDecorator(OpraSchema.Storage.Kind, options);
8
+ }
9
+ Object.assign(StorageDecorator, ResourceDecorator);
10
+ /*
11
+ * Action PropertyDecorator
12
+ */
13
+ (function (StorageDecorator) {
14
+ /**
15
+ * Action PropertyDecorator
16
+ */
17
+ function Action(options) {
18
+ const list = [];
19
+ return createActionDecorator(options, operationProperties, list);
20
+ }
21
+ StorageDecorator.Action = Action;
22
+ function Delete(options) {
23
+ const list = [];
24
+ return createOperationDecorator('delete', options, list);
25
+ }
26
+ StorageDecorator.Delete = Delete;
27
+ function Get(options) {
28
+ const list = [];
29
+ return createOperationDecorator('get', options, list);
30
+ }
31
+ StorageDecorator.Get = Get;
32
+ function Post(options) {
33
+ const list = [];
34
+ const decorator = createOperationDecorator('post', options, list);
35
+ decorator.MaxFields = (amount) => {
36
+ list.push(operationMeta => operationMeta.maxFields = amount);
37
+ return decorator;
38
+ };
39
+ decorator.MaxFieldSize = (amount) => {
40
+ list.push(operationMeta => operationMeta.maxFieldsSize = amount);
41
+ return decorator;
42
+ };
43
+ decorator.MaxFiles = (amount) => {
44
+ list.push(operationMeta => operationMeta.maxFiles = amount);
45
+ return decorator;
46
+ };
47
+ decorator.MaxFileSize = (sizeInBytes) => {
48
+ list.push(operationMeta => operationMeta.maxFileSize = sizeInBytes);
49
+ return decorator;
50
+ };
51
+ decorator.MaxTotalFileSize = (sizeInBytes) => {
52
+ list.push(operationMeta => operationMeta.maxTotalFileSize = sizeInBytes);
53
+ return decorator;
54
+ };
55
+ decorator.MinFileSize = (sizeInBytes) => {
56
+ list.push(operationMeta => operationMeta.minFileSize = sizeInBytes);
57
+ return decorator;
58
+ };
59
+ return decorator;
60
+ }
61
+ StorageDecorator.Post = Post;
62
+ })(StorageDecorator || (StorageDecorator = {}));
@@ -1,7 +1,7 @@
1
1
  import merge from 'putil-merge';
2
2
  import { DECORATOR } from '../constants.js';
3
- import { StorageDecorator } from '../decorators/storage.decorator.js';
4
3
  import { StorageClass } from './storage-class.js';
4
+ import { StorageDecorator } from './storage-decorator.js';
5
5
  /**
6
6
  * @class Storage
7
7
  * @decorator Storage
@@ -13,8 +13,8 @@ export const Storage = function (...args) {
13
13
  return Storage[DECORATOR].call(undefined, options);
14
14
  }
15
15
  // Constructor
16
- const [document, init] = args;
17
- merge(this, new StorageClass(document, init), { descriptor: true });
16
+ const [parent, init] = args;
17
+ merge(this, new StorageClass(parent, init), { descriptor: true });
18
18
  };
19
19
  Storage.prototype = StorageClass.prototype;
20
20
  Object.assign(Storage, StorageDecorator);
@@ -0,0 +1,191 @@
1
+ import { NotAcceptableError, NotFoundError } from '../exception/index.js';
2
+ import { ResponsiveMap } from '../helpers/index.js';
3
+ import { OpraSchema } from '../schema/index.js';
4
+ import { DATATYPE_METADATA, NAMESPACE_PATTERN } from './constants.js';
5
+ import { ComplexType } from './data-type/complex-type.js';
6
+ import { DocumentBase } from './document-base.js';
7
+ export class TypeDocument extends DocumentBase {
8
+ constructor() {
9
+ super();
10
+ this._designCtorMap = new Map();
11
+ this._typeCache = new ResponsiveMap();
12
+ this._typesCacheByCtor = new Map();
13
+ this.references = new ResponsiveMap();
14
+ this.types = new ResponsiveMap();
15
+ const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
16
+ const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
17
+ this._designCtorMap.set(String, 'string');
18
+ this._designCtorMap.set(Number, 'number');
19
+ this._designCtorMap.set(Boolean, 'boolean');
20
+ this._designCtorMap.set(Object, 'any');
21
+ this._designCtorMap.set(Date, 'timestamp');
22
+ this._designCtorMap.set(BigIntConstructor, 'bigint');
23
+ this._designCtorMap.set(ArrayBuffer, 'base64');
24
+ this._designCtorMap.set(SharedArrayBuffer, 'base64');
25
+ this._designCtorMap.set(BufferConstructor, 'base64');
26
+ }
27
+ /**
28
+ *
29
+ */
30
+ getDataType(arg0, silent) {
31
+ // Convert design ctor to type name (String -> 'string')
32
+ if (typeof arg0 === 'function') {
33
+ const x = this._designCtorMap.get(arg0);
34
+ if (x)
35
+ arg0 = x;
36
+ }
37
+ let dataType;
38
+ let name;
39
+ // Determine name
40
+ if (typeof arg0 === 'function') {
41
+ const metadata = Reflect.getMetadata(DATATYPE_METADATA, arg0);
42
+ name = metadata?.name || arg0.name;
43
+ }
44
+ else if (typeof arg0 === 'function') {
45
+ const metadata = Reflect.getMetadata(DATATYPE_METADATA, arg0);
46
+ name = metadata?.name;
47
+ }
48
+ else
49
+ name = String(arg0);
50
+ // Try to get instance from cache
51
+ const t = typeof arg0 === 'string'
52
+ ? this._typeCache.get(arg0)
53
+ : this._typesCacheByCtor.get(arg0);
54
+ if (t)
55
+ return t;
56
+ // If cached as null, it means "not found"
57
+ if (t === null) {
58
+ if (silent)
59
+ return;
60
+ throw new Error(`Data type "${name}" does not exists`);
61
+ }
62
+ if (typeof arg0 === 'string' && arg0) {
63
+ const m = NAMESPACE_PATTERN.exec(arg0);
64
+ if (!m)
65
+ throw new TypeError(`Invalid data type name "${name}"`);
66
+ // If given string has namespace pattern (ns:type_name)
67
+ if (m[2]) {
68
+ const ref = this.references.get(m[1]);
69
+ if (!ref) {
70
+ if (silent)
71
+ return;
72
+ throw new Error(`No referenced document found for given namespace "${m[1]}"`);
73
+ }
74
+ dataType = ref.getDataType(m[2], silent);
75
+ }
76
+ else {
77
+ // Get instance from own types
78
+ dataType = this.types.get(arg0);
79
+ // if not found, search in references (from last to first)
80
+ if (!dataType) {
81
+ const references = Array.from(this.references.values()).reverse();
82
+ for (const ref of references) {
83
+ dataType = ref.getDataType(name, true);
84
+ if (dataType)
85
+ break;
86
+ }
87
+ }
88
+ }
89
+ if (dataType) {
90
+ this._typeCache.set(arg0, dataType);
91
+ return dataType;
92
+ }
93
+ if (!silent)
94
+ throw new NotFoundError(`Data type "${arg0}" does not exists`);
95
+ return;
96
+ }
97
+ if (typeof arg0 === 'function') {
98
+ const types = Array.from(this.types.values()).reverse();
99
+ for (const dt of types) {
100
+ if (dt instanceof ComplexType && dt.isTypeOf(arg0)) {
101
+ dataType = dt;
102
+ break;
103
+ }
104
+ }
105
+ // if not found, search in references (from last to first)
106
+ if (!dataType) {
107
+ const references = Array.from(this.references.values()).reverse();
108
+ for (const ref of references) {
109
+ dataType = ref.getDataType(arg0, true);
110
+ if (dataType)
111
+ break;
112
+ }
113
+ }
114
+ if (dataType)
115
+ this._typesCacheByCtor.set(arg0, dataType);
116
+ if (dataType)
117
+ return dataType;
118
+ if (!silent)
119
+ throw new Error(`No data type mapping found for class "${name}"`);
120
+ return;
121
+ }
122
+ if (typeof arg0 === 'object') {
123
+ const metadata = arg0[DATATYPE_METADATA];
124
+ if (metadata && metadata.name) {
125
+ dataType = this.getDataType(metadata.name, true);
126
+ if (dataType)
127
+ return dataType;
128
+ if (!silent)
129
+ throw new Error(`No data type mapping found for class "${name}"`);
130
+ }
131
+ }
132
+ /* istanbul ignore next */
133
+ if (!silent)
134
+ throw new TypeError('Invalid argument');
135
+ }
136
+ getComplexType(nameOrCtor, silent) {
137
+ if (nameOrCtor === Object)
138
+ nameOrCtor = 'object';
139
+ const t = this.getDataType(nameOrCtor);
140
+ if (!t && silent)
141
+ return;
142
+ if (t && t.kind === OpraSchema.ComplexType.Kind)
143
+ return t;
144
+ throw new NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
145
+ }
146
+ getSimpleType(nameOrCtor, silent) {
147
+ const t = this.getDataType(nameOrCtor);
148
+ if (!t && silent)
149
+ return;
150
+ if (t && t.kind === OpraSchema.SimpleType.Kind)
151
+ return t;
152
+ throw new NotAcceptableError(`Data type "${t.name || t}" is not a SimpleType`);
153
+ }
154
+ getEnumType(nameOrCtor, silent) {
155
+ const t = this.getDataType(nameOrCtor);
156
+ if (!t && silent)
157
+ return;
158
+ if (t && t.kind === OpraSchema.EnumType.Kind)
159
+ return t;
160
+ throw new NotAcceptableError(`Data type "${t.name || t}" is not a EnumType`);
161
+ }
162
+ /**
163
+ * Export as Opra schema definition object
164
+ */
165
+ exportSchema(options) {
166
+ const schema = super.exportSchema();
167
+ if (this.references.size) {
168
+ const references = {};
169
+ let i = 0;
170
+ for (const [ns, r] of this.references.entries()) {
171
+ if (ns.toLowerCase() === 'opra')
172
+ continue;
173
+ references[ns] = r.url ? r.url : r.exportSchema(options);
174
+ i++;
175
+ }
176
+ if (i)
177
+ schema.references = references;
178
+ }
179
+ if (this.types.size) {
180
+ const types = schema.types = {};
181
+ for (const [name, r] of this.types.entries()) {
182
+ types[name] = r.exportSchema(options);
183
+ }
184
+ }
185
+ return schema;
186
+ }
187
+ invalidate() {
188
+ this._typeCache.clear();
189
+ this._typesCacheByCtor.clear();
190
+ }
191
+ }
@@ -13,6 +13,7 @@ export class InternalServerError extends OpraException {
13
13
  super.init({
14
14
  message: translate('error:INTERNAL_SERVER_ERROR', 'Internal server error'),
15
15
  code: 'INTERNAL_SERVER_ERROR',
16
+ severity: 'fatal',
16
17
  ...issue
17
18
  });
18
19
  }
@@ -1,6 +1,7 @@
1
1
  import { BadRequestError } from './http-errors/bad-request.error.js';
2
2
  import { FailedDependencyError } from './http-errors/failed-dependency.error.js';
3
3
  import { ForbiddenError } from './http-errors/forbidden.error.js';
4
+ import { InternalServerError } from './http-errors/internal-server.error.js';
4
5
  import { MethodNotAllowedError } from './http-errors/method-not-allowed.error.js';
5
6
  import { NotAcceptableError } from './http-errors/not-acceptable.error.js';
6
7
  import { NotFoundError } from './http-errors/not-found.error.js';
@@ -33,6 +34,6 @@ export function wrapException(error) {
33
34
  case 424:
34
35
  return new FailedDependencyError(error);
35
36
  default:
36
- return new FailedDependencyError(error);
37
+ return new InternalServerError(error);
37
38
  }
38
39
  }
@@ -49,11 +49,11 @@ export class ResponsiveMap extends Map {
49
49
  return this;
50
50
  }
51
51
  keys() {
52
- return this[kKeyOrder][Symbol.iterator]();
52
+ return [...this[kKeyOrder]][Symbol.iterator]();
53
53
  }
54
54
  values() {
55
55
  let i = -1;
56
- const arr = this[kKeyOrder];
56
+ const arr = [...this[kKeyOrder]];
57
57
  const map = this;
58
58
  return {
59
59
  [Symbol.iterator]() {
@@ -70,7 +70,7 @@ export class ResponsiveMap extends Map {
70
70
  }
71
71
  entries() {
72
72
  let i = -1;
73
- const arr = this[kKeyOrder];
73
+ const arr = [...this[kKeyOrder]];
74
74
  const map = this;
75
75
  return {
76
76
  [Symbol.iterator]() {
@@ -143,7 +143,7 @@ export class OpraURLPath {
143
143
  *
144
144
  * @class OpraURLPathComponent
145
145
  */
146
- class OpraURLPathComponent {
146
+ export class OpraURLPathComponent {
147
147
  constructor(init) {
148
148
  this.resource = init.resource;
149
149
  this.key = init.key;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -12,5 +12,7 @@ export * from './resource/resource.interface.js';
12
12
  export * from './resource/singleton.interface.js';
13
13
  export * from './resource/storage.interface.js';
14
14
  export * from './constants.js';
15
- export * from './document.interface.js';
15
+ export * from './document/document-base.interface.js';
16
+ export * from './document/type-document.interface.js';
17
+ export * from './document/api-document.interface.js';
16
18
  export * from './type-guards.js';
@@ -30,7 +30,7 @@ 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 isSource(obj) {
33
+ export function isResource(obj) {
34
34
  return obj && typeof obj === 'object' &&
35
35
  (obj.kind === Container.Kind ||
36
36
  obj.kind === Collection.Kind ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "0.25.5",
3
+ "version": "0.26.0",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -54,7 +54,7 @@
54
54
  "@types/encodeurl": "^1.0.0",
55
55
  "@types/validator": "^13.11.1",
56
56
  "path-browserify": "^1.0.1",
57
- "ts-gems": "^2.4.1"
57
+ "ts-gems": "^2.5.0"
58
58
  },
59
59
  "engines": {
60
60
  "node": ">=16.0",
@@ -1,99 +1,58 @@
1
- import { Type } from 'ts-gems';
2
- import { ResponsiveMap } from '../helpers/index.js';
3
1
  import { OpraSchema } from '../schema/index.js';
4
- import { ComplexType } from './data-type/complex-type.js';
5
- import type { DataType } from './data-type/data-type.js';
6
- import { SimpleType } from './data-type/simple-type.js';
7
- import { Collection } from './resource/collection.js';
8
- import { Resource } from './resource/resource.js';
9
- import { Singleton } from './resource/singleton.js';
10
- import { Storage } from './resource/storage.js';
11
- export declare class ApiDocument {
12
- protected _designCtorMap: Map<Function | Type<any>, string>;
13
- protected _typeCache: ResponsiveMap<DataType>;
14
- protected _typesCacheByCtor: Map<object | Type<any>, DataType>;
15
- protected _metadataCache?: OpraSchema.ApiDocument;
16
- url?: string;
17
- info: OpraSchema.DocumentInfo;
18
- references: ResponsiveMap<ApiDocument>;
19
- types: ResponsiveMap<DataType>;
20
- resources: ResponsiveMap<Resource>;
21
- constructor();
22
- /**
23
- * Returns DataType instance by name or Constructor. Returns undefined if not found
24
- * @param nameOrCtor
25
- * @param silent
26
- */
27
- getDataType(nameOrCtor: string | Type | object, silent: true): DataType | undefined;
28
- /**
29
- * Returns DataType instance by name or Constructor. Throws error if not found
30
- * @param nameOrCtor
31
- */
32
- getDataType(nameOrCtor: string | Type | object): DataType;
33
- /**
34
- * Returns ComplexType instance by name or Constructor.
35
- * Returns undefined if not found
36
- * Throws error if data type is not a ComplexType
37
- * @param nameOrCtor
38
- * @param silent
39
- */
40
- getComplexType(nameOrCtor: string | Type, silent: true): ComplexType | undefined;
41
- /**
42
- * Returns ComplexType instance by name or Constructor.
43
- * Throws error undefined if not found or data type is not a ComplexType
44
- * @param nameOrCtor
45
- */
46
- getComplexType(nameOrCtor: string | Type): ComplexType;
47
- /**
48
- * Returns SimpleType instance by name or Constructor.
49
- * Returns undefined if not found
50
- * Throws error if data type is not a SimpleType
51
- * @param nameOrCtor
52
- * @param silent
53
- */
54
- getSimpleType(nameOrCtor: string | Type, silent: true): SimpleType | undefined;
55
- /**
56
- Returns SimpleType instance by name or Constructor.
57
- Throws error undefined if not found or data type is not a SimpleType
58
- * @param nameOrCtor
59
- */
60
- getSimpleType(nameOrCtor: string | Type): SimpleType;
2
+ import type { Collection } from './resource/collection.js';
3
+ import { Container } from './resource/container.js';
4
+ import type { Resource } from './resource/resource.js';
5
+ import type { Singleton } from './resource/singleton.js';
6
+ import type { Storage } from './resource/storage.js';
7
+ import { TypeDocument } from './type-document.js';
8
+ export declare class ApiDocument extends TypeDocument {
9
+ root: Container;
61
10
  /**
62
11
  * Returns Resource instance by path. Returns undefined if not found
63
- * @param path
64
- * @param silent
65
12
  */
66
- getResource(path: string, silent: boolean): Resource | undefined;
13
+ getResource(path: string): Resource;
14
+ getResource(path: string, silent: false): Resource;
15
+ getResource(path: string, silent: true): Resource | undefined;
67
16
  /**
68
- * Returns Resource instance by path. Throws error if not found
69
- * @param path
17
+ * Returns Collection resource instance by path
18
+ * Returns undefined if not found
19
+ * Throws error if resource is not a Collection
70
20
  */
71
- getResource(path: string): Resource;
21
+ getContainer(path: string): Container;
22
+ getContainer(path: string, silent: false): Container;
23
+ getContainer(path: string, silent: true): Container | undefined;
24
+ getContainer(path: string, silent?: boolean): Container | undefined;
72
25
  /**
73
26
  * Returns Collection resource instance by path
74
27
  * Returns undefined if not found
75
28
  * Throws error if resource is not a Collection
76
- * @param path
77
- * @param silent
78
29
  */
79
- getCollection(path: string, silent: true): Collection | undefined;
80
30
  getCollection(path: string): Collection;
31
+ getCollection(path: string, silent: false): Collection;
32
+ getCollection(path: string, silent: true): Collection | undefined;
33
+ getCollection(path: string, silent?: boolean): Collection | undefined;
81
34
  /**
82
35
  * Returns Singleton resource instance by path
83
36
  * Returns undefined if not found
84
37
  * Throws error if resource is not a Collection
85
- * @param path
86
- * @param silent
87
38
  */
88
- getSingleton(path: string, silent: true): Singleton | undefined;
89
39
  getSingleton(path: string): Singleton;
90
- getStorage(path: string, silent: true): Storage | undefined;
40
+ getSingleton(path: string, silent: false): Singleton;
41
+ getSingleton(path: string, silent: true): Singleton | undefined;
42
+ getSingleton(path: string, silent?: boolean): Singleton | undefined;
43
+ /**
44
+ * Returns Storage resource instance by path
45
+ * Returns undefined if not found
46
+ * Throws error if resource is not a Collection
47
+ */
91
48
  getStorage(path: string): Storage;
49
+ getStorage(path: string, silent: false): Storage;
50
+ getStorage(path: string, silent: true): Storage | undefined;
51
+ getStorage(path: string, silent?: boolean): Storage | undefined;
92
52
  /**
93
53
  * Export as Opra schema definition object
94
54
  */
95
55
  exportSchema(options?: {
96
56
  webSafe?: boolean;
97
57
  }): OpraSchema.ApiDocument;
98
- invalidate(): void;
99
58
  }
@@ -12,9 +12,7 @@ import type { UnionType } from './union-type.js';
12
12
  * @class ComplexType
13
13
  */
14
14
  export declare class ComplexTypeClass extends DataType {
15
- protected _decode: vg.Validator<any, any>;
16
- protected _encode: vg.Validator<any, any>;
17
- readonly kind = "ComplexType";
15
+ readonly kind: OpraSchema.DataType.Kind;
18
16
  readonly ctor: Type;
19
17
  readonly base?: ComplexType | UnionType | MappedType;
20
18
  readonly own: ComplexType.OwnProperties;
@@ -22,12 +20,14 @@ export declare class ComplexTypeClass extends DataType {
22
20
  readonly abstract?: boolean;
23
21
  readonly additionalFields?: boolean | vg.Validator | 'error';
24
22
  constructor(document: ApiDocument, init: ComplexType.InitArguments);
25
- addField(init: ApiField.InitArguments): ApiField;
26
23
  findField(nameOrPath: string): ApiField | undefined;
27
24
  getField(nameOrPath: string): ApiField;
28
25
  iteratePath(path: string, silent?: boolean): IterableIterator<[string, ApiField | undefined, string]>;
29
26
  normalizeFieldPath(fieldPaths: string | string[]): string[] | undefined;
30
- exportSchema(): OpraSchema.ComplexType;
27
+ exportSchema(options?: {
28
+ webSafe?: boolean;
29
+ }): OpraSchema.ComplexType;
31
30
  isTypeOf(t: Type | Function): boolean;
32
31
  extendsFrom(t: string | Type | DataType): boolean;
32
+ generateCodec(codec: 'decode' | 'encode', options?: DataType.GenerateCodecOptions): vg.Validator;
33
33
  }