@opra/core 1.0.0-beta.1 → 1.0.0-beta.3

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 (85) hide show
  1. package/cjs/execution-context.js +2 -14
  2. package/cjs/index.js +3 -25
  3. package/cjs/platform-adapter.js +3 -3
  4. package/esm/execution-context.js +1 -13
  5. package/esm/index.js +3 -24
  6. package/esm/platform-adapter.js +2 -2
  7. package/package.json +5 -39
  8. package/types/execution-context.d.ts +3 -8
  9. package/types/index.d.cts +3 -23
  10. package/types/index.d.ts +3 -23
  11. package/types/interfaces/logger.interface.d.ts +1 -1
  12. package/types/platform-adapter.d.ts +3 -3
  13. package/types/service-base.d.ts +10 -0
  14. package/cjs/augmentation/http-controller.augmentation.js +0 -25
  15. package/cjs/http/express-adapter.js +0 -155
  16. package/cjs/http/http-adapter.js +0 -24
  17. package/cjs/http/http-context.js +0 -104
  18. package/cjs/http/http-handler.js +0 -609
  19. package/cjs/http/impl/http-incoming.host.js +0 -112
  20. package/cjs/http/impl/http-outgoing.host.js +0 -207
  21. package/cjs/http/impl/multipart-reader.js +0 -196
  22. package/cjs/http/impl/node-incoming-message.host.js +0 -109
  23. package/cjs/http/impl/node-outgoing-message.host.js +0 -195
  24. package/cjs/http/interfaces/http-incoming.interface.js +0 -25
  25. package/cjs/http/interfaces/http-outgoing.interface.js +0 -22
  26. package/cjs/http/interfaces/node-incoming-message.interface.js +0 -64
  27. package/cjs/http/interfaces/node-outgoing-message.interface.js +0 -15
  28. package/cjs/http/utils/body-reader.js +0 -216
  29. package/cjs/http/utils/common.js +0 -67
  30. package/cjs/http/utils/concat-readable.js +0 -19
  31. package/cjs/http/utils/convert-to-headers.js +0 -64
  32. package/cjs/http/utils/convert-to-raw-headers.js +0 -23
  33. package/cjs/http/utils/match-known-fields.js +0 -49
  34. package/cjs/http/utils/wrap-exception.js +0 -33
  35. package/cjs/type-guards.js +0 -22
  36. package/esm/augmentation/http-controller.augmentation.js +0 -23
  37. package/esm/http/express-adapter.js +0 -150
  38. package/esm/http/http-adapter.js +0 -20
  39. package/esm/http/http-context.js +0 -99
  40. package/esm/http/http-handler.js +0 -604
  41. package/esm/http/impl/http-incoming.host.js +0 -107
  42. package/esm/http/impl/http-outgoing.host.js +0 -202
  43. package/esm/http/impl/multipart-reader.js +0 -191
  44. package/esm/http/impl/node-incoming-message.host.js +0 -105
  45. package/esm/http/impl/node-outgoing-message.host.js +0 -191
  46. package/esm/http/interfaces/http-incoming.interface.js +0 -22
  47. package/esm/http/interfaces/http-outgoing.interface.js +0 -19
  48. package/esm/http/interfaces/node-incoming-message.interface.js +0 -61
  49. package/esm/http/interfaces/node-outgoing-message.interface.js +0 -12
  50. package/esm/http/utils/body-reader.js +0 -211
  51. package/esm/http/utils/common.js +0 -61
  52. package/esm/http/utils/concat-readable.js +0 -16
  53. package/esm/http/utils/convert-to-headers.js +0 -60
  54. package/esm/http/utils/convert-to-raw-headers.js +0 -20
  55. package/esm/http/utils/match-known-fields.js +0 -45
  56. package/esm/http/utils/wrap-exception.js +0 -30
  57. package/esm/type-guards.js +0 -16
  58. package/types/augmentation/http-controller.augmentation.d.ts +0 -20
  59. package/types/helpers/service-base.d.ts +0 -10
  60. package/types/http/express-adapter.d.ts +0 -13
  61. package/types/http/http-adapter.d.ts +0 -54
  62. package/types/http/http-context.d.ts +0 -44
  63. package/types/http/http-handler.d.ts +0 -75
  64. package/types/http/impl/http-incoming.host.d.ts +0 -22
  65. package/types/http/impl/http-outgoing.host.d.ts +0 -17
  66. package/types/http/impl/multipart-reader.d.ts +0 -46
  67. package/types/http/impl/node-incoming-message.host.d.ts +0 -45
  68. package/types/http/impl/node-outgoing-message.host.d.ts +0 -49
  69. package/types/http/interfaces/http-incoming.interface.d.ts +0 -192
  70. package/types/http/interfaces/http-outgoing.interface.d.ts +0 -144
  71. package/types/http/interfaces/node-incoming-message.interface.d.ts +0 -36
  72. package/types/http/interfaces/node-outgoing-message.interface.d.ts +0 -27
  73. package/types/http/utils/body-reader.d.ts +0 -38
  74. package/types/http/utils/common.d.ts +0 -17
  75. package/types/http/utils/concat-readable.d.ts +0 -2
  76. package/types/http/utils/convert-to-headers.d.ts +0 -2
  77. package/types/http/utils/convert-to-raw-headers.d.ts +0 -2
  78. package/types/http/utils/match-known-fields.d.ts +0 -6
  79. package/types/http/utils/wrap-exception.d.ts +0 -2
  80. package/types/type-guards.d.ts +0 -8
  81. /package/cjs/{http/impl/asset-cache.js → asset-cache.js} +0 -0
  82. /package/cjs/{helpers/service-base.js → service-base.js} +0 -0
  83. /package/esm/{http/impl/asset-cache.js → asset-cache.js} +0 -0
  84. /package/esm/{helpers/service-base.js → service-base.js} +0 -0
  85. /package/types/{http/impl/asset-cache.d.ts → asset-cache.d.ts} +0 -0
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExecutionContext = void 0;
4
- const strict_typed_events_1 = require("strict-typed-events");
4
+ const node_events_async_1 = require("node-events-async");
5
5
  /**
6
6
  * @class ExecutionContext
7
7
  */
8
- class ExecutionContext extends strict_typed_events_1.AsyncEventEmitter {
8
+ class ExecutionContext extends node_events_async_1.AsyncEventEmitter {
9
9
  constructor(init) {
10
10
  super();
11
11
  this.errors = [];
@@ -13,17 +13,5 @@ class ExecutionContext extends strict_typed_events_1.AsyncEventEmitter {
13
13
  this.protocol = init.protocol;
14
14
  this.platform = init.platform;
15
15
  }
16
- addListener(event, listener) {
17
- return super.addListener(event, listener);
18
- }
19
- removeListener(event, listener) {
20
- return super.removeListener(event, listener);
21
- }
22
- on(event, listener) {
23
- return super.on(event, listener);
24
- }
25
- off(event, listener) {
26
- return super.off(event, listener);
27
- }
28
16
  }
29
17
  exports.ExecutionContext = ExecutionContext;
package/cjs/index.js CHANGED
@@ -1,33 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.classes = void 0;
4
3
  const tslib_1 = require("tslib");
5
4
  require("reflect-metadata");
6
5
  require("./augmentation/18n.augmentation.js");
7
- require("./augmentation/http-controller.augmentation.js");
8
- const HttpIncomingHost_ = tslib_1.__importStar(require("./http/impl/http-incoming.host.js"));
9
- const HttpOutgoingHost_ = tslib_1.__importStar(require("./http/impl/http-outgoing.host.js"));
10
- const NodeIncomingMessageHost_ = tslib_1.__importStar(require("./http/impl/node-incoming-message.host.js"));
11
- const NodeOutgoingMessageHost_ = tslib_1.__importStar(require("./http/impl/node-outgoing-message.host.js"));
6
+ tslib_1.__exportStar(require("./asset-cache.js"), exports);
7
+ tslib_1.__exportStar(require("./constants.js"), exports);
12
8
  tslib_1.__exportStar(require("./execution-context.js"), exports);
13
- tslib_1.__exportStar(require("./helpers/service-base.js"), exports);
14
- tslib_1.__exportStar(require("./http/express-adapter.js"), exports);
15
- tslib_1.__exportStar(require("./http/http-adapter.js"), exports);
16
- tslib_1.__exportStar(require("./http/http-context.js"), exports);
17
- tslib_1.__exportStar(require("./http/http-handler.js"), exports);
18
- tslib_1.__exportStar(require("./http/impl/multipart-reader.js"), exports);
19
- tslib_1.__exportStar(require("./http/interfaces/http-incoming.interface.js"), exports);
20
- tslib_1.__exportStar(require("./http/interfaces/http-outgoing.interface.js"), exports);
21
- tslib_1.__exportStar(require("./http/interfaces/node-incoming-message.interface.js"), exports);
22
- tslib_1.__exportStar(require("./http/interfaces/node-outgoing-message.interface.js"), exports);
23
- tslib_1.__exportStar(require("./http/utils/wrap-exception.js"), exports);
24
9
  tslib_1.__exportStar(require("./interfaces/logger.interface.js"), exports);
25
10
  tslib_1.__exportStar(require("./platform-adapter.js"), exports);
26
- tslib_1.__exportStar(require("./type-guards.js"), exports);
27
- var classes;
28
- (function (classes) {
29
- classes.HttpIncomingHost = HttpIncomingHost_.HttpIncomingHost;
30
- classes.HttpOutgoingHost = HttpOutgoingHost_.HttpOutgoingHost;
31
- classes.NodeIncomingMessageHost = NodeIncomingMessageHost_.NodeIncomingMessageHost;
32
- classes.NodeOutgoingMessageHost = NodeOutgoingMessageHost_.NodeOutgoingMessageHost;
33
- })(classes || (exports.classes = classes = {}));
11
+ tslib_1.__exportStar(require("./service-base.js"), exports);
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlatformAdapter = void 0;
4
4
  require("./augmentation/18n.augmentation.js");
5
5
  const common_1 = require("@opra/common");
6
- const strict_typed_events_1 = require("strict-typed-events");
6
+ const node_events_async_1 = require("node-events-async");
7
+ const asset_cache_js_1 = require("./asset-cache.js");
7
8
  const constants_js_1 = require("./constants.js");
8
- const asset_cache_js_1 = require("./http/impl/asset-cache.js");
9
9
  /**
10
10
  * @class PlatformAdapter
11
11
  */
12
- class PlatformAdapter extends strict_typed_events_1.AsyncEventEmitter {
12
+ class PlatformAdapter extends node_events_async_1.AsyncEventEmitter {
13
13
  constructor(document, options) {
14
14
  super();
15
15
  this[constants_js_1.kAssetCache] = new asset_cache_js_1.AssetCache();
@@ -1,4 +1,4 @@
1
- import { AsyncEventEmitter } from 'strict-typed-events';
1
+ import { AsyncEventEmitter } from 'node-events-async';
2
2
  /**
3
3
  * @class ExecutionContext
4
4
  */
@@ -10,16 +10,4 @@ export class ExecutionContext extends AsyncEventEmitter {
10
10
  this.protocol = init.protocol;
11
11
  this.platform = init.platform;
12
12
  }
13
- addListener(event, listener) {
14
- return super.addListener(event, listener);
15
- }
16
- removeListener(event, listener) {
17
- return super.removeListener(event, listener);
18
- }
19
- on(event, listener) {
20
- return super.on(event, listener);
21
- }
22
- off(event, listener) {
23
- return super.off(event, listener);
24
- }
25
13
  }
package/esm/index.js CHANGED
@@ -1,29 +1,8 @@
1
1
  import 'reflect-metadata';
2
2
  import './augmentation/18n.augmentation.js';
3
- import './augmentation/http-controller.augmentation.js';
4
- import * as HttpIncomingHost_ from './http/impl/http-incoming.host.js';
5
- import * as HttpOutgoingHost_ from './http/impl/http-outgoing.host.js';
6
- import * as NodeIncomingMessageHost_ from './http/impl/node-incoming-message.host.js';
7
- import * as NodeOutgoingMessageHost_ from './http/impl/node-outgoing-message.host.js';
3
+ export * from './asset-cache.js';
4
+ export * from './constants.js';
8
5
  export * from './execution-context.js';
9
- export * from './helpers/service-base.js';
10
- export * from './http/express-adapter.js';
11
- export * from './http/http-adapter.js';
12
- export * from './http/http-context.js';
13
- export * from './http/http-handler.js';
14
- export * from './http/impl/multipart-reader.js';
15
- export * from './http/interfaces/http-incoming.interface.js';
16
- export * from './http/interfaces/http-outgoing.interface.js';
17
- export * from './http/interfaces/node-incoming-message.interface.js';
18
- export * from './http/interfaces/node-outgoing-message.interface.js';
19
- export * from './http/utils/wrap-exception.js';
20
6
  export * from './interfaces/logger.interface.js';
21
7
  export * from './platform-adapter.js';
22
- export * from './type-guards.js';
23
- export var classes;
24
- (function (classes) {
25
- classes.HttpIncomingHost = HttpIncomingHost_.HttpIncomingHost;
26
- classes.HttpOutgoingHost = HttpOutgoingHost_.HttpOutgoingHost;
27
- classes.NodeIncomingMessageHost = NodeIncomingMessageHost_.NodeIncomingMessageHost;
28
- classes.NodeOutgoingMessageHost = NodeOutgoingMessageHost_.NodeOutgoingMessageHost;
29
- })(classes || (classes = {}));
8
+ export * from './service-base.js';
@@ -1,8 +1,8 @@
1
1
  import './augmentation/18n.augmentation.js';
2
2
  import { I18n } from '@opra/common';
3
- import { AsyncEventEmitter } from 'strict-typed-events';
3
+ import { AsyncEventEmitter } from 'node-events-async';
4
+ import { AssetCache } from './asset-cache.js';
4
5
  import { kAssetCache } from './constants.js';
5
- import { AssetCache } from './http/impl/asset-cache.js';
6
6
  /**
7
7
  * @class PlatformAdapter
8
8
  */
package/package.json CHANGED
@@ -1,44 +1,14 @@
1
1
  {
2
2
  "name": "@opra/core",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Opra schema package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "@browsery/antlr4": "^4.13.3-r1",
9
- "@browsery/http-parser": "^0.5.9-r1",
10
- "@browsery/type-is": "^1.6.18-r5",
11
- "@opra/common": "^1.0.0-beta.1",
12
- "accepts": "^1.3.8",
13
- "base64-stream": "^1.0.0",
14
- "busboy": "^1.6.0",
15
- "bytes": "^3.1.2",
16
- "content-disposition": "^0.5.4",
17
- "content-type": "^1.0.5",
18
- "cookie": "^0.6.0",
19
- "cookie-signature": "^1.2.1",
20
- "cppzst": "^2.0.12",
21
- "encodeurl": "^2.0.0",
22
- "fast-tokenizer": "^1.7.0",
23
- "fresh": "^0.5.2",
24
- "iconv-lite": "^0.6.3",
25
- "mime-types": "^2.1.35",
26
- "power-tasks": "^1.11.0",
27
- "putil-isplainobject": "^1.1.5",
28
- "putil-merge": "^3.13.0",
29
- "putil-varhelpers": "^1.6.5",
30
- "range-parser": "^1.2.1",
31
- "raw-body": "^3.0.0",
8
+ "@opra/common": "^1.0.0-beta.3",
9
+ "node-events-async": "^1.0.0",
32
10
  "reflect-metadata": "^0.2.2",
33
- "strict-typed-events": "^2.8.0",
34
- "super-fast-md5": "^1.0.3",
35
- "tslib": "^2.7.0",
36
- "valgen": "^5.9.0",
37
- "vary": "^1.1.2"
38
- },
39
- "optionalDependencies": {
40
- "express": "^4.x.x || ^5.x.x",
41
- "fastify": "^4.x.x"
11
+ "tslib": "^2.7.0"
42
12
  },
43
13
  "type": "module",
44
14
  "exports": {
@@ -79,10 +49,6 @@
79
49
  "opra",
80
50
  "rest",
81
51
  "api",
82
- "openapi",
83
- "http",
84
- "web",
85
- "swagger",
86
- "raml"
52
+ "core"
87
53
  ]
88
54
  }
@@ -1,27 +1,22 @@
1
1
  import { ApiDocument, OpraHttpError, OpraSchema } from '@opra/common';
2
- import { AsyncEventEmitter } from 'strict-typed-events';
2
+ import { AsyncEventEmitter } from 'node-events-async';
3
3
  /**
4
4
  * @namespace ExecutionContext
5
5
  */
6
6
  export declare namespace ExecutionContext {
7
7
  interface Initiator {
8
8
  document: ApiDocument;
9
- protocol: OpraSchema.Protocol;
9
+ protocol: OpraSchema.Transport;
10
10
  platform: string;
11
11
  }
12
- type OnFinishListener = (error: Error | undefined, context: ExecutionContext) => void | Promise<void>;
13
12
  }
14
13
  /**
15
14
  * @class ExecutionContext
16
15
  */
17
16
  export declare abstract class ExecutionContext extends AsyncEventEmitter {
18
17
  readonly document: ApiDocument;
19
- readonly protocol: OpraSchema.Protocol;
18
+ readonly protocol: OpraSchema.Transport;
20
19
  readonly platform: string;
21
20
  errors: OpraHttpError[];
22
21
  protected constructor(init: ExecutionContext.Initiator);
23
- addListener(event: 'finish', listener: ExecutionContext.OnFinishListener): this;
24
- removeListener(event: 'finish', listener: ExecutionContext.OnFinishListener): this;
25
- on(event: 'finish', listener: ExecutionContext.OnFinishListener): this;
26
- off(event: 'finish', listener: ExecutionContext.OnFinishListener): this;
27
22
  }
package/types/index.d.cts CHANGED
@@ -1,28 +1,8 @@
1
1
  import 'reflect-metadata';
2
2
  import './augmentation/18n.augmentation.js';
3
- import './augmentation/http-controller.augmentation.js';
4
- import * as HttpIncomingHost_ from './http/impl/http-incoming.host.js';
5
- import * as HttpOutgoingHost_ from './http/impl/http-outgoing.host.js';
6
- import * as NodeIncomingMessageHost_ from './http/impl/node-incoming-message.host.js';
7
- import * as NodeOutgoingMessageHost_ from './http/impl/node-outgoing-message.host.js';
3
+ export * from './asset-cache.js';
4
+ export * from './constants.js';
8
5
  export * from './execution-context.js';
9
- export * from './helpers/service-base.js';
10
- export * from './http/express-adapter.js';
11
- export * from './http/http-adapter.js';
12
- export * from './http/http-context.js';
13
- export * from './http/http-handler.js';
14
- export * from './http/impl/multipart-reader.js';
15
- export * from './http/interfaces/http-incoming.interface.js';
16
- export * from './http/interfaces/http-outgoing.interface.js';
17
- export * from './http/interfaces/node-incoming-message.interface.js';
18
- export * from './http/interfaces/node-outgoing-message.interface.js';
19
- export * from './http/utils/wrap-exception.js';
20
6
  export * from './interfaces/logger.interface.js';
21
7
  export * from './platform-adapter.js';
22
- export * from './type-guards.js';
23
- export declare namespace classes {
24
- export import HttpIncomingHost = HttpIncomingHost_.HttpIncomingHost;
25
- export import HttpOutgoingHost = HttpOutgoingHost_.HttpOutgoingHost;
26
- export import NodeIncomingMessageHost = NodeIncomingMessageHost_.NodeIncomingMessageHost;
27
- export import NodeOutgoingMessageHost = NodeOutgoingMessageHost_.NodeOutgoingMessageHost;
28
- }
8
+ export * from './service-base.js';
package/types/index.d.ts CHANGED
@@ -1,28 +1,8 @@
1
1
  import 'reflect-metadata';
2
2
  import './augmentation/18n.augmentation.js';
3
- import './augmentation/http-controller.augmentation.js';
4
- import * as HttpIncomingHost_ from './http/impl/http-incoming.host.js';
5
- import * as HttpOutgoingHost_ from './http/impl/http-outgoing.host.js';
6
- import * as NodeIncomingMessageHost_ from './http/impl/node-incoming-message.host.js';
7
- import * as NodeOutgoingMessageHost_ from './http/impl/node-outgoing-message.host.js';
3
+ export * from './asset-cache.js';
4
+ export * from './constants.js';
8
5
  export * from './execution-context.js';
9
- export * from './helpers/service-base.js';
10
- export * from './http/express-adapter.js';
11
- export * from './http/http-adapter.js';
12
- export * from './http/http-context.js';
13
- export * from './http/http-handler.js';
14
- export * from './http/impl/multipart-reader.js';
15
- export * from './http/interfaces/http-incoming.interface.js';
16
- export * from './http/interfaces/http-outgoing.interface.js';
17
- export * from './http/interfaces/node-incoming-message.interface.js';
18
- export * from './http/interfaces/node-outgoing-message.interface.js';
19
- export * from './http/utils/wrap-exception.js';
20
6
  export * from './interfaces/logger.interface.js';
21
7
  export * from './platform-adapter.js';
22
- export * from './type-guards.js';
23
- export declare namespace classes {
24
- export import HttpIncomingHost = HttpIncomingHost_.HttpIncomingHost;
25
- export import HttpOutgoingHost = HttpOutgoingHost_.HttpOutgoingHost;
26
- export import NodeIncomingMessageHost = NodeIncomingMessageHost_.NodeIncomingMessageHost;
27
- export import NodeOutgoingMessageHost = NodeOutgoingMessageHost_.NodeOutgoingMessageHost;
28
- }
8
+ export * from './service-base.js';
@@ -1,8 +1,8 @@
1
1
  export interface ILogger {
2
+ error(message: any, ...optionalParams: any[]): void;
2
3
  log?(message: any, ...optionalParams: any[]): void;
3
4
  info?(message: any, ...optionalParams: any[]): void;
4
5
  warn?(message: any, ...optionalParams: any[]): void;
5
- error?(message: any, ...optionalParams: any[]): void;
6
6
  fatal?(message: any, ...optionalParams: any[]): void;
7
7
  debug?(message: any, ...optionalParams: any[]): void;
8
8
  verbose?(message: any, ...optionalParams: any[]): void;
@@ -1,8 +1,8 @@
1
1
  import './augmentation/18n.augmentation.js';
2
2
  import { ApiDocument, I18n, OpraSchema } from '@opra/common';
3
- import { AsyncEventEmitter } from 'strict-typed-events';
3
+ import { AsyncEventEmitter } from 'node-events-async';
4
+ import { AssetCache } from './asset-cache.js';
4
5
  import { kAssetCache } from './constants.js';
5
- import { AssetCache } from './http/impl/asset-cache.js';
6
6
  /**
7
7
  * @namespace PlatformAdapter
8
8
  */
@@ -17,7 +17,7 @@ export declare namespace PlatformAdapter {
17
17
  export declare abstract class PlatformAdapter extends AsyncEventEmitter {
18
18
  protected [kAssetCache]: AssetCache;
19
19
  readonly document: ApiDocument;
20
- abstract readonly protocol: OpraSchema.Protocol;
20
+ abstract readonly protocol: OpraSchema.Transport;
21
21
  i18n: I18n;
22
22
  protected constructor(document: ApiDocument, options?: PlatformAdapter.Options);
23
23
  abstract close(): Promise<void>;
@@ -0,0 +1,10 @@
1
+ import type { Nullish } from 'ts-gems';
2
+ import type { ExecutionContext } from './execution-context.js';
3
+ export declare abstract class ServiceBase {
4
+ protected _context: ExecutionContext;
5
+ get context(): ExecutionContext;
6
+ for<C extends ExecutionContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
7
+ }
8
+ export declare namespace ServiceBase {
9
+ const extendSymbol: unique symbol;
10
+ }
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const common_1 = require("@opra/common");
4
- // @ts-ignore
5
- const oldInitialize = common_1.HttpController.prototype._initialize;
6
- // @ts-ignore
7
- common_1.HttpController.prototype._initialize = function (initArgs) {
8
- oldInitialize?.call(this, initArgs);
9
- this.onInit = initArgs.onInit;
10
- this.onShutdown = initArgs.onShutdown;
11
- };
12
- common_1.HttpController.OnInit = function () {
13
- return (target, propertyKey) => {
14
- const sourceMetadata = (Reflect.getOwnMetadata(common_1.HTTP_CONTROLLER_METADATA, target.constructor) || {});
15
- sourceMetadata.onInit = target[propertyKey];
16
- Reflect.defineMetadata(common_1.HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
17
- };
18
- };
19
- common_1.HttpController.OnShutdown = function () {
20
- return (target, propertyKey) => {
21
- const sourceMetadata = (Reflect.getOwnMetadata(common_1.HTTP_CONTROLLER_METADATA, target.constructor) || {});
22
- sourceMetadata.onShutdown = target[propertyKey];
23
- Reflect.defineMetadata(common_1.HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
24
- };
25
- };
@@ -1,155 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExpressAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- const nodePath = tslib_1.__importStar(require("node:path"));
6
- const common_1 = require("@opra/common");
7
- const express_1 = require("express");
8
- const http_adapter_js_1 = require("./http-adapter.js");
9
- const http_context_js_1 = require("./http-context.js");
10
- const http_incoming_interface_js_1 = require("./interfaces/http-incoming.interface.js");
11
- const http_outgoing_interface_js_1 = require("./interfaces/http-outgoing.interface.js");
12
- const wrap_exception_js_1 = require("./utils/wrap-exception.js");
13
- class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
14
- constructor(app, document, options) {
15
- super(document, options);
16
- this._controllerInstances = new Map();
17
- this.app = app;
18
- if (!(this.document.api instanceof common_1.HttpApi))
19
- throw new TypeError('document.api must be instance of HttpApi');
20
- for (const c of this.api.controllers.values())
21
- this._createControllers(c);
22
- this._initRouter(options?.basePath);
23
- }
24
- get platform() {
25
- return 'express';
26
- }
27
- async close() {
28
- const processResource = async (resource) => {
29
- if (resource.controllers.size) {
30
- const subResources = Array.from(resource.controllers.values());
31
- subResources.reverse();
32
- for (const subResource of subResources) {
33
- await processResource(subResource);
34
- }
35
- }
36
- if (resource.onShutdown) {
37
- const instance = this._controllerInstances.get(resource) || resource.instance;
38
- if (instance) {
39
- try {
40
- await resource.onShutdown.call(instance, resource);
41
- }
42
- catch (e) {
43
- if (this.listenerCount('error'))
44
- this.emit('error', (0, wrap_exception_js_1.wrapException)(e));
45
- }
46
- }
47
- }
48
- };
49
- for (const c of this.api.controllers.values())
50
- await processResource(c);
51
- this._controllerInstances.clear();
52
- }
53
- getControllerInstance(controllerPath) {
54
- const controller = this.api.findController(controllerPath);
55
- return controller && this._controllerInstances.get(controller);
56
- }
57
- _initRouter(basePath) {
58
- const router = (0, express_1.Router)();
59
- if (basePath) {
60
- if (!basePath.startsWith('/'))
61
- basePath = '/' + basePath;
62
- if (basePath)
63
- this.app.use(basePath, router);
64
- }
65
- else
66
- this.app.use(router);
67
- const createContext = async (_req, _res, args) => {
68
- const request = http_incoming_interface_js_1.HttpIncoming.from(_req);
69
- const response = http_outgoing_interface_js_1.HttpOutgoing.from(_res);
70
- const ctx = new http_context_js_1.HttpContext({
71
- adapter: this,
72
- platform: this.platform,
73
- request,
74
- response,
75
- controller: args?.controller,
76
- controllerInstance: args?.controllerInstance,
77
- operation: args?.operation,
78
- operationHandler: args?.operationHandler,
79
- });
80
- await this.emitAsync('createContext', ctx);
81
- return ctx;
82
- };
83
- /** Add an endpoint that returns document schema */
84
- router.get('/\\$schema', (_req, _res, next) => {
85
- createContext(_req, _res)
86
- .then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
87
- .catch(next);
88
- });
89
- /** Add operation endpoints */
90
- if (this.api.controllers.size) {
91
- const processResource = (controller, currentPath) => {
92
- currentPath = nodePath.join(currentPath, controller.path);
93
- for (const operation of controller.operations.values()) {
94
- const routePath = currentPath + (operation.path || '');
95
- const controllerInstance = this._controllerInstances.get(controller);
96
- const operationHandler = controllerInstance[operation.name];
97
- if (!operationHandler)
98
- continue;
99
- /** Define router callback */
100
- router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
101
- createContext(_req, _res, {
102
- controller,
103
- controllerInstance,
104
- operation,
105
- operationHandler,
106
- })
107
- .then(ctx => this.handler.handleRequest(ctx))
108
- .then(() => {
109
- if (!_res.headersSent)
110
- _next();
111
- })
112
- .catch((e) => this.emit('error', e));
113
- });
114
- }
115
- if (controller.controllers.size) {
116
- for (const child of controller.controllers.values())
117
- processResource(child, currentPath);
118
- }
119
- };
120
- for (const c of this.api.controllers.values())
121
- processResource(c, '/');
122
- }
123
- /** Add an endpoint that returns 404 error at last */
124
- router.use('*', (_req, _res, next) => {
125
- createContext(_req, _res)
126
- .then(ctx => {
127
- ctx.errors.push(new common_1.NotFoundError({
128
- message: `No endpoint found at [${_req.method}]${_req.baseUrl}`,
129
- details: {
130
- path: _req.baseUrl,
131
- method: _req.method,
132
- },
133
- }));
134
- this.handler.sendResponse(ctx).catch(next);
135
- })
136
- .catch(next);
137
- });
138
- }
139
- _createControllers(controller) {
140
- let instance = controller.instance;
141
- if (!instance && controller.ctor)
142
- instance = new controller.ctor();
143
- if (instance) {
144
- if (typeof instance.onInit === 'function')
145
- instance.onInit.call(instance, this);
146
- this._controllerInstances.set(controller, instance);
147
- // Initialize sub resources
148
- for (const r of controller.controllers.values()) {
149
- this._createControllers(r);
150
- }
151
- }
152
- return instance;
153
- }
154
- }
155
- exports.ExpressAdapter = ExpressAdapter;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpAdapter = void 0;
4
- const common_1 = require("@opra/common");
5
- const platform_adapter_js_1 = require("../platform-adapter.js");
6
- const http_handler_js_1 = require("./http-handler.js");
7
- /**
8
- *
9
- * @class HttpAdapter
10
- */
11
- class HttpAdapter extends platform_adapter_js_1.PlatformAdapter {
12
- constructor(document, options) {
13
- super(document, options);
14
- this.protocol = 'http';
15
- if (!(document.api instanceof common_1.HttpApi))
16
- throw new TypeError(`The document does not expose an HTTP Api`);
17
- this.handler = new http_handler_js_1.HttpHandler(this);
18
- this.interceptors = [...(options?.interceptors || [])];
19
- }
20
- get api() {
21
- return this.document.api;
22
- }
23
- }
24
- exports.HttpAdapter = HttpAdapter;