@opra/nestjs 1.0.0-alpha.7 → 1.0.0-alpha.8

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,9 +3,9 @@ var OpraHttpCoreModule_1;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.OpraHttpCoreModule = void 0;
5
5
  const tslib_1 = require("tslib");
6
- const ts_gems_1 = require("ts-gems");
7
6
  const common_1 = require("@nestjs/common");
8
7
  const common_2 = require("@opra/common");
8
+ const ts_gems_1 = require("ts-gems");
9
9
  const opra_nestjs_adapter_js_1 = require("./opra-nestjs-adapter.js");
10
10
  const opra_middleware_js_1 = require("./services/opra-middleware.js");
11
11
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
@@ -28,20 +28,21 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
28
28
  useFactory: async () => {
29
29
  (0, ts_gems_1.asMutable)(opraAdapter).document = await common_2.ApiDocumentFactory.createDocument({
30
30
  ...options,
31
- api: { protocol: 'http', name: options.name, controllers: opraAdapter.controllers },
31
+ api: { protocol: 'http', name: options.name, controllers: options.controllers },
32
32
  });
33
33
  return opraAdapter;
34
34
  },
35
35
  },
36
36
  ];
37
- if (token !== opra_nestjs_adapter_js_1.OpraNestAdapter)
37
+ if (token !== opra_nestjs_adapter_js_1.OpraNestAdapter) {
38
38
  providers.push({
39
39
  provide: token,
40
40
  useValue: opraAdapter,
41
41
  });
42
+ }
42
43
  return {
43
44
  module: OpraHttpCoreModule_1,
44
- controllers: opraAdapter.controllers,
45
+ controllers: opraAdapter.nestControllers,
45
46
  imports: [...(options?.imports || [])],
46
47
  exports: [...(options?.exports || []), token],
47
48
  providers,
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpraNestAdapter = exports.kHandler = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const path_1 = tslib_1.__importDefault(require("path"));
6
- const ts_gems_1 = require("ts-gems");
7
5
  const common_1 = require("@nestjs/common");
8
6
  const common_2 = require("@opra/common");
9
7
  const core_1 = require("@opra/core");
8
+ const path_1 = tslib_1.__importDefault(require("path"));
9
+ const ts_gems_1 = require("ts-gems");
10
10
  const opra_exception_filter_js_1 = require("./services/opra-exception-filter.js");
11
11
  exports.kHandler = Symbol.for('kHandler');
12
12
  class OpraNestAdapter extends core_1.HttpAdapter {
@@ -16,42 +16,65 @@ class OpraNestAdapter extends core_1.HttpAdapter {
16
16
  document.api = new common_2.HttpApi(document);
17
17
  return document;
18
18
  })(), options);
19
- this.controllers = [];
19
+ this.nestControllers = [];
20
20
  let basePath = options.basePath || '/';
21
21
  if (!basePath.startsWith('/'))
22
22
  basePath = '/' + basePath;
23
+ this._addRootController(basePath);
23
24
  if (options.controllers)
24
- options.controllers.forEach(c => this._createNestControllers(c, basePath));
25
+ options.controllers.forEach(c => this._addToNestControllers(c, basePath));
25
26
  }
26
27
  async close() {
27
28
  //
28
29
  }
29
- _createNestControllers(source, currentPath) {
30
- const metadata = Reflect.getMetadata(common_2.HTTP_CONTROLLER_METADATA, source);
30
+ _addRootController(basePath) {
31
+ const _this = this;
32
+ let RootController = class RootController {
33
+ schema(_req, next) {
34
+ _this[exports.kHandler].sendDocumentSchema(_req.opraContext).catch(next);
35
+ }
36
+ };
37
+ tslib_1.__decorate([
38
+ (0, common_1.Get)('/\\$schema'),
39
+ tslib_1.__param(0, (0, common_1.Req)()),
40
+ tslib_1.__param(1, (0, common_1.Next)()),
41
+ tslib_1.__metadata("design:type", Function),
42
+ tslib_1.__metadata("design:paramtypes", [Object, Function]),
43
+ tslib_1.__metadata("design:returntype", void 0)
44
+ ], RootController.prototype, "schema", null);
45
+ RootController = tslib_1.__decorate([
46
+ (0, common_1.Controller)({
47
+ path: basePath,
48
+ })
49
+ ], RootController);
50
+ this.nestControllers.push(RootController);
51
+ }
52
+ _addToNestControllers(sourceClass, currentPath) {
53
+ const metadata = Reflect.getMetadata(common_2.HTTP_CONTROLLER_METADATA, sourceClass);
31
54
  if (!metadata)
32
55
  return;
33
56
  const newClass = {
34
- [source.name]: class extends source {
57
+ [sourceClass.name]: class extends sourceClass {
35
58
  },
36
- }[source.name];
59
+ }[sourceClass.name];
37
60
  const newPath = metadata.path ? path_1.default.join(currentPath, metadata.path) : currentPath;
38
61
  const adapter = this;
39
62
  /** Inject exception filter */
40
63
  (0, common_1.UseFilters)(new opra_exception_filter_js_1.OpraExceptionFilter(adapter))(newClass);
41
64
  (0, common_1.Controller)()(newClass);
42
- this.controllers.push(newClass);
65
+ this.nestControllers.push(newClass);
43
66
  if (metadata.operations) {
44
67
  for (const [k, v] of Object.entries(metadata.operations)) {
45
- const operationHandler = source.prototype[k];
68
+ const operationHandler = sourceClass.prototype[k];
46
69
  Object.defineProperty(newClass.prototype, k, {
47
70
  writable: true,
48
71
  /** NestJS handler method */
49
72
  async value(_req) {
50
73
  const api = adapter.document.api;
51
- const controller = api.findController(newClass);
74
+ const controller = api.findController(sourceClass);
52
75
  const operation = controller?.operations.get(k);
53
76
  const context = (0, ts_gems_1.asMutable)(_req.opraContext);
54
- if (!(context && operation && typeof operationHandler === 'function'))
77
+ if (!(context && operation && typeof operationHandler === 'function')) {
55
78
  throw new common_2.NotFoundError({
56
79
  message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
57
80
  details: {
@@ -59,6 +82,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
59
82
  method: _req.method,
60
83
  },
61
84
  });
85
+ }
62
86
  /** Configure the HttpContext */
63
87
  context.adapter = adapter;
64
88
  context.document = adapter.document;
@@ -114,6 +138,8 @@ class OpraNestAdapter extends core_1.HttpAdapter {
114
138
  /** Call @Search decorator over new property */
115
139
  (0, common_1.Search)(operationPath)(newClass.prototype, k, descriptor);
116
140
  break;
141
+ default:
142
+ break;
117
143
  }
118
144
  }
119
145
  }
@@ -121,7 +147,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
121
147
  for (const child of metadata.controllers) {
122
148
  if (!(0, common_2.isConstructor)(child))
123
149
  throw new TypeError('Controllers should be injectable a class');
124
- this._createNestControllers(child, newPath);
150
+ this._addToNestControllers(child, newPath);
125
151
  }
126
152
  }
127
153
  }
@@ -1,8 +1,8 @@
1
1
  var OpraHttpCoreModule_1;
2
2
  import { __decorate, __metadata } from "tslib";
3
- import { asMutable } from 'ts-gems';
4
3
  import { Global, Module, RequestMethod, } from '@nestjs/common';
5
4
  import { ApiDocumentFactory } from '@opra/common';
5
+ import { asMutable } from 'ts-gems';
6
6
  import { OpraNestAdapter } from './opra-nestjs-adapter.js';
7
7
  import { OpraMiddleware } from './services/opra-middleware.js';
8
8
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
@@ -25,20 +25,21 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
25
25
  useFactory: async () => {
26
26
  asMutable(opraAdapter).document = await ApiDocumentFactory.createDocument({
27
27
  ...options,
28
- api: { protocol: 'http', name: options.name, controllers: opraAdapter.controllers },
28
+ api: { protocol: 'http', name: options.name, controllers: options.controllers },
29
29
  });
30
30
  return opraAdapter;
31
31
  },
32
32
  },
33
33
  ];
34
- if (token !== OpraNestAdapter)
34
+ if (token !== OpraNestAdapter) {
35
35
  providers.push({
36
36
  provide: token,
37
37
  useValue: opraAdapter,
38
38
  });
39
+ }
39
40
  return {
40
41
  module: OpraHttpCoreModule_1,
41
- controllers: opraAdapter.controllers,
42
+ controllers: opraAdapter.nestControllers,
42
43
  imports: [...(options?.imports || [])],
43
44
  exports: [...(options?.exports || []), token],
44
45
  providers,
@@ -1,8 +1,9 @@
1
- import nodePath from 'path';
2
- import { asMutable } from 'ts-gems';
3
- import { Controller, Delete, Get, Head, Options, Patch, Post, Put, Req, Res, Search, UseFilters, } from '@nestjs/common';
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';
4
3
  import { ApiDocument, HTTP_CONTROLLER_METADATA, HttpApi, isConstructor, NotFoundError, } from '@opra/common';
5
4
  import { HttpAdapter } from '@opra/core';
5
+ import nodePath from 'path';
6
+ import { asMutable } from 'ts-gems';
6
7
  import { OpraExceptionFilter } from './services/opra-exception-filter.js';
7
8
  export const kHandler = Symbol.for('kHandler');
8
9
  export class OpraNestAdapter extends HttpAdapter {
@@ -12,42 +13,65 @@ export class OpraNestAdapter extends HttpAdapter {
12
13
  document.api = new HttpApi(document);
13
14
  return document;
14
15
  })(), options);
15
- this.controllers = [];
16
+ this.nestControllers = [];
16
17
  let basePath = options.basePath || '/';
17
18
  if (!basePath.startsWith('/'))
18
19
  basePath = '/' + basePath;
20
+ this._addRootController(basePath);
19
21
  if (options.controllers)
20
- options.controllers.forEach(c => this._createNestControllers(c, basePath));
22
+ options.controllers.forEach(c => this._addToNestControllers(c, basePath));
21
23
  }
22
24
  async close() {
23
25
  //
24
26
  }
25
- _createNestControllers(source, currentPath) {
26
- const metadata = Reflect.getMetadata(HTTP_CONTROLLER_METADATA, source);
27
+ _addRootController(basePath) {
28
+ const _this = this;
29
+ let RootController = class RootController {
30
+ schema(_req, next) {
31
+ _this[kHandler].sendDocumentSchema(_req.opraContext).catch(next);
32
+ }
33
+ };
34
+ __decorate([
35
+ Get('/\\$schema'),
36
+ __param(0, Req()),
37
+ __param(1, Next()),
38
+ __metadata("design:type", Function),
39
+ __metadata("design:paramtypes", [Object, Function]),
40
+ __metadata("design:returntype", void 0)
41
+ ], RootController.prototype, "schema", null);
42
+ RootController = __decorate([
43
+ Controller({
44
+ path: basePath,
45
+ })
46
+ ], RootController);
47
+ this.nestControllers.push(RootController);
48
+ }
49
+ _addToNestControllers(sourceClass, currentPath) {
50
+ const metadata = Reflect.getMetadata(HTTP_CONTROLLER_METADATA, sourceClass);
27
51
  if (!metadata)
28
52
  return;
29
53
  const newClass = {
30
- [source.name]: class extends source {
54
+ [sourceClass.name]: class extends sourceClass {
31
55
  },
32
- }[source.name];
56
+ }[sourceClass.name];
33
57
  const newPath = metadata.path ? nodePath.join(currentPath, metadata.path) : currentPath;
34
58
  const adapter = this;
35
59
  /** Inject exception filter */
36
60
  UseFilters(new OpraExceptionFilter(adapter))(newClass);
37
61
  Controller()(newClass);
38
- this.controllers.push(newClass);
62
+ this.nestControllers.push(newClass);
39
63
  if (metadata.operations) {
40
64
  for (const [k, v] of Object.entries(metadata.operations)) {
41
- const operationHandler = source.prototype[k];
65
+ const operationHandler = sourceClass.prototype[k];
42
66
  Object.defineProperty(newClass.prototype, k, {
43
67
  writable: true,
44
68
  /** NestJS handler method */
45
69
  async value(_req) {
46
70
  const api = adapter.document.api;
47
- const controller = api.findController(newClass);
71
+ const controller = api.findController(sourceClass);
48
72
  const operation = controller?.operations.get(k);
49
73
  const context = asMutable(_req.opraContext);
50
- if (!(context && operation && typeof operationHandler === 'function'))
74
+ if (!(context && operation && typeof operationHandler === 'function')) {
51
75
  throw new NotFoundError({
52
76
  message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
53
77
  details: {
@@ -55,6 +79,7 @@ export class OpraNestAdapter extends HttpAdapter {
55
79
  method: _req.method,
56
80
  },
57
81
  });
82
+ }
58
83
  /** Configure the HttpContext */
59
84
  context.adapter = adapter;
60
85
  context.document = adapter.document;
@@ -110,6 +135,8 @@ export class OpraNestAdapter extends HttpAdapter {
110
135
  /** Call @Search decorator over new property */
111
136
  Search(operationPath)(newClass.prototype, k, descriptor);
112
137
  break;
138
+ default:
139
+ break;
113
140
  }
114
141
  }
115
142
  }
@@ -117,7 +144,7 @@ export class OpraNestAdapter extends HttpAdapter {
117
144
  for (const child of metadata.controllers) {
118
145
  if (!isConstructor(child))
119
146
  throw new TypeError('Controllers should be injectable a class');
120
- this._createNestControllers(child, newPath);
147
+ this._addToNestControllers(child, newPath);
121
148
  }
122
149
  }
123
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "Opra NestJS module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -27,21 +27,23 @@
27
27
  "clean:cover": "rimraf ../../coverage/client"
28
28
  },
29
29
  "dependencies": {
30
- "@opra/common": "^1.0.0-alpha.7",
31
- "@opra/core": "^1.0.0-alpha.7",
30
+ "@opra/common": "^1.0.0-alpha.8",
31
+ "@opra/core": "^1.0.0-alpha.8",
32
32
  "fast-tokenizer": "^1.3.0",
33
33
  "lodash.head": "^4.0.1",
34
34
  "reflect-metadata": "^0.2.2"
35
35
  },
36
36
  "peerDependencies": {
37
- "@nestjs/common": "^10.3.9",
38
- "@nestjs/core": "^10.3.9"
37
+ "@nestjs/common": "^10.3.10",
38
+ "@nestjs/core": "^10.3.10"
39
39
  },
40
40
  "devDependencies": {
41
- "@nestjs/platform-express": "^10.3.9",
42
- "@nestjs/testing": "^10.3.9",
41
+ "@nestjs/platform-express": "^10.3.10",
42
+ "@nestjs/testing": "^10.3.10",
43
43
  "@types/lodash.head": "^4.0.9",
44
44
  "filedirname": "^3.4.0",
45
+ "rxjs": "^7.8.1",
46
+ "supertest": "^7.0.0",
45
47
  "ts-gems": "^3.4.0"
46
48
  },
47
49
  "type": "module",
@@ -3,8 +3,9 @@ import { HttpAdapter } from '@opra/core';
3
3
  import type { OpraHttpModule } from './opra-http.module';
4
4
  export declare const kHandler: unique symbol;
5
5
  export declare class OpraNestAdapter extends HttpAdapter {
6
- readonly controllers: Type[];
6
+ readonly nestControllers: Type[];
7
7
  constructor(options: OpraHttpModule.Options);
8
8
  close(): Promise<void>;
9
- protected _createNestControllers(source: Type, currentPath: string): void;
9
+ protected _addRootController(basePath: string): void;
10
+ protected _addToNestControllers(sourceClass: Type, currentPath: string): void;
10
11
  }
@@ -1,5 +1,5 @@
1
- import type { NextFunction, Request, Response } from 'express';
2
1
  import { NestMiddleware } from '@nestjs/common';
2
+ import type { NextFunction, Request, Response } from 'express';
3
3
  export declare class OpraMiddleware implements NestMiddleware {
4
4
  use(req: Request, res: Response, next: NextFunction): void;
5
5
  }