@opra/http 1.0.4 → 1.0.5

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.
@@ -10,19 +10,24 @@ const http_context_js_1 = require("./http-context.js");
10
10
  const http_incoming_interface_js_1 = require("./interfaces/http-incoming.interface.js");
11
11
  const http_outgoing_interface_js_1 = require("./interfaces/http-outgoing.interface.js");
12
12
  class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
13
- constructor(app, document, options) {
14
- super(document, options);
13
+ constructor(app, options) {
14
+ super(options);
15
15
  this._controllerInstances = new Map();
16
16
  this.app = app;
17
- if (!(this.document.api instanceof common_1.HttpApi))
18
- throw new TypeError('document.api must be instance of HttpApi');
19
- for (const c of this.api.controllers.values())
20
- this._createControllers(c);
21
- this._initRouter();
22
17
  }
23
18
  get platform() {
24
19
  return 'express';
25
20
  }
21
+ initialize(document) {
22
+ if (this._document)
23
+ throw new TypeError(`${this.constructor.name} already initialized.`);
24
+ if (!(document.api instanceof common_1.HttpApi))
25
+ throw new TypeError(`The document does not expose an HTTP Api`);
26
+ this._document = document;
27
+ for (const c of this.api.controllers.values())
28
+ this._createControllers(c);
29
+ this._initRouter();
30
+ }
26
31
  async close() {
27
32
  const processInstance = async (controller) => {
28
33
  if (controller.controllers.size) {
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpAdapter = void 0;
4
- const common_1 = require("@opra/common");
5
4
  const core_1 = require("@opra/core");
6
5
  const http_handler_js_1 = require("./http-handler.js");
7
6
  /**
@@ -9,11 +8,9 @@ const http_handler_js_1 = require("./http-handler.js");
9
8
  * @class HttpAdapter
10
9
  */
11
10
  class HttpAdapter extends core_1.PlatformAdapter {
12
- constructor(document, options) {
13
- super(document, options);
11
+ constructor(options) {
12
+ super(options);
14
13
  this.protocol = 'http';
15
- if (!(document.api instanceof common_1.HttpApi))
16
- throw new TypeError(`The document does not expose an HTTP Api`);
17
14
  this.handler = new http_handler_js_1.HttpHandler(this);
18
15
  this.interceptors = [...(options?.interceptors || [])];
19
16
  this.basePath = options?.basePath || '/';
@@ -394,8 +394,21 @@ class HttpHandler {
394
394
  async _sendErrorResponse(context) {
395
395
  context.errors = this._wrapExceptions(context.errors);
396
396
  try {
397
- await this.adapter.emitAsync('error', context);
398
- context.errors = this._wrapExceptions(context.errors);
397
+ if (context.listenerCount('error')) {
398
+ await this.adapter.emitAsync('error', context);
399
+ context.errors = this._wrapExceptions(context.errors);
400
+ }
401
+ if (this.adapter.listenerCount('error')) {
402
+ await this.adapter.emitAsync('error', context.errors[0], context);
403
+ context.errors = this._wrapExceptions(context.errors);
404
+ }
405
+ if (this.adapter.logger?.error) {
406
+ const logger = this.adapter.logger;
407
+ context.errors.forEach(e => {
408
+ if (e.status >= 500 && e.status < 600)
409
+ logger.error(e);
410
+ });
411
+ }
399
412
  }
400
413
  catch (e) {
401
414
  context.errors = this._wrapExceptions([e, ...context.errors]);
@@ -6,19 +6,24 @@ import { HttpContext } from './http-context.js';
6
6
  import { HttpIncoming } from './interfaces/http-incoming.interface.js';
7
7
  import { HttpOutgoing } from './interfaces/http-outgoing.interface.js';
8
8
  export class ExpressAdapter extends HttpAdapter {
9
- constructor(app, document, options) {
10
- super(document, options);
9
+ constructor(app, options) {
10
+ super(options);
11
11
  this._controllerInstances = new Map();
12
12
  this.app = app;
13
- if (!(this.document.api instanceof HttpApi))
14
- throw new TypeError('document.api must be instance of HttpApi');
15
- for (const c of this.api.controllers.values())
16
- this._createControllers(c);
17
- this._initRouter();
18
13
  }
19
14
  get platform() {
20
15
  return 'express';
21
16
  }
17
+ initialize(document) {
18
+ if (this._document)
19
+ throw new TypeError(`${this.constructor.name} already initialized.`);
20
+ if (!(document.api instanceof HttpApi))
21
+ throw new TypeError(`The document does not expose an HTTP Api`);
22
+ this._document = document;
23
+ for (const c of this.api.controllers.values())
24
+ this._createControllers(c);
25
+ this._initRouter();
26
+ }
22
27
  async close() {
23
28
  const processInstance = async (controller) => {
24
29
  if (controller.controllers.size) {
@@ -1,4 +1,3 @@
1
- import { HttpApi } from '@opra/common';
2
1
  import { PlatformAdapter } from '@opra/core';
3
2
  import { HttpHandler } from './http-handler.js';
4
3
  /**
@@ -6,11 +5,9 @@ import { HttpHandler } from './http-handler.js';
6
5
  * @class HttpAdapter
7
6
  */
8
7
  export class HttpAdapter extends PlatformAdapter {
9
- constructor(document, options) {
10
- super(document, options);
8
+ constructor(options) {
9
+ super(options);
11
10
  this.protocol = 'http';
12
- if (!(document.api instanceof HttpApi))
13
- throw new TypeError(`The document does not expose an HTTP Api`);
14
11
  this.handler = new HttpHandler(this);
15
12
  this.interceptors = [...(options?.interceptors || [])];
16
13
  this.basePath = options?.basePath || '/';
@@ -390,8 +390,21 @@ export class HttpHandler {
390
390
  async _sendErrorResponse(context) {
391
391
  context.errors = this._wrapExceptions(context.errors);
392
392
  try {
393
- await this.adapter.emitAsync('error', context);
394
- context.errors = this._wrapExceptions(context.errors);
393
+ if (context.listenerCount('error')) {
394
+ await this.adapter.emitAsync('error', context);
395
+ context.errors = this._wrapExceptions(context.errors);
396
+ }
397
+ if (this.adapter.listenerCount('error')) {
398
+ await this.adapter.emitAsync('error', context.errors[0], context);
399
+ context.errors = this._wrapExceptions(context.errors);
400
+ }
401
+ if (this.adapter.logger?.error) {
402
+ const logger = this.adapter.logger;
403
+ context.errors.forEach(e => {
404
+ if (e.status >= 500 && e.status < 600)
405
+ logger.error(e);
406
+ });
407
+ }
395
408
  }
396
409
  catch (e) {
397
410
  context.errors = this._wrapExceptions([e, ...context.errors]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/http",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Opra Http Server Adapter",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -8,8 +8,8 @@
8
8
  "@browsery/antlr4": "^4.13.3-r1",
9
9
  "@browsery/http-parser": "^0.5.9-r1",
10
10
  "@browsery/type-is": "^1.6.18-r5",
11
- "@opra/common": "^1.0.4",
12
- "@opra/core": "^1.0.4",
11
+ "@opra/common": "^1.0.5",
12
+ "@opra/core": "^1.0.5",
13
13
  "accepts": "^1.3.8",
14
14
  "base64-stream": "^1.0.0",
15
15
  "busboy": "^1.6.0",
@@ -33,7 +33,7 @@
33
33
  "raw-body": "^3.0.0",
34
34
  "reflect-metadata": "^0.2.2",
35
35
  "super-fast-md5": "^1.0.3",
36
- "tslib": "^2.7.0",
36
+ "tslib": "^2.8.0",
37
37
  "valgen": "^5.10.0",
38
38
  "vary": "^1.1.2"
39
39
  },
@@ -4,8 +4,9 @@ import { HttpAdapter } from './http-adapter.js';
4
4
  export declare class ExpressAdapter extends HttpAdapter {
5
5
  readonly app: Application;
6
6
  protected _controllerInstances: Map<HttpController, any>;
7
- constructor(app: Application, document: ApiDocument, options?: HttpAdapter.Options);
7
+ constructor(app: Application, options?: HttpAdapter.Options);
8
8
  get platform(): string;
9
+ initialize(document: ApiDocument): void;
9
10
  close(): Promise<void>;
10
11
  getControllerInstance<T>(controllerPath: string): T | undefined;
11
12
  protected _initRouter(): void;
@@ -1,4 +1,4 @@
1
- import { ApiDocument, HttpApi, OpraSchema } from '@opra/common';
1
+ import { HttpApi, OpraSchema } from '@opra/common';
2
2
  import { PlatformAdapter } from '@opra/core';
3
3
  import { HttpContext } from './http-context.js';
4
4
  import { HttpHandler } from './http-handler.js';
@@ -28,6 +28,6 @@ export declare abstract class HttpAdapter extends PlatformAdapter {
28
28
  readonly protocol: OpraSchema.Transport;
29
29
  readonly basePath: string;
30
30
  interceptors: (HttpAdapter.InterceptorFunction | HttpAdapter.IHttpInterceptor)[];
31
- protected constructor(document: ApiDocument, options?: HttpAdapter.Options);
31
+ protected constructor(options?: HttpAdapter.Options);
32
32
  get api(): HttpApi;
33
33
  }
@@ -1,4 +1,4 @@
1
- import { HttpController, HttpMediaType, HttpOperation, OpraSchema } from '@opra/common';
1
+ import { HttpController, HttpMediaType, HttpOperation, OpraHttpError, OpraSchema } from '@opra/common';
2
2
  import { ExecutionContext } from '@opra/core';
3
3
  import type { HttpAdapter } from './http-adapter';
4
4
  import { MultipartReader } from './impl/multipart-reader.js';
@@ -37,6 +37,7 @@ export declare class HttpContext extends ExecutionContext {
37
37
  readonly headers: Record<string, any>;
38
38
  readonly pathParams: Record<string, any>;
39
39
  readonly queryParams: Record<string, any>;
40
+ errors: OpraHttpError[];
40
41
  constructor(init: HttpContext.Initiator);
41
42
  get isMultipart(): boolean;
42
43
  getMultipartReader(): Promise<MultipartReader>;