mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
45 lines
2.0 KiB
TypeScript
45 lines
2.0 KiB
TypeScript
import type { ApiHandler } from './api';
|
|
import type { ConnectionHandler, Handler, NotFoundHandler } from './server';
|
|
|
|
export interface KnownHandlers {
|
|
NotFound: NotFoundHandler;
|
|
}
|
|
|
|
type MapHandlerEvents<N extends string, H extends Handler> =
|
|
Record<`handler/${HookType}/${N}`, (thisArg: H) => VoidReturn>
|
|
& Record<`handler/${HookWithMethod}/${N}/${Methods}`, (thisArg: H) => VoidReturn>;
|
|
|
|
type KnownHandlerEvents = {
|
|
[key in keyof KnownHandlers]: MapHandlerEvents<key, KnownHandlers[key]>
|
|
}[keyof KnownHandlers];
|
|
|
|
type HandlerEvents =
|
|
Record<`handler/${HookType}`, (thisArg: Handler) => VoidReturn>
|
|
& Record<`connection/${'create' | 'active' | 'close'}`, (thisArg: any) => VoidReturn>;
|
|
|
|
export type VoidReturn = Promise<any> | any;
|
|
export type HookType = 'before-prepare' | 'before' | 'before-operation' | 'after' | 'finish';
|
|
export type Methods = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
export type HookWithMethod = Exclude<HookType, 'before-operation'>;
|
|
|
|
export interface ServerEvents extends KnownHandlerEvents, HandlerEvents {
|
|
'handler/create': (thisArg: Handler | ConnectionHandler, type: 'ws' | 'http') => VoidReturn;
|
|
'handler/create/http': (thisArg: Handler) => VoidReturn;
|
|
'handler/create/ws': (thisArg: ConnectionHandler) => VoidReturn;
|
|
'handler/init': (thisArg: Handler) => VoidReturn;
|
|
'handler/error': (thisArg: Handler | ConnectionHandler, e: Error) => VoidReturn;
|
|
'handler/api/before': (thisArg: ApiHandler) => VoidReturn;
|
|
'api/before': (args: any) => VoidReturn;
|
|
[k: `handler/${HookType}/${string}`]: (thisArg: any) => VoidReturn;
|
|
[k: `handler/${HookWithMethod}/${string}/${Methods}`]: (thisArg: any) => VoidReturn;
|
|
[k: `handler/register/${string}`]: (HandlerClass: new (...args: any[]) => any) => VoidReturn;
|
|
[k: `handler/error/${string}`]: (thisArg: Handler, e: Error) => VoidReturn;
|
|
[k: `handler/api/before/${string}`]: (thisArg: ApiHandler) => VoidReturn;
|
|
[k: `api/before/${string}`]: (args: any) => VoidReturn;
|
|
}
|
|
|
|
declare module 'cordis' {
|
|
interface Events extends ServerEvents {
|
|
}
|
|
}
|