http-micro
Version:
Micro-framework on top of node's http module
61 lines (60 loc) • 3.48 kB
TypeScript
/// <reference types="node" />
/// <reference types="content-type" />
import * as http from "http";
import * as contentType from "content-type";
export declare class RequestUtils {
static getUpstreamIpAddresses(req: http.IncomingMessage): string[];
static getHost(req: http.IncomingMessage): string;
static isEncrypted(req: http.IncomingMessage): boolean;
static getProtocol(req: http.IncomingMessage): string;
static getContentType(req: http.IncomingMessage): contentType.MediaType;
}
export declare class ResponseUtils {
static send(res: http.ServerResponse, body: any, headers?: any, code?: number): void;
static sendText(res: http.ServerResponse, text: string): void;
static sendAsJson(res: http.ServerResponse, data: any, replacer?: (key: string, value: any) => any, spaces?: string | number): void;
static setHeaders(res: http.ServerResponse, headers: any): void;
static setHeader(res: http.ServerResponse, key: string, value: string, replace?: boolean): boolean;
/**
* Appends a value item to a mutli-value header key. It separates
* values with "," if there's an string value, or a appends to the
* array if there's an existing array. If none exists, creates an
* array with the item.
*
* @param key {String} The header key
* @param value {String} The header value
* @param forceAppend {Boolean} If true, the value will be appended
* regardless of whether the value is already present or not.
* Helpful performance optmization if it's known for certain
* that a value will not exist, as it avoids a regex call.
*/
static appendHeaderValue(res: http.ServerResponse, key: string, value: string, forceAppend?: boolean): void;
/**
* Removes a value from a multi-value header item. Multi-values
* header can either be a string separated by ", ", or an array.
*
* Note: The value provided should one be a single string, and
* not an array.
*
* @param key {String} Key of the header.
* @param value {String} The string value to be removed from the
* header item.
* @param removeHeaderIfEmpty {Boolean} If true, removes the entire
* header, if the header is empty after removal of the item.
*/
static removeHeaderValue(res: http.ServerResponse, key: string, value: string, removeHeaderIfEmpty?: boolean): void;
static setContentType(res: http.ServerResponse, value: string, force?: boolean): void;
static setStatus(res: http.ServerResponse, code: number, message?: string): void;
static sendStatus(res: http.ServerResponse, code: number, message?: string, headers?: any): void;
static sendMethodNotAllowed(res: http.ServerResponse, allowedMethods: string[] | string, reason?: string, headers?: any): void;
static setContentDisposition(res: http.ServerResponse, filename: string, type?: DispositionKind | string): void;
static sendNoContent(res: http.ServerResponse, headers?: any): void;
static sendResetContent(res: http.ServerResponse, headers?: any): void;
static sendBadRequest(res: http.ServerResponse, body: any, headers?: any): void;
static sendNotFound(res: http.ServerResponse, reason?: string, headers?: any): void;
static sendForbidden(res: http.ServerResponse, reason: any, headers?: any): void;
}
export declare enum DispositionKind {
Attachment = "attachment",
Inline = "inline",
}