@opra/nestjs 1.0.0-alpha.27 → 1.0.0-alpha.28

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/cjs/constants.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IS_PUBLIC_KEY = void 0;
3
+ exports.OPRA_HTTP_MODULE_OPTIONS = exports.IS_PUBLIC_KEY = void 0;
4
4
  exports.IS_PUBLIC_KEY = 'opra:isPublic';
5
+ exports.OPRA_HTTP_MODULE_OPTIONS = 'OPRA_HTTP_MODULE_OPTIONS';
@@ -7,6 +7,7 @@ const common_1 = require("@nestjs/common");
7
7
  const core_1 = require("@nestjs/core");
8
8
  const common_2 = require("@opra/common");
9
9
  const ts_gems_1 = require("ts-gems");
10
+ const constants_1 = require("./constants");
10
11
  const opra_nestjs_adapter_js_1 = require("./opra-nestjs-adapter.js");
11
12
  const opra_exception_filter_1 = require("./services/opra-exception-filter");
12
13
  const opra_middleware_js_1 = require("./services/opra-middleware.js");
@@ -25,13 +26,28 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
25
26
  const token = init?.id || opra_nestjs_adapter_js_1.OpraNestAdapter;
26
27
  const providers = [
27
28
  ...(init?.providers || []),
29
+ {
30
+ provide: constants_1.OPRA_HTTP_MODULE_OPTIONS,
31
+ useValue: { ...options },
32
+ },
28
33
  {
29
34
  provide: opra_nestjs_adapter_js_1.OpraNestAdapter,
30
- useFactory: async () => {
35
+ inject: [core_1.ModuleRef],
36
+ useFactory: async (moduleRef) => {
31
37
  (0, ts_gems_1.asMutable)(opraAdapter).document = await common_2.ApiDocumentFactory.createDocument({
32
38
  ...init,
33
39
  api: { protocol: 'http', name: init.name, controllers: init.controllers },
34
40
  });
41
+ opraAdapter.interceptors.map(x => {
42
+ if ((0, common_2.isConstructor)(x)) {
43
+ return (ctx, next) => {
44
+ const interceptor = moduleRef.get(x);
45
+ if (typeof interceptor.intercept === 'function')
46
+ return interceptor.intercept(ctx, next());
47
+ };
48
+ }
49
+ return x;
50
+ });
35
51
  return opraAdapter;
36
52
  },
37
53
  },
@@ -15,16 +15,15 @@ class OpraNestAdapter extends core_1.HttpAdapter {
15
15
  const document = new common_2.ApiDocument();
16
16
  document.api = new common_2.HttpApi(document);
17
17
  return document;
18
- })(), options);
18
+ })(), {
19
+ ...options,
20
+ interceptors: options?.interceptors,
21
+ });
19
22
  this.nestControllers = [];
20
23
  this.options = options;
21
24
  let basePath = options?.basePath || '/';
22
25
  if (!basePath.startsWith('/'))
23
26
  basePath = '/' + basePath;
24
- if (options?.onError)
25
- this.on('error', options.onError);
26
- if (options?.onRequest)
27
- this.on('request', options.onRequest);
28
27
  this._addRootController(basePath);
29
28
  if (init.controllers)
30
29
  init.controllers.forEach(c => this._addToNestControllers(c, basePath, []));
@@ -98,8 +97,6 @@ class OpraNestAdapter extends core_1.HttpAdapter {
98
97
  });
99
98
  }
100
99
  /** Configure the HttpContext */
101
- context.adapter = adapter;
102
- context.document = adapter.document;
103
100
  context.operation = operation;
104
101
  context.controller = operation.owner;
105
102
  context.controllerInstance = this;
@@ -4,21 +4,33 @@ exports.OpraMiddleware = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const common_1 = require("@nestjs/common");
6
6
  const core_1 = require("@opra/core");
7
+ const constants_1 = require("../constants");
8
+ const opra_nestjs_adapter_js_1 = require("../opra-nestjs-adapter.js");
7
9
  let OpraMiddleware = class OpraMiddleware {
10
+ constructor(opraAdapter, options) {
11
+ this.opraAdapter = opraAdapter;
12
+ this.options = options;
13
+ }
8
14
  use(req, res, next) {
9
15
  const request = core_1.HttpIncoming.from(req);
10
16
  const response = core_1.HttpOutgoing.from(res);
11
17
  /** Create the HttpContext */
12
- req.opraContext = new core_1.HttpContext({
13
- adapter: {},
18
+ const context = new core_1.HttpContext({
19
+ adapter: this.opraAdapter,
14
20
  platform: req.route ? 'express' : 'fastify',
15
21
  request,
16
22
  response,
17
23
  });
18
- next();
24
+ req.opraContext = context;
25
+ this.opraAdapter
26
+ .emitAsync('createContext', context)
27
+ .then(() => next())
28
+ .catch(next);
19
29
  }
20
30
  };
21
31
  exports.OpraMiddleware = OpraMiddleware;
22
32
  exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
23
- (0, common_1.Injectable)()
33
+ (0, common_1.Injectable)(),
34
+ tslib_1.__param(1, (0, common_1.Inject)(constants_1.OPRA_HTTP_MODULE_OPTIONS)),
35
+ tslib_1.__metadata("design:paramtypes", [opra_nestjs_adapter_js_1.OpraNestAdapter, Object])
24
36
  ], OpraMiddleware);
package/esm/constants.js CHANGED
@@ -1 +1,2 @@
1
1
  export const IS_PUBLIC_KEY = 'opra:isPublic';
2
+ export const OPRA_HTTP_MODULE_OPTIONS = 'OPRA_HTTP_MODULE_OPTIONS';
@@ -1,9 +1,10 @@
1
1
  var OpraHttpCoreModule_1;
2
2
  import { __decorate, __metadata } from "tslib";
3
3
  import { Global, Module, RequestMethod, } from '@nestjs/common';
4
- import { APP_FILTER } from '@nestjs/core';
5
- import { ApiDocumentFactory } from '@opra/common';
4
+ import { APP_FILTER, ModuleRef } from '@nestjs/core';
5
+ import { ApiDocumentFactory, isConstructor } from '@opra/common';
6
6
  import { asMutable } from 'ts-gems';
7
+ import { OPRA_HTTP_MODULE_OPTIONS } from './constants';
7
8
  import { OpraNestAdapter } from './opra-nestjs-adapter.js';
8
9
  import { OpraExceptionFilter } from './services/opra-exception-filter';
9
10
  import { OpraMiddleware } from './services/opra-middleware.js';
@@ -22,13 +23,28 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
22
23
  const token = init?.id || OpraNestAdapter;
23
24
  const providers = [
24
25
  ...(init?.providers || []),
26
+ {
27
+ provide: OPRA_HTTP_MODULE_OPTIONS,
28
+ useValue: { ...options },
29
+ },
25
30
  {
26
31
  provide: OpraNestAdapter,
27
- useFactory: async () => {
32
+ inject: [ModuleRef],
33
+ useFactory: async (moduleRef) => {
28
34
  asMutable(opraAdapter).document = await ApiDocumentFactory.createDocument({
29
35
  ...init,
30
36
  api: { protocol: 'http', name: init.name, controllers: init.controllers },
31
37
  });
38
+ opraAdapter.interceptors.map(x => {
39
+ if (isConstructor(x)) {
40
+ return (ctx, next) => {
41
+ const interceptor = moduleRef.get(x);
42
+ if (typeof interceptor.intercept === 'function')
43
+ return interceptor.intercept(ctx, next());
44
+ };
45
+ }
46
+ return x;
47
+ });
32
48
  return opraAdapter;
33
49
  },
34
50
  },
@@ -12,16 +12,15 @@ export class OpraNestAdapter extends HttpAdapter {
12
12
  const document = new ApiDocument();
13
13
  document.api = new HttpApi(document);
14
14
  return document;
15
- })(), options);
15
+ })(), {
16
+ ...options,
17
+ interceptors: options?.interceptors,
18
+ });
16
19
  this.nestControllers = [];
17
20
  this.options = options;
18
21
  let basePath = options?.basePath || '/';
19
22
  if (!basePath.startsWith('/'))
20
23
  basePath = '/' + basePath;
21
- if (options?.onError)
22
- this.on('error', options.onError);
23
- if (options?.onRequest)
24
- this.on('request', options.onRequest);
25
24
  this._addRootController(basePath);
26
25
  if (init.controllers)
27
26
  init.controllers.forEach(c => this._addToNestControllers(c, basePath, []));
@@ -95,8 +94,6 @@ export class OpraNestAdapter extends HttpAdapter {
95
94
  });
96
95
  }
97
96
  /** Configure the HttpContext */
98
- context.adapter = adapter;
99
- context.document = adapter.document;
100
97
  context.operation = operation;
101
98
  context.controller = operation.owner;
102
99
  context.controllerInstance = this;
@@ -1,21 +1,33 @@
1
- import { __decorate } from "tslib";
2
- import { Injectable } from '@nestjs/common';
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Inject, Injectable } from '@nestjs/common';
3
3
  import { HttpContext, HttpIncoming, HttpOutgoing } from '@opra/core';
4
+ import { OPRA_HTTP_MODULE_OPTIONS } from '../constants';
5
+ import { OpraNestAdapter } from '../opra-nestjs-adapter.js';
4
6
  let OpraMiddleware = class OpraMiddleware {
7
+ constructor(opraAdapter, options) {
8
+ this.opraAdapter = opraAdapter;
9
+ this.options = options;
10
+ }
5
11
  use(req, res, next) {
6
12
  const request = HttpIncoming.from(req);
7
13
  const response = HttpOutgoing.from(res);
8
14
  /** Create the HttpContext */
9
- req.opraContext = new HttpContext({
10
- adapter: {},
15
+ const context = new HttpContext({
16
+ adapter: this.opraAdapter,
11
17
  platform: req.route ? 'express' : 'fastify',
12
18
  request,
13
19
  response,
14
20
  });
15
- next();
21
+ req.opraContext = context;
22
+ this.opraAdapter
23
+ .emitAsync('createContext', context)
24
+ .then(() => next())
25
+ .catch(next);
16
26
  }
17
27
  };
18
28
  OpraMiddleware = __decorate([
19
- Injectable()
29
+ Injectable(),
30
+ __param(1, Inject(OPRA_HTTP_MODULE_OPTIONS)),
31
+ __metadata("design:paramtypes", [OpraNestAdapter, Object])
20
32
  ], OpraMiddleware);
21
33
  export { OpraMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/nestjs",
3
- "version": "1.0.0-alpha.27",
3
+ "version": "1.0.0-alpha.28",
4
4
  "description": "Opra NestJS module",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -27,10 +27,11 @@
27
27
  "clean:cover": "rimraf ../../coverage/client"
28
28
  },
29
29
  "dependencies": {
30
- "@opra/common": "^1.0.0-alpha.27",
31
- "@opra/core": "^1.0.0-alpha.27",
30
+ "@opra/common": "^1.0.0-alpha.28",
31
+ "@opra/core": "^1.0.0-alpha.28",
32
32
  "fast-tokenizer": "^1.3.0",
33
33
  "lodash.head": "^4.0.1",
34
+ "putil-promisify": "^1.10.1",
34
35
  "reflect-metadata": "^0.2.2"
35
36
  },
36
37
  "peerDependencies": {
@@ -1 +1,2 @@
1
1
  export declare const IS_PUBLIC_KEY = "opra:isPublic";
2
+ export declare const OPRA_HTTP_MODULE_OPTIONS = "OPRA_HTTP_MODULE_OPTIONS";
@@ -1,4 +1,4 @@
1
- import { DynamicModule } from '@nestjs/common';
1
+ import { DynamicModule, Type } from '@nestjs/common';
2
2
  import { ApiDocumentFactory } from '@opra/common';
3
3
  import { HttpAdapter } from '@opra/core';
4
4
  export declare namespace OpraHttpModule {
@@ -9,8 +9,7 @@ export declare namespace OpraHttpModule {
9
9
  interface Options {
10
10
  basePath?: string;
11
11
  schemaRouteIsPublic?: boolean;
12
- onRequest?: HttpAdapter.Events['request'];
13
- onError?: HttpAdapter.Events['error'];
12
+ interceptors?: (HttpAdapter.InterceptorFunction | HttpAdapter.IHttpInterceptor | Type<HttpAdapter.IHttpInterceptor>)[];
14
13
  }
15
14
  }
16
15
  export declare class OpraHttpModule {
@@ -1,5 +1,10 @@
1
1
  import { NestMiddleware } from '@nestjs/common';
2
2
  import type { NextFunction, Request, Response } from 'express';
3
+ import type { OpraHttpModule } from '../opra-http.module.js';
4
+ import { OpraNestAdapter } from '../opra-nestjs-adapter.js';
3
5
  export declare class OpraMiddleware implements NestMiddleware {
6
+ protected opraAdapter: OpraNestAdapter;
7
+ protected options: OpraHttpModule.Options;
8
+ constructor(opraAdapter: OpraNestAdapter, options: OpraHttpModule.Options);
4
9
  use(req: Request, res: Response, next: NextFunction): void;
5
10
  }