UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

119 lines (118 loc) 4.08 kB
import { GlobalContext, LocalContext } from "../../types/context"; import URLObject from "../URLObject"; import ValueCollection, { BaseCollection } from "../valueCollection"; import Server from "../server"; import { CookieSettings, ExtractParameters } from "../../types/internal"; import { Content } from "../../functions/parseContent"; export default class Base<Context extends Record<any, any> = {}, Path extends string = '/'> { /** * The Request Context Object used by the server * @since 8.1.4 */ ctx: LocalContext; /** * The Global Context Object used by the server * @since 8.1.4 */ ctg: GlobalContext; /** * Initializes a new Instance of a Web Context * @since 7.0.0 */ constructor(controller: Server<any, any>, localContext: LocalContext); /** * The Server Controller Class Instance * @example * ``` * ctr.controller.reload() * ``` * @since 3.0.0 */ controller: Omit<Server<any, any>, 'globalContext' | 'server' | 'socket'>; /** * A Collection of all Headers * @example * ``` * if (ctr.headers.has('Authorization')) console.log('Authorization Header is present') * * console.log(ctr.headers.get('Authorization')) // Will print undefined if not present * console.log(ctr.headers.get('Authorization', 'hello')) // Will print 'hello' if not present * ``` * @since 2.0.0 */ readonly headers: ValueCollection<string, string, Content>; /** * A Collection of all Client Cookies * @example * ``` * if (ctr.cookies.has('theme')) console.log('Theme Cookie is present') * * console.log(ctr.cookies.get('theme')) // Will print undefined if not present * console.log(ctr.cookies.get('theme', 'light')) // Will print 'light' if not present * ``` * @since 2.0.0 */ readonly cookies: ValueCollection<string, string, CookieSettings>; /** * A Collection of all Path Parameters * @example * ``` * console.log(ctr.params.get('server')) // Will print undefined if not present * ``` * @since 2.0.0 */ readonly params: BaseCollection<ExtractParameters<Path>, string>; /** * A Collection of all URL Queries * @example * ``` * if (ctr.queries.has('user')) console.log('User Query is present') * * console.log(ctr.queries.get('user')) // Will print undefined if not present * console.log(ctr.queries.get('user', 'default')) // Will print 'default' if not present * ``` * @since 2.0.0 */ readonly queries: BaseCollection<string, string>; /** * A Collection of all URL Fragments * @example * ``` * if (ctr.fragments.has('user')) console.log('User Fragment is present') * * console.log(ctr.fragments.get('user')) // Will print undefined if not present * console.log(ctr.fragments.get('user', 'default')) // Will print 'default' if not present * ``` * @since 7.0.0 */ readonly fragments: BaseCollection<string, string>; /** Client Infos */ readonly client: { /** * The User Agent of the Client * @since 3.0.0 */ readonly userAgent: string; /** * The Port that the Client is using * @since 3.0.0 */ readonly port: number; /** * The IP Address that the Client is using * * When a valid Proxy Request is made will be the proper IP * @since 3.0.0 */ readonly ip: string; }; /** * The Requested URL * @since 0.0.2 */ readonly url: URLObject; /** * The Domain this Request was made on * @since 5.9.6 */ readonly domain: string; /** * Set a Custom Variable * @example * ``` * ctr.setCustom('hello', 123) * * ctr["@"].hello // 123 * ``` * @since 1.2.1 */ setCustom<T extends keyof Context>(name: T, value: Context[T]): this; /** * Context Variables that are available everywhere in the requests lifespan * @since 1.2.1 */ '@': Context; }