Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/express-wrapper/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ export async function runMiddlewareChainIgnoringResults<
}
});
}
return input;
return { ...input, ...(req.body || {}) };
}
32 changes: 32 additions & 0 deletions packages/express-wrapper/test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ test('should handle errors passed to next()', async () => {
);
});

test('middleware that modifies req should pass changes through chain', async () => {
const testReq = { body: {}, headers: {} } as express.Request;
const testRes = {} as express.Response;

const modifyReqMiddleware: express.RequestHandler = (req, _res, next) => {
(req as any).userId = 123;
next();
};

const checkReqMiddleware: express.RequestHandler = (req, _res, next) => {
assert.equal(
(req as any).userId,
123,
'userId should still be on req in second middleware',
);
next();
};

await runMiddlewareChain(
{ foo: 'test' },
[modifyReqMiddleware, noopMiddleware, checkReqMiddleware],
testReq,
testRes,
);

assert.equal(
(testReq as any).userId,
123,
'req should still have userId after chain',
);
});

test('should work with middleware that return values', async () => {
const result = await runMiddlewareChain(
{ foo: 'test' },
Expand Down
56 changes: 56 additions & 0 deletions packages/express-wrapper/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,59 @@ test('should return a 400 when request fails to decode', async () => {

assert(response.body.error.startsWith('Invalid value undefined supplied to'));
});

test('middleware that modifies req.body should reach handler even without routeHandler()', async () => {
const PostWithData = httpRoute({
path: '/data',
method: 'POST',
request: httpRequest({
body: {
originalField: t.string,
addedByMiddleware: optional(t.string),
},
}),
response: {
200: t.type({
originalField: t.string,
addedByMiddleware: optional(t.string),
}),
},
});

const testApiSpec = apiSpec({
'test.route': {
post: PostWithData,
},
});

const modifyBodyMiddleware: express.RequestHandler = (req, _res, next) => {
req.body.addedByMiddleware = 'ADDED';
next();
};

const handler = async (params: { originalField: string; addedByMiddleware?: string }) => {
return {
type: 200,
payload: {
originalField: params.originalField,
addedByMiddleware: params.addedByMiddleware,
},
} as const;
};

const app = createServer(testApiSpec, (app: express.Application) => {
app.use(express.json());
return {
'test.route': {
post: { middleware: [modifyBodyMiddleware], handler },
},
};
});

const response = await supertest(app)
.post('/data')
.send({ originalField: 'test' })
.expect(200);

assert.equal(response.body.addedByMiddleware, 'ADDED', 'addedByMiddleware should be present because req.body is part of req.decoded');
});
Loading