@opra/core 0.4.0 → 0.6.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 (68) hide show
  1. package/cjs/adapter/adapter.js +103 -123
  2. package/cjs/adapter/classes/execution-context.host.js +17 -0
  3. package/cjs/adapter/classes/express-request-wrapper.host.js +37 -0
  4. package/cjs/adapter/classes/express-response-wrapper.host.js +56 -0
  5. package/cjs/adapter/classes/http-execution-context.host.js +31 -0
  6. package/cjs/adapter/{metadata-resource.js → classes/metadata.resource.js} +3 -3
  7. package/cjs/adapter/express-adapter.js +6 -104
  8. package/cjs/adapter/http-adapter.js +282 -81
  9. package/cjs/adapter/request-contexts/batch-request-context.js +12 -0
  10. package/cjs/adapter/{query-context.js → request-contexts/request-context.js} +9 -13
  11. package/cjs/adapter/request-contexts/single-request-context.js +15 -0
  12. package/cjs/index.js +2 -2
  13. package/cjs/interfaces/i18n-options.interface.js +2 -0
  14. package/cjs/services/json-collection-service.js +36 -39
  15. package/cjs/services/json-singleton-service.js +9 -10
  16. package/cjs/utils/create-i18n.js +2 -2
  17. package/esm/adapter/adapter.d.ts +13 -44
  18. package/esm/adapter/adapter.js +89 -109
  19. package/esm/adapter/classes/execution-context.host.d.ts +10 -0
  20. package/esm/adapter/classes/execution-context.host.js +13 -0
  21. package/esm/adapter/classes/express-request-wrapper.host.d.ts +19 -0
  22. package/esm/adapter/classes/express-request-wrapper.host.js +33 -0
  23. package/esm/adapter/classes/express-response-wrapper.host.d.ts +22 -0
  24. package/esm/adapter/classes/express-response-wrapper.host.js +52 -0
  25. package/esm/adapter/classes/http-execution-context.host.d.ts +13 -0
  26. package/esm/adapter/classes/http-execution-context.host.js +27 -0
  27. package/esm/adapter/classes/metadata.resource.d.ts +8 -0
  28. package/esm/adapter/{metadata-resource.js → classes/metadata.resource.js} +2 -2
  29. package/esm/adapter/express-adapter.d.ts +1 -1
  30. package/esm/adapter/express-adapter.js +5 -103
  31. package/esm/adapter/http-adapter.d.ts +23 -12
  32. package/esm/adapter/http-adapter.js +248 -47
  33. package/esm/adapter/request-contexts/batch-request-context.d.ts +7 -0
  34. package/esm/adapter/request-contexts/batch-request-context.js +8 -0
  35. package/esm/adapter/request-contexts/request-context.d.ts +22 -0
  36. package/esm/adapter/{query-context.js → request-contexts/request-context.js} +6 -10
  37. package/esm/adapter/request-contexts/single-request-context.d.ts +10 -0
  38. package/esm/adapter/request-contexts/single-request-context.js +11 -0
  39. package/esm/index.d.ts +2 -2
  40. package/esm/index.js +2 -2
  41. package/esm/interfaces/execution-context.interface.d.ts +19 -11
  42. package/esm/interfaces/i18n-options.interface.d.ts +28 -0
  43. package/esm/interfaces/i18n-options.interface.js +1 -0
  44. package/esm/interfaces/resource.interface.d.ts +1 -1
  45. package/esm/services/json-collection-service.d.ts +3 -4
  46. package/esm/services/json-collection-service.js +17 -20
  47. package/esm/services/json-singleton-service.d.ts +3 -3
  48. package/esm/services/json-singleton-service.js +8 -8
  49. package/esm/utils/create-i18n.d.ts +3 -3
  50. package/esm/utils/create-i18n.js +1 -1
  51. package/package.json +7 -8
  52. package/cjs/enums/http-headers.enum.js +0 -395
  53. package/cjs/enums/http-status.enum.js +0 -300
  54. package/cjs/enums/index.js +0 -5
  55. package/cjs/helpers/headers-map.js +0 -18
  56. package/cjs/utils/path-to-tree.js +0 -28
  57. package/esm/adapter/metadata-resource.d.ts +0 -8
  58. package/esm/adapter/query-context.d.ts +0 -25
  59. package/esm/enums/http-headers.enum.d.ts +0 -370
  60. package/esm/enums/http-headers.enum.js +0 -392
  61. package/esm/enums/http-status.enum.d.ts +0 -290
  62. package/esm/enums/http-status.enum.js +0 -297
  63. package/esm/enums/index.d.ts +0 -2
  64. package/esm/enums/index.js +0 -2
  65. package/esm/helpers/headers-map.d.ts +0 -5
  66. package/esm/helpers/headers-map.js +0 -14
  67. package/esm/utils/path-to-tree.d.ts +0 -4
  68. package/esm/utils/path-to-tree.js +0 -24
@@ -0,0 +1,10 @@
1
+ import { AsyncEventEmitter } from 'strict-typed-events';
2
+ import { ContextType, IExecutionContext, IHttpExecutionContext } from '../../interfaces/execution-context.interface.js';
3
+ export declare abstract class ExecutionContextHost extends AsyncEventEmitter implements IExecutionContext {
4
+ userContext: any;
5
+ protected constructor();
6
+ abstract getType(): ContextType;
7
+ abstract getPlatform(): string;
8
+ switchToHttp(): IHttpExecutionContext;
9
+ onFinish(fn: (...args: any[]) => (void | Promise<void>)): void;
10
+ }
@@ -0,0 +1,13 @@
1
+ import { AsyncEventEmitter } from 'strict-typed-events';
2
+ export class ExecutionContextHost extends AsyncEventEmitter {
3
+ userContext;
4
+ constructor() {
5
+ super();
6
+ }
7
+ switchToHttp() {
8
+ throw new Error(`This is not an http context`);
9
+ }
10
+ onFinish(fn) {
11
+ this.on('finish', fn);
12
+ }
13
+ }
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { Request } from 'express';
4
+ import type { IncomingHttpHeaders } from 'http';
5
+ import { Readable } from 'stream';
6
+ import type { IHttpRequestWrapper } from '../../interfaces/execution-context.interface.js';
7
+ export declare class ExpressRequestWrapperHost implements IHttpRequestWrapper {
8
+ readonly instance: Request;
9
+ constructor(instance: Request);
10
+ getInstance(): any;
11
+ getMethod(): string;
12
+ getUrl(): string;
13
+ getHeaderNames(): string[];
14
+ getHeader(name: string): string | string[] | undefined;
15
+ getHeaders(): IncomingHttpHeaders;
16
+ getBody(): any;
17
+ isCompleted(): boolean;
18
+ getStream(): Readable;
19
+ }
@@ -0,0 +1,33 @@
1
+ export class ExpressRequestWrapperHost {
2
+ instance;
3
+ constructor(instance) {
4
+ this.instance = instance;
5
+ }
6
+ getInstance() {
7
+ return this.instance;
8
+ }
9
+ getMethod() {
10
+ return this.instance.method;
11
+ }
12
+ getUrl() {
13
+ return this.instance.url;
14
+ }
15
+ getHeaderNames() {
16
+ return Object.keys(this.instance.headers);
17
+ }
18
+ getHeader(name) {
19
+ return this.instance.get(name);
20
+ }
21
+ getHeaders() {
22
+ return this.instance.headers;
23
+ }
24
+ getBody() {
25
+ return this.instance.body;
26
+ }
27
+ isCompleted() {
28
+ return this.instance.complete;
29
+ }
30
+ getStream() {
31
+ return this.instance;
32
+ }
33
+ }
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Response } from 'express';
4
+ import { OutgoingHttpHeaders } from 'http';
5
+ import { Writable } from 'stream';
6
+ import { IHttpResponseWrapper } from '../../interfaces/execution-context.interface.js';
7
+ export declare class ExpressResponseWrapperHost implements IHttpResponseWrapper {
8
+ readonly instance: Response;
9
+ constructor(instance: Response);
10
+ getInstance(): any;
11
+ getHeader(name: string): number | string | string[] | undefined;
12
+ getHeaders(): OutgoingHttpHeaders;
13
+ getHeaderNames(): string[];
14
+ hasHeader(name: string): boolean;
15
+ removeHeader(name: string): void;
16
+ setHeader(name: string, value: number | string | string[]): this;
17
+ getStatus(): number | undefined;
18
+ setStatus(value: number): this;
19
+ getStream(): Writable;
20
+ send(body: any): this;
21
+ end(): this;
22
+ }
@@ -0,0 +1,52 @@
1
+ export class ExpressResponseWrapperHost {
2
+ instance;
3
+ constructor(instance) {
4
+ this.instance = instance;
5
+ }
6
+ getInstance() {
7
+ return this.instance;
8
+ }
9
+ getHeader(name) {
10
+ return this.instance.get(name);
11
+ }
12
+ getHeaders() {
13
+ return this.instance.getHeaders();
14
+ }
15
+ getHeaderNames() {
16
+ return this.instance.getHeaderNames();
17
+ }
18
+ hasHeader(name) {
19
+ return this.instance.hasHeader(name);
20
+ }
21
+ removeHeader(name) {
22
+ return this.instance.removeHeader(name);
23
+ }
24
+ setHeader(name, value) {
25
+ this.instance.setHeader(name, value);
26
+ return this;
27
+ }
28
+ getStatus() {
29
+ return this.instance.statusCode;
30
+ }
31
+ setStatus(value) {
32
+ // noinspection SuspiciousTypeOfGuard
33
+ this.instance.status(typeof value === 'number'
34
+ ? value
35
+ : parseInt(value, 10) || 500);
36
+ return this;
37
+ }
38
+ getStream() {
39
+ return this.instance;
40
+ }
41
+ send(body) {
42
+ if (typeof body === 'string' || Buffer.isBuffer(body))
43
+ this.instance.send(body);
44
+ else
45
+ this.instance.json(body);
46
+ return this;
47
+ }
48
+ end() {
49
+ this.instance.end();
50
+ return this;
51
+ }
52
+ }
@@ -0,0 +1,13 @@
1
+ import { ContextType, IHttpExecutionContext, IHttpRequestWrapper, IHttpResponseWrapper } from '../../interfaces/execution-context.interface.js';
2
+ import { ExecutionContextHost } from './execution-context.host.js';
3
+ export declare class HttpExecutionContextHost extends ExecutionContextHost implements IHttpExecutionContext {
4
+ protected readonly _platform: string;
5
+ protected readonly _request: IHttpRequestWrapper;
6
+ protected readonly _response: IHttpResponseWrapper;
7
+ constructor(_platform: string, _request: IHttpRequestWrapper, _response: IHttpResponseWrapper);
8
+ getType(): ContextType;
9
+ getPlatform(): string;
10
+ getRequest(): IHttpRequestWrapper;
11
+ getResponse(): IHttpResponseWrapper;
12
+ switchToHttp(): IHttpExecutionContext;
13
+ }
@@ -0,0 +1,27 @@
1
+ import { ExecutionContextHost } from './execution-context.host.js';
2
+ export class HttpExecutionContextHost extends ExecutionContextHost {
3
+ _platform;
4
+ _request;
5
+ _response;
6
+ constructor(_platform, _request, _response) {
7
+ super();
8
+ this._platform = _platform;
9
+ this._request = _request;
10
+ this._response = _response;
11
+ }
12
+ getType() {
13
+ return 'http';
14
+ }
15
+ getPlatform() {
16
+ return this._platform;
17
+ }
18
+ getRequest() {
19
+ return this._request;
20
+ }
21
+ getResponse() {
22
+ return this._response;
23
+ }
24
+ switchToHttp() {
25
+ return this;
26
+ }
27
+ }
@@ -0,0 +1,8 @@
1
+ import { SingletonResourceInfo } from '@opra/common';
2
+ import { ISingletonResource } from '../../interfaces/resource.interface.js';
3
+ import { JsonSingletonService } from '../../services/json-singleton-service.js';
4
+ export declare class MetadataResource implements ISingletonResource<any> {
5
+ service: JsonSingletonService<any>;
6
+ init(resource: SingletonResourceInfo): void;
7
+ get(): any;
8
+ }
@@ -1,6 +1,6 @@
1
1
  import { __decorate } from "tslib";
2
- import { OprSingletonResource } from '@opra/schema';
3
- import { JsonSingletonService } from '../services/json-singleton-service.js';
2
+ import { OprSingletonResource } from '@opra/common';
3
+ import { JsonSingletonService } from '../../services/json-singleton-service.js';
4
4
  let MetadataResource = class MetadataResource {
5
5
  service;
6
6
  init(resource) {
@@ -1,5 +1,5 @@
1
1
  import type { Application } from 'express';
2
- import { OpraDocument } from '@opra/schema';
2
+ import { OpraDocument } from '@opra/common';
3
3
  import type { IHttpExecutionContext } from '../interfaces/execution-context.interface';
4
4
  import { OpraHttpAdapter } from './http-adapter.js';
5
5
  export declare namespace OpraExpressAdapter {
@@ -1,6 +1,8 @@
1
1
  import bodyParser from 'body-parser';
2
- import { AsyncEventEmitter } from 'strict-typed-events';
3
- import { normalizePath } from '@opra/url';
2
+ import { normalizePath } from '@opra/common';
3
+ import { ExpressRequestWrapperHost } from './classes/express-request-wrapper.host.js';
4
+ import { ExpressResponseWrapperHost } from './classes/express-response-wrapper.host.js';
5
+ import { HttpExecutionContextHost } from './classes/http-execution-context.host.js';
4
6
  import { OpraHttpAdapter } from './http-adapter.js';
5
7
  export class OpraExpressAdapter extends OpraHttpAdapter {
6
8
  static async init(app, document, options) {
@@ -10,110 +12,10 @@ export class OpraExpressAdapter extends OpraHttpAdapter {
10
12
  app.use(prefix, bodyParser.json());
11
13
  app.use(prefix, (request, response, next) => {
12
14
  (async () => {
13
- const executionContext = new ExpressExecutionContext(request, response);
15
+ const executionContext = new HttpExecutionContextHost('express', new ExpressRequestWrapperHost(request), new ExpressResponseWrapperHost(response));
14
16
  await adapter.handler(executionContext);
15
17
  })().catch(e => next(e));
16
18
  });
17
19
  return adapter;
18
20
  }
19
21
  }
20
- class ExpressExecutionContext extends AsyncEventEmitter {
21
- _request;
22
- _response;
23
- constructor(request, response) {
24
- super();
25
- this._request = new ExpressRequestWrapper(request);
26
- this._response = new ExpressResponseWrapper(response);
27
- }
28
- getType() {
29
- return 'http';
30
- }
31
- getPlatform() {
32
- return 'express';
33
- }
34
- switchToHttp() {
35
- return this;
36
- }
37
- getRequest() {
38
- return this._request.getInstance();
39
- }
40
- getResponse() {
41
- return this._response.getInstance();
42
- }
43
- getRequestWrapper() {
44
- return this._request;
45
- }
46
- getResponseWrapper() {
47
- return this._response;
48
- }
49
- onFinish(fn) {
50
- this.on('finish', fn);
51
- }
52
- }
53
- class ExpressRequestWrapper {
54
- instance;
55
- constructor(instance) {
56
- this.instance = instance;
57
- }
58
- getInstance() {
59
- return this.instance;
60
- }
61
- getMethod() {
62
- return this.instance.method;
63
- }
64
- getUrl() {
65
- return this.instance.url;
66
- }
67
- getHeaderNames() {
68
- return Object.keys(this.instance.headers);
69
- }
70
- getHeader(name) {
71
- return this.instance.get(name);
72
- }
73
- getHeaders() {
74
- return this.instance.headers;
75
- }
76
- getBody() {
77
- return this.instance.body;
78
- }
79
- }
80
- class ExpressResponseWrapper {
81
- instance;
82
- constructor(instance) {
83
- this.instance = instance;
84
- }
85
- getInstance() {
86
- return this.instance;
87
- }
88
- getHeaderNames() {
89
- return this.instance.getHeaderNames();
90
- }
91
- getHeader(name) {
92
- return this.instance.get(name);
93
- }
94
- setHeader(name, value) {
95
- this.instance.setHeader(name, value);
96
- return this;
97
- }
98
- getStatus() {
99
- return this.instance.statusCode;
100
- }
101
- setStatus(value) {
102
- // noinspection SuspiciousTypeOfGuard
103
- this.instance.status(typeof value === 'number'
104
- ? value
105
- : parseInt(value, 10) || 500);
106
- return this;
107
- }
108
- send(body) {
109
- if (typeof body === 'string' || Buffer.isBuffer(body))
110
- this.instance.send(body);
111
- else
112
- this.instance.json(body);
113
- return this;
114
- }
115
- end() {
116
- this.instance.end();
117
- return this;
118
- }
119
- }
@@ -1,9 +1,13 @@
1
- import { OpraException } from '@opra/exception';
2
- import { OpraQuery } from '@opra/schema';
3
- import { OpraURL } from '@opra/url';
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
5
+ import { Readable } from 'stream';
6
+ import { OpraException, OpraQuery, OpraURL } from '@opra/common';
4
7
  import { IHttpExecutionContext } from '../interfaces/execution-context.interface.js';
5
8
  import { OpraAdapter } from './adapter.js';
6
- import { QueryContext } from './query-context.js';
9
+ import { RequestContext } from './request-contexts/request-context.js';
10
+ import { SingleRequestContext } from './request-contexts/single-request-context.js';
7
11
  export declare namespace OpraHttpAdapter {
8
12
  type Options = OpraAdapter.Options & {
9
13
  prefix?: string;
@@ -11,16 +15,23 @@ export declare namespace OpraHttpAdapter {
11
15
  }
12
16
  interface PreparedOutput {
13
17
  status: number;
14
- headers: Record<string, string>;
15
- body?: any;
18
+ headers: OutgoingHttpHeaders;
19
+ body?: string | Readable | Buffer;
16
20
  }
17
- export declare class OpraHttpAdapter<TExecutionContext extends IHttpExecutionContext> extends OpraAdapter<IHttpExecutionContext> {
18
- protected prepareRequests(executionContext: TExecutionContext): QueryContext[];
19
- prepareRequest(executionContext: IHttpExecutionContext, url: OpraURL, method: string, headers: Map<string, string>, body?: any): QueryContext;
21
+ export declare class OpraHttpAdapter<TExecutionContext extends IHttpExecutionContext = IHttpExecutionContext> extends OpraAdapter<TExecutionContext> {
22
+ parse(executionContext: TExecutionContext): Promise<RequestContext>;
23
+ parseSingleQuery(args: {
24
+ executionContext: IHttpExecutionContext;
25
+ url: OpraURL;
26
+ method: string;
27
+ headers: IncomingHttpHeaders;
28
+ body?: any;
29
+ contentId?: string;
30
+ }): SingleRequestContext;
20
31
  buildQuery(url: OpraURL, method: string, body?: any): OpraQuery | undefined;
21
- protected sendResponse(executionContext: TExecutionContext, queryContexts: QueryContext[]): Promise<void>;
22
- protected isBatch(executionContext: TExecutionContext): boolean;
23
- protected createOutput(ctx: QueryContext): PreparedOutput;
32
+ protected sendResponse(executionContext: TExecutionContext, requestContext: RequestContext): Promise<void>;
33
+ protected sendSingleResponse(executionContext: TExecutionContext, requestContext: SingleRequestContext): Promise<void>;
34
+ protected createOutput(ctx: SingleRequestContext): PreparedOutput;
24
35
  protected sendError(executionContext: TExecutionContext, error: OpraException): Promise<void>;
25
36
  }
26
37
  export {};