@opra/nestjs-http 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-http
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-http
20
+ [npm-url]: https://npmjs.org/package/@opra/nestjs-http
21
+ [downloads-image]: https://img.shields.io/npm/dm/@opra/nestjs-http.svg
22
+ [downloads-url]: https://npmjs.org/package/@opra/nestjs-http
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
@@ -1,7 +1,21 @@
1
1
  import { type ArgumentsHost } from '@nestjs/common';
2
2
  import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
3
+ /**
4
+ * OpraExceptionFilter
5
+ *
6
+ * NestJS exception filter that catches errors during OPRA HTTP requests
7
+ * and returns responses according to OPRA standards.
8
+ */
3
9
  export declare class OpraExceptionFilter extends BaseExceptionFilter {
4
10
  private moduleRef;
5
11
  constructor(moduleRef: ModuleRef);
12
+ /**
13
+ * Processes the caught exception.
14
+ * If the request has an OPRA context, it responds by converting the error to the OPRA error format.
15
+ * Otherwise, it uses the default NestJS exception handling mechanism.
16
+ *
17
+ * @param exception - The caught exception object.
18
+ * @param host - The arguments host.
19
+ */
6
20
  catch(exception: any, host: ArgumentsHost): Promise<void> | undefined;
7
21
  }
@@ -2,12 +2,26 @@ import { __decorate, __metadata } from "tslib";
2
2
  import { Catch } from '@nestjs/common';
3
3
  import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
4
4
  import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
5
+ /**
6
+ * OpraExceptionFilter
7
+ *
8
+ * NestJS exception filter that catches errors during OPRA HTTP requests
9
+ * and returns responses according to OPRA standards.
10
+ */
5
11
  let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter {
6
12
  moduleRef;
7
13
  constructor(moduleRef) {
8
14
  super();
9
15
  this.moduleRef = moduleRef;
10
16
  }
17
+ /**
18
+ * Processes the caught exception.
19
+ * If the request has an OPRA context, it responds by converting the error to the OPRA error format.
20
+ * Otherwise, it uses the default NestJS exception handling mechanism.
21
+ *
22
+ * @param exception - The caught exception object.
23
+ * @param host - The arguments host.
24
+ */
11
25
  catch(exception, host) {
12
26
  const ctx = host.switchToHttp().getRequest().opraContext;
13
27
  if (ctx) {
@@ -1,13 +1,49 @@
1
1
  import { type Type } from '@nestjs/common';
2
2
  import { HttpAdapter } from '@opra/http';
3
+ /**
4
+ * OpraHttpNestjsAdapter
5
+ *
6
+ * HTTP adapter used to integrate OPRA APIs with the NestJS framework.
7
+ * This adapter converts OPRA controllers into NestJS controllers and
8
+ * exports the OPRA documentation ($schema).
9
+ */
3
10
  export declare class OpraHttpNestjsAdapter extends HttpAdapter {
11
+ /** List of controller classes to be registered with NestJS */
4
12
  readonly nestControllers: Type[];
13
+ /** Platform identifier */
5
14
  readonly platform = "nestjs";
15
+ /**
16
+ * Creates a new instance of OpraHttpNestjsAdapter.
17
+ *
18
+ * @param options - Adapter configuration options.
19
+ */
6
20
  constructor(options: HttpAdapter.Options & {
21
+ /** Indicates whether the schema is public */
7
22
  schemaIsPublic?: boolean;
23
+ /** Manually added NestJS controllers */
8
24
  controllers?: Type[];
9
25
  });
26
+ /**
27
+ * Closes the adapter.
28
+ * @returns {Promise<void>}
29
+ */
10
30
  close(): Promise<void>;
31
+ /**
32
+ * Adds the root controller that serves the OPRA schema.
33
+ *
34
+ * @param isPublic - Whether the schema is accessible without authentication.
35
+ * @protected
36
+ */
11
37
  protected _addRootController(isPublic?: boolean): void;
38
+ /**
39
+ * Adds the specified class and its sub-controllers to the NestJS controller list.
40
+ *
41
+ * @param sourceClass - Source OPRA controller class.
42
+ * @param currentPath - Current URL path.
43
+ * @param parentTree - List of parent controller classes.
44
+ * @protected
45
+ * @throws {@link NotFoundError} Thrown when no suitable endpoint is found for the operation.
46
+ * @throws {@link TypeError} Thrown when the controller is not a class.
47
+ */
12
48
  protected _addToNestControllers(sourceClass: Type, currentPath: string, parentTree: Type[]): void;
13
49
  }
@@ -6,9 +6,23 @@ import { HTTP_CONTROLLER_METADATA, NotFoundError, } from '@opra/common';
6
6
  import { HttpAdapter } from '@opra/http';
7
7
  import { OpraNestUtils, Public } from '@opra/nestjs';
8
8
  import { asMutable } from 'ts-gems';
9
+ /**
10
+ * OpraHttpNestjsAdapter
11
+ *
12
+ * HTTP adapter used to integrate OPRA APIs with the NestJS framework.
13
+ * This adapter converts OPRA controllers into NestJS controllers and
14
+ * exports the OPRA documentation ($schema).
15
+ */
9
16
  export class OpraHttpNestjsAdapter extends HttpAdapter {
17
+ /** List of controller classes to be registered with NestJS */
10
18
  nestControllers = [];
19
+ /** Platform identifier */
11
20
  platform = 'nestjs';
21
+ /**
22
+ * Creates a new instance of OpraHttpNestjsAdapter.
23
+ *
24
+ * @param options - Adapter configuration options.
25
+ */
12
26
  constructor(options) {
13
27
  super(options);
14
28
  this._addRootController(options.schemaIsPublic);
@@ -18,9 +32,19 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
18
32
  }
19
33
  }
20
34
  }
35
+ /**
36
+ * Closes the adapter.
37
+ * @returns {Promise<void>}
38
+ */
21
39
  async close() {
22
40
  //
23
41
  }
42
+ /**
43
+ * Adds the root controller that serves the OPRA schema.
44
+ *
45
+ * @param isPublic - Whether the schema is accessible without authentication.
46
+ * @protected
47
+ */
24
48
  _addRootController(isPublic) {
25
49
  const _this = this;
26
50
  let RootController = class RootController {
@@ -46,16 +70,26 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
46
70
  }
47
71
  this.nestControllers.push(RootController);
48
72
  }
73
+ /**
74
+ * Adds the specified class and its sub-controllers to the NestJS controller list.
75
+ *
76
+ * @param sourceClass - Source OPRA controller class.
77
+ * @param currentPath - Current URL path.
78
+ * @param parentTree - List of parent controller classes.
79
+ * @protected
80
+ * @throws {@link NotFoundError} Thrown when no suitable endpoint is found for the operation.
81
+ * @throws {@link TypeError} Thrown when the controller is not a class.
82
+ */
49
83
  _addToNestControllers(sourceClass, currentPath, parentTree) {
50
84
  const metadata = Reflect.getMetadata(HTTP_CONTROLLER_METADATA, sourceClass);
51
85
  if (!metadata)
52
86
  return;
53
- /** Create a new controller class */
87
+ /* Create a new controller class */
54
88
  const newClass = {
55
89
  [sourceClass.name]: class extends sourceClass {
56
90
  },
57
91
  }[sourceClass.name];
58
- /** Copy metadata keys from source class to new one */
92
+ /* Copy metadata keys from source class to new one */
59
93
  OpraNestUtils.copyDecoratorMetadata(newClass, ...parentTree);
60
94
  Controller()(newClass);
61
95
  const newPath = metadata.path
@@ -63,7 +97,7 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
63
97
  : currentPath;
64
98
  const adapter = this;
65
99
  // adapter.logger =
66
- /** Disable default error handler. Errors will be handled by OpraExceptionFilter */
100
+ /* Disable default error handler. Errors will be handled by OpraExceptionFilter */
67
101
  adapter.handler.onError = (context, error) => {
68
102
  throw error;
69
103
  };
@@ -74,7 +108,7 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
74
108
  const operationHandler = sourceClass.prototype[k];
75
109
  Object.defineProperty(newClass.prototype, k, {
76
110
  writable: true,
77
- /** NestJS handler method */
111
+ /* NestJS handler method */
78
112
  async value(_req, _res) {
79
113
  _res.statusCode = 200;
80
114
  const api = adapter.document.api;
@@ -90,17 +124,17 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
90
124
  },
91
125
  });
92
126
  }
93
- /** Configure the HttpContext */
127
+ /* Configure the HttpContext */
94
128
  context.__docNode = operation.node;
95
129
  context.__oprDef = operation;
96
130
  context.__contDef = operation.owner;
97
131
  context.__controller = this;
98
132
  context.__handler = operationHandler;
99
- /** Handle request */
133
+ /* Handle request */
100
134
  await adapter.handler.handleRequest(context);
101
135
  },
102
136
  });
103
- /** Copy metadata keys from source function to new one */
137
+ /* Copy metadata keys from source function to new one */
104
138
  metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
105
139
  const newFn = newClass.prototype[k];
106
140
  for (const key of metadataKeys) {
@@ -115,35 +149,35 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
115
149
  : nodePath.posix.join(newPath, v.path || '');
116
150
  switch (v.method || 'GET') {
117
151
  case 'DELETE':
118
- /** Call @Delete decorator over new property */
152
+ /* Call @Delete decorator over new property */
119
153
  Delete(operationPath)(newClass.prototype, k, descriptor);
120
154
  break;
121
155
  case 'GET':
122
- /** Call @Get decorator over new property */
156
+ /* Call @Get decorator over new property */
123
157
  Get(operationPath)(newClass.prototype, k, descriptor);
124
158
  break;
125
159
  case 'HEAD':
126
- /** Call @Head decorator over new property */
160
+ /* Call @Head decorator over new property */
127
161
  Head(operationPath)(newClass.prototype, k, descriptor);
128
162
  break;
129
163
  case 'OPTIONS':
130
- /** Call @Options decorator over new property */
164
+ /* Call @Options decorator over new property */
131
165
  Options(operationPath)(newClass.prototype, k, descriptor);
132
166
  break;
133
167
  case 'PATCH':
134
- /** Call @Patch decorator over new property */
168
+ /* Call @Patch decorator over new property */
135
169
  Patch(operationPath)(newClass.prototype, k, descriptor);
136
170
  break;
137
171
  case 'POST':
138
- /** Call @Post decorator over new property */
172
+ /* Call @Post decorator over new property */
139
173
  Post(operationPath)(newClass.prototype, k, descriptor);
140
174
  break;
141
175
  case 'PUT':
142
- /** Call @Put decorator over new property */
176
+ /* Call @Put decorator over new property */
143
177
  Put(operationPath)(newClass.prototype, k, descriptor);
144
178
  break;
145
179
  case 'SEARCH':
146
- /** Call @Search decorator over new property */
180
+ /* Call @Search decorator over new property */
147
181
  Search(operationPath)(newClass.prototype, k, descriptor);
148
182
  break;
149
183
  default:
@@ -2,35 +2,66 @@ import { type DynamicModule, Logger, type Type } from '@nestjs/common';
2
2
  import type { ApiDocumentFactory } from '@opra/common';
3
3
  import type { HttpAdapter } from '@opra/http';
4
4
  export declare namespace OpraHttpModule {
5
+ /**
6
+ * Synchronous configuration options for OpraHttpModule.
7
+ */
5
8
  export interface ModuleOptions extends BaseModuleOptions, ApiConfig {
6
9
  }
10
+ /**
11
+ * Asynchronous configuration options for OpraHttpModule.
12
+ */
7
13
  export interface AsyncModuleOptions extends BaseModuleOptions {
14
+ /** Providers to be injected into the factory function */
8
15
  inject?: any[];
16
+ /** Factory function that returns the ApiConfig object asynchronously */
9
17
  useFactory?: (...args: any[]) => Promise<ApiConfig> | ApiConfig;
10
18
  }
19
+ /**
20
+ * Base configuration options for the module.
21
+ */
11
22
  interface BaseModuleOptions extends Pick<DynamicModule, 'imports' | 'providers' | 'exports' | 'controllers' | 'global'> {
23
+ /** Custom token for the module */
12
24
  token?: any;
25
+ /** Base path for the API */
13
26
  basePath?: string;
27
+ /** Whether the API schema is public */
14
28
  schemaIsPublic?: boolean;
29
+ /** Interceptor list for the HTTP adapter */
15
30
  interceptors?: (HttpAdapter.InterceptorFunction | HttpAdapter.IHttpInterceptor | Type<HttpAdapter.IHttpInterceptor>)[];
16
31
  }
32
+ /**
33
+ * OPRA API configuration details.
34
+ */
17
35
  export interface ApiConfig extends Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'> {
36
+ /** API name */
18
37
  name: string;
38
+ /** API description */
19
39
  description?: string;
40
+ /** API scope */
20
41
  scope?: string;
42
+ /** Logger to be used */
21
43
  logger?: Logger;
22
44
  }
23
45
  export {};
24
46
  }
47
+ /**
48
+ * OpraHttpModule
49
+ *
50
+ * Module that integrates OPRA HTTP support into the NestJS application.
51
+ */
25
52
  export declare class OpraHttpModule {
26
53
  /**
54
+ * Configures the module synchronously and imports it at the root level.
27
55
  *
28
- * @param init
56
+ * @param init - Module configuration options.
57
+ * @returns {DynamicModule} NestJS dynamic module.
29
58
  */
30
59
  static forRoot(init: OpraHttpModule.ModuleOptions): DynamicModule;
31
60
  /**
61
+ * Configures the module asynchronously and imports it at the root level.
32
62
  *
33
- * @param options
63
+ * @param options - Asynchronous module configuration options.
64
+ * @returns {DynamicModule} NestJS dynamic module.
34
65
  */
35
66
  static forRootAsync(options: OpraHttpModule.AsyncModuleOptions): DynamicModule;
36
67
  }
@@ -2,10 +2,17 @@ var OpraHttpModule_1;
2
2
  import { __decorate } from "tslib";
3
3
  import { Module } from '@nestjs/common';
4
4
  import { OpraHttpCoreModule } from './opra-http-core.module.js';
5
+ /**
6
+ * OpraHttpModule
7
+ *
8
+ * Module that integrates OPRA HTTP support into the NestJS application.
9
+ */
5
10
  let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
6
11
  /**
12
+ * Configures the module synchronously and imports it at the root level.
7
13
  *
8
- * @param init
14
+ * @param init - Module configuration options.
15
+ * @returns {DynamicModule} NestJS dynamic module.
9
16
  */
10
17
  static forRoot(init) {
11
18
  return {
@@ -14,8 +21,10 @@ let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
14
21
  };
15
22
  }
16
23
  /**
24
+ * Configures the module asynchronously and imports it at the root level.
17
25
  *
18
- * @param options
26
+ * @param options - Asynchronous module configuration options.
27
+ * @returns {DynamicModule} NestJS dynamic module.
19
28
  */
20
29
  static forRootAsync(options) {
21
30
  return {
@@ -1,8 +1,21 @@
1
1
  import { type NestMiddleware } from '@nestjs/common';
2
2
  import type { NextFunction, Request, Response } from 'express';
3
3
  import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
4
+ /**
5
+ * OpraMiddleware
6
+ *
7
+ * NestJS middleware that creates an OPRA context (HttpContext) for each HTTP request
8
+ * and adds it to the request.
9
+ */
4
10
  export declare class OpraMiddleware implements NestMiddleware {
5
11
  protected opraAdapter: OpraHttpNestjsAdapter;
6
12
  constructor(opraAdapter: OpraHttpNestjsAdapter);
13
+ /**
14
+ * Processes requests and creates the OPRA context.
15
+ *
16
+ * @param req - Express request object.
17
+ * @param res - Express response object.
18
+ * @param next - Function that calls the next middleware.
19
+ */
7
20
  use(req: Request, res: Response, next: NextFunction): void;
8
21
  }
@@ -2,15 +2,28 @@ import { __decorate, __metadata } from "tslib";
2
2
  import { Injectable } from '@nestjs/common';
3
3
  import { HttpContext, HttpIncoming, HttpOutgoing } from '@opra/http';
4
4
  import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
5
+ /**
6
+ * OpraMiddleware
7
+ *
8
+ * NestJS middleware that creates an OPRA context (HttpContext) for each HTTP request
9
+ * and adds it to the request.
10
+ */
5
11
  let OpraMiddleware = class OpraMiddleware {
6
12
  opraAdapter;
7
13
  constructor(opraAdapter) {
8
14
  this.opraAdapter = opraAdapter;
9
15
  }
16
+ /**
17
+ * Processes requests and creates the OPRA context.
18
+ *
19
+ * @param req - Express request object.
20
+ * @param res - Express response object.
21
+ * @param next - Function that calls the next middleware.
22
+ */
10
23
  use(req, res, next) {
11
24
  const request = HttpIncoming.from(req);
12
25
  const response = HttpOutgoing.from(res);
13
- /** Create the HttpContext */
26
+ /* Create the HttpContext */
14
27
  const context = new HttpContext({
15
28
  __adapter: this.opraAdapter,
16
29
  platform: req.route ? 'express' : 'fastify',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs-http",
3
- "version": "1.26.2",
3
+ "version": "1.26.4",
4
4
  "description": "Opra NestJS Http Module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -9,10 +9,10 @@
9
9
  "tslib": "^2.8.1"
10
10
  },
11
11
  "peerDependencies": {
12
- "@opra/common": "^1.26.2",
13
- "@opra/core": "^1.26.2",
14
- "@opra/nestjs": "^1.26.2",
15
- "@opra/http": "^1.26.2",
12
+ "@opra/common": "^1.26.4",
13
+ "@opra/core": "^1.26.4",
14
+ "@opra/nestjs": "^1.26.4",
15
+ "@opra/http": "^1.26.4",
16
16
  "@nestjs/common": "^10.0.0 || ^11.0.0",
17
17
  "@nestjs/core": "^10.0.0 || ^11.0.0"
18
18
  },