http-micro
Version:
Micro-framework on top of node's http module
45 lines (44 loc) • 2.27 kB
TypeScript
/// <reference types="node" />
import * as http from "http";
import * as https from "https";
import * as utils from "./utils";
import { Context } from "./context";
import { Router } from "./router";
import { MicroServer } from "./server-utils";
export interface ItemsContainer {
items: Map<string, any>;
getItem<T = any>(key: string): T;
setItem<T = any>(key: string, value: T): void;
hasItem(key: string): boolean;
}
export interface IApplication extends ItemsContainer {
middlewares: Middleware<any>[];
createServer(secureOpts?: https.ServerOptions): MicroServer;
createHandler(): (req: http.IncomingMessage, res: http.ServerResponse) => void;
use(...middlewares: (Middleware<any> | Router<any>)[]): IApplication;
setErrorHandler(handler: ErrorHandler<any>): void;
setFallbackHandler(handler: Middleware<any>): void;
}
export declare type Middleware<T extends Context = Context> = (context: T, next: NextMiddleware) => MiddlewareResult;
export declare type MiddlewareResult = Promise<void>;
export declare type NextMiddleware = () => MiddlewareResult;
export declare type ErrorHandler<T extends Context = Context> = (err: Error, req: http.IncomingMessage, res: http.ServerResponse, context?: T) => void;
export declare class Application<T extends Context = Context> implements IApplication {
private _contextFactory;
private _errorHandler;
private _fallbackHandler;
middlewares: Middleware<T>[];
items: Map<string, any>;
constructor(_contextFactory: (app: Application<T>, req: http.IncomingMessage, res: http.ServerResponse) => T, _errorHandler?: ErrorHandler<T>, _fallbackHandler?: typeof utils.defaultFallbackNotFoundHandler);
createServer(secureOpts?: https.ServerOptions): MicroServer;
createHandler(): (req: http.IncomingMessage, res: http.ServerResponse) => void;
use(...middleware: (Middleware<T> | Router<T>)[]): Application<T>;
setErrorHandler(handler: ErrorHandler<T>): void;
setFallbackHandler(handler: Middleware<T>): void;
getItem<T = any>(key: string): T;
setItem<T = any>(key: string, value: T): void;
hasItem(key: string): boolean;
}
export declare class App extends Application<Context> {
constructor();
}