@opra/core 0.0.5

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 (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/cjs/constants.js +5 -0
  4. package/cjs/decorators/entity-resource.decorator.js +24 -0
  5. package/cjs/enums/http-headers.enum.js +393 -0
  6. package/cjs/enums/http-status.enum.js +300 -0
  7. package/cjs/enums/index.js +5 -0
  8. package/cjs/enums/issue-severity.enum.js +2 -0
  9. package/cjs/exception/api-exception.js +63 -0
  10. package/cjs/exception/errors/bad-request.error.js +22 -0
  11. package/cjs/exception/errors/failed-dependency.error.js +21 -0
  12. package/cjs/exception/errors/forbidden.error.js +23 -0
  13. package/cjs/exception/errors/internal-server.error.js +21 -0
  14. package/cjs/exception/errors/method-not-allowed.error.js +22 -0
  15. package/cjs/exception/errors/not-found.error.js +25 -0
  16. package/cjs/exception/errors/unauthorized.error.js +22 -0
  17. package/cjs/exception/errors/unprocessable-entity.error.js +21 -0
  18. package/cjs/exception/index.js +11 -0
  19. package/cjs/implementation/adapter/adapter.js +72 -0
  20. package/cjs/implementation/adapter/express-adapter.js +93 -0
  21. package/cjs/implementation/adapter/http-adapter.js +262 -0
  22. package/cjs/implementation/data-type/complex-type.js +39 -0
  23. package/cjs/implementation/data-type/data-type.js +35 -0
  24. package/cjs/implementation/data-type/entity-type.js +33 -0
  25. package/cjs/implementation/data-type/simple-type.js +30 -0
  26. package/cjs/implementation/execution-context.js +49 -0
  27. package/cjs/implementation/opra-document.js +116 -0
  28. package/cjs/implementation/opra-service.js +59 -0
  29. package/cjs/implementation/resource/container-resource-controller.js +26 -0
  30. package/cjs/implementation/resource/entity-resource-info.js +68 -0
  31. package/cjs/implementation/resource/resource-info.js +24 -0
  32. package/cjs/implementation/schema-generator.js +173 -0
  33. package/cjs/index.js +25 -0
  34. package/cjs/interfaces/entity-resource.interface.js +2 -0
  35. package/cjs/interfaces/execution-query.interface.js +190 -0
  36. package/cjs/interfaces/http-context.interface.js +2 -0
  37. package/cjs/interfaces/opra-schema.metadata.js +2 -0
  38. package/cjs/interfaces/resource-container.interface.js +2 -0
  39. package/cjs/package.json +3 -0
  40. package/cjs/services/data-service.js +9 -0
  41. package/cjs/services/json-data-service.js +15 -0
  42. package/cjs/types.js +2 -0
  43. package/cjs/utils/class-utils.js +37 -0
  44. package/cjs/utils/headers.js +58 -0
  45. package/cjs/utils/internal-data-types.js +44 -0
  46. package/cjs/utils/responsive-object.js +49 -0
  47. package/cjs/utils/string-path-to-object-tree.js +26 -0
  48. package/cjs/utils/terminal-utils.js +7 -0
  49. package/esm/constants.d.ts +2 -0
  50. package/esm/constants.js +2 -0
  51. package/esm/decorators/entity-resource.decorator.d.ts +5 -0
  52. package/esm/decorators/entity-resource.decorator.js +19 -0
  53. package/esm/enums/http-headers.enum.d.ts +368 -0
  54. package/esm/enums/http-headers.enum.js +390 -0
  55. package/esm/enums/http-status.enum.d.ts +290 -0
  56. package/esm/enums/http-status.enum.js +297 -0
  57. package/esm/enums/index.d.ts +2 -0
  58. package/esm/enums/index.js +2 -0
  59. package/esm/enums/issue-severity.enum.d.ts +1 -0
  60. package/esm/enums/issue-severity.enum.js +1 -0
  61. package/esm/exception/api-exception.d.ts +38 -0
  62. package/esm/exception/api-exception.js +59 -0
  63. package/esm/exception/errors/bad-request.error.d.ts +9 -0
  64. package/esm/exception/errors/bad-request.error.js +18 -0
  65. package/esm/exception/errors/failed-dependency.error.d.ts +8 -0
  66. package/esm/exception/errors/failed-dependency.error.js +17 -0
  67. package/esm/exception/errors/forbidden.error.d.ts +10 -0
  68. package/esm/exception/errors/forbidden.error.js +19 -0
  69. package/esm/exception/errors/internal-server.error.d.ts +8 -0
  70. package/esm/exception/errors/internal-server.error.js +17 -0
  71. package/esm/exception/errors/method-not-allowed.error.d.ts +9 -0
  72. package/esm/exception/errors/method-not-allowed.error.js +18 -0
  73. package/esm/exception/errors/not-found.error.d.ts +12 -0
  74. package/esm/exception/errors/not-found.error.js +21 -0
  75. package/esm/exception/errors/unauthorized.error.d.ts +9 -0
  76. package/esm/exception/errors/unauthorized.error.js +18 -0
  77. package/esm/exception/errors/unprocessable-entity.error.d.ts +8 -0
  78. package/esm/exception/errors/unprocessable-entity.error.js +17 -0
  79. package/esm/exception/index.d.ts +8 -0
  80. package/esm/exception/index.js +8 -0
  81. package/esm/implementation/adapter/adapter.d.ts +18 -0
  82. package/esm/implementation/adapter/adapter.js +68 -0
  83. package/esm/implementation/adapter/express-adapter.d.ts +12 -0
  84. package/esm/implementation/adapter/express-adapter.js +89 -0
  85. package/esm/implementation/adapter/http-adapter.d.ts +27 -0
  86. package/esm/implementation/adapter/http-adapter.js +258 -0
  87. package/esm/implementation/data-type/complex-type.d.ts +18 -0
  88. package/esm/implementation/data-type/complex-type.js +35 -0
  89. package/esm/implementation/data-type/data-type.d.ts +15 -0
  90. package/esm/implementation/data-type/data-type.js +31 -0
  91. package/esm/implementation/data-type/entity-type.d.ts +10 -0
  92. package/esm/implementation/data-type/entity-type.js +29 -0
  93. package/esm/implementation/data-type/simple-type.d.ts +15 -0
  94. package/esm/implementation/data-type/simple-type.js +26 -0
  95. package/esm/implementation/execution-context.d.ts +42 -0
  96. package/esm/implementation/execution-context.js +43 -0
  97. package/esm/implementation/opra-document.d.ts +26 -0
  98. package/esm/implementation/opra-document.js +111 -0
  99. package/esm/implementation/opra-service.d.ts +19 -0
  100. package/esm/implementation/opra-service.js +55 -0
  101. package/esm/implementation/resource/container-resource-controller.d.ts +12 -0
  102. package/esm/implementation/resource/container-resource-controller.js +22 -0
  103. package/esm/implementation/resource/entity-resource-info.d.ts +24 -0
  104. package/esm/implementation/resource/entity-resource-info.js +63 -0
  105. package/esm/implementation/resource/resource-info.d.ts +10 -0
  106. package/esm/implementation/resource/resource-info.js +20 -0
  107. package/esm/implementation/schema-generator.d.ts +21 -0
  108. package/esm/implementation/schema-generator.js +169 -0
  109. package/esm/index.d.ts +22 -0
  110. package/esm/index.js +22 -0
  111. package/esm/interfaces/entity-resource.interface.d.ts +9 -0
  112. package/esm/interfaces/entity-resource.interface.js +1 -0
  113. package/esm/interfaces/execution-query.interface.d.ts +102 -0
  114. package/esm/interfaces/execution-query.interface.js +186 -0
  115. package/esm/interfaces/http-context.interface.d.ts +23 -0
  116. package/esm/interfaces/http-context.interface.js +1 -0
  117. package/esm/interfaces/opra-schema.metadata.d.ts +14 -0
  118. package/esm/interfaces/opra-schema.metadata.js +1 -0
  119. package/esm/interfaces/resource-container.interface.d.ts +6 -0
  120. package/esm/interfaces/resource-container.interface.js +1 -0
  121. package/esm/services/data-service.d.ts +2 -0
  122. package/esm/services/data-service.js +5 -0
  123. package/esm/services/json-data-service.d.ts +9 -0
  124. package/esm/services/json-data-service.js +10 -0
  125. package/esm/types.d.ts +11 -0
  126. package/esm/types.js +1 -0
  127. package/esm/utils/class-utils.d.ts +6 -0
  128. package/esm/utils/class-utils.js +30 -0
  129. package/esm/utils/headers.d.ts +9 -0
  130. package/esm/utils/headers.js +55 -0
  131. package/esm/utils/internal-data-types.d.ts +4 -0
  132. package/esm/utils/internal-data-types.js +41 -0
  133. package/esm/utils/responsive-object.d.ts +3 -0
  134. package/esm/utils/responsive-object.js +45 -0
  135. package/esm/utils/string-path-to-object-tree.d.ts +4 -0
  136. package/esm/utils/string-path-to-object-tree.js +22 -0
  137. package/esm/utils/terminal-utils.d.ts +4 -0
  138. package/esm/utils/terminal-utils.js +4 -0
  139. package/package.json +77 -0
@@ -0,0 +1,26 @@
1
+ import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
2
+ import { DataType } from './data-type.js';
3
+ export class SimpleType extends DataType {
4
+ constructor(owner, args, base) {
5
+ super(owner, {
6
+ kind: 'SimpleType',
7
+ ...args
8
+ }, base);
9
+ }
10
+ get type() {
11
+ return this._args.type;
12
+ }
13
+ get format() {
14
+ return this._args.format;
15
+ }
16
+ get default() {
17
+ return this._args.default;
18
+ }
19
+ toString() {
20
+ return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
21
+ }
22
+ [nodeInspectCustom]() {
23
+ return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
24
+ ` ${colorFgMagenta + this.name + colorReset}]`;
25
+ }
26
+ }
@@ -0,0 +1,42 @@
1
+ import { SearchParams } from '@opra/url';
2
+ import { HttpStatus } from '../enums/index.js';
3
+ import { ApiException } from '../exception/index.js';
4
+ import { ExecutionQuery } from '../interfaces/execution-query.interface.js';
5
+ import { IHttpAdapterContext } from '../interfaces/http-context.interface.js';
6
+ import { HeadersObject } from '../utils/headers.js';
7
+ import { OpraService } from './opra-service.js';
8
+ export declare type ContextType = 'http' | 'ws' | 'rpc';
9
+ export declare type ExecutionContextArgs = Pick<ExecutionContext, 'type' | 'service' | 'request' | 'response' | 'adapterContext' | 'userContext' | 'continueOnError'>;
10
+ export declare class ExecutionContext {
11
+ readonly type: ContextType;
12
+ readonly service: OpraService;
13
+ readonly request: ExecutionRequest;
14
+ readonly response: ExecutionResponse;
15
+ readonly adapterContext: any;
16
+ userContext?: any;
17
+ continueOnError?: boolean;
18
+ constructor(args: ExecutionContextArgs);
19
+ switchToHttp(): IHttpAdapterContext;
20
+ }
21
+ export declare type ExecutionRequestArgs = Pick<ExecutionRequest, 'query' | 'parentValue'> & {
22
+ params?: SearchParams;
23
+ headers?: HeadersObject;
24
+ resultPath?: string;
25
+ };
26
+ export declare class ExecutionRequest {
27
+ readonly query: ExecutionQuery;
28
+ readonly params: SearchParams;
29
+ readonly headers: HeadersObject;
30
+ readonly parentValue?: any;
31
+ readonly resultPath: string;
32
+ constructor(args: ExecutionRequestArgs);
33
+ }
34
+ export declare type ExecutionResponseArgs = Pick<ExecutionResponse, 'status' | 'value' | 'total'>;
35
+ export declare class ExecutionResponse {
36
+ headers: HeadersObject;
37
+ errors: ApiException[];
38
+ status?: HttpStatus;
39
+ value?: any;
40
+ total?: number;
41
+ constructor(args?: ExecutionResponseArgs);
42
+ }
@@ -0,0 +1,43 @@
1
+ import { OpraURLSearchParams } from '@opra/url';
2
+ import { Headers } from '../utils/headers.js';
3
+ export class ExecutionContext {
4
+ type;
5
+ service;
6
+ request;
7
+ response;
8
+ adapterContext;
9
+ userContext;
10
+ continueOnError;
11
+ constructor(args) {
12
+ Object.assign(this, args);
13
+ }
14
+ switchToHttp() {
15
+ if (this.type !== 'http')
16
+ throw new Error(`You can't access http context within an ${this.type} context`);
17
+ return this.adapterContext;
18
+ }
19
+ }
20
+ export class ExecutionRequest {
21
+ query;
22
+ params;
23
+ headers;
24
+ parentValue;
25
+ resultPath;
26
+ constructor(args) {
27
+ Object.assign(this, args);
28
+ this.params = this.params || new OpraURLSearchParams();
29
+ this.headers = this.headers || Headers.create();
30
+ this.resultPath = this.resultPath || '';
31
+ }
32
+ }
33
+ export class ExecutionResponse {
34
+ headers = Headers.create();
35
+ errors = [];
36
+ status;
37
+ value;
38
+ total;
39
+ constructor(args) {
40
+ if (args)
41
+ Object.assign(this, args);
42
+ }
43
+ }
@@ -0,0 +1,26 @@
1
+ import { StrictOmit } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { ResponsiveObject } from '../utils/responsive-object.js';
4
+ import { nodeInspectCustom } from '../utils/terminal-utils.js';
5
+ import { ComplexType } from './data-type/complex-type.js';
6
+ import { DataType } from './data-type/data-type.js';
7
+ import { EntityType } from './data-type/entity-type.js';
8
+ import { SimpleType } from './data-type/simple-type.js';
9
+ import { SchemaGenerator } from './schema-generator.js';
10
+ export declare type OpraDocumentArgs = StrictOmit<OpraSchema.Document, 'version' | 'types'>;
11
+ export declare class OpraDocument {
12
+ protected readonly _args: OpraDocumentArgs;
13
+ protected _types: ResponsiveObject<DataType>;
14
+ constructor(schema: OpraSchema.Document);
15
+ get name(): string;
16
+ get info(): OpraSchema.DocumentInfo;
17
+ get types(): Record<string, DataType>;
18
+ getDataType(name: string): DataType;
19
+ getComplexDataType(name: string): ComplexType;
20
+ getEntityDataType(name: string): EntityType;
21
+ getSimpleDataType(name: string): SimpleType;
22
+ toString(): string;
23
+ [nodeInspectCustom](): string;
24
+ static create(args: SchemaGenerator.GenerateDocumentArgs): Promise<OpraDocument>;
25
+ protected _addDataTypes(dataTypes: OpraSchema.DataType[]): void;
26
+ }
@@ -0,0 +1,111 @@
1
+ import _ from 'lodash';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { internalDataTypes } from '../utils/internal-data-types.js';
4
+ import { Responsive } from '../utils/responsive-object.js';
5
+ import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../utils/terminal-utils.js';
6
+ import { ComplexType } from './data-type/complex-type.js';
7
+ import { EntityType } from './data-type/entity-type.js';
8
+ import { SimpleType } from './data-type/simple-type.js';
9
+ import { SchemaGenerator } from './schema-generator.js';
10
+ export class OpraDocument {
11
+ _args;
12
+ _types = Responsive();
13
+ constructor(schema) {
14
+ this._args = _.omit(schema, 'types');
15
+ if (schema.types)
16
+ this._addDataTypes(schema.types);
17
+ }
18
+ get name() {
19
+ return this._args.info?.title || '';
20
+ }
21
+ get info() {
22
+ return this._args.info;
23
+ }
24
+ get types() {
25
+ return this._types;
26
+ }
27
+ getDataType(name) {
28
+ const t = this.types[name];
29
+ if (!t)
30
+ throw new Error(`Data type "${name}" does not exists`);
31
+ return t;
32
+ }
33
+ getComplexDataType(name) {
34
+ const t = this.getDataType(name);
35
+ if (!(t instanceof ComplexType))
36
+ throw new Error(`Data type "${name}" is not a ComplexType`);
37
+ return t;
38
+ }
39
+ getEntityDataType(name) {
40
+ const t = this.getDataType(name);
41
+ if (!(t instanceof EntityType))
42
+ throw new Error(`Data type "${name}" is not an EntityType`);
43
+ return t;
44
+ }
45
+ getSimpleDataType(name) {
46
+ const t = this.getDataType(name);
47
+ if (!(t instanceof SimpleType))
48
+ throw new Error(`Data type "${name}" is not a SimpleType`);
49
+ return t;
50
+ }
51
+ toString() {
52
+ return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
53
+ }
54
+ [nodeInspectCustom]() {
55
+ return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
56
+ ` ${colorFgMagenta + this.name + colorReset}]`;
57
+ }
58
+ static async create(args) {
59
+ const schema = await SchemaGenerator.generateDocumentSchema(args);
60
+ return new OpraDocument(schema);
61
+ }
62
+ _addDataTypes(dataTypes) {
63
+ const recursiveSet = new Set();
64
+ const nameSet = new Set(dataTypes.map(x => x.name));
65
+ const processDataType = (schema) => {
66
+ if ((!internalDataTypes.has(schema.name) && !nameSet.has(schema.name)) || this.types[schema.name])
67
+ return;
68
+ if (recursiveSet.has(schema.name))
69
+ throw new TypeError(`Recursive dependency detected. ${Array.from(recursiveSet).join('>')}`);
70
+ recursiveSet.add(schema.name);
71
+ let baseType;
72
+ if (schema.base) {
73
+ if (!this.types[schema.base]) {
74
+ const baseSchema = dataTypes.find(dt => dt.name.toLowerCase() === schema.base?.toLowerCase()) ||
75
+ internalDataTypes.get(schema.base.toLowerCase());
76
+ if (!baseSchema)
77
+ throw new TypeError(`Base schema (${schema.base}) of data type "${schema.name}" does not exists`);
78
+ baseType = processDataType(baseSchema);
79
+ }
80
+ }
81
+ let dataType;
82
+ if (OpraSchema.isSimpleType(schema)) {
83
+ if (baseType && !(baseType instanceof SimpleType))
84
+ throw new TypeError(`Can't extend a SimpleType (${schema.name}) from a ComplexType "${baseType.name}"`);
85
+ dataType = new SimpleType(this, schema, baseType);
86
+ }
87
+ else if (OpraSchema.isComplexType(schema)) {
88
+ if (baseType && !(baseType instanceof ComplexType))
89
+ throw new TypeError(`Can't extend a ComplexType (${schema.name}) from a SimpleType "${baseType.name}"`);
90
+ dataType = new ComplexType(this, schema, baseType);
91
+ }
92
+ else if (OpraSchema.isEntityType(schema)) {
93
+ if (baseType && !(baseType instanceof ComplexType))
94
+ throw new TypeError(`Can't extend an EntityType (${schema.name}) from a SimpleType "${baseType.name}"`);
95
+ dataType = new EntityType(this, schema, baseType);
96
+ }
97
+ else
98
+ throw new TypeError(`Invalid data type schema`);
99
+ nameSet.delete(schema.name);
100
+ this.types[dataType.name] = dataType;
101
+ recursiveSet.delete(schema.name);
102
+ return dataType;
103
+ };
104
+ dataTypes.forEach(dataType => processDataType(dataType));
105
+ // Sort data types by name
106
+ const newTypes = Responsive();
107
+ Object.keys(this.types).sort()
108
+ .forEach(name => newTypes[name] = this.types[name]);
109
+ this._types = newTypes;
110
+ }
111
+ }
@@ -0,0 +1,19 @@
1
+ import { StrictOmit } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { IResourceContainer } from '../interfaces/resource-container.interface.js';
4
+ import { OpraDocument } from './opra-document.js';
5
+ import { EntityResourceInfo } from './resource/entity-resource-info.js';
6
+ import { ResourceInfo } from './resource/resource-info.js';
7
+ import { SchemaGenerator } from './schema-generator.js';
8
+ export declare type OpraServiceArgs = StrictOmit<OpraSchema.Service, 'version' | 'types' | 'resources'>;
9
+ export declare class OpraService extends OpraDocument implements IResourceContainer {
10
+ protected readonly _args: OpraServiceArgs;
11
+ protected _resources: import("../utils/responsive-object.js").ResponsiveObject<ResourceInfo>;
12
+ constructor(schema: OpraSchema.Service);
13
+ get resources(): Record<string, ResourceInfo>;
14
+ get servers(): OpraSchema.ServerInfo[] | undefined;
15
+ getResource<T extends ResourceInfo>(name: string): T;
16
+ getEntityResource(name: string): EntityResourceInfo;
17
+ protected _addResources(resources: OpraSchema.Resource[]): void;
18
+ static create(args: SchemaGenerator.GenerateServiceArgs): Promise<OpraService>;
19
+ }
@@ -0,0 +1,55 @@
1
+ import { OpraSchema } from '@opra/schema';
2
+ import { Responsive } from '../utils/responsive-object.js';
3
+ import { EntityType } from './data-type/entity-type.js';
4
+ import { OpraDocument } from './opra-document.js';
5
+ import { EntityResourceInfo } from './resource/entity-resource-info.js';
6
+ import { SchemaGenerator } from './schema-generator.js';
7
+ export class OpraService extends OpraDocument {
8
+ _resources = Responsive();
9
+ constructor(schema) {
10
+ super(schema);
11
+ if (schema.resources)
12
+ this._addResources(schema.resources);
13
+ }
14
+ get resources() {
15
+ return this._resources;
16
+ }
17
+ get servers() {
18
+ return this._args.servers;
19
+ }
20
+ getResource(name) {
21
+ const t = this.resources[name];
22
+ if (!t)
23
+ throw new Error(`Resource "${name}" does not exists`);
24
+ return t;
25
+ }
26
+ getEntityResource(name) {
27
+ const t = this.getResource(name);
28
+ if (!(t instanceof EntityResourceInfo))
29
+ throw new Error(`"${name}" is not an EntityResource`);
30
+ return t;
31
+ }
32
+ _addResources(resources) {
33
+ for (const r of resources) {
34
+ if (OpraSchema.isEntityResource(r)) {
35
+ const dataType = this.getDataType(r.type);
36
+ if (!dataType)
37
+ throw new TypeError(`Datatype "${r.type}" declared in EntityResource (${r.name}) does not exists`);
38
+ if (!(dataType instanceof EntityType))
39
+ throw new TypeError(`${r.type} is not an EntityType`);
40
+ this.resources[r.name] = new EntityResourceInfo({ ...r, service: this, dataType });
41
+ }
42
+ else
43
+ throw new TypeError(`Unknown resource kind (${r.kind})`);
44
+ }
45
+ // Sort data types by name
46
+ const newResources = Responsive();
47
+ Object.keys(this.resources).sort()
48
+ .forEach(name => newResources[name] = this.resources[name]);
49
+ this._resources = newResources;
50
+ }
51
+ static async create(args) {
52
+ const schema = await SchemaGenerator.generateServiceSchema(args);
53
+ return new OpraService(schema);
54
+ }
55
+ }
@@ -0,0 +1,12 @@
1
+ import { StrictOmit } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { IResourceContainer } from '../../interfaces/resource-container.interface.js';
4
+ import { EntityResourceInfo } from './entity-resource-info.js';
5
+ import { ResourceInfo } from './resource-info.js';
6
+ export declare type ContainerResourceControllerArgs = StrictOmit<OpraSchema.ContainerResource, 'kind'> & {};
7
+ export declare class ContainerResourceController extends ResourceInfo implements IResourceContainer {
8
+ protected readonly _args: OpraSchema.ContainerResource;
9
+ constructor(args: ContainerResourceControllerArgs);
10
+ getResource<T extends ResourceInfo>(name: string): T;
11
+ getEntityResource(name: string): EntityResourceInfo;
12
+ }
@@ -0,0 +1,22 @@
1
+ import { EntityResourceInfo } from './entity-resource-info.js';
2
+ import { ResourceInfo } from './resource-info.js';
3
+ export class ContainerResourceController extends ResourceInfo {
4
+ constructor(args) {
5
+ super({
6
+ kind: 'ContainerResource',
7
+ ...args
8
+ });
9
+ }
10
+ getResource(name) {
11
+ const t = this._args.resources[name];
12
+ if (!t)
13
+ throw new Error(`Resource "${name}" does not exists`);
14
+ return t;
15
+ }
16
+ getEntityResource(name) {
17
+ const t = this.getResource(name);
18
+ if (!(t instanceof EntityResourceInfo))
19
+ throw new Error(`"${name}" is not an EntityResource`);
20
+ return t;
21
+ }
22
+ }
@@ -0,0 +1,24 @@
1
+ import { Maybe, StrictOmit } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { EntityType } from '../data-type/entity-type.js';
4
+ import { ExecutionContext } from '../execution-context.js';
5
+ import { OpraService } from '../opra-service.js';
6
+ import { ResourceInfo } from './resource-info.js';
7
+ export declare type EntityResourceControllerArgs = StrictOmit<OpraSchema.EntityResource, 'kind'> & {
8
+ service: OpraService;
9
+ dataType: EntityType;
10
+ };
11
+ export declare class EntityResourceInfo extends ResourceInfo {
12
+ protected readonly _args: OpraSchema.EntityResource;
13
+ readonly service: OpraService;
14
+ readonly dataType: EntityType;
15
+ constructor(args: EntityResourceControllerArgs);
16
+ get search(): Maybe<Function>;
17
+ get create(): Maybe<Function>;
18
+ get read(): Maybe<Function>;
19
+ get update(): Maybe<Function>;
20
+ get updateAll(): Maybe<Function>;
21
+ get delete(): Maybe<Function>;
22
+ get deleteMany(): Maybe<Function>;
23
+ execute(ctx: ExecutionContext): Promise<void>;
24
+ }
@@ -0,0 +1,63 @@
1
+ import _ from 'lodash';
2
+ import { EntityType } from '../data-type/entity-type.js';
3
+ import { ResourceInfo } from './resource-info.js';
4
+ export class EntityResourceInfo extends ResourceInfo {
5
+ service;
6
+ dataType;
7
+ constructor(args) {
8
+ super({
9
+ kind: 'EntityResource',
10
+ ..._.omit(args, ['dataType', 'service'])
11
+ });
12
+ this.dataType = args.dataType;
13
+ this.service = args.service;
14
+ // noinspection SuspiciousTypeOfGuard
15
+ if (!(args.dataType instanceof EntityType))
16
+ throw new TypeError(`You should provide an EntityType for EntityResourceController`);
17
+ }
18
+ get search() {
19
+ return this._args.search;
20
+ }
21
+ get create() {
22
+ return this._args.create;
23
+ }
24
+ get read() {
25
+ return this._args.read;
26
+ }
27
+ get update() {
28
+ return this._args.update;
29
+ }
30
+ get updateAll() {
31
+ return this._args.updateMany;
32
+ }
33
+ get delete() {
34
+ return this._args.delete;
35
+ }
36
+ get deleteMany() {
37
+ return this._args.deleteMany;
38
+ }
39
+ async execute(ctx) {
40
+ const { query } = ctx.request;
41
+ const fn = this._args[query.queryType];
42
+ const result = typeof fn === 'function' ? (await fn(ctx)) : undefined;
43
+ if (query.queryType === 'search') {
44
+ ctx.response.value = Array.isArray(result)
45
+ ? result
46
+ : (ctx.response.value ? [result] : []);
47
+ }
48
+ else if (query.queryType === 'delete' ||
49
+ query.queryType === 'deleteMany' ||
50
+ query.queryType === 'updateMany') {
51
+ let affected = result;
52
+ if (affected && typeof affected === 'object')
53
+ affected = result.affected || result.affectedRows;
54
+ affected = typeof affected === 'number' ? affected :
55
+ (affected === false ? 0 : (affected ? 1 : 0));
56
+ ctx.response.value = {
57
+ affected
58
+ };
59
+ }
60
+ else
61
+ ctx.response.value = Array.isArray(result) ? result[0] : result;
62
+ }
63
+ }
@@ -0,0 +1,10 @@
1
+ import { OpraSchema } from '@opra/schema';
2
+ import { nodeInspectCustom } from '../../utils/terminal-utils.js';
3
+ export declare abstract class ResourceInfo {
4
+ protected readonly _args: OpraSchema.Resource;
5
+ protected constructor(args: OpraSchema.Resource);
6
+ get name(): string;
7
+ get description(): string | undefined;
8
+ toString(): string;
9
+ [nodeInspectCustom](): string;
10
+ }
@@ -0,0 +1,20 @@
1
+ import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
2
+ export class ResourceInfo {
3
+ _args;
4
+ constructor(args) {
5
+ this._args = args;
6
+ }
7
+ get name() {
8
+ return this._args.name;
9
+ }
10
+ get description() {
11
+ return this._args.description;
12
+ }
13
+ toString() {
14
+ return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
15
+ }
16
+ [nodeInspectCustom]() {
17
+ return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
18
+ ` ${colorFgMagenta + this.name + colorReset}]`;
19
+ }
20
+ }
@@ -0,0 +1,21 @@
1
+ import { StrictOmit, Type } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { ThunkAsync } from '../types.js';
4
+ export declare namespace SchemaGenerator {
5
+ type GenerateDocumentArgs = StrictOmit<OpraSchema.Document, 'version' | 'types'> & {
6
+ types?: ThunkAsync<Type | OpraSchema.DataType>[];
7
+ };
8
+ type GenerateServiceArgs = StrictOmit<OpraSchema.Service, 'version' | 'types' | 'resources'> & {
9
+ types?: ThunkAsync<Type | OpraSchema.DataType>[];
10
+ resources: any[];
11
+ };
12
+ }
13
+ export declare class SchemaGenerator {
14
+ protected _dataTypes: Record<string, OpraSchema.DataType>;
15
+ protected _resources: Record<string, OpraSchema.Resource>;
16
+ protected constructor();
17
+ protected addDataType(thunk: ThunkAsync<Type | OpraSchema.DataType>): Promise<OpraSchema.DataType>;
18
+ addResource(instance: any): Promise<void>;
19
+ static generateDocumentSchema(args: SchemaGenerator.GenerateDocumentArgs): Promise<OpraSchema.Document>;
20
+ static generateServiceSchema(args: SchemaGenerator.GenerateServiceArgs): Promise<OpraSchema.Service>;
21
+ }