Skip to content

Commit

Permalink
feat: add defineHandle and defineMiddleware type helpers (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 24, 2021
1 parent e4975dc commit 8260887
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Instead of adding helpers to `req` and `res`, h3 exposes them as composable util
- `appendHeader(res, name, value)`
- `createError({ statusCode, statusMessage, data? }`
- `sendError(res, error, debug?)`
- `defineHandle(handle)`
- `defineMiddleware(middlware)`

👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).

Expand Down
5 changes: 4 additions & 1 deletion src/handle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { withoutTrailingSlash, withoutBase } from 'ufo'
import type { IncomingMessage, ServerResponse } from './types/node'

export type Handle <T=any> = (req: IncomingMessage, res: ServerResponse) => T
export type Handle<T = any> = (req: IncomingMessage, res: ServerResponse) => T
export type PHandle = Handle<Promise<any>>
export type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any
export type LazyHandle = () => Handle | Promise<Handle>

export const defineHandle = <T>(handler: Handle<T>) => handler
export const defineMiddleware = (middleware: Middleware) => middleware

export function promisifyHandle (handle: Handle | Middleware): PHandle {
return function (req: IncomingMessage, res: ServerResponse) {
return callHandle(handle, req, res)
Expand Down

0 comments on commit 8260887

Please sign in to comment.