Skip to content

Commit

Permalink
'this' in res methods (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaziuk authored Aug 19, 2017
1 parent f0efd5e commit 369f714
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 15 additions & 0 deletions index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,19 @@ describe(middleware.name, () => {
done(err);
}
});
it("should this in res methods be instance of Response", async (done) => {
try {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/" });
const next = spy();
await handler(req as any, res as any, next as any);
expect(res.send.called).to.be.equal(true, `'res.send' was not called`);
expect(res.send.firstCall.thisValue).to.be.not.a("undefined", `'this' is undefined`);
expect(res.send.firstCall.thisValue).to.be.equal(res, `'res.send' not called with the 'res' as 'this'`);
done();
} catch (err) {
done(err);
}
});
});
27 changes: 14 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ export function mithrilExpressMiddleware(
html = (partial: Promise<string>) => partial,
} = opt;
try {
res.send(
await html(
routeRender(
routes,
req.path,
defaultRoute,
{
...(attrsCookies && req.cookies ? req.cookies : {}),
...(attrsBody && req.body ? req.body : {}),
...(attrsQuery && req.query ? req.query : {}),
...attrs,
},
),
const doc = await html(
routeRender(
routes,
req.path,
defaultRoute,
{
...(attrsCookies && req.cookies ? req.cookies : {}),
...(attrsBody && req.body ? req.body : {}),
...(attrsQuery && req.query ? req.query : {}),
...attrs,
},
),
);
res.send(
doc,
).end();
} catch (e) {
next(e);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mithril-express-middleware",
"version": "1.1.9",
"version": "1.1.10",
"description": "express middleware for mithril.js projects",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 369f714

Please sign in to comment.