@opra/core 0.0.13 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/cjs/enums/http-headers.enum.js +2 -1
  2. package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +84 -0
  3. package/cjs/implementation/adapter-utils/resource-execute.util.js +11 -0
  4. package/cjs/implementation/adapter-utils/resource-prepare.util.js +11 -0
  5. package/cjs/implementation/{adapter/adapter.js → adapter.js} +21 -18
  6. package/cjs/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
  7. package/cjs/implementation/{adapter/http-adapter.js → http-adapter.js} +26 -24
  8. package/cjs/implementation/query-context.js +5 -3
  9. package/cjs/index.js +5 -15
  10. package/cjs/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
  11. package/cjs/interfaces/query.interface.js +10 -10
  12. package/cjs/services/json-data-service.js +11 -5
  13. package/cjs/utils/{string-path-to-object-tree.js → path-to-tree.js} +3 -3
  14. package/esm/enums/http-headers.enum.d.ts +2 -1
  15. package/esm/enums/http-headers.enum.js +2 -1
  16. package/esm/implementation/adapter-utils/entity-resource-execute.util.d.ts +3 -0
  17. package/esm/implementation/adapter-utils/entity-resource-execute.util.js +80 -0
  18. package/esm/implementation/adapter-utils/resource-execute.util.d.ts +3 -0
  19. package/esm/implementation/adapter-utils/resource-execute.util.js +7 -0
  20. package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +3 -0
  21. package/esm/implementation/adapter-utils/resource-prepare.util.js +7 -0
  22. package/esm/implementation/{adapter/adapter.d.ts → adapter.d.ts} +5 -5
  23. package/esm/implementation/{adapter/adapter.js → adapter.js} +18 -15
  24. package/esm/implementation/{adapter/express-adapter.d.ts → express-adapter.d.ts} +2 -2
  25. package/esm/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
  26. package/esm/implementation/{adapter/http-adapter.d.ts → http-adapter.d.ts} +6 -7
  27. package/esm/implementation/{adapter/http-adapter.js → http-adapter.js} +25 -23
  28. package/esm/implementation/query-context.d.ts +3 -4
  29. package/esm/implementation/query-context.js +5 -3
  30. package/esm/index.d.ts +5 -15
  31. package/esm/index.js +5 -15
  32. package/esm/{services/entity-resource-controller.d.ts → interfaces/entity-service.interface.d.ts} +0 -0
  33. package/esm/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
  34. package/esm/interfaces/query.interface.d.ts +25 -26
  35. package/esm/interfaces/query.interface.js +10 -10
  36. package/esm/services/json-data-service.d.ts +2 -2
  37. package/esm/services/json-data-service.js +11 -5
  38. package/esm/types.d.ts +1 -8
  39. package/esm/utils/create-i18n.d.ts +1 -1
  40. package/esm/utils/path-to-tree.d.ts +4 -0
  41. package/esm/utils/{string-path-to-object-tree.js → path-to-tree.js} +1 -1
  42. package/package.json +6 -6
  43. package/cjs/constants.js +0 -6
  44. package/cjs/decorators/api-entity-resource.decorator.js +0 -24
  45. package/cjs/decorators/api-resolver.decorator.js +0 -13
  46. package/cjs/implementation/data-type/complex-type.js +0 -53
  47. package/cjs/implementation/data-type/data-type.js +0 -47
  48. package/cjs/implementation/data-type/entity-type.js +0 -34
  49. package/cjs/implementation/data-type/simple-type.js +0 -30
  50. package/cjs/implementation/opra-document.js +0 -112
  51. package/cjs/implementation/opra-service.js +0 -81
  52. package/cjs/implementation/resource/container-resource-handler.js +0 -30
  53. package/cjs/implementation/resource/entity-resource-handler.js +0 -103
  54. package/cjs/implementation/resource/resource-handler.js +0 -45
  55. package/cjs/implementation/schema-generator.js +0 -184
  56. package/cjs/interfaces/metadata/api-resolver.metadata.js +0 -2
  57. package/cjs/interfaces/metadata/opra-schema.metadata.js +0 -11
  58. package/cjs/interfaces/resource-container.interface.js +0 -2
  59. package/cjs/utils/class-utils.js +0 -37
  60. package/cjs/utils/headers.js +0 -58
  61. package/cjs/utils/internal-data-types.js +0 -81
  62. package/cjs/utils/responsive-object.js +0 -49
  63. package/cjs/utils/terminal-utils.js +0 -7
  64. package/esm/constants.d.ts +0 -3
  65. package/esm/constants.js +0 -3
  66. package/esm/decorators/api-entity-resource.decorator.d.ts +0 -5
  67. package/esm/decorators/api-entity-resource.decorator.js +0 -19
  68. package/esm/decorators/api-resolver.decorator.d.ts +0 -2
  69. package/esm/decorators/api-resolver.decorator.js +0 -9
  70. package/esm/implementation/data-type/complex-type.d.ts +0 -19
  71. package/esm/implementation/data-type/complex-type.js +0 -48
  72. package/esm/implementation/data-type/data-type.d.ts +0 -17
  73. package/esm/implementation/data-type/data-type.js +0 -43
  74. package/esm/implementation/data-type/entity-type.d.ts +0 -10
  75. package/esm/implementation/data-type/entity-type.js +0 -29
  76. package/esm/implementation/data-type/simple-type.d.ts +0 -15
  77. package/esm/implementation/data-type/simple-type.js +0 -26
  78. package/esm/implementation/opra-document.d.ts +0 -26
  79. package/esm/implementation/opra-document.js +0 -107
  80. package/esm/implementation/opra-service.d.ts +0 -20
  81. package/esm/implementation/opra-service.js +0 -76
  82. package/esm/implementation/resource/container-resource-handler.d.ts +0 -14
  83. package/esm/implementation/resource/container-resource-handler.js +0 -26
  84. package/esm/implementation/resource/entity-resource-handler.d.ts +0 -18
  85. package/esm/implementation/resource/entity-resource-handler.js +0 -98
  86. package/esm/implementation/resource/resource-handler.d.ts +0 -15
  87. package/esm/implementation/resource/resource-handler.js +0 -40
  88. package/esm/implementation/schema-generator.d.ts +0 -21
  89. package/esm/implementation/schema-generator.js +0 -180
  90. package/esm/interfaces/metadata/api-resolver.metadata.d.ts +0 -3
  91. package/esm/interfaces/metadata/api-resolver.metadata.js +0 -1
  92. package/esm/interfaces/metadata/opra-schema.metadata.d.ts +0 -7
  93. package/esm/interfaces/metadata/opra-schema.metadata.js +0 -10
  94. package/esm/interfaces/resource-container.interface.d.ts +0 -6
  95. package/esm/interfaces/resource-container.interface.js +0 -1
  96. package/esm/utils/class-utils.d.ts +0 -6
  97. package/esm/utils/class-utils.js +0 -30
  98. package/esm/utils/headers.d.ts +0 -9
  99. package/esm/utils/headers.js +0 -55
  100. package/esm/utils/internal-data-types.d.ts +0 -5
  101. package/esm/utils/internal-data-types.js +0 -78
  102. package/esm/utils/responsive-object.d.ts +0 -3
  103. package/esm/utils/responsive-object.js +0 -45
  104. package/esm/utils/string-path-to-object-tree.d.ts +0 -4
  105. package/esm/utils/terminal-utils.d.ts +0 -4
  106. package/esm/utils/terminal-utils.js +0 -4
@@ -1,48 +0,0 @@
1
- import merge from 'putil-merge';
2
- import { Responsive } from '../../utils/responsive-object.js';
3
- import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
4
- import { DataType } from './data-type.js';
5
- export class ComplexType extends DataType {
6
- ownProperties;
7
- properties;
8
- constructor(owner, args, base) {
9
- super(owner, {
10
- kind: 'ComplexType',
11
- ...args
12
- }, base);
13
- this.ownProperties = args?.properties && Responsive(args.properties);
14
- this.properties = (base?.properties || this.ownProperties) &&
15
- Responsive({ ...base?.properties, ...this.ownProperties });
16
- }
17
- get abstract() {
18
- return !!this._args.abstract;
19
- }
20
- get additionalProperties() {
21
- return this._args.additionalProperties;
22
- }
23
- getProperty(name) {
24
- const t = this.properties?.[name];
25
- if (!t)
26
- throw new Error(`"${this.name}" type has no property named "${name}"`);
27
- return t;
28
- }
29
- getMetadata() {
30
- const out = super.getMetadata();
31
- if (this.additionalProperties)
32
- out.additionalProperties = this.additionalProperties;
33
- if (this.ownProperties) {
34
- out.properties = {};
35
- for (const [k, prop] of Object.entries(this.ownProperties)) {
36
- out.properties[k] = merge({}, prop, { deep: true });
37
- }
38
- }
39
- return out;
40
- }
41
- toString() {
42
- return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
43
- }
44
- [nodeInspectCustom]() {
45
- return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
46
- ` ${colorFgMagenta + this.name + colorReset}]`;
47
- }
48
- }
@@ -1,17 +0,0 @@
1
- import { Type } from 'ts-gems';
2
- import { OpraSchema } from '@opra/schema';
3
- import type { OpraDocument } from '../opra-document.js';
4
- export declare abstract class DataType {
5
- protected _owner: OpraDocument;
6
- protected readonly _args: any;
7
- readonly base?: DataType;
8
- protected constructor(owner: OpraDocument, args: OpraSchema.DataType, base?: DataType);
9
- get owner(): OpraDocument;
10
- get kind(): OpraSchema.DataTypeKind;
11
- get name(): string;
12
- get description(): string | undefined;
13
- get abstract(): boolean;
14
- get ctor(): Type;
15
- is(typeName: string): boolean;
16
- getMetadata(): any;
17
- }
@@ -1,43 +0,0 @@
1
- export class DataType {
2
- _owner;
3
- _args;
4
- base;
5
- constructor(owner, args, base) {
6
- this._args = { ...args };
7
- this._owner = owner;
8
- if (base) {
9
- this.base = base;
10
- Object.setPrototypeOf(this._args, base._args);
11
- }
12
- }
13
- get owner() {
14
- return this._owner;
15
- }
16
- get kind() {
17
- return this._args.kind;
18
- }
19
- get name() {
20
- return this._args.name;
21
- }
22
- get description() {
23
- return this._args.description;
24
- }
25
- get abstract() {
26
- return !!this._args.abstract;
27
- }
28
- get ctor() {
29
- return this._args.ctor;
30
- }
31
- is(typeName) {
32
- return this.name === typeName || !!(this.base && this.base.is(typeName));
33
- }
34
- getMetadata() {
35
- return {
36
- kind: this.kind,
37
- base: this.base?.name,
38
- abstract: this.abstract ? true : undefined,
39
- name: this.name,
40
- description: this.description,
41
- };
42
- }
43
- }
@@ -1,10 +0,0 @@
1
- import { StrictOmit } from 'ts-gems';
2
- import { OpraSchema } from '@opra/schema';
3
- import { OpraDocument } from '../opra-document.js';
4
- import { ComplexType } from './complex-type.js';
5
- export declare type EntityTypeArgs = StrictOmit<OpraSchema.EntityType, 'kind'>;
6
- export declare class EntityType extends ComplexType {
7
- protected readonly _args: StrictOmit<EntityTypeArgs, 'properties'>;
8
- constructor(owner: OpraDocument, args: EntityTypeArgs, base?: ComplexType | EntityType);
9
- get primaryKey(): string;
10
- }
@@ -1,29 +0,0 @@
1
- import * as Optionals from '@opra/optionals';
2
- import { ComplexType } from './complex-type.js';
3
- export class EntityType extends ComplexType {
4
- constructor(owner, args, base) {
5
- super(owner, {
6
- ...args
7
- }, base);
8
- this._args.kind = 'EntityType';
9
- // Try to determine primary key info from SQB
10
- if (Optionals.SqbConnect && args.ctor) {
11
- const sqbEntity = Optionals.SqbConnect.EntityMetadata.get(args.ctor);
12
- if (sqbEntity?.indexes) {
13
- const primaryIndex = sqbEntity.indexes.find(x => x.primary);
14
- if (primaryIndex) {
15
- if (primaryIndex.columns.length > 1)
16
- throw new TypeError(`Multi-key indexes is not implemented yet`);
17
- this._args.primaryKey = primaryIndex.columns[0];
18
- }
19
- }
20
- }
21
- if (!this.primaryKey)
22
- throw new TypeError(`You must provide primaryKey fo "${this.name}" entity`);
23
- if (!this.getProperty(this.primaryKey))
24
- throw new TypeError(`"${this.name}" entity has no such property named "${this.primaryKey}" which defined as primary key`);
25
- }
26
- get primaryKey() {
27
- return this._args.primaryKey;
28
- }
29
- }
@@ -1,15 +0,0 @@
1
- import { StrictOmit } from 'ts-gems';
2
- import { OpraSchema } from '@opra/schema';
3
- import { nodeInspectCustom } from '../../utils/terminal-utils.js';
4
- import type { OpraDocument } from '../opra-document.js';
5
- import { DataType } from './data-type.js';
6
- export declare class SimpleType extends DataType {
7
- protected readonly _args: OpraSchema.SimpleType;
8
- readonly base?: SimpleType;
9
- constructor(owner: OpraDocument, args: StrictOmit<OpraSchema.SimpleType, 'kind'>, base?: SimpleType);
10
- get type(): 'boolean' | 'number' | 'integer' | 'string';
11
- get format(): string | undefined;
12
- get default(): boolean | number | string | undefined;
13
- toString(): string;
14
- [nodeInspectCustom](): string;
15
- }
@@ -1,26 +0,0 @@
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
- }
@@ -1,26 +0,0 @@
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
- }
@@ -1,107 +0,0 @@
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
- baseType = this.types[schema.base];
74
- if (!baseType) {
75
- const baseSchema = dataTypes.find(dt => dt.name.toLowerCase() === schema.base?.toLowerCase()) ||
76
- internalDataTypes.get(schema.base.toLowerCase());
77
- if (!baseSchema)
78
- throw new TypeError(`Base schema (${schema.base}) of data type "${schema.name}" does not exists`);
79
- baseType = processDataType(baseSchema);
80
- }
81
- }
82
- let dataType;
83
- if (OpraSchema.isSimpleType(schema)) {
84
- if (baseType && !(baseType instanceof SimpleType))
85
- throw new TypeError(`Can't extend a SimpleType (${schema.name}) from a ComplexType "${baseType.name}"`);
86
- dataType = new SimpleType(this, schema, baseType);
87
- }
88
- else if (OpraSchema.isComplexType(schema)) {
89
- if (baseType && !(baseType instanceof ComplexType))
90
- throw new TypeError(`Can't extend a ComplexType (${schema.name}) from a SimpleType "${baseType.name}"`);
91
- dataType = new ComplexType(this, schema, baseType);
92
- }
93
- else if (OpraSchema.isEntityType(schema)) {
94
- if (baseType && !(baseType instanceof ComplexType))
95
- throw new TypeError(`Can't extend an EntityType (${schema.name}) from a SimpleType "${baseType.name}"`);
96
- dataType = new EntityType(this, schema, baseType);
97
- }
98
- else
99
- throw new TypeError(`Invalid data type schema`);
100
- nameSet.delete(schema.name);
101
- this.types[dataType.name] = dataType;
102
- recursiveSet.delete(schema.name);
103
- return dataType;
104
- };
105
- dataTypes.forEach(dataType => processDataType(dataType));
106
- }
107
- }
@@ -1,20 +0,0 @@
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 { EntityResourceHandler } from './resource/entity-resource-handler.js';
6
- import { ResourceHandler } from './resource/resource-handler.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<ResourceHandler>;
12
- constructor(schema: OpraSchema.Service);
13
- get resources(): Record<string, ResourceHandler>;
14
- get servers(): OpraSchema.ServerInfo[] | undefined;
15
- getResource<T extends ResourceHandler>(name: string): T;
16
- getEntityResource(name: string): EntityResourceHandler;
17
- getMetadata(): OpraSchema.ServiceMetadata;
18
- protected _addResources(resources: OpraSchema.Resource[]): void;
19
- static create(args: SchemaGenerator.GenerateServiceArgs): Promise<OpraService>;
20
- }
@@ -1,76 +0,0 @@
1
- import merge from 'putil-merge';
2
- import { OpraSchema } from '@opra/schema';
3
- import { OpraVersion } from '../constants.js';
4
- import { internalDataTypes } from '../utils/internal-data-types.js';
5
- import { Responsive } from '../utils/responsive-object.js';
6
- import { EntityType } from './data-type/entity-type.js';
7
- import { OpraDocument } from './opra-document.js';
8
- import { EntityResourceHandler } from './resource/entity-resource-handler.js';
9
- import { SchemaGenerator } from './schema-generator.js';
10
- export class OpraService extends OpraDocument {
11
- _resources = Responsive();
12
- constructor(schema) {
13
- super(schema);
14
- if (schema.resources)
15
- this._addResources(schema.resources);
16
- }
17
- get resources() {
18
- return this._resources;
19
- }
20
- get servers() {
21
- return this._args.servers;
22
- }
23
- getResource(name) {
24
- const t = this.resources[name];
25
- if (!t)
26
- throw new Error(`Resource "${name}" does not exists`);
27
- return t;
28
- }
29
- getEntityResource(name) {
30
- const t = this.getResource(name);
31
- if (!(t instanceof EntityResourceHandler))
32
- throw new Error(`"${name}" is not an EntityResource`);
33
- return t;
34
- }
35
- getMetadata() {
36
- const out = {
37
- '@opra:metadata': '/$metadata',
38
- version: OpraVersion,
39
- servers: this.servers?.map(x => merge({}, x, { deep: true })),
40
- info: merge({}, this.info, { deep: true }),
41
- types: [],
42
- resources: []
43
- };
44
- for (const [k, dataType] of Object.entries(this.types)) {
45
- if (!internalDataTypes.has(k))
46
- out.types.push(dataType.getMetadata());
47
- }
48
- for (const resource of Object.values(this.resources)) {
49
- out.resources.push(resource.getMetadata());
50
- }
51
- return out;
52
- }
53
- _addResources(resources) {
54
- for (const r of resources) {
55
- if (OpraSchema.isEntityResource(r)) {
56
- const dataType = this.getDataType(r.type);
57
- if (!dataType)
58
- throw new TypeError(`Datatype "${r.type}" declared in EntityResource (${r.name}) does not exists`);
59
- if (!(dataType instanceof EntityType))
60
- throw new TypeError(`${r.type} is not an EntityType`);
61
- this.resources[r.name] = new EntityResourceHandler({ ...r, service: this, dataType });
62
- }
63
- else
64
- throw new TypeError(`Unknown resource kind (${r.kind})`);
65
- }
66
- // Sort data types by name
67
- const newResources = Responsive();
68
- Object.keys(this.resources).sort()
69
- .forEach(name => newResources[name] = this.resources[name]);
70
- this._resources = newResources;
71
- }
72
- static async create(args) {
73
- const schema = await SchemaGenerator.generateServiceSchema(args);
74
- return new OpraService(schema);
75
- }
76
- }
@@ -1,14 +0,0 @@
1
- import { StrictOmit } from 'ts-gems';
2
- import { OpraSchema } from '@opra/schema';
3
- import { IResourceContainer } from '../../interfaces/resource-container.interface.js';
4
- import { QueryContext } from '../query-context.js';
5
- import { EntityResourceHandler } from './entity-resource-handler.js';
6
- import { ResourceHandler } from './resource-handler.js';
7
- export declare type ContainerResourceControllerArgs = StrictOmit<OpraSchema.ContainerResource, 'kind'> & {};
8
- export declare class ContainerResourceHandler extends ResourceHandler implements IResourceContainer {
9
- protected readonly _args: OpraSchema.ContainerResource;
10
- constructor(args: ContainerResourceControllerArgs);
11
- getResource<T extends ResourceHandler>(name: string): T;
12
- getEntityResource(name: string): EntityResourceHandler;
13
- execute(ctx: QueryContext): Promise<void>;
14
- }
@@ -1,26 +0,0 @@
1
- import { EntityResourceHandler } from './entity-resource-handler.js';
2
- import { ResourceHandler } from './resource-handler.js';
3
- export class ContainerResourceHandler extends ResourceHandler {
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 EntityResourceHandler))
19
- throw new Error(`"${name}" is not an EntityResource`);
20
- return t;
21
- }
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- execute(ctx) {
24
- return Promise.resolve(undefined);
25
- }
26
- }
@@ -1,18 +0,0 @@
1
- import { StrictOmit } from 'ts-gems';
2
- import { OpraSchema } from '@opra/schema';
3
- import { EntityType } from '../data-type/entity-type.js';
4
- import { OpraService } from '../opra-service.js';
5
- import { QueryContext } from '../query-context.js';
6
- import { ResourceHandler } from './resource-handler.js';
7
- export declare type EntityResourceControllerArgs = StrictOmit<OpraSchema.EntityResource, 'kind'> & {
8
- service: OpraService;
9
- dataType: EntityType;
10
- };
11
- export declare class EntityResourceHandler extends ResourceHandler {
12
- protected readonly _args: OpraSchema.EntityResource;
13
- readonly service: OpraService;
14
- readonly dataType: EntityType;
15
- constructor(args: EntityResourceControllerArgs);
16
- execute(ctx: QueryContext): Promise<void>;
17
- _executeFn(ctx: QueryContext, queryType: string): Promise<any>;
18
- }
@@ -1,98 +0,0 @@
1
- import _ from 'lodash';
2
- import { translate } from '@opra/i18n';
3
- import { ForbiddenError, ResourceNotFoundError } from '../../exception/index.js';
4
- import { OpraQuery } from '../../interfaces/query.interface.js';
5
- import { ComplexType } from '../data-type/complex-type.js';
6
- import { EntityType } from '../data-type/entity-type.js';
7
- import { ResourceHandler } from './resource-handler.js';
8
- export class EntityResourceHandler extends ResourceHandler {
9
- service;
10
- dataType;
11
- constructor(args) {
12
- super({
13
- kind: 'EntityResource',
14
- ..._.omit(args, ['dataType', 'service'])
15
- });
16
- this.dataType = args.dataType;
17
- this.service = args.service;
18
- // noinspection SuspiciousTypeOfGuard
19
- if (!(args.dataType instanceof EntityType))
20
- throw new TypeError(`You should provide an EntityType for EntityResourceController`);
21
- }
22
- async execute(ctx) {
23
- const { query } = ctx;
24
- if (OpraQuery.isSearchQuery(query)) {
25
- const promises = [];
26
- let search;
27
- let count;
28
- promises.push(this._executeFn(ctx, query.queryType)
29
- .then(v => search = v));
30
- if (query.count) {
31
- promises.push(this._executeFn(ctx, 'count')
32
- .then(v => count = v));
33
- }
34
- await Promise.all(promises);
35
- ctx.response.value = {
36
- ...search,
37
- ...count
38
- };
39
- return;
40
- }
41
- ctx.response.value = await this._executeFn(ctx, query.queryType);
42
- }
43
- async _executeFn(ctx, queryType) {
44
- const resolverInfo = this._args.resolvers?.[queryType];
45
- if (resolverInfo.forbidden || !resolverInfo.handler)
46
- throw new ForbiddenError({
47
- message: translate('RESOLVER_FORBIDDEN', { queryType }),
48
- severity: 'error',
49
- code: 'RESOLVER_FORBIDDEN'
50
- });
51
- let result = await resolverInfo.handler(ctx);
52
- switch (queryType) {
53
- case 'search':
54
- return {
55
- '@opra:metadata': '/$metadata/types/' + this.dataType.name,
56
- items: Array.isArray(result) ? result : (ctx.response.value ? [result] : [])
57
- };
58
- case 'get':
59
- case 'update':
60
- if (!result) {
61
- const query = ctx.query;
62
- throw new ResourceNotFoundError(this.name, query.keyValue);
63
- }
64
- break;
65
- case 'count':
66
- return { count: result || 0 };
67
- case 'delete':
68
- case 'deleteMany':
69
- case 'updateMany':
70
- let affectedRecords;
71
- if (typeof result === 'number')
72
- affectedRecords = result;
73
- if (typeof result === 'boolean')
74
- affectedRecords = result ? 1 : 0;
75
- if (typeof result === 'object')
76
- affectedRecords = result.affectedRows || result.affectedRecords;
77
- return { affectedRecords };
78
- }
79
- if (!result)
80
- return;
81
- result = Array.isArray(result) ? result[0] : result;
82
- let dataType = this.dataType;
83
- if (ctx.resultPath) {
84
- const pathArray = ctx.resultPath.split('.');
85
- for (const property of pathArray) {
86
- const prop = dataType instanceof ComplexType ? dataType.properties?.[property] : undefined;
87
- dataType = prop && prop.type ? this.service.types[prop.type || 'string'] : undefined;
88
- result = result && typeof result === 'object' && result[property];
89
- }
90
- }
91
- if (queryType === 'create')
92
- ctx.response.status = 201;
93
- return {
94
- '@opra:metadata': dataType ? '/$metadata/types/' + dataType.name : '__unknown__',
95
- ...result
96
- };
97
- }
98
- }
@@ -1,15 +0,0 @@
1
- import { OpraSchema } from '@opra/schema';
2
- import { nodeInspectCustom } from '../../utils/terminal-utils.js';
3
- import { QueryContext } from '../query-context.js';
4
- export declare abstract class ResourceHandler {
5
- protected readonly _args: OpraSchema.Resource;
6
- protected path: string;
7
- protected constructor(args: OpraSchema.Resource);
8
- get name(): string;
9
- get description(): string | undefined;
10
- toString(): string;
11
- prepare(ctx: QueryContext): Promise<void>;
12
- getMetadata(): any;
13
- abstract execute(ctx: QueryContext): Promise<void>;
14
- [nodeInspectCustom](): string;
15
- }
@@ -1,40 +0,0 @@
1
- import merge from 'putil-merge';
2
- import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
3
- export class ResourceHandler {
4
- _args;
5
- path;
6
- constructor(args) {
7
- this._args = args;
8
- }
9
- get name() {
10
- return this._args.name;
11
- }
12
- get description() {
13
- return this._args.description;
14
- }
15
- toString() {
16
- return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
17
- }
18
- async prepare(ctx) {
19
- const { query } = ctx;
20
- const fn = this._args['pre_' + query.queryType];
21
- if (fn && typeof fn === 'function') {
22
- await fn(ctx);
23
- }
24
- }
25
- getMetadata() {
26
- return merge({}, {
27
- ...this._args,
28
- instance: undefined,
29
- }, {
30
- deep: true,
31
- filter: (source, key) => {
32
- return (key !== 'instance' && typeof source[key] !== 'function' && source[key] != null);
33
- }
34
- });
35
- }
36
- [nodeInspectCustom]() {
37
- return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
38
- ` ${colorFgMagenta + this.name + colorReset}]`;
39
- }
40
- }
@@ -1,21 +0,0 @@
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
- }