Skip to content

Commit

Permalink
fix(core): ensure state is passed to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
OYED committed Mar 24, 2022
1 parent 7fcc1da commit 0d4b752
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/instance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const lamware = <H extends Handler = Handler>(options?: Options) => {
const basePayload: MiddlewarePayload<H> = {
debug,
logger: localLogger,
state: {},
state: compileState(),
};

let payload: BeforeMiddlewarePayload<H> = {
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/lamware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ test('should allow middleware to initialize with global state', async () => {
expect(result.body).toBe(JSON.stringify({ hello: true, hello2: false }));
});

test('should allow middleware to access state set by init', async () => {
const { handler } = lamware<APIGatewayProxyHandlerV2<any>>()
.use<Middleware<APIGatewayProxyHandlerV2<any>, { testing123: boolean; testing1234: boolean }>>({
id: 'test-1',
pure: true,
init: async () => ({ testing123: true }),
before: async (payload) => {
payload.state.testing1234 = payload.state.testing123;
return payload;
},
})
.execute(async ({ state }) => {
return {
statusCode: 200,
body: JSON.stringify({ hello: state.testing1234 }),
};
});
const result = await execute(handler);

expect(result.statusCode).toBe(200);
expect(result.body).toBe(JSON.stringify({ hello: true }));
});

test('should allow middleware to modify a global state', async () => {
const { handler } = lamware<APIGatewayProxyHandlerV2<any>>()
.use<Middleware<APIGatewayProxyHandlerV2<any>, { testing123: boolean }>>({
Expand Down

0 comments on commit 0d4b752

Please sign in to comment.