@opra/nestjs 1.26.2 → 1.26.4

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.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # OPRA
1
+ # @opra/nestjs
2
2
 
3
- Open Protocol for Restful Api
3
+ [![NPM Version][npm-image]][npm-url]
4
+ [![NPM Downloads][downloads-image]][downloads-url]
5
+ [![CI Tests][ci-test-image]][ci-test-url]
6
+ [![Test Coverage][coveralls-image]][coveralls-url]
7
+
8
+
9
+ ## Support
10
+ You can report bugs and discuss features on the [GitHub issues](https://github.com/panates/opra/issues) page.
11
+
12
+ ## Node Compatibility
13
+ - node >= 20.x
14
+
15
+
16
+ ## License
17
+ Available under [MIT](LICENSE) license.
18
+
19
+ [npm-image]: https://img.shields.io/npm/v/@opra/nestjs
20
+ [npm-url]: https://npmjs.org/package/@opra/nestjs
21
+ [downloads-image]: https://img.shields.io/npm/dm/@opra/nestjs.svg
22
+ [downloads-url]: https://npmjs.org/package/@opra/nestjs
23
+ [ci-test-image]: https://github.com/panates/opra/actions/workflows/test.yml/badge.svg
24
+ [ci-test-url]: https://github.com/panates/opra/actions/workflows/test.yml
25
+ [coveralls-image]: https://coveralls.io/repos/github/panates/opra/badge.svg?branch=main
26
+ [coveralls-url]: https://coveralls.io/github/panates/opra?branch=main
@@ -3,6 +3,7 @@ import { ExternalExceptionFilter } from '@nestjs/core/exceptions/external-except
3
3
  import { classes, } from '@opra/common';
4
4
  var WSControllerDecoratorFactory = classes.WSControllerDecoratorFactory;
5
5
  const { MQControllerDecoratorFactory } = classes;
6
+ /* Augment ExternalExceptionFilter to prevent error logging for Opra controllers */
6
7
  const oldCatchMethod = ExternalExceptionFilter.prototype.catch;
7
8
  ExternalExceptionFilter.prototype.catch = function (exception, host) {
8
9
  const opraContext = host.getArgByIndex(3);
@@ -11,11 +12,13 @@ ExternalExceptionFilter.prototype.catch = function (exception, host) {
11
12
  throw exception;
12
13
  oldCatchMethod(exception, host);
13
14
  };
15
+ /* Augment MQControllerDecoratorFactory to automatically apply @Controller() decorator */
14
16
  MQControllerDecoratorFactory.augment((decorator, decoratorChain) => {
15
17
  decoratorChain.push((_, target) => {
16
18
  Controller()(target);
17
19
  });
18
20
  });
21
+ /* Augment WSControllerDecoratorFactory to automatically apply @Controller() decorator */
19
22
  WSControllerDecoratorFactory.augment((decorator, decoratorChain) => {
20
23
  decoratorChain.push((_, target) => {
21
24
  Controller()(target);
package/constants.d.ts CHANGED
@@ -1 +1,4 @@
1
+ /**
2
+ * Metadata key for marking controllers or methods as public.
3
+ */
1
4
  export declare const IS_PUBLIC_KEY = "opra:isPublic";
package/constants.js CHANGED
@@ -1 +1,4 @@
1
+ /**
2
+ * Metadata key for marking controllers or methods as public.
3
+ */
1
4
  export const IS_PUBLIC_KEY = 'opra:isPublic';
@@ -1 +1,4 @@
1
+ /**
2
+ * Decorator that marks a controller or method as public.
3
+ */
1
4
  export declare const Public: () => import("@nestjs/common").CustomDecorator<string>;
@@ -1,3 +1,6 @@
1
1
  import { SetMetadata } from '@nestjs/common';
2
2
  import { IS_PUBLIC_KEY } from '../constants.js';
3
+ /**
4
+ * Decorator that marks a controller or method as public.
5
+ */
3
6
  export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
@@ -1,4 +1,7 @@
1
1
  import { RpcControllerFactory } from './rpc-controller.factory.js';
2
+ /**
3
+ * Factory class that wraps Opra Message Queue controllers into NestJS controllers.
4
+ */
2
5
  export declare class MQControllerFactory extends RpcControllerFactory {
3
6
  protected _metadataKey: symbol;
4
7
  }
@@ -1,5 +1,8 @@
1
1
  import { MQ_CONTROLLER_METADATA } from '@opra/common';
2
2
  import { RpcControllerFactory } from './rpc-controller.factory.js';
3
+ /**
4
+ * Factory class that wraps Opra Message Queue controllers into NestJS controllers.
5
+ */
3
6
  export class MQControllerFactory extends RpcControllerFactory {
4
7
  _metadataKey = MQ_CONTROLLER_METADATA;
5
8
  }
@@ -1,4 +1,14 @@
1
1
  import type { Type } from '@nestjs/common';
2
+ /**
3
+ * Utility class for Opra-NestJS integration.
4
+ */
2
5
  export declare class OpraNestUtils {
6
+ /**
7
+ * Copies Opra-related decorator metadata from source classes to a target class.
8
+ * Also merges NestJS guards, interceptors, and exception filters.
9
+ *
10
+ * @param target - The target class to receive the metadata.
11
+ * @param source - The source classes to copy metadata from.
12
+ */
3
13
  static copyDecoratorMetadata(target: Type, ...source: Type[]): void;
4
14
  }
@@ -1,7 +1,17 @@
1
1
  const GUARDS_METADATA = '__guards__';
2
2
  const INTERCEPTORS_METADATA = '__interceptors__';
3
3
  const EXCEPTION_FILTERS_METADATA = '__exceptionFilters__';
4
+ /**
5
+ * Utility class for Opra-NestJS integration.
6
+ */
4
7
  export class OpraNestUtils {
8
+ /**
9
+ * Copies Opra-related decorator metadata from source classes to a target class.
10
+ * Also merges NestJS guards, interceptors, and exception filters.
11
+ *
12
+ * @param target - The target class to receive the metadata.
13
+ * @param source - The source classes to copy metadata from.
14
+ */
5
15
  static copyDecoratorMetadata(target, ...source) {
6
16
  for (const parent of source) {
7
17
  const metadataKeys = Reflect.getOwnMetadataKeys(parent);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs",
3
- "version": "1.26.2",
3
+ "version": "1.26.4",
4
4
  "description": "Opra NestJS module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -10,8 +10,8 @@
10
10
  "tslib": "^2.8.1"
11
11
  },
12
12
  "peerDependencies": {
13
- "@opra/common": "^1.26.2",
14
- "@opra/core": "^1.26.2",
13
+ "@opra/common": "^1.26.4",
14
+ "@opra/core": "^1.26.4",
15
15
  "@nestjs/common": "^10.0.0 || ^11.0.0",
16
16
  "@nestjs/core": "^10.0.0 || ^11.0.0",
17
17
  "@nestjs/microservices": "^10.0.0 || ^11.0.0"
@@ -3,6 +3,9 @@ import { ModulesContainer } from '@nestjs/core';
3
3
  import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator.js';
4
4
  import type { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
5
5
  import type { Module } from '@nestjs/core/injector/module.js';
6
+ /**
7
+ * Factory class that wraps Opra controllers into NestJS controllers.
8
+ */
6
9
  export declare class RpcControllerFactory {
7
10
  private readonly modulesContainer;
8
11
  private readonly externalContextCreator;
@@ -11,9 +14,37 @@ export declare class RpcControllerFactory {
11
14
  private readonly paramsFactory;
12
15
  private readonly injector;
13
16
  constructor(modulesContainer: ModulesContainer, externalContextCreator: ExternalContextCreator);
17
+ /**
18
+ * Wraps and returns the controllers as NestJS compatible classes.
19
+ *
20
+ * @returns An array of NestJS compatible controller classes.
21
+ */
14
22
  wrapControllers(): Type[];
23
+ /**
24
+ * Creates a context callback for a controller method.
25
+ *
26
+ * @param instance - The controller instance.
27
+ * @param wrapper - The instance wrapper.
28
+ * @param moduleRef - The module reference.
29
+ * @param methodName - The name of the method to wrap.
30
+ * @param isRequestScoped - Whether the controller is request-scoped.
31
+ * @param contextType - The NestJS context type.
32
+ * @param options - Optional external context options.
33
+ * @returns A callback function that handles the execution context.
34
+ */
15
35
  private _createContextCallback;
36
+ /**
37
+ * Registers a context provider for the given request and context ID.
38
+ *
39
+ * @param request - The request object (Opra execution context).
40
+ * @param contextId - The NestJS context ID.
41
+ */
16
42
  private registerContextProvider;
43
+ /**
44
+ * Explores and returns all Opra controllers within the NestJS application.
45
+ *
46
+ * @returns An array of objects containing the module and instance wrapper for each controller.
47
+ */
17
48
  exploreControllers(): {
18
49
  module: Module;
19
50
  wrapper: InstanceWrapper;
@@ -9,6 +9,9 @@ import { PARAM_ARGS_METADATA } from '@nestjs/microservices/constants.js';
9
9
  import { RPC_CONTROLLER_METADATA } from '@opra/common';
10
10
  import { OpraNestUtils } from './opra-nest-utils.js';
11
11
  import { RpcParamsFactory } from './rpc-params.factory.js';
12
+ /**
13
+ * Factory class that wraps Opra controllers into NestJS controllers.
14
+ */
12
15
  let RpcControllerFactory = class RpcControllerFactory {
13
16
  modulesContainer;
14
17
  externalContextCreator;
@@ -20,6 +23,11 @@ let RpcControllerFactory = class RpcControllerFactory {
20
23
  this.modulesContainer = modulesContainer;
21
24
  this.externalContextCreator = externalContextCreator;
22
25
  }
26
+ /**
27
+ * Wraps and returns the controllers as NestJS compatible classes.
28
+ *
29
+ * @returns An array of NestJS compatible controller classes.
30
+ */
23
31
  wrapControllers() {
24
32
  const out = [];
25
33
  for (const { module, wrapper } of this.exploreControllers()) {
@@ -27,12 +35,12 @@ let RpcControllerFactory = class RpcControllerFactory {
27
35
  const sourceClass = instance.constructor;
28
36
  const metadata = Reflect.getMetadata(this._metadataKey, sourceClass);
29
37
  const isRequestScoped = !wrapper.isDependencyTreeStatic();
30
- /** Create a new controller class */
38
+ /* Create a new controller class */
31
39
  const newClass = {
32
40
  [sourceClass.name]: class extends sourceClass {
33
41
  },
34
42
  }[sourceClass.name];
35
- /** Copy metadata keys from source class to new one */
43
+ /* Copy metadata keys from source class to new one */
36
44
  OpraNestUtils.copyDecoratorMetadata(newClass, sourceClass);
37
45
  Controller()(newClass);
38
46
  out.push(newClass);
@@ -46,6 +54,18 @@ let RpcControllerFactory = class RpcControllerFactory {
46
54
  }
47
55
  return out;
48
56
  }
57
+ /**
58
+ * Creates a context callback for a controller method.
59
+ *
60
+ * @param instance - The controller instance.
61
+ * @param wrapper - The instance wrapper.
62
+ * @param moduleRef - The module reference.
63
+ * @param methodName - The name of the method to wrap.
64
+ * @param isRequestScoped - Whether the controller is request-scoped.
65
+ * @param contextType - The NestJS context type.
66
+ * @param options - Optional external context options.
67
+ * @returns A callback function that handles the execution context.
68
+ */
49
69
  _createContextCallback(instance, wrapper, moduleRef, methodName, isRequestScoped, contextType, options) {
50
70
  const paramsFactory = this.paramsFactory;
51
71
  if (isRequestScoped) {
@@ -65,6 +85,12 @@ let RpcControllerFactory = class RpcControllerFactory {
65
85
  }
66
86
  return this.externalContextCreator.create(instance, instance[methodName], methodName, PARAM_ARGS_METADATA, paramsFactory, undefined, undefined, options, contextType);
67
87
  }
88
+ /**
89
+ * Registers a context provider for the given request and context ID.
90
+ *
91
+ * @param request - The request object (Opra execution context).
92
+ * @param contextId - The NestJS context ID.
93
+ */
68
94
  registerContextProvider(request, contextId) {
69
95
  if (!this._coreModuleRef) {
70
96
  const coreModuleArray = [...this.modulesContainer.entries()]
@@ -84,6 +110,11 @@ let RpcControllerFactory = class RpcControllerFactory {
84
110
  isResolved: true,
85
111
  });
86
112
  }
113
+ /**
114
+ * Explores and returns all Opra controllers within the NestJS application.
115
+ *
116
+ * @returns An array of objects containing the module and instance wrapper for each controller.
117
+ */
87
118
  exploreControllers() {
88
119
  const scannedModules = new Set();
89
120
  const controllers = new Set();
@@ -1,4 +1,15 @@
1
1
  import type { ParamsFactory } from '@nestjs/core/helpers/external-context-creator.js';
2
+ /**
3
+ * Factory class that provides parameters for NestJS RPC controllers.
4
+ */
2
5
  export declare class RpcParamsFactory implements ParamsFactory {
6
+ /**
7
+ * Exchanges a metadata key for a value from the arguments.
8
+ *
9
+ * @param type - The metadata type.
10
+ * @param data - The metadata data.
11
+ * @param args - The arguments array.
12
+ * @returns The first argument if available, otherwise null.
13
+ */
3
14
  exchangeKeyForValue(type: number, data: any, args: any): any;
4
15
  }
@@ -1,4 +1,15 @@
1
+ /**
2
+ * Factory class that provides parameters for NestJS RPC controllers.
3
+ */
1
4
  export class RpcParamsFactory {
5
+ /**
6
+ * Exchanges a metadata key for a value from the arguments.
7
+ *
8
+ * @param type - The metadata type.
9
+ * @param data - The metadata data.
10
+ * @param args - The arguments array.
11
+ * @returns The first argument if available, otherwise null.
12
+ */
2
13
  exchangeKeyForValue(type, data, args) {
3
14
  if (!args) {
4
15
  return null;
@@ -1,4 +1,7 @@
1
1
  import { RpcControllerFactory } from './rpc-controller.factory.js';
2
+ /**
3
+ * Factory class that wraps Opra WebSocket controllers into NestJS controllers.
4
+ */
2
5
  export declare class WSControllerFactory extends RpcControllerFactory {
3
6
  protected _metadataKey: symbol;
4
7
  }
@@ -1,5 +1,8 @@
1
1
  import { WS_CONTROLLER_METADATA } from '@opra/common';
2
2
  import { RpcControllerFactory } from './rpc-controller.factory.js';
3
+ /**
4
+ * Factory class that wraps Opra WebSocket controllers into NestJS controllers.
5
+ */
3
6
  export class WSControllerFactory extends RpcControllerFactory {
4
7
  _metadataKey = WS_CONTROLLER_METADATA;
5
8
  }