bttmly / koa-detour

An expressive router for Koa v2 applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issues with `handleSuccess` and `handleError`

bttmly opened this issue · comments

  • having handle(event, fn), handleSuccess(ctx, result), and handleError(ctx, err) is confusing!
  • the functionality of handleSuccess and handleError can be implemented without library-level hooks – users simply do the following
const app = new Koa();
const router = new Detour();
const middleware = router.middleware();
app.use(function (ctx) {
  return middleware(ctx)
    .then(result => successHandler(ctx, result))
    .catch(err => errorHandler(ctx, err));
});

In fact, this is how it's implemented in the library, and avoiding having the library handle it is better for a couple reasons:

  • smaller API surface area
  • more flexible (i.e. complex .then/.catch schemes are possible when not hidden inside library)

There is a downside however – the most common case for handleSuccess/handleError would probably be for other library code to install those handlers and have the whole thing be mostly transparent to the user. But it can still be something like app.use(responder(router)) but it's a little wonky because we're using app.use rather than router.use