Skip to content

Commit

Permalink
feat: pass a new res.locals.params object to http-incoming
Browse files Browse the repository at this point in the history
This is in line with Fastify podlet and layout which provide reply.app.params to http-incoming.
It is not, however, in line with Podium layout which passes the whole res.locals object. Passing
the whole res.locals object can cause circular reference issues since we set http-incoming on res.locals.podium as well and should be changed.
  • Loading branch information
digitalsadhu committed Apr 11, 2024
1 parent f2db5d6 commit b9ef277
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/podlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,10 @@ export default class PodiumPodlet {
*/
middleware() {
return async (req, res, next) => {
const incoming = new HttpIncoming(req, res);
// initialise res.locals.params so that it can be added to anywhere including in route hooks
// or middleware added after this middleware
res.locals.params = res.locals.params || {};
const incoming = new HttpIncoming(req, res, res.locals.params);
// @ts-ignore
incoming.url = new URL(
req.originalUrl,
Expand Down
18 changes: 18 additions & 0 deletions tests/podlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,24 @@ tap.test('.view() - append a custom wireframe document - should render developme
await server.close();
});

tap.test('.view() - append a custom wireframe document - should render development output with custom wireframe document', async (t) => {
const options = { ...DEFAULT_OPTIONS, development: true };

const podlet = new Podlet(options);
podlet.view((incoming, data) => `<div data-foo="${incoming.params.foo}">${data}</div>`);

const server = new FakeExpressServer(podlet, (req, res) => {
res.locals.params.foo = 'bar';
res.podiumSend('<h1>OK!</h1>');
});

await server.listen();
const result = await server.get({ raw: true });

t.equal(result.response, '<div data-foo="bar"><h1>OK!</h1></div>');
await server.close();
});

// #############################################
// .metrics()
// #############################################
Expand Down

0 comments on commit b9ef277

Please sign in to comment.