UNPKG

@teclone/r-server

Version:

A lightweight, extensible web-server with inbuilt routing-engine, static file server, file upload handler, request body parser, middleware support and lots more

101 lines (100 loc) 4.22 kB
/// <reference types="node" /> /// <reference types="node" /> import { ServerResponse as Http1ServerResponse } from 'http'; import { Http2ServerResponse } from 'http2'; import { Logger } from './Logger'; import { FileServer } from './FileServer'; import { ErrorCallback, RouteResponse } from '../@types'; import { ServerRequest } from './Request'; export type ServerResponse<T extends typeof Http2ServerResponse | typeof Http1ServerResponse = typeof Http2ServerResponse | typeof Http1ServerResponse> = Omit<InstanceType<T>, 'end' | 'setHeader' | 'setHeaders' | 'removeHeader' | 'removeHeaders'> & { prototype: ServerResponse<T>; req: ServerRequest; logger: Logger; fileServer: FileServer; errorCallback: ErrorCallback | null; startedAt: Date; endedAt: Date; end(this: ServerResponse<T>, cb?: () => void): Promise<boolean>; end(this: ServerResponse<T>, data?: any, cb?: () => void): Promise<boolean>; /** * ends the response with optional response data, and optional data encoding * @param data optional data to send. either string or buffer * @param encoding data encoding if not buffer */ end(this: ServerResponse<T>, data?: any, encoding?: string | (() => void), cb?: () => void): Promise<boolean>; /** * sets response header * @param name response header name * @param value response header value */ setHeader(this: ServerResponse<T>, name: string, value: string | number | string[]): ServerResponse<T>; /** * sets multiple response headers * @param headers object containing response header name value pairs */ setHeaders(this: ServerResponse<T>, headers: { [p: string]: string | number | string[]; }): ServerResponse<T>; /** * removes a single set response header at a time. function is chainable * @param name response header to remove */ removeHeader(this: ServerResponse<T>, name: string): ServerResponse<T>; /** * remove response headers that are already set. function is chainable * @param names - comma separated list of header names to remove */ removeHeaders(this: ServerResponse<T>, ...names: string[]): ServerResponse<T>; /** * sets response http status code and message if given * @param code - the status code * @param message - the http status message to given sent */ status(this: ServerResponse<T>, code: number, message?: string): ServerResponse<T>; /** * sends json response back to the client. * @param data - the json string or json object which will be stringified */ json(this: ServerResponse<T>, data?: object | string): Promise<boolean>; /** * Redirect client to the given url */ redirect(this: ServerResponse<T>, path: string, status?: number): Promise<boolean>; /** * sends a file download attachment to the client * @param filePath - relative or absolute file path * @param filename - suggested file download name */ download(this: ServerResponse<T>, filePath: string, filename?: string): Promise<boolean>; /** * sends json error data back to the client */ jsonError<Data, Errors>(this: ServerResponse<T>, response?: RouteResponse<Data, Errors>): Promise<boolean>; /** * sends json success data back to the client */ jsonSuccess<Data, Errors>(this: ServerResponse<T>, response?: RouteResponse<Data, Errors>): Promise<boolean>; /** * waits for the given time */ wait(this: ServerResponse<T>, time: number): Promise<ServerResponse<T>>; /** * it process a json api response, automatically handling * the then and catch responses, * @param response * @returns */ processRouteResponse<Data, Errors>(this: ServerResponse<T>, /** * the response promise */ responsePromise: Promise<RouteResponse<Data, Errors>>): Promise<boolean>; }; declare class HTTP1BaseResponse extends Http1ServerResponse { constructor(req: any); } export declare const Http1Response: typeof HTTP1BaseResponse; declare class HTTP2BaseResponse extends Http2ServerResponse { } export declare const Http2Response: typeof HTTP2BaseResponse; export {};