I can use single-line to ensure if a variable is true like this in Koa:
ctx.assert(ctx.state.user, 401, 'User not found. Please login!');
So I wonder that if we could add same method to res?
res.assert(ctx.state.user, 401, 'User not found. Please login!');
Hey @wxs77577, while I think this is an interesting sugar layer on top of responding conditionally based on an assertion, I am not a fan of adding things like this to express directly. I think this is a great thing as an separate library, maybe Koa even already published this as a stand alone module?
I love express because of it's built-in functions like view render, body-parser,static-server, router, so I hope you could add more great tools, but it's ok if you think res.assert should not be add as a built-in function.
Yes, let's keep core Express as bare-bones as possible.
Most helpful comment
I love express because of it's built-in functions like
view render,body-parser,static-server,router, so I hope you could add more great tools, but it's ok if you thinkres.assertshould not be add as a built-in function.