@grandlinex/kernel 0.27.0 → 0.27.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [v0.27.1] - 2023-01-26
5
+ ### Added
6
+ - Add raw body field for express calls
7
+ - change BaseEndpoint variables from private to protected
8
+
4
9
  ## [v0.25.2] - 2022-08-07
5
10
  ### Added
6
11
  - Add express timing api in dev mode
package/dist/Kernel.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import CoreKernel, { CoreLogger } from '@grandlinex/core';
2
- import { Request } from 'express';
3
2
  import { ICClient, IKernel } from './lib';
3
+ import { XRequest } from './lib/express';
4
4
  /**
5
5
  * @class Kernel
6
6
  */
@@ -23,6 +23,6 @@ export default class Kernel extends CoreKernel<ICClient> implements IKernel {
23
23
  setAppServerPort(port: number): void;
24
24
  responseCodeFunction(data: {
25
25
  code: number;
26
- req: Request;
26
+ req: XRequest;
27
27
  }): void;
28
28
  }
@@ -1,6 +1,6 @@
1
- import e from 'express';
2
1
  import { IBaseKernelModule } from '../lib';
3
2
  import { BaseApiAction, JwtToken } from '../classes';
3
+ import { XRequest, XResponse } from '../lib/express';
4
4
  /**
5
5
  * @name ApiAuthTestAction
6
6
  *
@@ -23,5 +23,5 @@ import { BaseApiAction, JwtToken } from '../classes';
23
23
  */
24
24
  export default class ApiAuthTestAction extends BaseApiAction {
25
25
  constructor(module: IBaseKernelModule<any, any, any, any>);
26
- handler(req: e.Request, res: e.Response, next: () => void, data: JwtToken): Promise<void>;
26
+ handler(req: XRequest, res: XResponse, next: () => void, data: JwtToken): Promise<void>;
27
27
  }
@@ -1,7 +1,7 @@
1
- import e from 'express';
2
1
  import { IBaseKernelModule } from '../lib';
3
2
  import { BaseApiAction } from '../classes';
4
3
  import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
4
+ import { XRequest, XResponse } from '../lib/express';
5
5
  /**
6
6
  * @name ApiVersionAction
7
7
  *
@@ -27,5 +27,5 @@ import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
27
27
  */
28
28
  export default class ApiVersionAction extends BaseApiAction {
29
29
  constructor(module: IBaseKernelModule);
30
- handler(req: e.Request, res: e.Response, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
30
+ handler(req: XRequest, res: XResponse, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
31
31
  }
@@ -1,7 +1,7 @@
1
- import { Request, Response } from 'express';
2
1
  import { IBaseKernelModule } from '../lib';
3
2
  import { BaseApiAction } from '../classes';
4
3
  import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
4
+ import { XRequest, XResponse } from '../lib/express';
5
5
  /**
6
6
  * @openapi
7
7
  * /token:
@@ -40,5 +40,5 @@ export default class GetTokenAction extends BaseApiAction {
40
40
  * @param module Parent Module
41
41
  */
42
42
  constructor(module: IBaseKernelModule<any, any, any, any>);
43
- handler(req: Request, res: Response, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
43
+ handler(req: XRequest, res: XResponse, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
44
44
  }
@@ -1,8 +1,8 @@
1
- import { Request, Response } from 'express';
2
1
  import { CoreAction, IDataBase } from '@grandlinex/core';
3
2
  import { IBaseAction, IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib';
4
3
  import { JwtToken } from './BaseAuthProvider';
5
4
  import { IExtensionInterface } from './timing/ExpressServerTiming';
5
+ import { XNextFc, XRequest, XResponse } from '../lib/express';
6
6
  export declare enum ActionMode {
7
7
  'DEFAULT' = 0,
8
8
  'DMZ' = 1,
@@ -11,8 +11,8 @@ export declare enum ActionMode {
11
11
  export default abstract class BaseAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends CoreAction<K, T, P, C, E> implements IBaseAction<K, T, P, C, E> {
12
12
  mode: ActionMode;
13
13
  constructor(chanel: string, module: IBaseKernelModule<K, T, P, C, E>);
14
- abstract handler(req: Request, res: Response, next: () => void, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
15
- secureHandler(req: Request, res: Response, next: () => void): Promise<void>;
14
+ abstract handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
15
+ secureHandler(req: XRequest, res: XResponse, next: () => void): Promise<void>;
16
16
  setMode(mode: ActionMode): void;
17
17
  abstract register(): void;
18
18
  private initExtension;
@@ -1,4 +1,4 @@
1
- import { Request } from 'express';
1
+ import { XRequest } from '../lib/express';
2
2
  export interface JwtTokenData {
3
3
  username: string;
4
4
  userid: string;
@@ -14,10 +14,10 @@ export type AuthResult = {
14
14
  export interface IAuthProvider {
15
15
  authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
16
16
  validateAccess(token: JwtToken, requestType: string): Promise<boolean>;
17
- bearerTokenValidation(req: Request): Promise<JwtToken | null>;
17
+ bearerTokenValidation(req: XRequest): Promise<JwtToken | null>;
18
18
  }
19
19
  export default abstract class BaseAuthProvider implements IAuthProvider {
20
20
  abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
21
21
  abstract validateAccess(token: JwtToken, requestType: string): Promise<boolean>;
22
- abstract bearerTokenValidation(req: Request): Promise<JwtToken | null>;
22
+ abstract bearerTokenValidation(req: XRequest): Promise<JwtToken | null>;
23
23
  }
@@ -1,12 +1,15 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import express from 'express';
3
4
  import http from 'http';
4
5
  import { CorePresenter, IDataBase } from '@grandlinex/core';
5
6
  import { IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib';
7
+ import { XRequest, XResponse } from '../lib/express';
8
+ export declare function keepRawBody(req: XRequest, res: XResponse, buf: Buffer, encoding: string): void;
6
9
  export default abstract class BaseEndpoint<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends CorePresenter<express.Express, K, T, P, C, E> implements IBasePresenter {
7
- private appServer;
8
- private httpServer;
9
- private port;
10
+ protected appServer: express.Express;
11
+ protected httpServer: http.Server;
12
+ protected port: number;
10
13
  constructor(chanel: string, module: IBaseKernelModule<any, any, any, any>, port: number);
11
14
  start(): Promise<boolean>;
12
15
  stop(): Promise<boolean>;
@@ -3,16 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.keepRawBody = void 0;
6
7
  const express_1 = __importDefault(require("express"));
7
8
  const http_1 = __importDefault(require("http"));
8
9
  const body_parser_1 = require("body-parser");
9
10
  const core_1 = require("@grandlinex/core");
11
+ function keepRawBody(req, res, buf, encoding) {
12
+ if (buf && buf.length) {
13
+ try {
14
+ req.rawBody = buf.toString(encoding || 'utf8');
15
+ }
16
+ catch (e) {
17
+ req.rawBody = null;
18
+ }
19
+ }
20
+ }
21
+ exports.keepRawBody = keepRawBody;
10
22
  class BaseEndpoint extends core_1.CorePresenter {
11
23
  constructor(chanel, module, port) {
12
24
  super(`endpoint-${chanel}`, module);
13
25
  this.port = port;
14
26
  this.appServer = (0, express_1.default)();
15
- this.appServer.use((0, body_parser_1.json)());
27
+ this.appServer.use((0, body_parser_1.json)({ verify: keepRawBody }));
16
28
  this.httpServer = http_1.default.createServer(this.appServer);
17
29
  }
18
30
  start() {
@@ -1,9 +1,9 @@
1
1
  import { CoreBridge as BaseBridge, CoreCache as BaseCache, CoreClient as BaseClient, CoreElement as BaseElement, CoreLoopService as BaseLoopService, CoreService as BaseService } from '@grandlinex/core';
2
2
  import BaseAction from './BaseAction';
3
- import BaseEndpoint from './BaseEndpoint';
3
+ import BaseEndpoint, { keepRawBody } from './BaseEndpoint';
4
4
  import BaseKernelModule from './BaseKernelModule';
5
5
  import BaseApiAction from './BaseApiAction';
6
6
  import BaseAuthProvider from './BaseAuthProvider';
7
7
  export * from './BaseAuthProvider';
8
8
  export * from './timing';
9
- export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, BaseCache, BaseAction, BaseClient, BaseBridge, };
9
+ export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, BaseCache, BaseAction, BaseClient, BaseBridge, keepRawBody, };
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
13
25
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
27
  };
@@ -17,7 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
29
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
30
  };
19
31
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
32
+ exports.keepRawBody = exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
21
33
  const core_1 = require("@grandlinex/core");
22
34
  Object.defineProperty(exports, "BaseBridge", { enumerable: true, get: function () { return core_1.CoreBridge; } });
23
35
  Object.defineProperty(exports, "BaseCache", { enumerable: true, get: function () { return core_1.CoreCache; } });
@@ -27,8 +39,9 @@ Object.defineProperty(exports, "BaseLoopService", { enumerable: true, get: funct
27
39
  Object.defineProperty(exports, "BaseService", { enumerable: true, get: function () { return core_1.CoreService; } });
28
40
  const BaseAction_1 = __importDefault(require("./BaseAction"));
29
41
  exports.BaseAction = BaseAction_1.default;
30
- const BaseEndpoint_1 = __importDefault(require("./BaseEndpoint"));
42
+ const BaseEndpoint_1 = __importStar(require("./BaseEndpoint"));
31
43
  exports.BaseEndpoint = BaseEndpoint_1.default;
44
+ Object.defineProperty(exports, "keepRawBody", { enumerable: true, get: function () { return BaseEndpoint_1.keepRawBody; } });
32
45
  const BaseKernelModule_1 = __importDefault(require("./BaseKernelModule"));
33
46
  exports.BaseKernelModule = BaseKernelModule_1.default;
34
47
  const BaseApiAction_1 = __importDefault(require("./BaseApiAction"));
@@ -1,5 +1,5 @@
1
- import { Response } from 'express';
2
1
  import { CoreElement } from '@grandlinex/core';
2
+ import { XResponse } from '../../lib/express';
3
3
  export type IExtensionInterface = {
4
4
  done: () => void;
5
5
  timing: ExpressServerTiming;
@@ -12,6 +12,6 @@ export default class ExpressServerTiming {
12
12
  startFunc<T>(chanel: string, fc: () => Promise<T>): Promise<T>;
13
13
  dbQuery<T>(fc: () => Promise<T>): Promise<T>;
14
14
  getHeader(): string;
15
- addHeader(res: Response): void;
16
- static init(baseApiAction: CoreElement<any>, res: Response): [ExpressServerTiming, () => void];
15
+ addHeader(res: XResponse): void;
16
+ static init(baseApiAction: CoreElement<any>, res: XResponse): [ExpressServerTiming, () => void];
17
17
  }
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from './api';
9
9
  export * from './classes';
10
10
  export * from './modules/crypto';
11
11
  export * from './lib';
12
+ export * from './lib/express';
12
13
  export * from '@grandlinex/core';
13
14
  export { KernelModule, Kernel };
14
15
  export default Kernel;
package/dist/index.js CHANGED
@@ -31,5 +31,6 @@ __exportStar(require("./api"), exports);
31
31
  __exportStar(require("./classes"), exports);
32
32
  __exportStar(require("./modules/crypto"), exports);
33
33
  __exportStar(require("./lib"), exports);
34
+ __exportStar(require("./lib/express"), exports);
34
35
  __exportStar(require("@grandlinex/core"), exports);
35
36
  exports.default = Kernel_1.default;
@@ -0,0 +1,6 @@
1
+ import express from 'express';
2
+ export type XRequest = express.Request & {
3
+ rawBody?: string | null | undefined;
4
+ };
5
+ export type XResponse = express.Response;
6
+ export type XNextFc = express.NextFunction;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,8 @@
1
- import express, { Request, Response } from 'express';
2
1
  import { ICoreAction, ICoreBridge, ICoreCache, ICoreCClient, ICoreClient, ICoreElement, ICoreKernel, ICoreKernelModule, ICorePresenter, ICoreService, IDataBase } from '@grandlinex/core';
2
+ import express from 'express';
3
3
  import { IAuthProvider, JwtToken } from '../classes/BaseAuthProvider';
4
4
  import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
5
+ import { XNextFc, XRequest, XResponse } from './express';
5
6
  export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
6
7
  export interface ICClient extends ICoreCClient {
7
8
  setAuthProvider(provider: IAuthProvider): boolean;
@@ -14,14 +15,14 @@ export interface ICClient extends ICoreCClient {
14
15
  userId: string | null;
15
16
  }>;
16
17
  permissionValidation(token: JwtToken, requestType: string): Promise<boolean>;
17
- bearerTokenValidation(req: Request): Promise<JwtToken | null>;
18
+ bearerTokenValidation(req: XRequest): Promise<JwtToken | null>;
18
19
  }
19
20
  export interface IKernel extends ICoreKernel<ICClient> {
20
21
  getAppServerPort(): number;
21
22
  setAppServerPort(port: number): void;
22
23
  responseCodeFunction(data: {
23
24
  code: number;
24
- req: Request;
25
+ req: XRequest;
25
26
  }): void;
26
27
  }
27
28
  export type IBaseKernelModule<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreKernelModule<K, T, P, C, E>;
@@ -32,5 +33,5 @@ export type IBaseBrige = ICoreBridge;
32
33
  export type IBaseCache<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreCache<K, T, P, C, E>;
33
34
  export type IBaseElement<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreElement<K, T, P, C, E>;
34
35
  export interface IBaseAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends ICoreAction<K, T, P, C, E> {
35
- handler(req: Request, res: Response, next: () => void, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
36
+ handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
36
37
  }
@@ -1,7 +1,7 @@
1
- import { Request } from 'express';
2
1
  import { CoreCryptoClient } from '@grandlinex/core';
3
2
  import { ICClient, IKernel } from '../../lib';
4
3
  import { IAuthProvider, JwtToken, JwtTokenData } from '../../classes/BaseAuthProvider';
4
+ import { XRequest } from '../../lib/express';
5
5
  export default class CryptoClient extends CoreCryptoClient implements ICClient {
6
6
  protected authProvider: IAuthProvider | null;
7
7
  protected kernel: IKernel;
@@ -15,5 +15,5 @@ export default class CryptoClient extends CoreCryptoClient implements ICClient {
15
15
  userId: string | null;
16
16
  }>;
17
17
  permissionValidation(token: JwtToken, requestType: string): Promise<boolean>;
18
- bearerTokenValidation(req: Request): Promise<JwtToken | null>;
18
+ bearerTokenValidation(req: XRequest): Promise<JwtToken | null>;
19
19
  }
@@ -1,3 +1,3 @@
1
- import { NextFunction, Request, Response } from 'express';
2
- export type CorsMiddleWare = (req: Request, res: Response, next: NextFunction) => void;
1
+ import { XNextFc, XRequest, XResponse } from '../../../lib/express';
2
+ export type CorsMiddleWare = (req: XRequest, res: XResponse, next: XNextFc) => void;
3
3
  export declare const cors: CorsMiddleWare;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grandlinex/kernel",
3
- "version": "0.27.0",
3
+ "version": "0.27.1",
4
4
  "description": "GrandLineX is an out-of-the-box server framework on top of ExpressJs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,11 +27,11 @@
27
27
  },
28
28
  "license": "BSD-3-Clause",
29
29
  "dependencies": {
30
- "@grandlinex/core": "^0.27.0",
31
- "axios": "^0.27.2",
32
- "body-parser": "^1.20.1",
33
- "express": "^4.18.2",
34
- "jsonwebtoken": "^8.5.1"
30
+ "@grandlinex/core": "0.27.0",
31
+ "axios": "0.27.2",
32
+ "body-parser": "1.20.1",
33
+ "express": "4.18.2",
34
+ "jsonwebtoken": "9.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/express": "^4.17.14",