@opra/nestjs-http 1.20.0 → 1.22.0

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.
@@ -3,6 +3,7 @@ import { Catch } from '@nestjs/common';
3
3
  import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
4
4
  import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
5
5
  let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter {
6
+ moduleRef;
6
7
  constructor(moduleRef) {
7
8
  super();
8
9
  this.moduleRef = moduleRef;
@@ -10,6 +10,7 @@ import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
10
10
  import { OpraMiddleware } from './opra-middleware.js';
11
11
  const opraHttpNestjsAdapterToken = Symbol('OpraHttpNestjsAdapter');
12
12
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
13
+ opraAdapter;
13
14
  constructor(opraAdapter) {
14
15
  this.opraAdapter = opraAdapter;
15
16
  }
@@ -7,9 +7,9 @@ import { HttpAdapter } from '@opra/http';
7
7
  import { OpraNestUtils, Public } from '@opra/nestjs';
8
8
  import { asMutable } from 'ts-gems';
9
9
  export class OpraHttpNestjsAdapter extends HttpAdapter {
10
+ nestControllers = [];
10
11
  constructor(options) {
11
12
  super(options);
12
- this.nestControllers = [];
13
13
  this._addRootController(options.schemaIsPublic);
14
14
  if (options.controllers) {
15
15
  for (const c of options.controllers) {
@@ -90,6 +90,7 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
90
90
  });
91
91
  }
92
92
  /** Configure the HttpContext */
93
+ context.__docNode = operation.node;
93
94
  context.__oprDef = operation;
94
95
  context.__contDef = operation.owner;
95
96
  context.__controller = this;
@@ -3,6 +3,7 @@ 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
5
  let OpraMiddleware = class OpraMiddleware {
6
+ opraAdapter;
6
7
  constructor(opraAdapter) {
7
8
  this.opraAdapter = opraAdapter;
8
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs-http",
3
- "version": "1.20.0",
3
+ "version": "1.22.0",
4
4
  "description": "Opra NestJS Http Module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -9,48 +9,31 @@
9
9
  "tslib": "^2.8.1"
10
10
  },
11
11
  "peerDependencies": {
12
- "@opra/common": "^1.20.0",
13
- "@opra/core": "^1.20.0",
14
- "@opra/nestjs": "^1.20.0",
15
- "@opra/http": "^1.20.0",
12
+ "@opra/common": "^1.22.0",
13
+ "@opra/core": "^1.22.0",
14
+ "@opra/nestjs": "^1.22.0",
15
+ "@opra/http": "^1.22.0",
16
16
  "@nestjs/common": "^10.0.0 || ^11.0.0",
17
17
  "@nestjs/core": "^10.0.0 || ^11.0.0"
18
18
  },
19
- "type": "module",
20
19
  "exports": {
21
20
  ".": {
22
- "import": {
23
- "types": "./types/index.d.ts",
24
- "default": "./esm/index.js"
25
- },
26
- "require": {
27
- "types": "./types/index.d.cts",
28
- "default": "./cjs/index.js"
29
- },
30
- "default": "./esm/index.js"
21
+ "types": "./index.d.ts",
22
+ "default": "./index.js"
31
23
  },
32
24
  "./package.json": "./package.json"
33
25
  },
34
- "main": "./cjs/index.js",
35
- "module": "./esm/index.js",
36
- "types": "./types/index.d.ts",
26
+ "type": "module",
27
+ "module": "./index.js",
28
+ "types": "./index.d.ts",
29
+ "engines": {
30
+ "node": ">=20.0"
31
+ },
37
32
  "repository": {
38
33
  "type": "git",
39
34
  "url": "git+https://github.com/panates/opra.git",
40
35
  "directory": "packages/nestjs-http"
41
36
  },
42
- "engines": {
43
- "node": ">=16.0",
44
- "npm": ">=7.0.0"
45
- },
46
- "files": [
47
- "bin/",
48
- "cjs/",
49
- "esm/",
50
- "types/",
51
- "LICENSE",
52
- "README.md"
53
- ],
54
37
  "keywords": [
55
38
  "opra",
56
39
  "nestjs",
package/cjs/constants.js DELETED
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OPRA_HTTP_API_CONFIG = void 0;
4
- exports.OPRA_HTTP_API_CONFIG = 'OPRA_HTTP_API_CONFIG';
package/cjs/index.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Public = exports.OpraNestUtils = exports.IS_PUBLIC_KEY = void 0;
4
- const tslib_1 = require("tslib");
5
- const nestjs_1 = require("@opra/nestjs");
6
- Object.defineProperty(exports, "IS_PUBLIC_KEY", { enumerable: true, get: function () { return nestjs_1.IS_PUBLIC_KEY; } });
7
- Object.defineProperty(exports, "OpraNestUtils", { enumerable: true, get: function () { return nestjs_1.OpraNestUtils; } });
8
- Object.defineProperty(exports, "Public", { enumerable: true, get: function () { return nestjs_1.Public; } });
9
- tslib_1.__exportStar(require("./constants.js"), exports);
10
- tslib_1.__exportStar(require("./opra-exception-filter.js"), exports);
11
- tslib_1.__exportStar(require("./opra-http.module.js"), exports);
12
- tslib_1.__exportStar(require("./opra-http-nestjs-adapter.js"), exports);
13
- tslib_1.__exportStar(require("./opra-middleware.js"), exports);
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
8
- let OpraExceptionFilter = class OpraExceptionFilter extends core_1.BaseExceptionFilter {
9
- constructor(moduleRef) {
10
- super();
11
- this.moduleRef = moduleRef;
12
- }
13
- catch(exception, host) {
14
- const ctx = host.switchToHttp().getRequest().opraContext;
15
- if (ctx) {
16
- const adapter = this.moduleRef.get(opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter);
17
- ctx.errors.push(exception);
18
- return adapter.handler.sendResponse(ctx);
19
- }
20
- super.catch(exception, host);
21
- }
22
- };
23
- exports.OpraExceptionFilter = OpraExceptionFilter;
24
- exports.OpraExceptionFilter = OpraExceptionFilter = tslib_1.__decorate([
25
- (0, common_1.Catch)(),
26
- tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
27
- ], OpraExceptionFilter);
@@ -1,119 +0,0 @@
1
- "use strict";
2
- var OpraHttpCoreModule_1;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.OpraHttpCoreModule = void 0;
5
- const tslib_1 = require("tslib");
6
- const objects_1 = require("@jsopen/objects");
7
- const common_1 = require("@nestjs/common");
8
- const core_1 = require("@nestjs/core");
9
- const common_2 = require("@opra/common");
10
- const constants_js_1 = require("./constants.js");
11
- const opra_exception_filter_js_1 = require("./opra-exception-filter.js");
12
- const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
13
- const opra_middleware_js_1 = require("./opra-middleware.js");
14
- const opraHttpNestjsAdapterToken = Symbol('OpraHttpNestjsAdapter');
15
- let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
16
- constructor(opraAdapter) {
17
- this.opraAdapter = opraAdapter;
18
- }
19
- configure(consumer) {
20
- consumer.apply(opra_middleware_js_1.OpraMiddleware).forRoutes({
21
- path: '*',
22
- method: common_1.RequestMethod.ALL,
23
- });
24
- }
25
- static forRoot(moduleOptions) {
26
- return this._getDynamicModule({
27
- ...moduleOptions,
28
- providers: [
29
- ...(moduleOptions?.providers || []),
30
- {
31
- provide: constants_js_1.OPRA_HTTP_API_CONFIG,
32
- useValue: { ...moduleOptions },
33
- },
34
- ],
35
- });
36
- }
37
- static forRootAsync(moduleOptions) {
38
- if (!moduleOptions.useFactory)
39
- throw new Error('Invalid configuration. Must provide "useFactory"');
40
- return this._getDynamicModule({
41
- ...moduleOptions,
42
- providers: [
43
- ...(moduleOptions?.providers || []),
44
- {
45
- provide: constants_js_1.OPRA_HTTP_API_CONFIG,
46
- inject: moduleOptions.inject,
47
- useFactory: moduleOptions.useFactory,
48
- },
49
- ],
50
- });
51
- }
52
- static _getDynamicModule(moduleOptions) {
53
- const token = moduleOptions?.token || opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter;
54
- const opraNestAdapter = new opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter({
55
- ...moduleOptions,
56
- interceptors: undefined,
57
- });
58
- const adapterProvider = {
59
- provide: token,
60
- inject: [core_1.ModuleRef, constants_js_1.OPRA_HTTP_API_CONFIG],
61
- useFactory: async (moduleRef, apiConfig) => {
62
- opraNestAdapter.scope = apiConfig.scope;
63
- opraNestAdapter.logger =
64
- opraNestAdapter.logger || new common_1.Logger(apiConfig.name);
65
- opraNestAdapter._document =
66
- await common_2.ApiDocumentFactory.createDocument({
67
- ...apiConfig,
68
- api: {
69
- transport: 'http',
70
- name: apiConfig.name,
71
- description: apiConfig.description,
72
- controllers: moduleOptions.controllers,
73
- },
74
- });
75
- if (moduleOptions.interceptors) {
76
- opraNestAdapter.interceptors = moduleOptions.interceptors.map(x => {
77
- if ((0, objects_1.isConstructor)(x)) {
78
- return async (ctx, next) => {
79
- const interceptor = moduleRef.get(x);
80
- if (typeof interceptor.intercept === 'function')
81
- return interceptor.intercept(ctx, next);
82
- };
83
- }
84
- return x;
85
- });
86
- }
87
- return opraNestAdapter;
88
- },
89
- };
90
- return {
91
- global: moduleOptions.global,
92
- module: OpraHttpCoreModule_1,
93
- controllers: opraNestAdapter.nestControllers,
94
- providers: [
95
- ...(moduleOptions?.providers || []),
96
- adapterProvider,
97
- {
98
- provide: opraHttpNestjsAdapterToken,
99
- useExisting: token,
100
- },
101
- {
102
- provide: core_1.APP_FILTER,
103
- useClass: opra_exception_filter_js_1.OpraExceptionFilter,
104
- },
105
- ],
106
- imports: [...(moduleOptions?.imports || [])],
107
- exports: [...(moduleOptions?.exports || []), adapterProvider],
108
- };
109
- }
110
- async onModuleDestroy() {
111
- await this.opraAdapter.close();
112
- }
113
- };
114
- exports.OpraHttpCoreModule = OpraHttpCoreModule;
115
- exports.OpraHttpCoreModule = OpraHttpCoreModule = OpraHttpCoreModule_1 = tslib_1.__decorate([
116
- (0, common_1.Module)({}),
117
- (0, common_1.Global)(),
118
- tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
119
- ], OpraHttpCoreModule);
@@ -1,167 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraHttpNestjsAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
6
- const objects_1 = require("@jsopen/objects");
7
- const common_1 = require("@nestjs/common");
8
- const common_2 = require("@opra/common");
9
- const http_1 = require("@opra/http");
10
- const nestjs_1 = require("@opra/nestjs");
11
- const ts_gems_1 = require("ts-gems");
12
- class OpraHttpNestjsAdapter extends http_1.HttpAdapter {
13
- constructor(options) {
14
- super(options);
15
- this.nestControllers = [];
16
- this._addRootController(options.schemaIsPublic);
17
- if (options.controllers) {
18
- for (const c of options.controllers) {
19
- this._addToNestControllers(c, this.basePath, []);
20
- }
21
- }
22
- }
23
- async close() {
24
- //
25
- }
26
- _addRootController(isPublic) {
27
- const _this = this;
28
- let RootController = class RootController {
29
- schema(_req, next) {
30
- _this.handler.sendDocumentSchema(_req.opraContext).catch(() => next());
31
- }
32
- };
33
- tslib_1.__decorate([
34
- (0, common_1.Get)('/\\$schema'),
35
- tslib_1.__param(0, (0, common_1.Req)()),
36
- tslib_1.__param(1, (0, common_1.Next)()),
37
- tslib_1.__metadata("design:type", Function),
38
- tslib_1.__metadata("design:paramtypes", [Object, Function]),
39
- tslib_1.__metadata("design:returntype", void 0)
40
- ], RootController.prototype, "schema", null);
41
- RootController = tslib_1.__decorate([
42
- (0, common_1.Controller)({
43
- path: this.basePath,
44
- })
45
- ], RootController);
46
- if (isPublic) {
47
- (0, nestjs_1.Public)()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
48
- }
49
- this.nestControllers.push(RootController);
50
- }
51
- _addToNestControllers(sourceClass, currentPath, parentTree) {
52
- const metadata = Reflect.getMetadata(common_2.HTTP_CONTROLLER_METADATA, sourceClass);
53
- if (!metadata)
54
- return;
55
- /** Create a new controller class */
56
- const newClass = {
57
- [sourceClass.name]: class extends sourceClass {
58
- },
59
- }[sourceClass.name];
60
- /** Copy metadata keys from source class to new one */
61
- nestjs_1.OpraNestUtils.copyDecoratorMetadata(newClass, ...parentTree);
62
- (0, common_1.Controller)()(newClass);
63
- const newPath = metadata.path
64
- ? node_path_1.default.posix.join(currentPath, metadata.path)
65
- : currentPath;
66
- const adapter = this;
67
- // adapter.logger =
68
- /** Disable default error handler. Errors will be handled by OpraExceptionFilter */
69
- adapter.handler.onError = (context, error) => {
70
- throw error;
71
- };
72
- this.nestControllers.push(newClass);
73
- let metadataKeys;
74
- if (metadata.operations) {
75
- for (const [k, v] of Object.entries(metadata.operations)) {
76
- const operationHandler = sourceClass.prototype[k];
77
- Object.defineProperty(newClass.prototype, k, {
78
- writable: true,
79
- /** NestJS handler method */
80
- async value(_req, _res) {
81
- _res.statusCode = 200;
82
- const api = adapter.document.api;
83
- const controller = api.findController(sourceClass);
84
- const operation = controller?.operations.get(k);
85
- const context = (0, ts_gems_1.asMutable)(_req.opraContext);
86
- if (!(context && operation && typeof operationHandler === 'function')) {
87
- throw new common_2.NotFoundError({
88
- message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
89
- details: {
90
- path: _req.baseUrl,
91
- method: _req.method,
92
- },
93
- });
94
- }
95
- /** Configure the HttpContext */
96
- context.__oprDef = operation;
97
- context.__contDef = operation.owner;
98
- context.__controller = this;
99
- context.__handler = operationHandler;
100
- /** Handle request */
101
- await adapter.handler.handleRequest(context);
102
- },
103
- });
104
- /** Copy metadata keys from source function to new one */
105
- metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
106
- const newFn = newClass.prototype[k];
107
- for (const key of metadataKeys) {
108
- const m = Reflect.getMetadata(key, operationHandler);
109
- Reflect.defineMetadata(key, m, newFn);
110
- }
111
- (0, common_1.Req)()(newClass.prototype, k, 0);
112
- (0, common_1.Res)()(newClass.prototype, k, 1);
113
- const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
114
- const operationPath = v.mergePath
115
- ? newPath + (v.path || '')
116
- : node_path_1.default.posix.join(newPath, v.path || '');
117
- switch (v.method || 'GET') {
118
- case 'DELETE':
119
- /** Call @Delete decorator over new property */
120
- (0, common_1.Delete)(operationPath)(newClass.prototype, k, descriptor);
121
- break;
122
- case 'GET':
123
- /** Call @Get decorator over new property */
124
- (0, common_1.Get)(operationPath)(newClass.prototype, k, descriptor);
125
- break;
126
- case 'HEAD':
127
- /** Call @Head decorator over new property */
128
- (0, common_1.Head)(operationPath)(newClass.prototype, k, descriptor);
129
- break;
130
- case 'OPTIONS':
131
- /** Call @Options decorator over new property */
132
- (0, common_1.Options)(operationPath)(newClass.prototype, k, descriptor);
133
- break;
134
- case 'PATCH':
135
- /** Call @Patch decorator over new property */
136
- (0, common_1.Patch)(operationPath)(newClass.prototype, k, descriptor);
137
- break;
138
- case 'POST':
139
- /** Call @Post decorator over new property */
140
- (0, common_1.Post)(operationPath)(newClass.prototype, k, descriptor);
141
- break;
142
- case 'PUT':
143
- /** Call @Put decorator over new property */
144
- (0, common_1.Put)(operationPath)(newClass.prototype, k, descriptor);
145
- break;
146
- case 'SEARCH':
147
- /** Call @Search decorator over new property */
148
- (0, common_1.Search)(operationPath)(newClass.prototype, k, descriptor);
149
- break;
150
- default:
151
- break;
152
- }
153
- }
154
- }
155
- if (metadata.controllers) {
156
- for (const child of metadata.controllers) {
157
- if (!(0, objects_1.isConstructor)(child))
158
- throw new TypeError('Controllers should be injectable a class');
159
- this._addToNestControllers(child, newPath, [
160
- ...parentTree,
161
- sourceClass,
162
- ]);
163
- }
164
- }
165
- }
166
- }
167
- exports.OpraHttpNestjsAdapter = OpraHttpNestjsAdapter;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- var OpraHttpModule_1;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.OpraHttpModule = void 0;
5
- const tslib_1 = require("tslib");
6
- const common_1 = require("@nestjs/common");
7
- const opra_http_core_module_js_1 = require("./opra-http-core.module.js");
8
- let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
9
- /**
10
- *
11
- * @param init
12
- */
13
- static forRoot(init) {
14
- return {
15
- module: OpraHttpModule_1,
16
- imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRoot(init)],
17
- };
18
- }
19
- /**
20
- *
21
- * @param options
22
- */
23
- static forRootAsync(options) {
24
- return {
25
- module: OpraHttpModule_1,
26
- imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRootAsync(options)],
27
- };
28
- }
29
- };
30
- exports.OpraHttpModule = OpraHttpModule;
31
- exports.OpraHttpModule = OpraHttpModule = OpraHttpModule_1 = tslib_1.__decorate([
32
- (0, common_1.Module)({})
33
- ], OpraHttpModule);
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraMiddleware = void 0;
4
- const tslib_1 = require("tslib");
5
- const common_1 = require("@nestjs/common");
6
- const http_1 = require("@opra/http");
7
- const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
8
- let OpraMiddleware = class OpraMiddleware {
9
- constructor(opraAdapter) {
10
- this.opraAdapter = opraAdapter;
11
- }
12
- use(req, res, next) {
13
- const request = http_1.HttpIncoming.from(req);
14
- const response = http_1.HttpOutgoing.from(res);
15
- /** Create the HttpContext */
16
- const context = new http_1.HttpContext({
17
- __adapter: this.opraAdapter,
18
- platform: req.route ? 'express' : 'fastify',
19
- request,
20
- response,
21
- });
22
- req.opraContext = context;
23
- this.opraAdapter
24
- .emitAsync('createContext', context)
25
- .then(() => next())
26
- .catch(next);
27
- }
28
- };
29
- exports.OpraMiddleware = OpraMiddleware;
30
- exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
31
- (0, common_1.Injectable)(),
32
- tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
33
- ], OpraMiddleware);
package/cjs/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,7 +0,0 @@
1
- import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
2
- export * from './constants.js';
3
- export * from './opra-exception-filter.js';
4
- export * from './opra-http.module.js';
5
- export * from './opra-http-nestjs-adapter.js';
6
- export * from './opra-middleware.js';
7
- export { IS_PUBLIC_KEY, OpraNestUtils, Public };
File without changes
File without changes
File without changes
File without changes