You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 31, 2024. It is now read-only.
I am working on a Koa v1 app and would like to implement new routes with async/await instead of generator functions. I found this library has convert.back() which is mostly working for me. However, the following example code will illustrate my problem:
router.get('/endpoint',
async function(next) {
const token = this.request.headers.token;
if (!token) {
this.response.status = 401;
return;
}
// I assume next is a Promise so this is not an issue?
await next;
},
async function(next) {
// This code still executes, even if there is no token defined on the headers.
// Do something important that shouldn't execute with invalid auth...
});
return convert.back(router.routes());
If there is no token defined, the endpoint does return a 401 and exits the first function, but the second function is still executed. If I set up the first function as a generator function and use yield next, it works as would be expected.
I think [email protected] is still using koa-v1 signature (generator function) and does not support koa-v2+ signature (async await function).
How about using koa-router@7 (it support koa-v2+ signature) with following code ?
// [email protected]router.get('/endpoint',async(ctx,next)=>{consttoken=ctx.request.headers.token;if(!token){ctx.response.status=401;return;}awaitnext();},async(ctx,next)=>{// This code still executes, even if there is no token defined on the headers.// Do something important that shouldn't execute with invalid auth...});// `router.routes()` will return async-await style middleware in koa-router@7// `convert.back` will convert async-await style middleware to generator function style middleware// then it can be used for koa-v1 app.returnconvert.back(router.routes());
Or if you have to use [email protected], you can convert each individual async functions like following:
// [email protected]router.get('/endpoint',convert.back(async(ctx,next)=>{consttoken=ctx.request.headers.token;if(!token){ctx.response.status=401;return;}// I assume next is a Promise so this is not an issue?awaitnext();}),convert.back(async(ctx,next)=>{// This code still executes, even if there is no token defined on the headers.// Do something important that shouldn't execute with invalid auth...}));// no need `convert.back` here because it's already koa-v1 style middleware// and it can be consumed by koa-v1 app directly.returnrouter.routes();
I think the easiest router forward at this point is to just stick with 5.4.2 and wrap them all until I have the time to upgrade to v7 and test everything.
Was there somewhere I could have found this in documentation? I was struggling to find info about properly implementing the convert.back() feature. This issue will at least exist for anyone stuck in this situation in the future.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I am working on a Koa v1 app and would like to implement new routes with async/await instead of generator functions. I found this library has
convert.back()
which is mostly working for me. However, the following example code will illustrate my problem:If there is no token defined, the endpoint does return a 401 and exits the first function, but the second function is still executed. If I set up the first function as a generator function and use
yield next
, it works as would be expected.Versions:
node: 6.13.1
npm: 3.10.10
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
The text was updated successfully, but these errors were encountered: