-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return middleware chain promise from callback()
#847
Conversation
Without this, there's no way of hooking into after Koa is done with the response. This can be extremely important if used with `ctx.respond = false`, as in koajs#846 (see there for use case). While this is technically a breaking change, I don't expect it to break anything.
i.m.o. it's not really a breaking change :) |
Yeah, the one situation I could see this breaking anything is |
Current coverage is 100% (diff: 100%)@@ master #847 diff @@
====================================
Files 4 4
Lines 417 419 +2
Methods 81 81
Messages 0 0
Branches 102 104 +2
====================================
+ Hits 417 419 +2
Misses 0 0
Partials 0 0
|
I don't think that's how diffs work... 😄 |
can we add a test for the use-case if we add this? not opposed to it. |
This is great! One thing to be aware of is that even if you do |
yes :) suggestions welcomed. i haven't thought about this yet, so feel free to make suggestions. |
Maybe |
Disables default response handler. See docs for more info.
Okay, I've just added in |
im ok with it. anyone else? @koajs/owners though maybe we could use a name with only 1-2 words :P |
Yeah a better name for the app variable would be appreciated. |
I can rebase and pull the changes into the v2 branch if you're ready to merge. |
Any updates on this? |
If a better config name is needed, maybe |
why not |
Because that describes a common use case, not its function. |
So as I look at this again, it occurs to me that we may only need to return the promise from const app = new Koa();
// very first thing we do, set `ctx.status` to null
// very last thing we do is check if `ctx.status` is still null
app.use(async (ctx, next) => {
ctx.status = null;
await next();
if (ctx.status == null) ctx.respond = false;
});
// main app logic and routes here -- in this simplified example,
// Koa is just supposed to handle a single route `/user`
app.use(async ctx => {
if (ctx.url !== "/user") {
return; // let ctx.status remain `null` -- defer handling to enclosing app
}
// assume `ctx.session` has some relevant data, elided here
const user = await db.getUser(ctx.session);
ctx.body = user;
ctx.status = 200; // <-- set status here, means Koa is responsible for sending response
});
module.exports = app; usage from existing express/connect app const expressApp = express();
const koaCallback = require("./myKoaApp").callback();
expressApp.use(function (req, res, next) {
koaCallback(req, res).then(function () {
if (res.headersSent) return;
next();
});
}) Pros:
Cons:
Alternately -- which I think is less-good than above but maybe better than Thoughts? |
@PlasmaPower @jonathanong any thoughts on the comment above? would love to get support for embedding Koa into existing applications merged ASAP. Happy with the current implementation if people feel it's superior to what I proposed above. |
@nickb1080 yes, that looks feasible. It's been a while since I looked at this issue. if all we want to merge is |
@jonathanong How do you want to proceed? Do you want me to open a PR against |
@nickb1080 why not master? |
Why does the branch |
Ah! I hadn't noticed the switchover had already happened. This PR (847) has some extraneous stuff that doesn't seem necessary -- the simple change in https://github.com/koajs/koa/pull/848/files would be sufficient I think. |
Yeah I agree. Closing this in favor of #848. |
Without this, there's no way of hooking into after Koa is done with the response. This can be extremely important if used with
ctx.respond = false
, as in #846 (see there for use case). While this is technically a breaking change, I don't expect it to break anything.