Skip to content

Commit cf62dba

Browse files
committed
feat(core): add middleware data in request object
1 parent f5b7563 commit cf62dba

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

packages/core/__tests__/__snapshots__/core.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Object {
8989
"getCommandById": [Function],
9090
"getCommandByName": [Function],
9191
"getCommands": [Function],
92+
"getMiddlewares": [Function],
9293
"runCommand": [Function],
9394
},
9495
"root": StringContaining "/packages/core/__tests__",

packages/core/__tests__/core.spec.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,29 @@ describe("Core", () => {
103103
it("should get all middlewares", () => {
104104
expect(core["mwCtrl"].get()).toEqual({
105105
END: [
106-
{ on: "END", run: expect.any(Function) },
107-
{ on: "END", run: expect.any(Function) },
106+
{ on: "END", run: expect.any(Function), source: "__OBJ__" },
107+
{
108+
on: "END",
109+
run: expect.any(Function),
110+
source: `${__dirname}/plugin/middleware.js`,
111+
},
112+
],
113+
INIT: [{ on: "INIT", run: expect.any(Function), source: "__OBJ__" }],
114+
POST_PARSE: [
115+
{ on: "POST_PARSE", run: expect.any(Function), source: "__OBJ__" },
108116
],
109-
INIT: [{ on: "INIT", run: expect.any(Function) }],
110-
POST_PARSE: [{ on: "POST_PARSE", run: expect.any(Function) }],
111117
POST_RUN: [],
112118
POST_VALIDATE: [],
113-
PRE_PARSE: [{ on: "PRE_PARSE", run: expect.any(Function) }],
114-
PRE_RUN: [{ on: "PRE_RUN", run: expect.any(Function) }],
115-
PRE_VALIDATE: [{ on: "PRE_VALIDATE", run: expect.any(Function) }],
116-
START: [{ on: "START", run: expect.any(Function) }],
119+
PRE_PARSE: [
120+
{ on: "PRE_PARSE", run: expect.any(Function), source: "__OBJ__" },
121+
],
122+
PRE_RUN: [
123+
{ on: "PRE_RUN", run: expect.any(Function), source: "__OBJ__" },
124+
],
125+
PRE_VALIDATE: [
126+
{ on: "PRE_VALIDATE", run: expect.any(Function), source: "__OBJ__" },
127+
],
128+
START: [{ on: "START", run: expect.any(Function), source: "__OBJ__" }],
117129
});
118130
});
119131
it("should get config", () => {

packages/core/src/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class LesyCoreClass {
107107
getCommandById: this.cmdCtrl.getCommandById.bind(this.cmdCtrl),
108108
getCommandByName: this.cmdCtrl.getCommandByName.bind(this.cmdCtrl),
109109
getCommands: this.cmdCtrl.getCommands.bind(this.cmdCtrl),
110+
getMiddlewares: this.mwCtrl ? this.mwCtrl.get.bind(this.mwCtrl) : {},
110111
};
111112
}
112113

packages/core/src/middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class LesyMiddleware {
1919
END: [],
2020
};
2121

22-
add(mw: Middleware) {
22+
add(mw: Middleware, source: string) {
23+
mw.source = source;
2324
this.middlewares[mw.on].push(mw);
2425
}
2526

packages/core/src/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type MiddlewareFn = (
3434
export interface Middleware {
3535
on: MiddlewarePlacement;
3636
run: MiddlewareFn;
37+
source?: string;
3738
}
3839

3940
export type MiddlewareContext = Record<string, any>;

0 commit comments

Comments
 (0)