Skip to content

Commit

Permalink
feat: typecheck handler to be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 29, 2022
1 parent 333a4ca commit 38493eb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export function defineLazyEventHandler (factory: LazyEventHandler): EventHandler
if (_resolved) { return Promise.resolve(_resolved) }
if (!_promise) {
_promise = Promise.resolve(factory()).then((r: any) => {
const handler = r.default || r
if (typeof handler !== 'function') {
throw new TypeError('Invalid lazy handler result. It should be a function:', handler)
}
_resolved = toEventHandler(r.default || r)
return _resolved
})
Expand All @@ -61,6 +65,9 @@ export function toEventHandler (handler: CompatibilityEventHandler): EventHandle
if (isEventHandler(handler)) {
return handler
}
if (typeof handler !== 'function') {
throw new TypeError('Invalid handler. It should be a function:', handler)
}
return defineEventHandler((event) => {
return callHandler(handler, event.req as IncomingMessage, event.res) as Promise<H3Response>
})
Expand Down

0 comments on commit 38493eb

Please sign in to comment.