@opra/core 0.3.0 → 0.5.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 (58) hide show
  1. package/cjs/adapter/adapter.js +318 -0
  2. package/cjs/{implementation → adapter}/express-adapter.js +3 -6
  3. package/cjs/adapter/http-adapter.js +241 -0
  4. package/cjs/adapter/metadata-resource.js +23 -0
  5. package/cjs/{implementation → adapter}/query-context.js +3 -3
  6. package/cjs/index.js +6 -6
  7. package/cjs/interfaces/resource.interface.js +2 -0
  8. package/cjs/services/json-collection-service.js +14 -14
  9. package/cjs/services/json-singleton-service.js +97 -0
  10. package/esm/{implementation → adapter}/adapter.d.ts +17 -9
  11. package/esm/adapter/adapter.js +314 -0
  12. package/esm/{implementation → adapter}/express-adapter.d.ts +2 -2
  13. package/esm/{implementation → adapter}/express-adapter.js +3 -6
  14. package/esm/{implementation → adapter}/http-adapter.d.ts +2 -3
  15. package/esm/adapter/http-adapter.js +237 -0
  16. package/esm/adapter/metadata-resource.d.ts +8 -0
  17. package/esm/adapter/metadata-resource.js +20 -0
  18. package/esm/{implementation → adapter}/query-context.d.ts +6 -7
  19. package/esm/{implementation → adapter}/query-context.js +1 -1
  20. package/esm/index.d.ts +6 -6
  21. package/esm/index.js +6 -6
  22. package/esm/interfaces/resource.interface.d.ts +22 -0
  23. package/esm/interfaces/resource.interface.js +1 -0
  24. package/esm/services/json-collection-service.d.ts +11 -12
  25. package/esm/services/json-collection-service.js +15 -15
  26. package/esm/services/json-singleton-service.d.ts +39 -0
  27. package/esm/services/json-singleton-service.js +92 -0
  28. package/esm/types.d.ts +2 -8
  29. package/esm/utils/create-i18n.d.ts +1 -1
  30. package/package.json +15 -13
  31. package/cjs/enums/http-headers.enum.js +0 -395
  32. package/cjs/enums/http-status.enum.js +0 -300
  33. package/cjs/enums/index.js +0 -5
  34. package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +0 -86
  35. package/cjs/implementation/adapter-utils/resource-execute.util.js +0 -11
  36. package/cjs/implementation/adapter-utils/resource-prepare.util.js +0 -11
  37. package/cjs/implementation/adapter.js +0 -130
  38. package/cjs/implementation/headers-map.js +0 -18
  39. package/cjs/implementation/http-adapter.js +0 -253
  40. package/cjs/interfaces/entity-service.interface.js +0 -30
  41. package/esm/enums/http-headers.enum.d.ts +0 -370
  42. package/esm/enums/http-headers.enum.js +0 -392
  43. package/esm/enums/http-status.enum.d.ts +0 -290
  44. package/esm/enums/http-status.enum.js +0 -297
  45. package/esm/enums/index.d.ts +0 -2
  46. package/esm/enums/index.js +0 -2
  47. package/esm/implementation/adapter-utils/entity-resource-execute.util.d.ts +0 -3
  48. package/esm/implementation/adapter-utils/entity-resource-execute.util.js +0 -82
  49. package/esm/implementation/adapter-utils/resource-execute.util.d.ts +0 -3
  50. package/esm/implementation/adapter-utils/resource-execute.util.js +0 -7
  51. package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +0 -3
  52. package/esm/implementation/adapter-utils/resource-prepare.util.js +0 -7
  53. package/esm/implementation/adapter.js +0 -126
  54. package/esm/implementation/headers-map.d.ts +0 -5
  55. package/esm/implementation/headers-map.js +0 -14
  56. package/esm/implementation/http-adapter.js +0 -249
  57. package/esm/interfaces/entity-service.interface.d.ts +0 -19
  58. package/esm/interfaces/entity-service.interface.js +0 -26
@@ -1,249 +0,0 @@
1
- import { BadRequestError, InternalServerError, IssueSeverity, MethodNotAllowedError, NotFoundError, OpraException, wrapException } from '@opra/exception';
2
- import { ComplexType, ContainerResource, EntityResource, OpraCreateInstanceQuery, OpraDeleteCollectionQuery, OpraDeleteInstanceQuery, OpraGetFieldQuery, OpraGetInstanceQuery, OpraGetMetadataQuery, OpraSchema, OpraSearchCollectionQuery, OpraUpdateCollectionQuery, OpraUpdateInstanceQuery, } from '@opra/schema';
3
- import { OpraURL } from '@opra/url';
4
- import { HttpHeaders, HttpStatus } from '../enums/index.js';
5
- import { OpraAdapter } from './adapter.js';
6
- import { HeadersMap } from './headers-map.js';
7
- import { QueryContext } from './query-context.js';
8
- export class OpraHttpAdapter extends OpraAdapter {
9
- prepareRequests(executionContext) {
10
- const req = executionContext.getRequestWrapper();
11
- // todo implement batch requests
12
- if (this.isBatch(executionContext)) {
13
- throw new Error('not implemented yet');
14
- }
15
- const url = new OpraURL(req.getUrl());
16
- return [
17
- this.prepareRequest(executionContext, url, req.getMethod(), new HeadersMap(req.getHeaders()), req.getBody())
18
- ];
19
- }
20
- prepareRequest(executionContext, url, method, headers, body) {
21
- if (!url.path.size)
22
- throw new BadRequestError();
23
- if (method !== 'GET' && url.path.size > 1)
24
- throw new BadRequestError();
25
- const query = this.buildQuery(url, method, body);
26
- if (!query)
27
- throw new MethodNotAllowedError({
28
- message: `Method "${method}" is not allowed by target endpoint`
29
- });
30
- return new QueryContext({
31
- service: this.service,
32
- executionContext,
33
- query,
34
- headers: new HeadersMap(),
35
- params: url.searchParams,
36
- continueOnError: query.operation === 'read'
37
- });
38
- }
39
- buildGGetMetadataQuery(url) {
40
- const pathLen = url.path.size;
41
- const resourcePath = [];
42
- let pathIndex = 0;
43
- while (pathIndex < pathLen) {
44
- const p = url.path.get(pathIndex++);
45
- if (p.key)
46
- throw new BadRequestError();
47
- if (p.resource !== '$metadata') {
48
- if (pathIndex === 1)
49
- resourcePath.push('resources');
50
- resourcePath.push(p.resource);
51
- }
52
- }
53
- const opts = {
54
- pick: url.searchParams.get('$pick'),
55
- omit: url.searchParams.get('$omit'),
56
- include: url.searchParams.get('$include'),
57
- resourcePath
58
- };
59
- return new OpraGetMetadataQuery(opts);
60
- }
61
- buildQuery(url, method, body) {
62
- let container = this.service;
63
- try {
64
- const pathLen = url.path.size;
65
- // Check if requesting metadata
66
- for (let i = 0; i < pathLen; i++) {
67
- const p = url.path.get(i);
68
- if (p.resource === '$metadata') {
69
- if (method !== 'GET')
70
- return;
71
- return this.buildGGetMetadataQuery(url);
72
- }
73
- }
74
- let pathIndex = 0;
75
- while (pathIndex < pathLen) {
76
- let p = url.path.get(pathIndex++);
77
- const resource = container.getResource(p.resource);
78
- // Move through path directories (containers)
79
- if (resource instanceof ContainerResource) {
80
- container = resource;
81
- continue;
82
- }
83
- method = method.toUpperCase();
84
- if (resource instanceof EntityResource) {
85
- const scope = p.key ? 'instance' : 'collection';
86
- if (pathIndex < pathLen && !(method === 'GET' && scope === 'instance'))
87
- return;
88
- let query;
89
- switch (method) {
90
- case 'GET': {
91
- if (scope === 'collection') {
92
- query = new OpraSearchCollectionQuery(resource, {
93
- filter: url.searchParams.get('$filter'),
94
- limit: url.searchParams.get('$limit'),
95
- skip: url.searchParams.get('$skip'),
96
- distinct: url.searchParams.get('$distinct'),
97
- count: url.searchParams.get('$count'),
98
- sort: url.searchParams.get('$sort'),
99
- pick: url.searchParams.get('$pick'),
100
- omit: url.searchParams.get('$omit'),
101
- include: url.searchParams.get('$include'),
102
- });
103
- }
104
- else {
105
- query = new OpraGetInstanceQuery(resource, p.key, {
106
- pick: url.searchParams.get('$pick'),
107
- omit: url.searchParams.get('$omit'),
108
- include: url.searchParams.get('$include')
109
- });
110
- // Move through properties
111
- let dataType = resource.dataType;
112
- const curPath = [];
113
- let parent = query;
114
- while (pathIndex < pathLen) {
115
- if (!(dataType instanceof ComplexType))
116
- throw new TypeError(`"${resource.name}.${curPath.join()}" is not a ComplexType and has no fields.`);
117
- p = url.path.get(pathIndex++);
118
- curPath.push(p.resource);
119
- const field = dataType.getField(p.resource);
120
- parent.nested = new OpraGetFieldQuery(parent, field.name);
121
- parent = parent.nested;
122
- dataType = parent.dataType;
123
- }
124
- }
125
- break;
126
- }
127
- case 'DELETE': {
128
- query = scope === 'collection'
129
- ? new OpraDeleteCollectionQuery(resource, {
130
- filter: url.searchParams.get('$filter'),
131
- })
132
- : new OpraDeleteInstanceQuery(resource, p.key);
133
- break;
134
- }
135
- case 'POST': {
136
- if (scope === 'collection') {
137
- query = new OpraCreateInstanceQuery(resource, body, {
138
- pick: url.searchParams.get('$pick'),
139
- omit: url.searchParams.get('$omit'),
140
- include: url.searchParams.get('$include')
141
- });
142
- }
143
- break;
144
- }
145
- case 'PATCH': {
146
- query = scope === 'collection'
147
- ? new OpraUpdateCollectionQuery(resource, body, {
148
- filter: url.searchParams.get('$filter')
149
- })
150
- : new OpraUpdateInstanceQuery(resource, p.key, body, {
151
- pick: url.searchParams.get('$pick'),
152
- omit: url.searchParams.get('$omit'),
153
- include: url.searchParams.get('$include')
154
- });
155
- break;
156
- }
157
- }
158
- return query;
159
- }
160
- }
161
- throw new InternalServerError();
162
- }
163
- catch (e) {
164
- if (e instanceof OpraException)
165
- throw e;
166
- throw new BadRequestError(e);
167
- }
168
- }
169
- async sendResponse(executionContext, queryContexts) {
170
- const outputPackets = [];
171
- for (const ctx of queryContexts) {
172
- const v = this.createOutput(ctx);
173
- outputPackets.push(v);
174
- }
175
- if (this.isBatch(executionContext)) {
176
- // this.writeError([], new InternalServerError({message: 'Not implemented yet'}));
177
- return;
178
- }
179
- if (!outputPackets.length)
180
- return this.sendError(executionContext, new NotFoundError());
181
- const out = outputPackets[0];
182
- const resp = executionContext.getResponseWrapper();
183
- resp.setStatus(out.status);
184
- resp.setHeader(HttpHeaders.Content_Type, 'application/opra+json');
185
- resp.setHeader(HttpHeaders.Cache_Control, 'no-cache');
186
- resp.setHeader(HttpHeaders.Pragma, 'no-cache');
187
- resp.setHeader(HttpHeaders.Expires, '-1');
188
- resp.setHeader(HttpHeaders.X_Opra_Version, OpraSchema.Version);
189
- if (out.headers) {
190
- for (const [k, v] of Object.entries(out.headers)) {
191
- resp.setHeader(k, v);
192
- }
193
- }
194
- resp.send(JSON.stringify(out.body));
195
- }
196
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
197
- isBatch(executionContext) {
198
- return false;
199
- }
200
- createOutput(ctx) {
201
- const { query } = ctx;
202
- let body;
203
- let status = ctx.status || 0;
204
- const errors = ctx.errors.map(e => wrapException(e));
205
- if (errors && errors.length) {
206
- // Sort errors from fatal to info
207
- errors.sort((a, b) => {
208
- const i = IssueSeverity.Keys.indexOf(a.issue.severity) - IssueSeverity.Keys.indexOf(b.issue.severity);
209
- if (i === 0)
210
- return b.status - a.status;
211
- return i;
212
- });
213
- if (!status || status < HttpStatus.BAD_REQUEST) {
214
- status = errors[0].status;
215
- if (status < HttpStatus.BAD_REQUEST)
216
- status = HttpStatus.INTERNAL_SERVER_ERROR;
217
- }
218
- body = {
219
- operation: ctx.query.method,
220
- errors: errors.map(e => e.issue)
221
- };
222
- }
223
- else {
224
- body = ctx.response;
225
- status = status || (query.operation === 'create' ? HttpStatus.CREATED : HttpStatus.OK);
226
- }
227
- body = this.i18n.deep(body);
228
- return {
229
- status,
230
- headers: ctx.responseHeaders.toObject(),
231
- body
232
- };
233
- }
234
- async sendError(executionContext, error) {
235
- const resp = executionContext.getResponseWrapper();
236
- resp.setStatus(error.status || 500);
237
- resp.setHeader(HttpHeaders.Content_Type, 'application/json');
238
- resp.setHeader(HttpHeaders.Cache_Control, 'no-cache');
239
- resp.setHeader(HttpHeaders.Pragma, 'no-cache');
240
- resp.setHeader(HttpHeaders.Expires, '-1');
241
- resp.setHeader(HttpHeaders.X_Opra_Version, OpraSchema.Version);
242
- const issue = this.i18n.deep(error.issue);
243
- const body = {
244
- operation: 'unknown',
245
- errors: [issue]
246
- };
247
- resp.send(JSON.stringify(body));
248
- }
249
- }
@@ -1,19 +0,0 @@
1
- import { Maybe } from 'ts-gems';
2
- import { IResource, OpraResource } from '@opra/schema';
3
- import { EntityOutput } from '@sqb/connect';
4
- import { QueryContext } from '../implementation/query-context.js';
5
- export interface IEntityService {
6
- processRequest(ctx: QueryContext): any;
7
- }
8
- export declare abstract class EntityResourceController<T, TOutput = EntityOutput<T>> implements IResource {
9
- search(ctx: QueryContext): Promise<TOutput[]>;
10
- get(ctx: QueryContext): Promise<Maybe<TOutput>>;
11
- count(ctx: QueryContext): Promise<number>;
12
- create(ctx: QueryContext): Promise<TOutput>;
13
- update(ctx: QueryContext): Promise<Maybe<TOutput>>;
14
- updateMany(ctx: QueryContext): Promise<Maybe<number>>;
15
- delete(ctx: QueryContext): Promise<Maybe<boolean | number>>;
16
- deleteMany(ctx: QueryContext): Promise<Maybe<number>>;
17
- init?(service: OpraResource): void | Promise<void>;
18
- abstract getService(ctx: QueryContext): IEntityService | Promise<IEntityService>;
19
- }
@@ -1,26 +0,0 @@
1
- export class EntityResourceController {
2
- async search(ctx) {
3
- return (await this.getService(ctx)).processRequest(ctx);
4
- }
5
- async get(ctx) {
6
- return (await this.getService(ctx)).processRequest(ctx);
7
- }
8
- async count(ctx) {
9
- return (await this.getService(ctx)).processRequest(ctx);
10
- }
11
- async create(ctx) {
12
- return (await this.getService(ctx)).processRequest(ctx);
13
- }
14
- async update(ctx) {
15
- return (await this.getService(ctx)).processRequest(ctx);
16
- }
17
- async updateMany(ctx) {
18
- return (await this.getService(ctx)).processRequest(ctx);
19
- }
20
- async delete(ctx) {
21
- return (await this.getService(ctx)).processRequest(ctx);
22
- }
23
- async deleteMany(ctx) {
24
- return (await this.getService(ctx)).processRequest(ctx);
25
- }
26
- }