UNPKG

rjweb-server

Version:

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

186 lines (185 loc) 5.72 kB
import HTTPRequest from "./web/HttpRequest"; import HTMLTag from "../types/htmlTag"; import { RealAny } from "../types/internal"; import HTMLComponent from "./HTMLComponent"; export type HTMLContent = string | number | boolean | undefined; export type HTMLAttribute = string | number | boolean | undefined | Function | HTMLAttribute[] | HTMLAttributeRecord; export type FnArgument = { name: string; value: any; }; export type GetEvery = { id: string; getter(ctr: HTTPRequest): RealAny; callback(tag: HTMLBuilder, data: any): HTMLBuilder; fnArguments: FnArgument[]; }; interface HTMLAttributeRecord extends Record<string, HTMLAttribute> { } export declare const getFunctionArguments: (fn: Function) => string[]; export declare const parseAttributes: (attributes: Record<string, HTMLAttribute>, fnArguments: FnArgument[]) => string; export default class HTMLBuilder { protected html: string; protected fnArguments: FnArgument[]; protected getEveries: GetEvery[]; private route; private everyId; constructor(route: string, fnArguments?: FnArgument[], getEvery?: GetEvery[], everyId?: { n: number; }); /** * Add a new HTML Tag * @example * ``` * ctr.printHTML((html) => html * .t('div', {}, (t) => t * .t('p', {}, (t) => t * .raw('hello world!') * ) * .t('p', {}, 'hello world!') * ) * ) * ``` * @since 6.6.0 */ t( /** The HTML Tag name */ tag: HTMLTag, /** The HTML Attributes to add */ attributes: Record<string, HTMLAttribute>, /** The Callback or Content to escape */ callback: ((tag: HTMLBuilder) => HTMLBuilder) | HTMLContent | HTMLComponent): this; /** * Add a new HTML Component * @example * ``` * const component = new HTMLComponent((html, options) => html * .t('p', {}, options?.hello) * ) * * ctr.printHTML((html) => html * .t('div', {}, (t) => t * .t('p', {}, (t) => t * .raw('hello world!') * ) * .c(component, { hello: 'Hello world!' }) * ) * ) * ``` * @since 6.7.0 */ c<Component extends HTMLComponent>( /** The Component Object */ component: Component, /** The Options to add */ options?: Component extends HTMLComponent<infer Options> ? Options : never): this; /** * Add raw Content * @example * ``` * ctr.printHTML((html) => html * .t('div', {}, (t) => t * .t('p', {}, (t) => t * .raw('hello world!') * ) * ) * ) * ``` * @since 6.6.0 */ raw( /** The Raw Content to add */ content: HTMLContent): this; /** * Add escaped Content * @example * ``` * ctr.printHTML((html) => html * .t('div', {}, (t) => t * .t('p', {}, (t) => t * .escaped('hello world!') * ) * ) * ) * ``` * @since 6.6.2 */ escaped( /** The Raw Content to add */ content: HTMLContent): this; /** * Register Variables that will be added to every context * @warning This is required for native functions because they are being replicated 1:1 * @example * ``` * const version = '1.0.0' * * ctr.printHTML((html) => html * .var({ version }) * .t('button', { onclick() { alert(version) } }, (t) => t * .raw('click me for version!') * ) * ) * ``` * @since 6.6.0 */ var( /** The Variables as Object */ variables: Record<string, any>): this; /** * Register Variables that will be added to every context * @example * ``` * const version = '1.0.0' * const showVersion = true * * ctr.printHTML((html) => html * .var({ version }) * .if(showVersion, (html) => html * .t('button', { onclick: () => { alert(version) } }, (t) => t * .raw('click me for version!') * ) * ) * ) * ``` * @since 6.6.1 */ if( /** The Boolean to check */ state: boolean, /** The Callback for when the Boolean is truthy */ callback: ((tag: HTMLBuilder) => HTMLBuilder) | HTMLContent | HTMLComponent): this; /** * Fetch a custom function every x ms * @example * ``` * const getNumbers = () => Array.from({ length: 5 }).map(() => Math.random()) * * ctr.printHTML((html) => html * .getEvery(getNumbers, 1000, (t, numbers) => t * .forEach(numbers, (t, number) => t * .if(number > 0.5, (t) => t * .t('h1', {}, 'Number is bigger than 0.5!') * ) * .t('p', {}, (t) => t * .raw(`Crazy Number: ${number}`) * ) * ) * ) * ) * ``` * @since 6.6.0 */ getEvery<T extends (ctr: HTTPRequest) => RealAny>( /** The Function to get Data */ getter: T, /** The Interval for Iterations (ms) */ interval: number, /** The Callback for each Iteration */ callback: (tag: HTMLBuilder, item: Awaited<ReturnType<T>>) => HTMLBuilder): this; /** * Loop over an Array of Items * @example * ``` * const items = [ * 'Ben', * 'John', * 'Pork' * ] * * ctr.printHTML((html) => html * .t('div', {}, (t) => t * .forEach(items, (t, name) => t * .t('p', {}, (t) => t * .raw(`I love ${name}`) * ) * ) * ) * ) * ``` * @since 6.6.0 */ forEach<T extends Array<any>>( /** The Items to loop over */ items: T, /** The Callback for each Item */ callback: (tag: HTMLBuilder, item: T[number]) => HTMLBuilder): this; } export {};