rexuws
Version:
An express-like framework built on top of uWebsocket.js aims at simple codebase and high performance
107 lines (106 loc) • 3.27 kB
TypeScript
/// <reference types="node" />
import { HttpRequest } from 'uWebSockets.js';
import { IRequest, HttpMethod, TResponseExposedMethods } from './utils/types';
import { GET_HEADER, GET_METHOD, GET_PARAMS, GET_QUERY, GET_URL, FOR_EACH, FROM_RES } from './utils/symbol';
/**
* Map parameter's name to its index in url
*/
export declare type ParametersMap = string[];
/**
* Specify Request options such as: parameters map, cache req default value, etc,...
*/
export interface IRequestOptions {
/**
* Map parameter to specific id to get the exact value by uWS method req.getParams(id: number)
*/
paramsMap?: ParametersMap;
/**
* Retrieve req data from uWS `req` and cache these values
*
* For POST/PUT/DELETE method which needs to parse body from uWS `res`, those `req` values must be retrieve before the
* `res.OnData()` method in order not to violates memory allocation policies of uWS
*
* Set forceInit to `true` to cache all the values coming from the req's methods execution's result
*
* @default false
*/
forceInit?: {
/**
* Cache headers value by req.forEach()
*/
headers?: boolean;
/**
* Cache query value
*/
query?: boolean;
/**
* Cache params value
*/
params?: boolean;
/**
* Cache method value
*/
method?: boolean;
/**
* Cache url value
*/
url?: boolean;
} | true;
/**
* Enable dedicated cookie parser method
*/
cookieParser?: boolean;
/**
* The prefixed url comming from application set up
*/
baseUrl?: string;
}
export default class Request implements IRequest {
[key: string]: unknown;
/**
* uWS Original req object
*/
originalReq: HttpRequest;
[FROM_RES]: TResponseExposedMethods;
private [GET_HEADER];
private [GET_PARAMS];
private [GET_URL];
private [GET_METHOD];
private [GET_QUERY];
private [FOR_EACH];
private parametersMap?;
accepted: string[];
private _method?;
private _url?;
private _params?;
private _query?;
private _headers;
private _parsedBody?;
raw?: Buffer;
private _hasCalledGetHeader;
private _cookies?;
private _hasCookieParser;
private _baseUrl?;
private _originalUrl?;
constructor(req: HttpRequest, opts?: IRequestOptions);
get body(): any;
set body(data: any);
get headers(): Record<string, string>;
get method(): HttpMethod;
get query(): Record<string, string>;
get url(): string;
get params(): Record<string, string>;
get ip(): string;
get ips(): string[];
get hostname(): string | undefined;
get cookies(): any;
get baseUrl(): string | undefined;
get originalUrl(): string;
accepts(...type: string[]): string | string[] | false;
acceptsCharsets(...charset: string[]): string[] | string | false;
acceptsEncodings(...encoding: string[]): string[] | string | false;
acceptsLanguages(...charset: string[]): string[] | string | false;
get(name: string): string | undefined;
header: (name: string) => string | undefined;
is(type: string | string[]): string | false | null;
}