-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: use events api for utils with compatibility layer (#75)
BREAKING CHANGE: All `handle` exports and properties are renamed to `hanhttps://assets.grammarly.com/emoji/v1/1f610.svgdler` with some backward compatibilities. BREAKING CHANGE: Legacy handlers are promisified by default
- Loading branch information
Showing
17 changed files
with
294 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,97 @@ | ||
import type { IncomingMessage, ServerResponse } from './types/node' | ||
import type { Handle, Middleware } from './handle' | ||
import type http from 'http' | ||
import type { IncomingMessage, ServerResponse, Handler, Middleware } from './types' | ||
import { callHandler } from './handler' | ||
|
||
export interface H3Event { | ||
'__is_event__': true | ||
event: H3Event | ||
req: IncomingMessage | ||
res: ServerResponse, | ||
next?: (err?: Error) => void | ||
res: ServerResponse | ||
/** | ||
* Request params only filled with h3 Router handlers | ||
*/ | ||
params?: Record<string, any> | ||
} | ||
|
||
export type CompatibilityEvent = H3Event | IncomingMessage | ServerResponse | ||
|
||
export type _JSONValue<T=string|number|boolean> = T | T[] | Record<string, T> | ||
export type JSONValue = _JSONValue<_JSONValue> | ||
export type H3Response = void | JSONValue | Buffer | ||
|
||
export interface H3EventHandler { | ||
__is_handler__?: true | ||
(event: H3Event): H3Response| Promise<H3Response> | ||
export interface EventHandler { | ||
'__is_handler__'?: true | ||
(event: CompatibilityEvent): H3Response| Promise<H3Response> | ||
} | ||
|
||
export function defineEventHandler (handler: H3EventHandler) { | ||
export function defineEventHandler (handler: EventHandler) { | ||
handler.__is_handler__ = true | ||
return handler | ||
} | ||
|
||
export function isEventHandler (handler: H3EventHandler | Handle | Middleware): handler is H3EventHandler { | ||
return '__is_handler__' in handler | ||
export function defineLazyEventHandler (factory: () => EventHandler | Promise<EventHandler>): EventHandler { | ||
let _promise: Promise<EventHandler> | ||
let _resolved: EventHandler | ||
const resolveHandler = () => { | ||
if (_resolved) { return Promise.resolve(_resolved) } | ||
if (!_promise) { | ||
_promise = Promise.resolve(factory()).then((r: any) => { | ||
_resolved = r.default || r | ||
return _resolved | ||
}) | ||
} | ||
return _promise | ||
} | ||
return defineEventHandler((event) => { | ||
if (_resolved) { | ||
return _resolved(event) | ||
} | ||
return resolveHandler().then(handler => handler(event)) | ||
}) | ||
} | ||
|
||
export function toEventHandler (handler: H3EventHandler | Handle | Middleware): H3EventHandler { | ||
export function isEventHandler (input: any): input is EventHandler { | ||
return '__is_handler__' in input | ||
} | ||
|
||
export function toEventHandler (handler: EventHandler | Handler | Middleware): EventHandler { | ||
if (isEventHandler(handler)) { | ||
return handler | ||
} | ||
if (handler.length > 2) { | ||
return defineEventHandler((event) => { | ||
return (handler as Middleware)(event.req, event.res, event.next!) | ||
}) | ||
} else { | ||
return defineEventHandler((event) => { | ||
return (handler as Handle)(event.req, event.res) | ||
}) | ||
} | ||
return defineEventHandler((event) => { | ||
return callHandler(handler, event.req as IncomingMessage, event.res) as Promise<H3Response> | ||
}) | ||
} | ||
|
||
export function createEvent (req: IncomingMessage, res: ServerResponse): H3Event { | ||
return { | ||
export function createEvent (req: http.IncomingMessage, res: http.ServerResponse): CompatibilityEvent { | ||
const event = { | ||
__is_event__: true, | ||
req, | ||
res | ||
} | ||
} as H3Event | ||
|
||
// Backward comatibility for interchangable usage of {event,req,res}.{req,res} | ||
// TODO: Remove in future versions | ||
// @ts-ignore | ||
event.event = event | ||
// @ts-ignore | ||
req.event = event | ||
// @ts-ignore | ||
req.req = req | ||
// @ts-ignore | ||
req.res = res | ||
// @ts-ignore | ||
res.event = event | ||
// @ts-ignore | ||
res.res = res | ||
// @ts-ignore | ||
res.req.res = res | ||
// @ts-ignore | ||
res.req.req = req | ||
|
||
return event | ||
} | ||
|
||
export function isEvent (input: any): input is H3Event { | ||
return '__is_event__' in input | ||
} |
Oops, something went wrong.