Skip to content
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

[feat] Prefer returning the next middleware's result in the documentation examples #1710

Open
2 tasks done
zkulbeda opened this issue Nov 12, 2022 · 0 comments
Open
2 tasks done

Comments

@zkulbeda
Copy link

There are examples in the documentation that show that it is not necessary to return a result of the next middleware. If the developers follow this example for writing packages, then it will be impossible to do this things:

app.use(async (ctx, next) => {
  const result = await next();
  if(result instanceof CustomError){
    ctx.body = "something wrong";
    ctx.status = 500;
    return;
  }
  if(!result && typeof result === "object"){
    ctx.body = result; // response with json
  }
})

app.use(async (ctx, next) => {
  // example code from external package
  await next();
  // the result is lost
});

app.use(async ctx => {
  if(ctx.request.charset !== 'utf-8') // some logic
    return new CustomError();

  return { ok: true }
});

Actually, I see this use case as extremely convenient. Why not allow something to be returned for processing? And, for example, consider it as the body of the response. It doesn't matter whether it is a string, an object or a stream, just treat it as a response.

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
@zkulbeda zkulbeda changed the title [feat] Prefer returning next middleware's result in documentation examples [feat] Prefer returning the next middleware's result in the documentation examples Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant