@opra/core 0.0.12 → 0.1.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 (117) hide show
  1. package/cjs/enums/http-headers.enum.js +2 -1
  2. package/cjs/exception/http-errors/not-acceptable.error.js +26 -0
  3. package/cjs/exception/index.js +2 -0
  4. package/cjs/exception/resource-errors/resource-not-found.error.js +19 -0
  5. package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +84 -0
  6. package/cjs/implementation/adapter-utils/resource-execute.util.js +11 -0
  7. package/cjs/implementation/adapter-utils/resource-prepare.util.js +11 -0
  8. package/cjs/implementation/{adapter/adapter.js → adapter.js} +40 -7
  9. package/cjs/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
  10. package/cjs/implementation/http-adapter.js +267 -0
  11. package/cjs/implementation/query-context.js +5 -3
  12. package/cjs/index.js +5 -15
  13. package/cjs/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
  14. package/cjs/interfaces/query.interface.js +26 -9
  15. package/cjs/services/json-data-service.js +241 -4
  16. package/cjs/utils/create-i18n.js +1 -1
  17. package/cjs/utils/get-caller-file.util.js +6 -1
  18. package/cjs/utils/{string-path-to-object-tree.js → path-to-tree.js} +3 -3
  19. package/esm/enums/http-headers.enum.d.ts +2 -1
  20. package/esm/enums/http-headers.enum.js +2 -1
  21. package/esm/exception/http-errors/not-acceptable.error.d.ts +10 -0
  22. package/esm/exception/http-errors/not-acceptable.error.js +22 -0
  23. package/esm/exception/index.d.ts +2 -0
  24. package/esm/exception/index.js +2 -0
  25. package/esm/exception/resource-errors/resource-not-found.error.d.ts +4 -0
  26. package/esm/exception/resource-errors/resource-not-found.error.js +15 -0
  27. package/esm/implementation/adapter-utils/entity-resource-execute.util.d.ts +3 -0
  28. package/esm/implementation/adapter-utils/entity-resource-execute.util.js +80 -0
  29. package/esm/implementation/adapter-utils/resource-execute.util.d.ts +3 -0
  30. package/esm/implementation/adapter-utils/resource-execute.util.js +7 -0
  31. package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +3 -0
  32. package/esm/implementation/adapter-utils/resource-prepare.util.js +7 -0
  33. package/esm/implementation/{adapter/adapter.d.ts → adapter.d.ts} +5 -9
  34. package/esm/implementation/{adapter/adapter.js → adapter.js} +39 -6
  35. package/esm/implementation/{adapter/express-adapter.d.ts → express-adapter.d.ts} +2 -2
  36. package/esm/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
  37. package/esm/implementation/{adapter/http-adapter.d.ts → http-adapter.d.ts} +6 -6
  38. package/esm/implementation/http-adapter.js +263 -0
  39. package/esm/implementation/query-context.d.ts +3 -4
  40. package/esm/implementation/query-context.js +5 -3
  41. package/esm/index.d.ts +5 -15
  42. package/esm/index.js +5 -15
  43. package/esm/{services/entity-resource-controller.d.ts → interfaces/entity-service.interface.d.ts} +0 -0
  44. package/esm/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
  45. package/esm/interfaces/query.interface.d.ts +31 -25
  46. package/esm/interfaces/query.interface.js +26 -9
  47. package/esm/services/json-data-service.d.ts +66 -7
  48. package/esm/services/json-data-service.js +241 -4
  49. package/esm/types.d.ts +10 -8
  50. package/esm/utils/create-i18n.d.ts +1 -1
  51. package/esm/utils/create-i18n.js +1 -1
  52. package/esm/utils/get-caller-file.util.d.ts +1 -1
  53. package/esm/utils/get-caller-file.util.js +6 -1
  54. package/esm/utils/path-to-tree.d.ts +4 -0
  55. package/esm/utils/{string-path-to-object-tree.js → path-to-tree.js} +1 -1
  56. package/i18n/en/error.json +3 -0
  57. package/package.json +6 -7
  58. package/cjs/constants.js +0 -5
  59. package/cjs/decorators/entity-resource.decorator.js +0 -24
  60. package/cjs/implementation/adapter/http-adapter.js +0 -238
  61. package/cjs/implementation/data-type/complex-type.js +0 -39
  62. package/cjs/implementation/data-type/data-type.js +0 -35
  63. package/cjs/implementation/data-type/entity-type.js +0 -33
  64. package/cjs/implementation/data-type/simple-type.js +0 -30
  65. package/cjs/implementation/opra-document.js +0 -116
  66. package/cjs/implementation/opra-service.js +0 -59
  67. package/cjs/implementation/resource/container-resource-handler.js +0 -30
  68. package/cjs/implementation/resource/entity-resource-handler.js +0 -80
  69. package/cjs/implementation/resource/resource-handler.js +0 -31
  70. package/cjs/implementation/schema-generator.js +0 -163
  71. package/cjs/interfaces/opra-schema.metadata.js +0 -2
  72. package/cjs/interfaces/resource-container.interface.js +0 -2
  73. package/cjs/utils/class-utils.js +0 -37
  74. package/cjs/utils/headers.js +0 -58
  75. package/cjs/utils/internal-data-types.js +0 -81
  76. package/cjs/utils/responsive-object.js +0 -49
  77. package/cjs/utils/terminal-utils.js +0 -7
  78. package/esm/constants.d.ts +0 -2
  79. package/esm/constants.js +0 -2
  80. package/esm/decorators/entity-resource.decorator.d.ts +0 -5
  81. package/esm/decorators/entity-resource.decorator.js +0 -19
  82. package/esm/implementation/adapter/http-adapter.js +0 -234
  83. package/esm/implementation/data-type/complex-type.d.ts +0 -18
  84. package/esm/implementation/data-type/complex-type.js +0 -35
  85. package/esm/implementation/data-type/data-type.d.ts +0 -15
  86. package/esm/implementation/data-type/data-type.js +0 -31
  87. package/esm/implementation/data-type/entity-type.d.ts +0 -10
  88. package/esm/implementation/data-type/entity-type.js +0 -29
  89. package/esm/implementation/data-type/simple-type.d.ts +0 -15
  90. package/esm/implementation/data-type/simple-type.js +0 -26
  91. package/esm/implementation/opra-document.d.ts +0 -26
  92. package/esm/implementation/opra-document.js +0 -111
  93. package/esm/implementation/opra-service.d.ts +0 -19
  94. package/esm/implementation/opra-service.js +0 -55
  95. package/esm/implementation/resource/container-resource-handler.d.ts +0 -14
  96. package/esm/implementation/resource/container-resource-handler.js +0 -26
  97. package/esm/implementation/resource/entity-resource-handler.d.ts +0 -18
  98. package/esm/implementation/resource/entity-resource-handler.js +0 -75
  99. package/esm/implementation/resource/resource-handler.d.ts +0 -15
  100. package/esm/implementation/resource/resource-handler.js +0 -27
  101. package/esm/implementation/schema-generator.d.ts +0 -21
  102. package/esm/implementation/schema-generator.js +0 -159
  103. package/esm/interfaces/opra-schema.metadata.d.ts +0 -14
  104. package/esm/interfaces/opra-schema.metadata.js +0 -1
  105. package/esm/interfaces/resource-container.interface.d.ts +0 -6
  106. package/esm/interfaces/resource-container.interface.js +0 -1
  107. package/esm/utils/class-utils.d.ts +0 -6
  108. package/esm/utils/class-utils.js +0 -30
  109. package/esm/utils/headers.d.ts +0 -9
  110. package/esm/utils/headers.js +0 -55
  111. package/esm/utils/internal-data-types.d.ts +0 -5
  112. package/esm/utils/internal-data-types.js +0 -78
  113. package/esm/utils/responsive-object.d.ts +0 -3
  114. package/esm/utils/responsive-object.js +0 -45
  115. package/esm/utils/string-path-to-object-tree.d.ts +0 -4
  116. package/esm/utils/terminal-utils.d.ts +0 -4
  117. package/esm/utils/terminal-utils.js +0 -4
@@ -1,234 +0,0 @@
1
- import { OpraURL } from '@opra/url';
2
- import { OpraVersion } from '../../constants.js';
3
- import { HttpHeaders, HttpStatus } from '../../enums/index.js';
4
- import { ApiException, BadRequestError, InternalServerError, MethodNotAllowedError, NotFoundError, } from '../../exception/index.js';
5
- import { wrapError } from '../../exception/wrap-error.js';
6
- import { OpraQuery } from '../../interfaces/query.interface.js';
7
- import { Headers } from '../../utils/headers.js';
8
- import { ComplexType } from '../data-type/complex-type.js';
9
- import { QueryContext } from '../query-context.js';
10
- import { ContainerResourceHandler } from '../resource/container-resource-handler.js';
11
- import { EntityResourceHandler } from '../resource/entity-resource-handler.js';
12
- import { OpraAdapter } from './adapter.js';
13
- export class OpraHttpAdapter extends OpraAdapter {
14
- prepareRequests(executionContext) {
15
- const req = executionContext.getRequestWrapper();
16
- // todo implement batch requests
17
- if (this.isBatch(executionContext)) {
18
- throw new Error('not implemented yet');
19
- }
20
- const url = new OpraURL(req.getUrl());
21
- return [
22
- this.prepareRequest(executionContext, url, req.getMethod(), Headers.from(req.getHeaders()), req.getBody())
23
- ];
24
- }
25
- prepareRequest(executionContext, url, method, headers, body) {
26
- if (!url.path.size)
27
- throw new BadRequestError();
28
- if (method !== 'GET' && url.path.size > 1)
29
- throw new BadRequestError();
30
- const query = this.buildQuery(url, method, body);
31
- if (!query)
32
- throw new MethodNotAllowedError({
33
- message: `Method "${method}" is not allowed by target resource`
34
- });
35
- return new QueryContext({
36
- service: this.service,
37
- executionContext,
38
- query,
39
- headers,
40
- params: url.searchParams,
41
- continueOnError: query.operation === 'read'
42
- });
43
- }
44
- buildQuery(url, method, body) {
45
- let container = this.service;
46
- try {
47
- let pathIndex = 0;
48
- const pathLen = url.path.size;
49
- while (pathIndex < pathLen) {
50
- let p = url.path.get(pathIndex++);
51
- const resource = container.getResource(p.resource);
52
- // Move through path directories (containers)
53
- if (resource instanceof ContainerResourceHandler) {
54
- container = resource;
55
- }
56
- else {
57
- method = method.toUpperCase();
58
- if (resource instanceof EntityResourceHandler) {
59
- const scope = p.key ? 'instance' : 'collection';
60
- if (pathIndex < pathLen && !(method === 'GET' && scope === 'instance'))
61
- return;
62
- let query;
63
- switch (method) {
64
- case 'GET': {
65
- if (scope === 'collection') {
66
- query = OpraQuery.forSearch(resource, {
67
- filter: url.searchParams.get('$filter'),
68
- limit: url.searchParams.get('$limit'),
69
- skip: url.searchParams.get('$skip'),
70
- distinct: url.searchParams.get('$distinct'),
71
- count: url.searchParams.get('$count'),
72
- sort: url.searchParams.get('$sort'),
73
- pick: url.searchParams.get('$pick'),
74
- omit: url.searchParams.get('$omit'),
75
- include: url.searchParams.get('$include'),
76
- });
77
- }
78
- else {
79
- query = OpraQuery.forGet(resource, p.key, {
80
- pick: url.searchParams.get('$pick'),
81
- omit: url.searchParams.get('$omit'),
82
- include: url.searchParams.get('$include')
83
- });
84
- // Move through properties
85
- let nested;
86
- let path = resource.name;
87
- while (pathIndex < pathLen) {
88
- const dataType = nested
89
- ? this.service.getDataType(nested.property.type || 'string')
90
- : query.resource.dataType;
91
- if (!(dataType instanceof ComplexType))
92
- throw new Error(`"${path}" is not a ComplexType and has no properties.`);
93
- p = url.path.get(pathIndex++);
94
- path += '.' + p.resource;
95
- const prop = dataType.properties?.[p.resource];
96
- if (!prop)
97
- throw new NotFoundError({ message: `Invalid or unknown resource path (${path})` });
98
- const q = OpraQuery.forGetProperty(prop);
99
- if (nested) {
100
- nested.nested = q;
101
- }
102
- else {
103
- query.nested = q;
104
- }
105
- nested = q;
106
- }
107
- }
108
- break;
109
- }
110
- case 'DELETE': {
111
- if (scope === 'collection') {
112
- query = OpraQuery.forDeleteMany(resource, {
113
- filter: url.searchParams.get('$filter'),
114
- });
115
- }
116
- else {
117
- query = OpraQuery.forDelete(resource, p.key);
118
- }
119
- break;
120
- }
121
- case 'POST': {
122
- if (scope === 'collection') {
123
- query = OpraQuery.forCreate(resource, body, {
124
- pick: url.searchParams.get('$pick'),
125
- omit: url.searchParams.get('$omit'),
126
- include: url.searchParams.get('$include')
127
- });
128
- }
129
- break;
130
- }
131
- case 'PATCH': {
132
- if (scope === 'collection') {
133
- query = OpraQuery.forUpdateMany(resource, body, {
134
- filter: url.searchParams.get('$filter')
135
- });
136
- }
137
- else {
138
- query = OpraQuery.forUpdate(resource, p.key, body, {
139
- pick: url.searchParams.get('$pick'),
140
- omit: url.searchParams.get('$omit'),
141
- include: url.searchParams.get('$include')
142
- });
143
- }
144
- break;
145
- }
146
- }
147
- return query;
148
- }
149
- }
150
- }
151
- throw new InternalServerError();
152
- }
153
- catch (e) {
154
- if (e instanceof ApiException)
155
- throw e;
156
- throw new BadRequestError({ message: e.message });
157
- }
158
- }
159
- async sendResponse(executionContext, queryContexts) {
160
- const outputPackets = [];
161
- for (const ctx of queryContexts) {
162
- const v = this.createOutput(ctx);
163
- outputPackets.push(v);
164
- }
165
- if (this.isBatch(executionContext)) {
166
- // this.writeError([], new InternalServerError({message: 'Not implemented yet'}));
167
- return;
168
- }
169
- if (!outputPackets.length) {
170
- const err = new NotFoundError();
171
- outputPackets.push({
172
- status: err.status,
173
- body: {
174
- errors: [err.response]
175
- }
176
- });
177
- }
178
- const out = outputPackets[0];
179
- const resp = executionContext.getResponseWrapper();
180
- resp.setStatus(out.status);
181
- resp.setHeader(HttpHeaders.Content_Type, 'application/json');
182
- resp.setHeader(HttpHeaders.Cache_Control, 'no-cache');
183
- resp.setHeader(HttpHeaders.Pragma, 'no-cache');
184
- resp.setHeader(HttpHeaders.Expires, '-1');
185
- resp.setHeader(HttpHeaders.X_Opra_Version, OpraVersion);
186
- if (out.headers) {
187
- for (const [k, v] of Object.entries(out.headers)) {
188
- resp.setHeader(k, v);
189
- }
190
- }
191
- resp.send(JSON.stringify(out.body));
192
- }
193
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
194
- isBatch(executionContext) {
195
- return false;
196
- }
197
- createOutput(ctx) {
198
- const { query } = ctx;
199
- let status = ctx.response.status;
200
- let body = ctx.response.value || {};
201
- const errors = ctx.response.errors?.map(e => wrapError(e));
202
- if (errors && errors.length) {
203
- if (!status || status < 400) {
204
- status = 0;
205
- for (const e of errors) {
206
- status = Math.max(status, e.status || status);
207
- }
208
- if (status < HttpStatus.BAD_REQUEST)
209
- status = HttpStatus.INTERNAL_SERVER_ERROR;
210
- }
211
- body.errors = errors.map(e => e.response);
212
- }
213
- else {
214
- delete body.errors;
215
- status = status || (query.operation === 'create' ? HttpStatus.CREATED : HttpStatus.OK);
216
- }
217
- body = this.i18n.deep(body);
218
- return {
219
- status,
220
- headers: ctx.response.headers,
221
- body
222
- };
223
- }
224
- async sendError(executionContext, error) {
225
- const resp = executionContext.getResponseWrapper();
226
- resp.setStatus(error.status || 500);
227
- resp.setHeader(HttpHeaders.Content_Type, 'application/json');
228
- resp.setHeader(HttpHeaders.Cache_Control, 'no-cache');
229
- resp.setHeader(HttpHeaders.Pragma, 'no-cache');
230
- resp.setHeader(HttpHeaders.Expires, '-1');
231
- resp.setHeader(HttpHeaders.X_Opra_Version, OpraVersion);
232
- resp.send(JSON.stringify(error.response));
233
- }
234
- }
@@ -1,18 +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 type { OpraDocument } from '../opra-document.js';
6
- import { DataType } from './data-type.js';
7
- export declare type ComplexTypeArgs = StrictOmit<OpraSchema.ComplexType, 'kind'>;
8
- export declare class ComplexType extends DataType {
9
- protected readonly _args: StrictOmit<ComplexTypeArgs, 'properties'>;
10
- readonly ownProperties?: ResponsiveObject<OpraSchema.Property>;
11
- readonly properties?: ResponsiveObject<OpraSchema.Property>;
12
- constructor(owner: OpraDocument, args: ComplexTypeArgs, base?: ComplexType);
13
- get abstract(): boolean;
14
- get additionalProperties(): boolean | string | Pick<OpraSchema.Property, 'type' | 'format' | 'isArray' | 'enum'> | undefined;
15
- getProperty(name: string): OpraSchema.Property;
16
- toString(): string;
17
- [nodeInspectCustom](): string;
18
- }
@@ -1,35 +0,0 @@
1
- import { Responsive } from '../../utils/responsive-object.js';
2
- import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
3
- import { DataType } from './data-type.js';
4
- export class ComplexType extends DataType {
5
- ownProperties;
6
- properties;
7
- constructor(owner, args, base) {
8
- super(owner, {
9
- kind: 'ComplexType',
10
- ...args
11
- }, base);
12
- this.ownProperties = args?.properties && Responsive(args.properties);
13
- this.properties = (base?.properties || this.ownProperties) &&
14
- Responsive({ ...base?.properties, ...this.ownProperties });
15
- }
16
- get abstract() {
17
- return !!this._args.abstract;
18
- }
19
- get additionalProperties() {
20
- return this._args.additionalProperties;
21
- }
22
- getProperty(name) {
23
- const t = this.properties?.[name];
24
- if (!t)
25
- throw new Error(`"${this.name}" type has no property named "${name}"`);
26
- return t;
27
- }
28
- toString() {
29
- return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
30
- }
31
- [nodeInspectCustom]() {
32
- return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
33
- ` ${colorFgMagenta + this.name + colorReset}]`;
34
- }
35
- }
@@ -1,15 +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 ctor(): Type;
14
- is(typeName: string): boolean;
15
- }
@@ -1,31 +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 ctor() {
26
- return this._args.ctor;
27
- }
28
- is(typeName) {
29
- return this.name === typeName || !!(this.base && this.base.is(typeName));
30
- }
31
- }
@@ -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 { SqbConnect } 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 (args.ctor) {
11
- const sqbEntity = 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,111 +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
- 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
- }
@@ -1,19 +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
- protected _addResources(resources: OpraSchema.Resource[]): void;
18
- static create(args: SchemaGenerator.GenerateServiceArgs): Promise<OpraService>;
19
- }
@@ -1,55 +0,0 @@
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 { EntityResourceHandler } from './resource/entity-resource-handler.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 EntityResourceHandler))
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 EntityResourceHandler({ ...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
- }
@@ -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
- }