@powership/server
Version:
106 lines (105 loc) • 3.92 kB
TypeScript
import * as HTTP from 'http';
import type { AddressInfo } from 'net';
import { Hope } from '@powership/utils';
import { Compute } from '@powership/utils';
import { RequestBody } from './BaseRequestHandler';
import { ServerRequest } from './ServerRequest';
import { ServerResponse } from './ServerResponse';
export type ServerDefinition = {
handlers: Handler<any>[];
};
export declare class Server {
defaultHandlers: Handler<undefined>[];
constructor(definition: ServerDefinition);
static create: (definition: ServerDefinition) => Server;
hooks: {
willStartServer: import("plugin-hooks").AsyncPlugin<HTTP.Server<typeof HTTP.IncomingMessage, typeof HTTP.ServerResponse>, {
app: Server;
}>;
started: import("plugin-hooks").AsyncPlugin<HTTP.Server<typeof HTTP.IncomingMessage, typeof HTTP.ServerResponse>, {
app: Server;
}>;
onParseBody: import("plugin-hooks").AsyncPlugin<RequestBody, {
req: ServerRequest;
res: ServerResponse;
app: Server;
close: CloseResponseFunction;
}>;
onRequest: import("plugin-hooks").AsyncPlugin<ServerRequest, {
response: ServerResponse;
app: Server;
close: CloseResponseFunction;
}>;
onResponse: import("plugin-hooks").AsyncPlugin<ServerResponse, {
request: ServerRequest;
app: Server;
close: CloseResponseFunction;
}>;
onError: import("plugin-hooks").AsyncPlugin<ServerResponse, {
request: ServerRequest;
error: Error;
close: CloseResponseFunction;
}>;
};
private server?;
closeServer: () => Promise<"NOT_STARTED" | "CLOSED">;
usedDefinition?: ServerDefinition;
hasStarted: boolean;
readonly definitionInput: ServerDefinition | ((app: Server) => ServerDefinition);
readonly handlers: Handler[];
handleRequest: (request: ServerRequest) => Hope<ServerResponse>;
withServer: ServerServerInfo | false;
start(port: number): Promise<ServerStartResult>;
start(port?: undefined): Promise<Server & {
withServer: false;
}>;
private _startHandlers;
}
export type Handler<Data extends Record<string, any> | undefined = {} | undefined> = {
name: string;
hooks: ServerHooksRecord;
data: Data;
};
export type ServerHooksRecord = {
[K in keyof ServerHooks]?: Parameters<ServerHooks[K]>[0] extends infer Register ? Register : never;
};
export type ServerHooks = ReturnType<typeof createHooks>;
declare function createHooks(): {
willStartServer: import("plugin-hooks").AsyncPlugin<HTTP.Server<typeof HTTP.IncomingMessage, typeof HTTP.ServerResponse>, {
app: Server;
}>;
started: import("plugin-hooks").AsyncPlugin<HTTP.Server<typeof HTTP.IncomingMessage, typeof HTTP.ServerResponse>, {
app: Server;
}>;
onParseBody: import("plugin-hooks").AsyncPlugin<RequestBody, {
req: ServerRequest;
res: ServerResponse;
app: Server;
close: CloseResponseFunction;
}>;
onRequest: import("plugin-hooks").AsyncPlugin<ServerRequest, {
response: ServerResponse;
app: Server;
close: CloseResponseFunction;
}>;
onResponse: import("plugin-hooks").AsyncPlugin<ServerResponse, {
request: ServerRequest;
app: Server;
close: CloseResponseFunction;
}>;
onError: import("plugin-hooks").AsyncPlugin<ServerResponse, {
request: ServerRequest;
error: Error;
close: CloseResponseFunction;
}>;
};
export type ServerServerInfo = Compute<{
server: HTTP.Server;
} & AddressInfo>;
export type ServerStartResult = Server & ServerServerInfo & {
withServer: ServerServerInfo;
};
export interface CloseResponseFunction {
(response?: ServerResponse | ServerResponse['statusCode']): void;
}
export {};