@opra/common 0.25.4 → 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 +1843 -1355
  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 +28 -21
  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/opra-exception.js +18 -12
  41. package/cjs/exception/wrap-exception.js +2 -1
  42. package/cjs/helpers/responsive-map.js +3 -3
  43. package/cjs/http/opra-url-path.js +5 -1
  44. package/cjs/schema/document/document-base.interface.js +2 -0
  45. package/cjs/schema/document/type-document.interface.js +2 -0
  46. package/cjs/schema/opra-schema.ns.js +3 -1
  47. package/cjs/schema/type-guards.js +3 -3
  48. package/esm/document/api-document.js +16 -220
  49. package/esm/document/data-type/complex-type-class.js +40 -19
  50. package/esm/document/data-type/complex-type.js +1 -1
  51. package/esm/document/data-type/data-type.js +2 -1
  52. package/esm/document/data-type/enum-type-class.js +15 -12
  53. package/esm/document/data-type/enum-type.js +26 -12
  54. package/esm/document/data-type/field-class.js +20 -3
  55. package/esm/document/data-type/field-decorator.js +0 -19
  56. package/esm/document/data-type/mapped-type-class.js +9 -11
  57. package/esm/document/data-type/mapped-type.js +1 -1
  58. package/esm/document/data-type/simple-type-class.js +15 -6
  59. package/esm/document/data-type/simple-type.js +1 -1
  60. package/esm/document/data-type/union-type-class.js +11 -9
  61. package/esm/document/document-base.js +20 -0
  62. package/esm/document/factory/api-document-factory.js +227 -0
  63. package/esm/document/factory/type-document-factory.js +320 -0
  64. package/esm/document/index.js +7 -1
  65. package/esm/document/resource/action-decorator.js +23 -0
  66. package/esm/document/resource/collection-class.js +95 -44
  67. package/esm/document/resource/collection-decorator.js +178 -0
  68. package/esm/document/resource/collection.js +4 -4
  69. package/esm/document/resource/container-class.js +78 -0
  70. package/esm/document/resource/container-decorator.js +20 -0
  71. package/esm/document/resource/container.js +25 -0
  72. package/esm/document/resource/crud-resource.js +27 -0
  73. package/esm/document/resource/endpoint.js +27 -21
  74. package/esm/document/resource/operation-decorator.js +23 -0
  75. package/esm/document/resource/parameter.js +46 -0
  76. package/esm/document/resource/resource-decorator.js +28 -0
  77. package/esm/document/resource/resource.js +20 -5
  78. package/esm/document/resource/singleton-class.js +58 -38
  79. package/esm/document/resource/singleton-decorator.js +53 -0
  80. package/esm/document/resource/singleton.js +4 -4
  81. package/esm/document/resource/storage-class.js +9 -15
  82. package/esm/document/resource/storage-decorator.js +62 -0
  83. package/esm/document/resource/storage.js +3 -3
  84. package/esm/document/type-document.js +191 -0
  85. package/esm/exception/http-errors/internal-server.error.js +1 -0
  86. package/esm/exception/opra-exception.js +18 -12
  87. package/esm/exception/wrap-exception.js +2 -1
  88. package/esm/helpers/responsive-map.js +3 -3
  89. package/esm/http/opra-url-path.js +4 -1
  90. package/esm/schema/document/document-base.interface.js +1 -0
  91. package/esm/schema/document/type-document.interface.js +1 -0
  92. package/esm/schema/opra-schema.ns.js +3 -1
  93. package/esm/schema/type-guards.js +1 -1
  94. package/package.json +2 -2
  95. package/types/document/api-document.d.ts +32 -73
  96. package/types/document/data-type/complex-type-class.d.ts +5 -5
  97. package/types/document/data-type/complex-type.d.ts +8 -3
  98. package/types/document/{decorators → data-type}/complex-type.decorator.d.ts +1 -1
  99. package/types/document/data-type/data-type.d.ts +11 -1
  100. package/types/document/data-type/enum-type-class.d.ts +6 -6
  101. package/types/document/data-type/enum-type.d.ts +12 -6
  102. package/types/document/data-type/field-class.d.ts +9 -1
  103. package/types/document/data-type/field-decorator.d.ts +1 -1
  104. package/types/document/data-type/field.d.ts +4 -3
  105. package/types/document/data-type/mapped-type-class.d.ts +5 -9
  106. package/types/document/data-type/mapped-type.d.ts +6 -7
  107. package/types/document/data-type/simple-type-class.d.ts +6 -3
  108. package/types/document/{decorators → data-type}/simple-type.decorator.d.ts +1 -1
  109. package/types/document/data-type/union-type-class.d.ts +2 -8
  110. package/types/document/data-type/union-type.d.ts +7 -6
  111. package/types/document/document-base.d.ts +10 -0
  112. package/types/document/factory/api-document-factory.d.ts +50 -0
  113. package/types/document/factory/type-document-factory.d.ts +56 -0
  114. package/types/document/index.d.ts +7 -1
  115. package/types/document/resource/action-decorator.d.ts +6 -0
  116. package/types/document/resource/collection-class.d.ts +17 -12
  117. package/types/document/resource/collection-decorator.d.ts +192 -0
  118. package/types/document/resource/collection.d.ts +11 -19
  119. package/types/document/resource/container-class.d.ts +66 -0
  120. package/types/document/resource/container-decorator.d.ts +37 -0
  121. package/types/document/resource/container.d.ts +25 -0
  122. package/types/document/resource/crud-resource.d.ts +16 -0
  123. package/types/document/resource/endpoint.d.ts +26 -19
  124. package/types/document/resource/operation-decorator.d.ts +6 -0
  125. package/types/document/resource/parameter.d.ts +35 -0
  126. package/types/document/resource/resource-decorator.d.ts +32 -0
  127. package/types/document/resource/resource.d.ts +15 -13
  128. package/types/document/resource/singleton-class.d.ts +15 -14
  129. package/types/document/resource/singleton-decorator.d.ts +125 -0
  130. package/types/document/resource/singleton.d.ts +11 -16
  131. package/types/document/resource/storage-class.d.ts +13 -6
  132. package/types/document/resource/storage-decorator.d.ts +98 -0
  133. package/types/document/resource/storage.d.ts +9 -12
  134. package/types/document/type-document.d.ts +68 -0
  135. package/types/exception/opra-exception.d.ts +1 -1
  136. package/types/http/opra-url-path.d.ts +2 -1
  137. package/types/schema/data-type/complex-type.interface.d.ts +1 -1
  138. package/types/schema/data-type/data-type.interface.d.ts +3 -3
  139. package/types/schema/data-type/enum-type.interface.d.ts +6 -7
  140. package/types/schema/data-type/mapped-type.interface.d.ts +4 -3
  141. package/types/schema/data-type/simple-type.interface.d.ts +3 -3
  142. package/types/schema/data-type/union-type.interface.d.ts +6 -4
  143. package/types/schema/document/api-document.interface.d.ts +11 -0
  144. package/types/schema/document/document-base.interface.d.ts +24 -0
  145. package/types/schema/document/type-document.interface.d.ts +6 -0
  146. package/types/schema/opra-schema.ns.d.ts +3 -1
  147. package/types/schema/resource/collection.interface.d.ts +31 -31
  148. package/types/schema/resource/container.interface.d.ts +4 -3
  149. package/types/schema/resource/resource.interface.d.ts +1 -1
  150. package/types/schema/resource/singleton.interface.d.ts +15 -10
  151. package/types/schema/resource/storage.interface.d.ts +49 -45
  152. package/types/schema/type-guards.d.ts +3 -2
  153. package/cjs/document/decorators/collection-decorator.js +0 -38
  154. package/cjs/document/decorators/singleton.decorator.js +0 -36
  155. package/cjs/document/decorators/storage.decorator.js +0 -35
  156. package/cjs/document/factory/add-references.js +0 -20
  157. package/cjs/document/factory/create-document.js +0 -83
  158. package/cjs/document/factory/factory.js +0 -66
  159. package/cjs/document/factory/import-resource-class.js +0 -54
  160. package/cjs/document/factory/import-type-class.js +0 -146
  161. package/cjs/document/factory/index.js +0 -4
  162. package/cjs/document/factory/process-resources.js +0 -70
  163. package/cjs/document/factory/process-types.js +0 -191
  164. package/cjs/document/utils/generate-codec.js +0 -39
  165. package/esm/document/decorators/collection-decorator.js +0 -34
  166. package/esm/document/decorators/resource.decorator.js +0 -33
  167. package/esm/document/decorators/singleton.decorator.js +0 -32
  168. package/esm/document/decorators/storage.decorator.js +0 -31
  169. package/esm/document/factory/add-references.js +0 -16
  170. package/esm/document/factory/create-document.js +0 -77
  171. package/esm/document/factory/factory.js +0 -62
  172. package/esm/document/factory/import-resource-class.js +0 -48
  173. package/esm/document/factory/import-type-class.js +0 -136
  174. package/esm/document/factory/index.js +0 -1
  175. package/esm/document/factory/process-resources.js +0 -63
  176. package/esm/document/factory/process-types.js +0 -185
  177. package/esm/document/utils/generate-codec.js +0 -33
  178. package/types/document/decorators/collection-decorator.d.ts +0 -30
  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 -22
  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
@@ -1,77 +0,0 @@
1
- import { OpraSchema } from '../../schema/index.js';
2
- import { AnyType, Base64Type, BigintType, BooleanType, DateType, IntegerType, NullType, NumberType, ObjectIdType, ObjectType, StringType, TimestampType, TimeType, UuidType } from '../data-type/builtin/index.js';
3
- export async function createDocument(init, options) {
4
- this.document.url = init.url;
5
- if (init.info)
6
- Object.assign(this.document.info, init.info);
7
- if (!options?.noBuiltinTypes) {
8
- const builtinDocument = await this.createBuiltinTypeDocument();
9
- this.document.references.set('Opra', builtinDocument);
10
- for (const [c, s] of Object.getPrototypeOf(this).constructor.designTypeMap.entries())
11
- this.document._designTypeMap.set(c, s);
12
- }
13
- if (init.references)
14
- await this.addReferences(init.references);
15
- if (init.types) {
16
- this.curPath.push('types');
17
- if (Array.isArray(init.types)) {
18
- for (const type of init.types)
19
- await this.importTypeClass(type);
20
- }
21
- else
22
- this.typeQueue.setAll(init.types);
23
- await this.processTypes();
24
- this.curPath.pop();
25
- }
26
- if (init.resources) {
27
- this.curPath.push('resources');
28
- if (Array.isArray(init.resources)) {
29
- for (const res of init.resources)
30
- await this.importSourceClass(res);
31
- }
32
- else
33
- this.sourceQueue.setAll(init.resources);
34
- await this.processSourceQueue();
35
- this.curPath.pop();
36
- }
37
- this.document.types.sort();
38
- this.document.resources.sort();
39
- return this.document;
40
- }
41
- /**
42
- *
43
- * @param url
44
- * @protected
45
- */
46
- export async function createDocumentFromUrl(url) {
47
- const resp = await fetch(url, { method: 'GET' });
48
- const init = await resp.json();
49
- if (!init)
50
- throw new TypeError(`Invalid response returned from url: ${url}`);
51
- return await this.createDocument({ ...init, url });
52
- }
53
- export async function createBuiltinTypeDocument() {
54
- const init = {
55
- version: OpraSchema.SpecVersion,
56
- info: {
57
- version: OpraSchema.SpecVersion,
58
- title: 'Opra built-in types',
59
- contact: [{
60
- url: 'https://github.com/oprajs/opra'
61
- }
62
- ],
63
- license: {
64
- url: 'https://github.com/oprajs/opra/blob/main/LICENSE',
65
- name: 'MIT'
66
- }
67
- },
68
- types: [AnyType, Base64Type, BigintType, BooleanType,
69
- DateType, UuidType, IntegerType, NullType,
70
- NumberType, ObjectType, ObjectIdType, StringType,
71
- TimeType, TimestampType
72
- ]
73
- };
74
- const factoryClass = Object.getPrototypeOf(this).constructor;
75
- const factory = new factoryClass();
76
- return await factory.createDocument(init, { noBuiltinTypes: true });
77
- }
@@ -1,62 +0,0 @@
1
- var _a;
2
- import { ResponsiveMap } from '../../helpers/index.js';
3
- import { ApiDocument } from '../api-document.js';
4
- import { addReferences } from './add-references.js';
5
- import { createBuiltinTypeDocument, createDocument, createDocumentFromUrl } from './create-document.js';
6
- import { extractCollectionSchema, extractSingletonSchema, importResourceClass } from './import-resource-class.js';
7
- import { extractComplexTypeSchema, extractEnumTypeSchema, extractFieldSchema, extractMappedTypeSchema, extractSimpleTypeSchema, extractUnionTypeSchema, importTypeClass } from './import-type-class.js';
8
- import { createCollectionResource, createSingletonResource, createStorageResource, processSourceQueue } from './process-resources.js';
9
- import { addDataType, createDataTypeInstance, processTypes } from './process-types.js';
10
- /**
11
- * @class DocumentFactory
12
- */
13
- export class DocumentFactory {
14
- constructor() {
15
- this.document = new ApiDocument();
16
- this.typeQueue = new ResponsiveMap();
17
- this.sourceQueue = new ResponsiveMap();
18
- this.circularRefs = new ResponsiveMap();
19
- this.curPath = [];
20
- this.cache = new Map();
21
- }
22
- /**
23
- * Creates ApiDocument instance from given schema object
24
- * @param init
25
- */
26
- static async createDocument(init) {
27
- const factory = new DocumentFactory();
28
- return factory.createDocument(init);
29
- }
30
- /**
31
- * Downloads schema from the given URL and creates the document instance * @param url
32
- */
33
- static async createDocumentFromUrl(url) {
34
- const factory = new DocumentFactory();
35
- return factory.createDocumentFromUrl(url);
36
- }
37
- }
38
- _a = DocumentFactory;
39
- DocumentFactory.designTypeMap = new Map();
40
- (() => {
41
- _a.prototype.createDocument = createDocument;
42
- _a.prototype.createDocumentFromUrl = createDocumentFromUrl;
43
- _a.prototype.createBuiltinTypeDocument = createBuiltinTypeDocument;
44
- _a.prototype.addReferences = addReferences;
45
- _a.prototype.importTypeClass = importTypeClass;
46
- _a.prototype.extractSimpleTypeSchema = extractSimpleTypeSchema;
47
- _a.prototype.extractComplexTypeSchema = extractComplexTypeSchema;
48
- _a.prototype.extractMappedTypeSchema = extractMappedTypeSchema;
49
- _a.prototype.extractUnionTypeSchema = extractUnionTypeSchema;
50
- _a.prototype.extractEnumTypeSchema = extractEnumTypeSchema;
51
- _a.prototype.extractFieldSchema = extractFieldSchema;
52
- _a.prototype.processTypes = processTypes;
53
- _a.prototype.createDataTypeInstance = createDataTypeInstance;
54
- _a.prototype.addDataType = addDataType;
55
- _a.prototype.importSourceClass = importResourceClass;
56
- _a.prototype.extractSingletonSchema = extractSingletonSchema;
57
- _a.prototype.extractCollectionSchema = extractCollectionSchema;
58
- _a.prototype.processSourceQueue = processSourceQueue;
59
- _a.prototype.createCollectionSource = createCollectionResource;
60
- _a.prototype.createSingletonSource = createSingletonResource;
61
- _a.prototype.createFileSource = createStorageResource;
62
- })();
@@ -1,48 +0,0 @@
1
- import 'reflect-metadata';
2
- import { cloneObject, resolveClass, resolveThunk } from '../../helpers/index.js';
3
- import { OpraSchema } from '../../schema/index.js';
4
- import { RESOURCE_METADATA } from '../constants.js';
5
- export async function importResourceClass(thunk) {
6
- const { document, sourceQueue, cache } = this;
7
- const controller = await resolveThunk(thunk);
8
- const cached = cache.get(controller);
9
- if (cached)
10
- return cached;
11
- const ctor = typeof thunk === 'function' ? thunk : Object.getPrototypeOf(thunk).constructor;
12
- let metadata = Reflect.getMetadata(RESOURCE_METADATA, ctor);
13
- if (!metadata && OpraSchema.isSource(metadata))
14
- throw new TypeError(`Class "${ctor.name}" doesn't have a valid Resource metadata`);
15
- // const controller = typeof thunk === 'function' ? new ctor() : thunk;
16
- // Clone metadata to prevent changing its contents
17
- metadata = cloneObject(metadata);
18
- const schema = cloneObject(metadata);
19
- schema.controller = controller;
20
- cache.set(thunk, schema);
21
- if (OpraSchema.isSingleton(schema) || OpraSchema.isCollection(schema)) {
22
- if (!document.getDataType(metadata.type, true) && (typeof metadata.type === 'function')) {
23
- await this.importTypeClass(metadata.type);
24
- await this.processTypes();
25
- const dataTypeCtor = await resolveClass(metadata.type);
26
- const dataType = document.getComplexType(dataTypeCtor);
27
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
- schema.type = dataType.name;
29
- }
30
- // Check if data type exists
31
- document.getComplexType(schema.type);
32
- }
33
- if (OpraSchema.isSingleton(schema))
34
- await this.extractSingletonSchema(schema, ctor, metadata, controller);
35
- if (OpraSchema.isCollection(schema))
36
- await this.extractCollectionSchema(schema, ctor, metadata, controller);
37
- sourceQueue.set(metadata.name, schema);
38
- }
39
- export async function extractSingletonSchema(
40
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
41
- target, ctor, metadata, controller) {
42
- // Do nothing. This method is used by external modules for extending the factory
43
- }
44
- export async function extractCollectionSchema(
45
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
46
- target, ctor, metadata, controller) {
47
- // Do nothing. This method is used by external modules for extending the factory
48
- }
@@ -1,136 +0,0 @@
1
- import { validator } from 'valgen';
2
- import { cloneObject, isConstructor, resolveThunk } from '../../helpers/index.js';
3
- import { OpraSchema } from '../../schema/index.js';
4
- import { DATATYPE_METADATA } from '../constants.js';
5
- export async function importTypeClass(typeThunk) {
6
- const { document, typeQueue, cache } = this;
7
- const thunk = await resolveThunk(typeThunk);
8
- const cached = cache.get(thunk);
9
- if (cached)
10
- return cached;
11
- const dt = document.getDataType(thunk, true);
12
- if (dt && dt.name)
13
- return dt.name;
14
- const metadata = Reflect.getMetadata(DATATYPE_METADATA, thunk);
15
- if (!(metadata && OpraSchema.isDataType(metadata))) {
16
- // If thunk is a Type class
17
- if (isConstructor(thunk))
18
- throw new TypeError(`Class "${thunk.name}" doesn't have a valid DataType metadata`);
19
- // If thunk is an Enum object
20
- throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(thunk).substring(0, 20)}...`);
21
- }
22
- // Clone metadata to prevent changing its contents
23
- const name = metadata.name;
24
- const schema = cloneObject(metadata);
25
- const result = name || schema;
26
- if (name) {
27
- if (this.document.types.has(name) || typeQueue.has(name))
28
- throw new TypeError(`Type "${name}" already imported`);
29
- cache.set(thunk, result);
30
- typeQueue.set(name, schema);
31
- }
32
- // If thunk is a DataType class
33
- if (isConstructor(thunk)) {
34
- const ctor = thunk;
35
- if (OpraSchema.isSimpleType(schema))
36
- await this.extractSimpleTypeSchema(schema, ctor, metadata);
37
- else if (OpraSchema.isComplexType(schema))
38
- await this.extractComplexTypeSchema(schema, ctor, metadata);
39
- else if (OpraSchema.isMappedType(schema))
40
- await this.extractMappedTypeSchema(schema, ctor, metadata);
41
- else if (OpraSchema.isUnionType(schema))
42
- await this.extractUnionTypeSchema(schema, ctor, metadata);
43
- else
44
- throw new TypeError(`Class "${ctor.name}" doesn't have a valid DataType metadata`);
45
- return result;
46
- }
47
- // If thunk is a EnumType object
48
- const enumObject = thunk;
49
- if (OpraSchema.isEnumType(schema)) {
50
- let baseType;
51
- if (metadata.base && Reflect.hasMetadata(DATATYPE_METADATA, metadata.base)) {
52
- baseType = await this.importTypeClass(metadata.base);
53
- }
54
- schema.base = baseType;
55
- await this.extractEnumTypeSchema(schema, enumObject, metadata);
56
- return result;
57
- }
58
- throw new TypeError(`No EnumType metadata found for object ${JSON.stringify(enumObject).substring(0, 20)}...`);
59
- }
60
- // noinspection JSUnusedLocalSymbols
61
- export async function extractSimpleTypeSchema(target, ctor,
62
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
63
- metadata) {
64
- const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
65
- if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
66
- target.base = await this.importTypeClass(baseClass);
67
- if (typeof ctor.prototype.decode === 'function')
68
- target.decoder = validator(metadata.name, ctor.prototype.decode);
69
- if (typeof ctor.prototype.encode === 'function')
70
- target.encoder = validator(metadata.name, ctor.prototype.encode);
71
- }
72
- export async function extractComplexTypeSchema(target, ctor, metadata) {
73
- const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
74
- if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
75
- target.base = await this.importTypeClass(baseClass);
76
- target.ctor = target.ctor || ctor;
77
- // Fields
78
- if (metadata.fields) {
79
- const fields = target.fields = {};
80
- for (const [elemName, elemMeta] of Object.entries(metadata.fields)) {
81
- try {
82
- const t = await elemMeta.type;
83
- const type = typeof t === 'function'
84
- ? await this.importTypeClass(t)
85
- : (t || '');
86
- const elemSchema = fields[elemName] = {
87
- ...elemMeta,
88
- type
89
- };
90
- if (elemMeta.enum)
91
- elemSchema.type = await this.importTypeClass(elemMeta.enum);
92
- if (!elemSchema.type && elemMeta.designType) {
93
- const mappingType = this.document.getDataType(elemMeta.designType, true);
94
- if (mappingType)
95
- elemSchema.type = mappingType.name;
96
- else
97
- elemSchema.type = await this.importTypeClass(elemMeta.designType);
98
- }
99
- await this.extractFieldSchema(elemSchema, ctor, elemMeta, elemName);
100
- // Check enum again. External packages may modify enum value
101
- if (elemMeta.enum)
102
- elemSchema.type = await this.importTypeClass(elemMeta.enum);
103
- if (typeof elemSchema.type === 'function')
104
- elemSchema.type = await this.importTypeClass(elemSchema.type);
105
- if (elemSchema.isArray && !elemSchema.type)
106
- throw new TypeError(`"type" must be defined explicitly for array properties`);
107
- elemSchema.type = elemSchema.type || 'any';
108
- }
109
- catch (e) {
110
- e.message = `Error in class "${ctor.name}.${elemName}". ` + e.message;
111
- throw e;
112
- }
113
- }
114
- }
115
- }
116
- export async function extractMappedTypeSchema(target, ctor, metadata) {
117
- target.type = await this.importTypeClass(metadata.type);
118
- }
119
- export async function extractUnionTypeSchema(target, ctor, metadata) {
120
- const oldTypes = metadata.types;
121
- target.types = [];
122
- for (const type of oldTypes)
123
- target.types.push(await this.importTypeClass(type));
124
- }
125
- // noinspection JSUnusedLocalSymbols
126
- export async function extractEnumTypeSchema(
127
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
128
- target, enumObject, metadata) {
129
- // Do nothing. This method is used by external modules for extending the factory
130
- }
131
- // noinspection JSUnusedLocalSymbols
132
- export async function extractFieldSchema(
133
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
134
- target, ctor, metadata, name) {
135
- // Do nothing. This method is used by external modules for extending the factory
136
- }
@@ -1 +0,0 @@
1
- export * from './factory.js';
@@ -1,63 +0,0 @@
1
- import { OpraSchema } from '../../schema/index.js';
2
- import { Collection } from '../resource/collection.js';
3
- import { Singleton } from '../resource/singleton.js';
4
- import { Storage } from '../resource/storage.js';
5
- export async function processSourceQueue() {
6
- const { document, sourceQueue } = this;
7
- const sourceNames = Array.from(sourceQueue.keys());
8
- for (const name of sourceNames) {
9
- const schema = sourceQueue.get(name);
10
- if (!schema)
11
- continue;
12
- try {
13
- if (OpraSchema.isCollection(schema)) {
14
- const resource = await this.createCollectionSource(name, schema);
15
- document.resources.set(name, resource);
16
- continue;
17
- }
18
- if (OpraSchema.isSingleton(schema)) {
19
- const resource = await this.createSingletonSource(name, schema);
20
- document.resources.set(name, resource);
21
- continue;
22
- }
23
- if (OpraSchema.isStorage(schema)) {
24
- const resource = await this.createFileSource(name, schema);
25
- document.resources.set(name, resource);
26
- continue;
27
- }
28
- }
29
- catch (e) {
30
- e.message = `Error in Resource schema (${name}): ` + e.message;
31
- throw e;
32
- }
33
- throw new TypeError(`Invalid Resource schema: ${JSON.stringify(schema).substring(0, 20)}...`);
34
- }
35
- }
36
- export async function createCollectionResource(name, schema) {
37
- const { document } = this;
38
- const dataType = document.getComplexType(schema.type);
39
- const initArgs = {
40
- ...schema,
41
- name,
42
- type: dataType
43
- };
44
- return new Collection(document, initArgs);
45
- }
46
- export async function createSingletonResource(name, schema) {
47
- const { document } = this;
48
- const dataType = document.getComplexType(schema.type);
49
- const initArgs = {
50
- ...schema,
51
- name,
52
- type: dataType
53
- };
54
- return new Singleton(document, initArgs);
55
- }
56
- export async function createStorageResource(name, schema) {
57
- const { document } = this;
58
- const initArgs = {
59
- ...schema,
60
- name
61
- };
62
- return new Storage(document, initArgs);
63
- }
@@ -1,185 +0,0 @@
1
- import { OpraSchema } from '../../schema/index.js';
2
- import { ComplexType } from '../data-type/complex-type.js';
3
- import { EnumType } from '../data-type/enum-type.js';
4
- import { MappedType } from '../data-type/mapped-type.js';
5
- import { SimpleType } from '../data-type/simple-type.js';
6
- import { UnionType } from '../data-type/union-type.js';
7
- export async function processTypes() {
8
- const { document, typeQueue } = this;
9
- // Create DataType instances
10
- for (const [name, schema] of typeQueue.entries()) {
11
- const dataType = this.createDataTypeInstance(schema.kind, name);
12
- document.types.set(name, dataType);
13
- }
14
- // Process schemas
15
- const typeNames = Array.from(typeQueue.keys());
16
- for (const name of typeNames) {
17
- if (!typeQueue.has(name))
18
- continue;
19
- this.addDataType(name);
20
- }
21
- document.invalidate();
22
- }
23
- export function createDataTypeInstance(kind, name) {
24
- const dataType = {
25
- document: this.document,
26
- kind,
27
- name
28
- };
29
- switch (kind) {
30
- case OpraSchema.ComplexType.Kind:
31
- Object.setPrototypeOf(dataType, ComplexType.prototype);
32
- break;
33
- case OpraSchema.EnumType.Kind:
34
- Object.setPrototypeOf(dataType, EnumType.prototype);
35
- break;
36
- case OpraSchema.MappedType.Kind:
37
- Object.setPrototypeOf(dataType, MappedType.prototype);
38
- break;
39
- case OpraSchema.SimpleType.Kind:
40
- Object.setPrototypeOf(dataType, SimpleType.prototype);
41
- break;
42
- case OpraSchema.UnionType.Kind:
43
- Object.setPrototypeOf(dataType, UnionType.prototype);
44
- break;
45
- default:
46
- throw new TypeError(`Unknown DataType kind (${kind})`);
47
- }
48
- return dataType;
49
- }
50
- export function addDataType(schemaOrName) {
51
- const { document, typeQueue, circularRefs, curPath } = this;
52
- const name = typeof schemaOrName === 'string' ? schemaOrName : undefined;
53
- let schema;
54
- let dataType;
55
- if (name) {
56
- // Check if data type exists in document
57
- dataType = document.getDataType(name);
58
- // Get schema from stack, it is already done if not exist
59
- schema = typeQueue.get(name);
60
- if (!schema)
61
- return dataType;
62
- // Detect circular refs
63
- if (circularRefs.has(name))
64
- throw new TypeError(`Circular reference detected. ${[...Array.from(circularRefs.keys()), name].join('>')}`);
65
- circularRefs.set(name, 1);
66
- }
67
- else
68
- schema = schemaOrName;
69
- try {
70
- // Init base
71
- let base;
72
- if ((OpraSchema.isSimpleType(schema) || OpraSchema.isComplexType(schema) || OpraSchema.isEnumType(schema)) && schema.base) {
73
- curPath.push(typeof schema.base === 'string' ? schema.base : '[base]');
74
- base = this.addDataType(schema.base);
75
- curPath.pop();
76
- }
77
- // **** Init SimpleType ****
78
- if (OpraSchema.isSimpleType(schema)) {
79
- const initArgs = {
80
- ...schema,
81
- name,
82
- base
83
- };
84
- dataType = dataType || this.createDataTypeInstance(schema.kind, name);
85
- if (name)
86
- curPath.push(name);
87
- SimpleType.apply(dataType, [document, initArgs]);
88
- if (name)
89
- curPath.pop();
90
- return dataType;
91
- }
92
- // **** Init ComplexType ****
93
- if (OpraSchema.isComplexType(schema)) {
94
- const initArgs = {
95
- ...schema,
96
- name,
97
- base
98
- };
99
- dataType = dataType || this.createDataTypeInstance(schema.kind, name);
100
- if (name)
101
- curPath.push(name);
102
- ComplexType.apply(dataType, [document, initArgs]);
103
- if (name)
104
- typeQueue.delete(name);
105
- // process fields
106
- if (schema.fields) {
107
- for (const [elemName, v] of Object.entries(schema.fields)) {
108
- const elemSchema = typeof v === 'object' ? v : { type: v };
109
- curPath.push(`${name}.${elemName}[type]`);
110
- const elemType = this.addDataType(elemSchema.type);
111
- dataType.addField({
112
- ...elemSchema,
113
- name: elemName,
114
- type: elemType
115
- });
116
- curPath.pop();
117
- }
118
- }
119
- if (name)
120
- curPath.pop();
121
- return dataType;
122
- }
123
- // **** Init EnumType ****
124
- if (OpraSchema.isEnumType(schema)) {
125
- const initArgs = {
126
- ...schema,
127
- name,
128
- base
129
- };
130
- dataType = dataType || this.createDataTypeInstance(schema.kind, name);
131
- if (name)
132
- curPath.push(name);
133
- EnumType.apply(dataType, [document, initArgs]);
134
- if (name)
135
- curPath.pop();
136
- return dataType;
137
- }
138
- // **** Init UnionType ****
139
- if (OpraSchema.isUnionType(schema)) {
140
- const unionTypes = schema.types.map(t => this.addDataType(t));
141
- const initArgs = {
142
- ...schema,
143
- name,
144
- types: unionTypes
145
- };
146
- dataType = dataType || this.createDataTypeInstance(schema.kind, name);
147
- if (name)
148
- curPath.push(name);
149
- UnionType.apply(dataType, [document, initArgs]);
150
- if (name)
151
- curPath.pop();
152
- return dataType;
153
- }
154
- // **** Init MappedType ****
155
- if (OpraSchema.isMappedType(schema)) {
156
- const dt = this.addDataType(schema.type);
157
- if (!(dt instanceof ComplexType))
158
- throw new TypeError(`MappedType requires a ComplexType`);
159
- const initArgs = {
160
- ...schema,
161
- name,
162
- type: dt
163
- };
164
- dataType = dataType || this.createDataTypeInstance(schema.kind, name);
165
- if (name)
166
- curPath.push(name);
167
- MappedType.apply(dataType, [document, initArgs]);
168
- if (name)
169
- curPath.pop();
170
- return dataType;
171
- }
172
- }
173
- catch (e) {
174
- if (curPath.length)
175
- e.message = `Error at ${curPath.join('/')}: ` + e.message;
176
- throw e;
177
- }
178
- finally {
179
- if (name) {
180
- circularRefs.delete(name);
181
- typeQueue.delete(name);
182
- }
183
- }
184
- throw new TypeError(`Invalid DataType schema: ${JSON.stringify(schema).substring(0, 20)}...`);
185
- }
@@ -1,33 +0,0 @@
1
- import * as vg from 'valgen';
2
- import { ComplexType } from '../data-type/complex-type.js';
3
- import { EnumType } from '../data-type/enum-type.js';
4
- import { MappedType } from '../data-type/mapped-type.js';
5
- import { SimpleType } from '../data-type/simple-type.js';
6
- import { UnionType } from '../data-type/union-type.js';
7
- export function generateCodec(type, codec, options) {
8
- return _generateCodec(type, codec, options);
9
- }
10
- export function _generateCodec(type, codec, options) {
11
- const schema = {};
12
- for (const f of type.fields.values()) {
13
- let fn;
14
- if (f.type instanceof SimpleType || f.type instanceof EnumType) {
15
- fn = f.type[codec];
16
- }
17
- else if (f.type instanceof ComplexType || f.type instanceof MappedType || f.type instanceof UnionType) {
18
- fn = _generateCodec(f.type, codec, options);
19
- }
20
- /* istanbul ignore next */
21
- if (!fn)
22
- throw new TypeError(`Can't generate codec for (${f.type})`);
23
- if (f.isArray)
24
- fn = vg.isArray(fn);
25
- schema[f.name] = !options.partial && f.required ? vg.required(fn) : vg.optional(fn);
26
- }
27
- return vg.isObject(schema, {
28
- ctor: type.ctor,
29
- additionalFields: type.additionalFields ?? false,
30
- name: type.name,
31
- caseInSensitive: true,
32
- });
33
- }
@@ -1,30 +0,0 @@
1
- import { StrictOmit, Type } from 'ts-gems';
2
- import type { Collection } from '../resource/collection';
3
- import { Resource } from '../resource/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 declare function CollectionDecorator(type: Type | string, options?: Collection.DecoratorOptions): ClassDecorator;
9
- export declare namespace CollectionDecorator {
10
- var Create: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
11
- var Delete: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
12
- var DeleteMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
13
- var Get: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
14
- var FindMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
15
- var Update: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
16
- var UpdateMany: (options?: unknown) => (target: Object, propertyKey: string | symbol) => void;
17
- var Action: (options: any) => PropertyDecorator;
18
- }
19
- export interface CollectionDecorator extends StrictOmit<ResourceDecorator, 'Action'> {
20
- <T>(type: Type<T> | string, options?: Collection.DecoratorOptions<T>): ClassDecorator;
21
- 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);
22
- Create: (options?: Collection.CreateEndpointOptions) => ((target: Object, propertyKey: 'create') => void);
23
- Delete: (options?: Collection.DeleteEndpointOptions) => ((target: Object, propertyKey: 'delete') => void);
24
- DeleteMany: (options?: Collection.DeleteManyEndpointOptions) => ((target: Object, propertyKey: 'deleteMany') => void);
25
- Get: (options?: Collection.GetEndpointOptions) => ((target: Object, propertyKey: 'get') => void);
26
- FindMany: (options?: Collection.FindManyEndpointOptions) => ((target: Object, propertyKey: 'findMany') => void);
27
- Update: (options?: Collection.UpdateEndpointOptions) => ((target: Object, propertyKey: 'update') => void);
28
- UpdateMany: (options?: Collection.UpdateManyEndpointOptions) => ((target: Object, propertyKey: 'updateMany') => void);
29
- }
30
- export {};
@@ -1,9 +0,0 @@
1
- import { OpraSchema } from '../../schema/index.js';
2
- import type { Resource } from '../resource/resource';
3
- export interface ResourceDecorator {
4
- Action: (options?: Resource.ActionOptions) => PropertyDecorator;
5
- }
6
- export declare function ResourceDecorator<O extends Resource.DecoratorOptions>(kind: OpraSchema.Resource.Kind, options?: O): ClassDecorator;
7
- export declare namespace ResourceDecorator {
8
- var Action: (options: any) => PropertyDecorator;
9
- }