From cf62dbaf39d1e1cc02f243ccec0b6e15b8b472b0 Mon Sep 17 00:00:00 2001 From: lokesh-coder Date: Sun, 22 Nov 2020 09:26:46 +0530 Subject: [PATCH] feat(core): add middleware data in request object --- .../__tests__/__snapshots__/core.spec.ts.snap | 1 + packages/core/__tests__/core.spec.ts | 28 +++++++++++++------ packages/core/src/core.ts | 1 + packages/core/src/middleware.ts | 3 +- packages/core/src/model.ts | 1 + 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/core/__tests__/__snapshots__/core.spec.ts.snap b/packages/core/__tests__/__snapshots__/core.spec.ts.snap index 73e5e0f..8467083 100644 --- a/packages/core/__tests__/__snapshots__/core.spec.ts.snap +++ b/packages/core/__tests__/__snapshots__/core.spec.ts.snap @@ -89,6 +89,7 @@ Object { "getCommandById": [Function], "getCommandByName": [Function], "getCommands": [Function], + "getMiddlewares": [Function], "runCommand": [Function], }, "root": StringContaining "/packages/core/__tests__", diff --git a/packages/core/__tests__/core.spec.ts b/packages/core/__tests__/core.spec.ts index aeff44f..860948c 100644 --- a/packages/core/__tests__/core.spec.ts +++ b/packages/core/__tests__/core.spec.ts @@ -103,17 +103,29 @@ describe("Core", () => { it("should get all middlewares", () => { expect(core["mwCtrl"].get()).toEqual({ END: [ - { on: "END", run: expect.any(Function) }, - { on: "END", run: expect.any(Function) }, + { on: "END", run: expect.any(Function), source: "__OBJ__" }, + { + on: "END", + run: expect.any(Function), + source: `${__dirname}/plugin/middleware.js`, + }, + ], + INIT: [{ on: "INIT", run: expect.any(Function), source: "__OBJ__" }], + POST_PARSE: [ + { on: "POST_PARSE", run: expect.any(Function), source: "__OBJ__" }, ], - INIT: [{ on: "INIT", run: expect.any(Function) }], - POST_PARSE: [{ on: "POST_PARSE", run: expect.any(Function) }], POST_RUN: [], POST_VALIDATE: [], - PRE_PARSE: [{ on: "PRE_PARSE", run: expect.any(Function) }], - PRE_RUN: [{ on: "PRE_RUN", run: expect.any(Function) }], - PRE_VALIDATE: [{ on: "PRE_VALIDATE", run: expect.any(Function) }], - START: [{ on: "START", run: expect.any(Function) }], + PRE_PARSE: [ + { on: "PRE_PARSE", run: expect.any(Function), source: "__OBJ__" }, + ], + PRE_RUN: [ + { on: "PRE_RUN", run: expect.any(Function), source: "__OBJ__" }, + ], + PRE_VALIDATE: [ + { on: "PRE_VALIDATE", run: expect.any(Function), source: "__OBJ__" }, + ], + START: [{ on: "START", run: expect.any(Function), source: "__OBJ__" }], }); }); it("should get config", () => { diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 61d44d3..b5040fe 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -107,6 +107,7 @@ class LesyCoreClass { getCommandById: this.cmdCtrl.getCommandById.bind(this.cmdCtrl), getCommandByName: this.cmdCtrl.getCommandByName.bind(this.cmdCtrl), getCommands: this.cmdCtrl.getCommands.bind(this.cmdCtrl), + getMiddlewares: this.mwCtrl ? this.mwCtrl.get.bind(this.mwCtrl) : {}, }; } diff --git a/packages/core/src/middleware.ts b/packages/core/src/middleware.ts index 0f37445..55873f0 100644 --- a/packages/core/src/middleware.ts +++ b/packages/core/src/middleware.ts @@ -19,7 +19,8 @@ class LesyMiddleware { END: [], }; - add(mw: Middleware) { + add(mw: Middleware, source: string) { + mw.source = source; this.middlewares[mw.on].push(mw); } diff --git a/packages/core/src/model.ts b/packages/core/src/model.ts index 4e591d1..31fe11f 100644 --- a/packages/core/src/model.ts +++ b/packages/core/src/model.ts @@ -34,6 +34,7 @@ export type MiddlewareFn = ( export interface Middleware { on: MiddlewarePlacement; run: MiddlewareFn; + source?: string; } export type MiddlewareContext = Record;