web-signature
Version:
Primitive and fast framework for rendering web interfaces
250 lines (242 loc) • 8.09 kB
TypeScript
type TypesMap$1 = {
string: string;
number: number;
boolean: boolean;
array: any[];
null: null;
};
interface Prop$1 {
/**
* The type of the property.
*/
type: keyof TypesMap$1;
/**
* Checks the validity of the value and the specified type
* @param {string | number | boolean | null} value
* @return boolean
*/
isValid: (value: TypesMap$1[keyof TypesMap$1]) => boolean;
/**
* Indicates whether the property is required.
*/
required?: boolean;
/**
* A function to validate the value of the property.
* @param {string | number | boolean | null} value
* @return boolean
*/
validate?: (value: TypesMap$1[keyof TypesMap$1]) => boolean;
}
type Options = {
/**
* Specifies whether the component must have a ref.
*/
generateRefIfNotSpecified?: boolean;
};
type Ref = {
/**
* Unique identifier for the component.
*/
id: string;
/**
* Function to contact the component with props.
* @param {...any[]} props - The properties to send to the component.
* @returns {any}
*/
contact: (...props: any[]) => any;
/**
* Function to update the component.
*/
update: () => void;
};
interface Component$1 {
name: string;
options: Options;
/**
* Optional reference to the component.
* @type {Ref}
*/
ref?: Ref;
/**
* Optional content that is specified in the component tag.
*/
content?: string;
/**
* Defining component properties.
*/
props: Record<string, Prop$1>;
/**
* The properties of the component.
*/
data: Record<string, string | number | boolean | null>;
/**
* Returns the component as a string.
* @returns {string} The rendered component as a string.
*/
render(): string | Promise<string>;
/**
* Lifecycle hook that is called when the component is initialized.
*/
onInit?(): void;
/**
* Lifecycle hook that is called when the component is rendered.
*/
onRender?(): void;
/**
* Lifecycle hook that is called when the component is mounted to the DOM.
* @param {Element} el The element to which the component is mounted.
*/
onMount?(el: Element): void;
/**
* Lifecycle hook called via Signature.contactWith
* @param {...any[]} props
*/
onContact?(...props: any[]): any;
/**
* Lifecycle hook that is called when the component's props are parsed.
*/
onPropsParsed?(): void;
/**
* Lifecycle hook that is called when a prop is parsed.
* @param {Prop} prop The prop that was parsed.
* @param {string | number | boolean | null} value The value of the prop.
*/
onPropParsed?(prop: Prop$1, value: string | number | boolean | null): void;
}
declare abstract class Component implements Component$1 {
abstract readonly name: string;
content?: string;
options: Options;
ref?: Ref;
readonly props: Record<string, Prop$1>;
readonly data: Record<string, string | number | boolean | null>;
abstract render(): string | Promise<string>;
onInit?(): void;
onRender?(): void;
onMount?(el: Element): void;
onContact?(...props: any[]): any;
onPropsParsed?(): void;
onPropParsed?(prop: Prop$1, value: string | number | boolean | null): void;
}
type ComponentConstructor = new () => Component;
type LibMeta = {
name: string;
version?: string;
author?: string;
components: string[];
dependencies: Record<string, LibMeta>;
};
declare class Library {
readonly name: string;
readonly version?: string;
readonly author?: string;
libs: Record<string, LibMeta>;
private components;
/**
* @param {string} name The name of the library.
* @param {string} [author] Optional author of the library.
* @param {string} [version] Optional version of the library.
*/
constructor(name: string, author?: string, version?: string);
/**
* Registers a component in the library.
* @param {ComponentConstructor} component The component to register.
* @param {string} [name] Optional name for the component. If not provided, uses the component's name property.
*/
add(component: ComponentConstructor, name?: string): void;
/**
* Registers a library.
* @import {Library} from "./Library.js";
* @param {Library} library The library to register.
* @param {string[]} exclude Optional array of component names to exclude from the library registration.
*/
register(library: Library, ...exclude: string[]): void;
/**
* Retrieves a component by its name.
* @param {string} name The name of the component to retrieve.
* @return {ComponentConstructor | undefined} The component associated with the name, or undefined if it does not exist.
*/
get(name: string): ComponentConstructor | undefined;
/**
* Returns a library.
* @param {string} name The name of the library.
* @return {LibMeta}
*/
lib(name: string): LibMeta | undefined;
/**
* Lists all registered components in the library.
* @return {Array<{ component: ComponentConstructor, name: string }>} An array of objects containing component constructors and their names.
*/
list(): Array<{
component: ComponentConstructor;
name: string;
}>;
}
type ResolvedLib = {
components: string[];
dependencies: Record<string, ResolvedLib>;
};
declare class Signature {
private components;
private refs;
private libs;
constructor();
/**
* Adds a component to the signature.
* @param {ComponentConstructor} component The component to add.
* @param {string} name Optional name for the component. If not provided, uses the component's name property.
*/
add(component: ComponentConstructor, name?: string): void;
/**
* Registers a library in the signature.
* @import {Library} from "./Library.js";
* @param {Library} library The library to register.
* @param {string[]} exclude Optional array of component names to exclude from the library registration.
*/
register(library: Library, ...exclude: string[]): void;
/**
* Returns a library.
* @param {string} name The name of the library.
* @return {LibMeta}
*/
lib(name: string): LibMeta | undefined;
/**
* Returns a formatted object of all libraries in the signature.
* @return {Record<string, ResolvedLib>} A object of formatted libraries with their components and dependencies.
*/
libraries(): Record<string, ResolvedLib>;
/**
* Contacts the Component.onContact method through its reference.
* @param {string} name The name of the reference.
* @param {...any[]} props The properties to pass to the component's onContact method.
*/
contactWith(name: string, ...props: any[]): any;
/**
* Updates the reference.
* @param {string} name The name of the reference to update.
*/
updateRef(name: string): void;
/**
* Starts rendering in the specified area.
* @param {string} selector The selector of the element where the signature should be rendered.
* @param {() => void} [callback] Optional callback that will be called after rendering is complete.
*/
contact(selector: string, callback?: () => void): void;
private render;
}
type TypesMap = {
string: string;
number: number;
boolean: boolean;
array: any[];
null: null;
};
declare class Prop<T extends keyof TypesMap> implements Prop$1 {
readonly type: T;
readonly required: boolean;
readonly validate: (value: string | number | boolean | any[] | null) => boolean;
constructor(type: T, required?: boolean, validate?: (value: string | number | boolean | any[] | null) => boolean);
isValid(value: TypesMap[keyof TypesMap]): boolean;
}
declare function export_default(): Signature;
export { Component, Library, Prop, Signature, export_default as default };