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!: more flexible koa server creation #88

Merged
merged 11 commits into from
Nov 11, 2023
Merged

Conversation

mnahkies
Copy link
Owner

@mnahkies mnahkies commented Oct 31, 2023

  • split out a generated createRouter function that only creates
    the koa router, making use of the bootstrap / startServer
    functions optional

  • allow passing options to the cors / koa-body middlewares, or
    disabling these completely

  • support passing a ListenOptions instead of port number, allowing
    to bind to a specific network interface

  • return the koa app and the AddressInfo from startServer /
    bootstrap

  • make startServer an async function that rejects if app.listen
    fails

this is a breaking change, and probably not the last to this interface.
whilst the prior iteration was far too inflexible, this doesn't feel quite right still:

  • startServer still pretty inflexible/simple, and doesn't support all options
  • but also now flexible enough to shoot yourself in the foot, eg: body: "disable" and no suitable alternative provided
  • bootstrap function basically redundant now as well, though that may not remain true so can stay for the moment

closes #94

- split out a generated `createRouter` function that only creates
  the koa router, making use of the `bootstrap` / `startServer`
  optional

- allow passing options to the cors / koa-body middlewares, or
  disabling these completely

- return the koa `app` and the `AddressInfo` from `startServer` /
  `bootstrap`

- make `startServer` and `async` function that rejects if `app.listen`
  fails

**this is a breaking change**, and probably not the last to this interface.
whilst the prior iteration was far too inflexible, this doesn't feel
quite right as it makes it a little too easy to construct a server that
doesn't work (eg: disabling body parser without providing alternative).
the bootstrap function is kinda redundant now as well, though that may
not remain true so can stay for the moment.

**migration:** see integration-tests/typescript-koa/src/petstore-expanded.yaml/index.ts
for example
```
TS2742: The inferred type of  createRouter  cannot be named without a
reference to  koa-body/node_modules/@types/koa.
This is likely not portable. A type annotation is necessary.
```
@mnahkies mnahkies marked this pull request as ready for review November 11, 2023 09:35
app.use(router.allowedMethods())
app.use(router.routes())

return new Promise((resolve, reject) => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty ugly use of new Promise and several try-catch blocks, but whilst I don't think sever.address() throws right now, there's a FIXME comment suggesting it might in future, and doing this means that any exceptions will definitely stay in the promise chain correctly.

Also starts handling errors like EADDRINUSE properly, as these get emitted as an error event

@mnahkies mnahkies merged commit 6669abe into main Nov 11, 2023
1 check passed
@mnahkies mnahkies deleted the mn/feat/more-flexibility branch November 11, 2023 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow more config options for ServerConfig, or allow passing our own Koa instance.
1 participant