@opra/nestjs 1.0.0-alpha.23 → 1.0.0-alpha.24

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.
@@ -4,9 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.OpraHttpCoreModule = void 0;
5
5
  const tslib_1 = require("tslib");
6
6
  const common_1 = require("@nestjs/common");
7
+ const core_1 = require("@nestjs/core");
7
8
  const common_2 = require("@opra/common");
8
9
  const ts_gems_1 = require("ts-gems");
9
10
  const opra_nestjs_adapter_js_1 = require("./opra-nestjs-adapter.js");
11
+ const opra_exception_filter_1 = require("./services/opra-exception-filter");
10
12
  const opra_middleware_js_1 = require("./services/opra-middleware.js");
11
13
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
12
14
  constructor(opraAdapter) {
@@ -33,6 +35,10 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
33
35
  return opraAdapter;
34
36
  },
35
37
  },
38
+ {
39
+ provide: core_1.APP_FILTER,
40
+ useClass: opra_exception_filter_1.OpraExceptionFilter,
41
+ },
36
42
  ];
37
43
  if (token !== opra_nestjs_adapter_js_1.OpraNestAdapter) {
38
44
  providers.push({
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraNestAdapter = exports.kHandler = void 0;
3
+ exports.OpraNestAdapter = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const common_1 = require("@nestjs/common");
6
6
  const constants_1 = require("@nestjs/common/constants");
@@ -9,8 +9,6 @@ const core_1 = require("@opra/core");
9
9
  const path_1 = tslib_1.__importDefault(require("path"));
10
10
  const ts_gems_1 = require("ts-gems");
11
11
  const public_decorator_js_1 = require("./decorators/public.decorator.js");
12
- const opra_exception_filter_js_1 = require("./services/opra-exception-filter.js");
13
- exports.kHandler = Symbol.for('kHandler');
14
12
  class OpraNestAdapter extends core_1.HttpAdapter {
15
13
  constructor(init, options) {
16
14
  super((function () {
@@ -23,6 +21,10 @@ class OpraNestAdapter extends core_1.HttpAdapter {
23
21
  let basePath = options?.basePath || '/';
24
22
  if (!basePath.startsWith('/'))
25
23
  basePath = '/' + basePath;
24
+ if (options?.onError)
25
+ this.on('error', options.onError);
26
+ if (options?.onRequest)
27
+ this.on('request', options.onRequest);
26
28
  this._addRootController(basePath);
27
29
  if (init.controllers)
28
30
  init.controllers.forEach(c => this._addToNestControllers(c, basePath, []));
@@ -34,7 +36,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
34
36
  const _this = this;
35
37
  let RootController = class RootController {
36
38
  schema(_req, next) {
37
- _this[exports.kHandler].sendDocumentSchema(_req.opraContext).catch(next);
39
+ _this.handler.sendDocumentSchema(_req.opraContext).catch(() => next());
38
40
  }
39
41
  };
40
42
  tslib_1.__decorate([
@@ -68,8 +70,11 @@ class OpraNestAdapter extends core_1.HttpAdapter {
68
70
  OpraNestAdapter.copyDecoratorMetadataToChild(newClass, parentTree);
69
71
  const newPath = metadata.path ? path_1.default.join(currentPath, metadata.path) : currentPath;
70
72
  const adapter = this;
71
- /** Inject exception filter */
72
- (0, common_1.UseFilters)(new opra_exception_filter_js_1.OpraExceptionFilter(adapter))(newClass);
73
+ // adapter.logger =
74
+ /** Disable default error handler. Errors will be handled by OpraExceptionFilter */
75
+ adapter.handler.onError = (context, error) => {
76
+ throw error;
77
+ };
73
78
  (0, common_1.Controller)()(newClass);
74
79
  this.nestControllers.push(newClass);
75
80
  if (metadata.operations) {
@@ -100,7 +105,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
100
105
  context.controllerInstance = this;
101
106
  context.operationHandler = operationHandler;
102
107
  /** Handle request */
103
- await adapter[exports.kHandler].handleRequest(context);
108
+ await adapter.handler.handleRequest(context);
104
109
  },
105
110
  });
106
111
  /** Copy metadata keys from source function to new one */
@@ -113,7 +118,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
113
118
  (0, common_1.Req)()(newClass.prototype, k, 0);
114
119
  (0, common_1.Res)()(newClass.prototype, k, 1);
115
120
  const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
116
- const operationPath = newPath + (v.path || '');
121
+ const operationPath = v.mergePath ? newPath + (v.path || '') : path_1.default.posix.join(newPath, v.path || '');
117
122
  switch (v.method || 'GET') {
118
123
  case 'DELETE':
119
124
  /** Call @Delete decorator over new property */
@@ -1,18 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraExceptionFilter = exports.kHandler = void 0;
4
- const core_1 = require("@opra/core");
5
- exports.kHandler = Symbol.for('kHandler');
6
- class OpraExceptionFilter {
7
- constructor(adapter) {
8
- this.adapter = adapter;
3
+ exports.OpraExceptionFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const core_1 = require("@nestjs/core");
7
+ const opra_nestjs_adapter_js_1 = require("../opra-nestjs-adapter.js");
8
+ let OpraExceptionFilter = class OpraExceptionFilter extends core_1.BaseExceptionFilter {
9
+ constructor(moduleRef) {
10
+ super();
11
+ this.moduleRef = moduleRef;
9
12
  }
10
13
  catch(exception, host) {
11
- const ctx = host.switchToHttp();
12
- const _res = ctx.getResponse();
13
- const error = (0, core_1.wrapException)(exception);
14
- const response = core_1.HttpOutgoing.from(_res);
15
- return this.adapter[exports.kHandler].sendErrorResponse(response, [error]);
14
+ const ctx = host.switchToHttp().getRequest().opraContext;
15
+ if (ctx) {
16
+ const adapter = this.moduleRef.get(opra_nestjs_adapter_js_1.OpraNestAdapter);
17
+ return adapter.handler.sendErrorResponse(ctx, [exception]);
18
+ }
16
19
  }
17
- }
20
+ };
18
21
  exports.OpraExceptionFilter = OpraExceptionFilter;
22
+ exports.OpraExceptionFilter = OpraExceptionFilter = tslib_1.__decorate([
23
+ (0, common_1.Catch)(),
24
+ tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
25
+ ], OpraExceptionFilter);
@@ -1,9 +1,11 @@
1
1
  var OpraHttpCoreModule_1;
2
2
  import { __decorate, __metadata } from "tslib";
3
3
  import { Global, Module, RequestMethod, } from '@nestjs/common';
4
+ import { APP_FILTER } from '@nestjs/core';
4
5
  import { ApiDocumentFactory } from '@opra/common';
5
6
  import { asMutable } from 'ts-gems';
6
7
  import { OpraNestAdapter } from './opra-nestjs-adapter.js';
8
+ import { OpraExceptionFilter } from './services/opra-exception-filter';
7
9
  import { OpraMiddleware } from './services/opra-middleware.js';
8
10
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
9
11
  constructor(opraAdapter) {
@@ -30,6 +32,10 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
30
32
  return opraAdapter;
31
33
  },
32
34
  },
35
+ {
36
+ provide: APP_FILTER,
37
+ useClass: OpraExceptionFilter,
38
+ },
33
39
  ];
34
40
  if (token !== OpraNestAdapter) {
35
41
  providers.push({
@@ -1,13 +1,11 @@
1
1
  import { __decorate, __metadata, __param } from "tslib";
2
- import { Controller, Delete, Get, Head, Next, Options, Patch, Post, Put, Req, Res, Search, UseFilters, } from '@nestjs/common';
2
+ import { Controller, Delete, Get, Head, Next, Options, Patch, Post, Put, Req, Res, Search } from '@nestjs/common';
3
3
  import { EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA } from '@nestjs/common/constants';
4
4
  import { ApiDocument, HTTP_CONTROLLER_METADATA, HttpApi, isConstructor, NotFoundError, } from '@opra/common';
5
5
  import { HttpAdapter } from '@opra/core';
6
6
  import nodePath from 'path';
7
7
  import { asMutable } from 'ts-gems';
8
8
  import { Public } from './decorators/public.decorator.js';
9
- import { OpraExceptionFilter } from './services/opra-exception-filter.js';
10
- export const kHandler = Symbol.for('kHandler');
11
9
  export class OpraNestAdapter extends HttpAdapter {
12
10
  constructor(init, options) {
13
11
  super((function () {
@@ -20,6 +18,10 @@ export class OpraNestAdapter extends HttpAdapter {
20
18
  let basePath = options?.basePath || '/';
21
19
  if (!basePath.startsWith('/'))
22
20
  basePath = '/' + basePath;
21
+ if (options?.onError)
22
+ this.on('error', options.onError);
23
+ if (options?.onRequest)
24
+ this.on('request', options.onRequest);
23
25
  this._addRootController(basePath);
24
26
  if (init.controllers)
25
27
  init.controllers.forEach(c => this._addToNestControllers(c, basePath, []));
@@ -31,7 +33,7 @@ export class OpraNestAdapter extends HttpAdapter {
31
33
  const _this = this;
32
34
  let RootController = class RootController {
33
35
  schema(_req, next) {
34
- _this[kHandler].sendDocumentSchema(_req.opraContext).catch(next);
36
+ _this.handler.sendDocumentSchema(_req.opraContext).catch(() => next());
35
37
  }
36
38
  };
37
39
  __decorate([
@@ -65,8 +67,11 @@ export class OpraNestAdapter extends HttpAdapter {
65
67
  OpraNestAdapter.copyDecoratorMetadataToChild(newClass, parentTree);
66
68
  const newPath = metadata.path ? nodePath.join(currentPath, metadata.path) : currentPath;
67
69
  const adapter = this;
68
- /** Inject exception filter */
69
- UseFilters(new OpraExceptionFilter(adapter))(newClass);
70
+ // adapter.logger =
71
+ /** Disable default error handler. Errors will be handled by OpraExceptionFilter */
72
+ adapter.handler.onError = (context, error) => {
73
+ throw error;
74
+ };
70
75
  Controller()(newClass);
71
76
  this.nestControllers.push(newClass);
72
77
  if (metadata.operations) {
@@ -97,7 +102,7 @@ export class OpraNestAdapter extends HttpAdapter {
97
102
  context.controllerInstance = this;
98
103
  context.operationHandler = operationHandler;
99
104
  /** Handle request */
100
- await adapter[kHandler].handleRequest(context);
105
+ await adapter.handler.handleRequest(context);
101
106
  },
102
107
  });
103
108
  /** Copy metadata keys from source function to new one */
@@ -110,7 +115,7 @@ export class OpraNestAdapter extends HttpAdapter {
110
115
  Req()(newClass.prototype, k, 0);
111
116
  Res()(newClass.prototype, k, 1);
112
117
  const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
113
- const operationPath = newPath + (v.path || '');
118
+ const operationPath = v.mergePath ? newPath + (v.path || '') : nodePath.posix.join(newPath, v.path || '');
114
119
  switch (v.method || 'GET') {
115
120
  case 'DELETE':
116
121
  /** Call @Delete decorator over new property */
@@ -1,14 +1,22 @@
1
- import { HttpOutgoing, wrapException } from '@opra/core';
2
- export const kHandler = Symbol.for('kHandler');
3
- export class OpraExceptionFilter {
4
- constructor(adapter) {
5
- this.adapter = adapter;
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Catch } from '@nestjs/common';
3
+ import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
4
+ import { OpraNestAdapter } from '../opra-nestjs-adapter.js';
5
+ let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter {
6
+ constructor(moduleRef) {
7
+ super();
8
+ this.moduleRef = moduleRef;
6
9
  }
7
10
  catch(exception, host) {
8
- const ctx = host.switchToHttp();
9
- const _res = ctx.getResponse();
10
- const error = wrapException(exception);
11
- const response = HttpOutgoing.from(_res);
12
- return this.adapter[kHandler].sendErrorResponse(response, [error]);
11
+ const ctx = host.switchToHttp().getRequest().opraContext;
12
+ if (ctx) {
13
+ const adapter = this.moduleRef.get(OpraNestAdapter);
14
+ return adapter.handler.sendErrorResponse(ctx, [exception]);
15
+ }
13
16
  }
14
- }
17
+ };
18
+ OpraExceptionFilter = __decorate([
19
+ Catch(),
20
+ __metadata("design:paramtypes", [ModuleRef])
21
+ ], OpraExceptionFilter);
22
+ export { OpraExceptionFilter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs",
3
- "version": "1.0.0-alpha.23",
3
+ "version": "1.0.0-alpha.24",
4
4
  "description": "Opra NestJS module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -27,8 +27,8 @@
27
27
  "clean:cover": "rimraf ../../coverage/client"
28
28
  },
29
29
  "dependencies": {
30
- "@opra/common": "^1.0.0-alpha.23",
31
- "@opra/core": "^1.0.0-alpha.23",
30
+ "@opra/common": "^1.0.0-alpha.24",
31
+ "@opra/core": "^1.0.0-alpha.24",
32
32
  "fast-tokenizer": "^1.3.0",
33
33
  "lodash.head": "^4.0.1",
34
34
  "reflect-metadata": "^0.2.2"
@@ -41,6 +41,7 @@
41
41
  "@nestjs/platform-express": "^10.3.10",
42
42
  "@nestjs/testing": "^10.3.10",
43
43
  "@types/lodash.head": "^4.0.9",
44
+ "express": "^4.19.2",
44
45
  "filedirname": "^3.4.0",
45
46
  "rxjs": "^7.8.1",
46
47
  "supertest": "^7.0.0",
@@ -1,5 +1,6 @@
1
1
  import { DynamicModule } from '@nestjs/common';
2
2
  import { ApiDocumentFactory } from '@opra/common';
3
+ import { HttpAdapter } from '@opra/core';
3
4
  export declare namespace OpraHttpModule {
4
5
  interface Initiator extends Pick<DynamicModule, 'imports' | 'providers' | 'exports' | 'controllers'>, Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'> {
5
6
  id?: any;
@@ -8,6 +9,8 @@ export declare namespace OpraHttpModule {
8
9
  interface Options {
9
10
  basePath?: string;
10
11
  schemaRouteIsPublic?: boolean;
12
+ onRequest?: HttpAdapter.Events['request'];
13
+ onError?: HttpAdapter.Events['error'];
11
14
  }
12
15
  }
13
16
  export declare class OpraHttpModule {
@@ -1,7 +1,6 @@
1
1
  import { Type } from '@nestjs/common';
2
2
  import { HttpAdapter } from '@opra/core';
3
3
  import type { OpraHttpModule } from './opra-http.module.js';
4
- export declare const kHandler: unique symbol;
5
4
  export declare class OpraNestAdapter extends HttpAdapter {
6
5
  readonly nestControllers: Type[];
7
6
  readonly options?: OpraHttpModule.Options;
@@ -1,8 +1,7 @@
1
- import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
2
- import { OpraNestAdapter } from '../opra-nestjs-adapter.js';
3
- export declare const kHandler: unique symbol;
4
- export declare class OpraExceptionFilter implements ExceptionFilter {
5
- readonly adapter: OpraNestAdapter;
6
- constructor(adapter: OpraNestAdapter);
7
- catch(exception: any, host: ArgumentsHost): Promise<void>;
1
+ import { ArgumentsHost } from '@nestjs/common';
2
+ import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
3
+ export declare class OpraExceptionFilter extends BaseExceptionFilter {
4
+ private moduleRef;
5
+ constructor(moduleRef: ModuleRef);
6
+ catch(exception: any, host: ArgumentsHost): Promise<void> | undefined;
8
7
  }