Skip to content

Commit

Permalink
feat(core): add middleware data in request object
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-coder committed Nov 22, 2020
1 parent f5b7563 commit cf62dba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/core/__tests__/__snapshots__/core.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Object {
"getCommandById": [Function],
"getCommandByName": [Function],
"getCommands": [Function],
"getMiddlewares": [Function],
"runCommand": [Function],
},
"root": StringContaining "/packages/core/__tests__",
Expand Down
28 changes: 20 additions & 8 deletions packages/core/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : {},
};
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class LesyMiddleware {
END: [],
};

add(mw: Middleware) {
add(mw: Middleware, source: string) {
mw.source = source;
this.middlewares[mw.on].push(mw);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type MiddlewareFn = (
export interface Middleware {
on: MiddlewarePlacement;
run: MiddlewareFn;
source?: string;
}

export type MiddlewareContext = Record<string, any>;
Expand Down

0 comments on commit cf62dba

Please sign in to comment.