UNPKG

rjweb-server

Version:

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

104 lines (103 loc) 3.65 kB
import URLObject from "../URLObject"; import ValueCollection, { BaseCollection } from "../ValueCollection"; import { Content } from "../../types/global"; import Cookie from "../Cookie"; import RequestContext from "../../types/internal/classes/RequestContext"; import GlobalContext from "../../types/internal/classes/GlobalContext"; import { network } from "@rjweb/utils"; export default class Base<Context extends Record<any, any> = {}> { /** * The Request Context Object used by the server * @since 9.0.0 */ context: RequestContext; /** * The Global Context Object used by the server * @since 9.0.0 */ global: GlobalContext; /** * Initializes a new Instance of a Web Context * @since 7.0.0 */ constructor(context: RequestContext); /** * 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 Path Parameters * @example * ``` * console.log(ctr.params.get('server')) // Will print undefined if not present * ``` * @since 2.0.0 */ readonly params: BaseCollection<string, string>; /** * A Collection of all Client Cookies * @example * ``` * import { Cookie } from "rjweb-server" * * 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 * * ctr.cookies.set('session', new Cookie(Math.random(), { * path: '/' * })) * ``` * @since 2.0.0 */ get cookies(): ValueCollection<string, string, Cookie>; /** * 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 */ get 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 */ get 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 (and proxy is enabled) will be the proper IP * @since 3.0.0 */ readonly ip: network.IPAddress; }; /** * The Requested URL * @since 0.0.2 */ readonly url: URLObject; /** * Context Variables that are available everywhere in the requests lifespan * @since 1.2.1 */ '@': Context; }