@opra/nestjs 1.0.0-alpha.2 → 1.0.0-alpha.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.
@@ -7,10 +7,17 @@ const ts_gems_1 = require("ts-gems");
7
7
  const common_1 = require("@nestjs/common");
8
8
  const common_2 = require("@opra/common");
9
9
  const opra_nestjs_adapter_js_1 = require("./opra-nestjs-adapter.js");
10
+ const opra_middleware_js_1 = require("./services/opra-middleware.js");
10
11
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
11
12
  constructor(opraAdapter) {
12
13
  this.opraAdapter = opraAdapter;
13
14
  }
15
+ configure(consumer) {
16
+ consumer.apply(opra_middleware_js_1.OpraMiddleware).forRoutes({
17
+ path: '*',
18
+ method: common_1.RequestMethod.ALL,
19
+ });
20
+ }
14
21
  static forRoot(options) {
15
22
  const opraAdapter = new opra_nestjs_adapter_js_1.OpraNestAdapter(options);
16
23
  const token = options?.id || opra_nestjs_adapter_js_1.OpraNestAdapter;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpraNestAdapter = exports.kHandler = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const path_1 = tslib_1.__importDefault(require("path"));
6
+ const ts_gems_1 = require("ts-gems");
6
7
  const common_1 = require("@nestjs/common");
7
8
  const common_2 = require("@opra/common");
8
9
  const core_1 = require("@opra/core");
@@ -15,7 +16,6 @@ class OpraNestAdapter extends core_1.HttpAdapter {
15
16
  document.api = new common_2.HttpApi(document);
16
17
  return document;
17
18
  })(), options);
18
- // protected _platform: string = 'express';
19
19
  this.controllers = [];
20
20
  let basePath = options.basePath || '/';
21
21
  if (!basePath.startsWith('/'))
@@ -46,41 +46,30 @@ class OpraNestAdapter extends core_1.HttpAdapter {
46
46
  Object.defineProperty(newClass.prototype, k, {
47
47
  writable: true,
48
48
  /** NestJS handler method */
49
- async value(_req, _res) {
50
- const request = core_1.HttpIncoming.from(_req);
51
- const response = core_1.HttpOutgoing.from(_res);
49
+ async value(_req) {
52
50
  const api = adapter.document.api;
53
51
  const controller = api.findController(newClass);
54
52
  const operation = controller?.operations.get(k);
55
- if (!(operation && typeof operationHandler === 'function'))
53
+ const context = (0, ts_gems_1.asMutable)(_req.opraContext);
54
+ if (!(context && operation && typeof operationHandler === 'function'))
56
55
  throw new common_2.NotFoundError({
57
- message: `No endpoint found for [${request.method}]${request.baseUrl}`,
56
+ message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
58
57
  details: {
59
- path: request.baseUrl,
60
- method: request.method,
58
+ path: _req.baseUrl,
59
+ method: _req.method,
61
60
  },
62
61
  });
63
- /** Create the HttpContext */
64
- const context = new core_1.HttpContext({
65
- adapter,
66
- platform: _req.route ? 'express' : 'fastify',
67
- platformArgs: {
68
- request,
69
- response,
70
- },
71
- request,
72
- response,
73
- operation,
74
- controller: operation.owner,
75
- controllerInstance: this,
76
- operationHandler,
77
- });
62
+ /** Configure the HttpContext */
63
+ context.adapter = adapter;
64
+ context.document = adapter.document;
65
+ context.operation = operation;
66
+ context.controller = operation.owner;
67
+ context.controllerInstance = this;
68
+ context.operationHandler = operationHandler;
78
69
  /** Handle request */
79
70
  await adapter[exports.kHandler].handleRequest(context);
80
71
  },
81
72
  });
82
- (0, common_1.Req)()(newClass.prototype, k, 0);
83
- (0, common_1.Res)()(newClass.prototype, k, 1);
84
73
  /** Copy metadata keys from source function to new one */
85
74
  const metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
86
75
  const newFn = newClass.prototype[k];
@@ -88,6 +77,8 @@ class OpraNestAdapter extends core_1.HttpAdapter {
88
77
  const m = Reflect.getMetadata(key, operationHandler);
89
78
  Reflect.defineMetadata(key, m, newFn);
90
79
  }
80
+ (0, common_1.Req)()(newClass.prototype, k, 0);
81
+ (0, common_1.Res)()(newClass.prototype, k, 1);
91
82
  const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
92
83
  const operationPath = newPath + (v.path || '');
93
84
  switch (v.method || 'GET') {
@@ -0,0 +1,24 @@
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 core_1 = require("@opra/core");
7
+ let OpraMiddleware = class OpraMiddleware {
8
+ use(req, res, next) {
9
+ const request = core_1.HttpIncoming.from(req);
10
+ const response = core_1.HttpOutgoing.from(res);
11
+ /** Create the HttpContext */
12
+ req.opraContext = new core_1.HttpContext({
13
+ adapter: {},
14
+ platform: req.route ? 'express' : 'fastify',
15
+ request,
16
+ response,
17
+ });
18
+ next();
19
+ }
20
+ };
21
+ exports.OpraMiddleware = OpraMiddleware;
22
+ exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
23
+ (0, common_1.Injectable)()
24
+ ], OpraMiddleware);
@@ -1,13 +1,20 @@
1
1
  var OpraHttpCoreModule_1;
2
2
  import { __decorate, __metadata } from "tslib";
3
3
  import { asMutable } from 'ts-gems';
4
- import { Global, Module } from '@nestjs/common';
4
+ import { Global, Module, RequestMethod, } from '@nestjs/common';
5
5
  import { ApiDocumentFactory } from '@opra/common';
6
6
  import { OpraNestAdapter } from './opra-nestjs-adapter.js';
7
+ import { OpraMiddleware } from './services/opra-middleware.js';
7
8
  let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
8
9
  constructor(opraAdapter) {
9
10
  this.opraAdapter = opraAdapter;
10
11
  }
12
+ configure(consumer) {
13
+ consumer.apply(OpraMiddleware).forRoutes({
14
+ path: '*',
15
+ method: RequestMethod.ALL,
16
+ });
17
+ }
11
18
  static forRoot(options) {
12
19
  const opraAdapter = new OpraNestAdapter(options);
13
20
  const token = options?.id || OpraNestAdapter;
@@ -1,7 +1,8 @@
1
1
  import nodePath from 'path';
2
+ import { asMutable } from 'ts-gems';
2
3
  import { Controller, Delete, Get, Head, Options, Patch, Post, Put, Req, Res, Search, UseFilters, } from '@nestjs/common';
3
4
  import { ApiDocument, HTTP_CONTROLLER_METADATA, HttpApi, isConstructor, NotFoundError, } from '@opra/common';
4
- import { HttpAdapter, HttpContext, HttpIncoming, HttpOutgoing } from '@opra/core';
5
+ import { HttpAdapter } from '@opra/core';
5
6
  import { OpraExceptionFilter } from './services/opra-exception-filter.js';
6
7
  export const kHandler = Symbol.for('kHandler');
7
8
  export class OpraNestAdapter extends HttpAdapter {
@@ -11,7 +12,6 @@ export class OpraNestAdapter extends HttpAdapter {
11
12
  document.api = new HttpApi(document);
12
13
  return document;
13
14
  })(), options);
14
- // protected _platform: string = 'express';
15
15
  this.controllers = [];
16
16
  let basePath = options.basePath || '/';
17
17
  if (!basePath.startsWith('/'))
@@ -42,41 +42,30 @@ export class OpraNestAdapter extends HttpAdapter {
42
42
  Object.defineProperty(newClass.prototype, k, {
43
43
  writable: true,
44
44
  /** NestJS handler method */
45
- async value(_req, _res) {
46
- const request = HttpIncoming.from(_req);
47
- const response = HttpOutgoing.from(_res);
45
+ async value(_req) {
48
46
  const api = adapter.document.api;
49
47
  const controller = api.findController(newClass);
50
48
  const operation = controller?.operations.get(k);
51
- if (!(operation && typeof operationHandler === 'function'))
49
+ const context = asMutable(_req.opraContext);
50
+ if (!(context && operation && typeof operationHandler === 'function'))
52
51
  throw new NotFoundError({
53
- message: `No endpoint found for [${request.method}]${request.baseUrl}`,
52
+ message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
54
53
  details: {
55
- path: request.baseUrl,
56
- method: request.method,
54
+ path: _req.baseUrl,
55
+ method: _req.method,
57
56
  },
58
57
  });
59
- /** Create the HttpContext */
60
- const context = new HttpContext({
61
- adapter,
62
- platform: _req.route ? 'express' : 'fastify',
63
- platformArgs: {
64
- request,
65
- response,
66
- },
67
- request,
68
- response,
69
- operation,
70
- controller: operation.owner,
71
- controllerInstance: this,
72
- operationHandler,
73
- });
58
+ /** Configure the HttpContext */
59
+ context.adapter = adapter;
60
+ context.document = adapter.document;
61
+ context.operation = operation;
62
+ context.controller = operation.owner;
63
+ context.controllerInstance = this;
64
+ context.operationHandler = operationHandler;
74
65
  /** Handle request */
75
66
  await adapter[kHandler].handleRequest(context);
76
67
  },
77
68
  });
78
- Req()(newClass.prototype, k, 0);
79
- Res()(newClass.prototype, k, 1);
80
69
  /** Copy metadata keys from source function to new one */
81
70
  const metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
82
71
  const newFn = newClass.prototype[k];
@@ -84,6 +73,8 @@ export class OpraNestAdapter extends HttpAdapter {
84
73
  const m = Reflect.getMetadata(key, operationHandler);
85
74
  Reflect.defineMetadata(key, m, newFn);
86
75
  }
76
+ Req()(newClass.prototype, k, 0);
77
+ Res()(newClass.prototype, k, 1);
87
78
  const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
88
79
  const operationPath = newPath + (v.path || '');
89
80
  switch (v.method || 'GET') {
@@ -0,0 +1,21 @@
1
+ import { __decorate } from "tslib";
2
+ import { Injectable } from '@nestjs/common';
3
+ import { HttpContext, HttpIncoming, HttpOutgoing } from '@opra/core';
4
+ let OpraMiddleware = class OpraMiddleware {
5
+ use(req, res, next) {
6
+ const request = HttpIncoming.from(req);
7
+ const response = HttpOutgoing.from(res);
8
+ /** Create the HttpContext */
9
+ req.opraContext = new HttpContext({
10
+ adapter: {},
11
+ platform: req.route ? 'express' : 'fastify',
12
+ request,
13
+ response,
14
+ });
15
+ next();
16
+ }
17
+ };
18
+ OpraMiddleware = __decorate([
19
+ Injectable()
20
+ ], OpraMiddleware);
21
+ export { OpraMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "Opra NestJS module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -18,8 +18,8 @@
18
18
  "postbuild": "cp README.md package.json ../../LICENSE ../../build/nestjs && cp ../../package.cjs.json ../../build/nestjs/cjs/package.json",
19
19
  "lint": "eslint . --max-warnings=0",
20
20
  "format": "prettier . --write --log-level=warn",
21
- "test": "jest",
22
- "cover": "jest --collect-coverage",
21
+ "test": "jest --passWithNoTests",
22
+ "cover": "jest --passWithNoTests --collect-coverage",
23
23
  "clean": "npm run clean:src && npm run clean:test && npm run clean:dist && npm run clean:cover",
24
24
  "clean:src": "ts-cleanup -s src --all",
25
25
  "clean:test": "ts-cleanup -s test --all",
@@ -27,8 +27,8 @@
27
27
  "clean:cover": "rimraf ../../coverage/client"
28
28
  },
29
29
  "dependencies": {
30
- "@opra/common": "^1.0.0-alpha.2",
31
- "@opra/core": "^1.0.0-alpha.2",
30
+ "@opra/common": "^1.0.0-alpha.4",
31
+ "@opra/core": "^1.0.0-alpha.4",
32
32
  "fast-tokenizer": "^1.3.0",
33
33
  "lodash.head": "^4.0.1",
34
34
  "reflect-metadata": "^0.2.2"
@@ -1,9 +1,10 @@
1
- import { DynamicModule, OnModuleDestroy } from '@nestjs/common';
1
+ import { DynamicModule, MiddlewareConsumer, NestModule, OnModuleDestroy } from '@nestjs/common';
2
2
  import type { OpraHttpModule } from './opra-http.module';
3
3
  import { OpraNestAdapter } from './opra-nestjs-adapter.js';
4
- export declare class OpraHttpCoreModule implements OnModuleDestroy {
4
+ export declare class OpraHttpCoreModule implements OnModuleDestroy, NestModule {
5
5
  protected opraAdapter: OpraNestAdapter;
6
6
  constructor(opraAdapter: OpraNestAdapter);
7
+ configure(consumer: MiddlewareConsumer): void;
7
8
  static forRoot(options: OpraHttpModule.Options): DynamicModule;
8
9
  onModuleDestroy(): Promise<void>;
9
10
  }
@@ -0,0 +1,5 @@
1
+ import type { NextFunction, Request, Response } from 'express';
2
+ import { NestMiddleware } from '@nestjs/common';
3
+ export declare class OpraMiddleware implements NestMiddleware {
4
+ use(req: Request, res: Response, next: NextFunction): void;
5
+ }