Skip to content

Commit

Permalink
feat: useAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 21, 2020
1 parent 36ce7c9 commit 236e979
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const app = createApp()
app.use('/api', (req) => ({ url: req.url }))
app.use(() => 'Hello world!')

// app.useAsync(async () => {})
// app.use('/big', () => import('./big'), { lazy: true })

const port = process.env.PORT || 3000
const server = new Server(app.handle)

Expand Down Expand Up @@ -65,6 +68,9 @@ Converts a classic middleware (`req, res, next?`) into promisifier version ready
- If calling middleware throws an immediate error, promise will be rejected
- On `close` and `close` events of res, promise will `resolve/reject` (to ensure if middleware simply calls `res.end`)

When calling `app.use`, middleware will be automatically promisified.
If you are already making an async aware middleware, can use `app.useAsync`

## License

MIT
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export function createApp (options: AppOptions = {}): App {
app.stack = stack
app.handle = handle
// @ts-ignore
app.use = (...args) => use(app, ...args)
app.use = (arg1, arg2, arg3) => use(app, arg1, arg2, arg3)
// @ts-ignore
app.useAsync = (arg1, arg2) =>
use(arg1, arg2 !== undefined ? arg2 : { promisify: false }, { promisify: false })

return app as App
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface App {
stack: Stack
handle: PHandle
use: AppUse
useAsync: AppUse
}

export interface AppOptions {
Expand Down

0 comments on commit 236e979

Please sign in to comment.