hmpl-js
Version:
🐜 Server-oriented customizable templating for JavaScript
214 lines (213 loc) • 7.62 kB
TypeScript
/**
* get function in options object
*/
type HMPLRequestGet = (prop: string, value: any, context: HMPLInstanceContext, request?: HMPLRequest) => void;
/**
* headers object in options object
*/
interface HMPLHeadersInit {
[key: string]: string;
}
/**
* A set of parameters that apply to fetch. Based almost entirely on [RequestInit](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit).
*/
interface HMPLRequestInit {
mode?: RequestMode;
cache?: RequestCache;
redirect?: RequestRedirect;
referrerPolicy?: ReferrerPolicy;
integrity?: string;
referrer?: string;
get?: HMPLRequestGet;
body?: BodyInit | null;
signal?: AbortSignal | null;
window?: any;
credentials?: RequestCredentials;
headers?: HMPLHeadersInit;
timeout?: number;
}
/**
* The context of the current request sent to the HMPLInstance.
*/
interface HMPLRequestContext {
event?: Event;
clearInterval?: HMPLClearInterval;
}
/**
* The HMPLInstance context contains information about requests sent to the server.
*/
interface HMPLInstanceContext {
request: HMPLRequestContext;
}
/**
* HMPLRequestInit generation function. Needed to work with context.
*/
type HMPLRequestInitFunction = (context: HMPLInstanceContext) => HMPLRequestInit;
/**
* An object containing information about a request and optionally related DOM elements or metadata.
*/
interface HMPLRequestsObject extends HMPLRequestInfo {
arrId?: number;
el?: Comment;
nodeId?: number;
}
/**
* Statuses based on the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) state, as well as those based on HTTP codes without success.
*/
type HMPLInitalStatus = "pending" | "rejected" | 100 | 101 | 102 | 103 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
/**
* Sets which trigger the indicator will be shown on
*/
type HMPLIndicatorTrigger = HMPLInitalStatus | "error";
/**
* Interface for indicator object.
*/
interface HMPLIndicator {
trigger: HMPLIndicatorTrigger;
content: string;
}
/**
* Represents the allowed content types for a request or response.
* Can be either an array of strings specifying content type substrings (e.g., ["text/html", "application/json"])
* or a wildcard "*" indicating that all content types are allowed.
*/
type HMPLContentTypes = string[] | "*";
/**
* Valid tags to remove from response.
*/
type HMPLDisallowedTag = "script" | "style" | "iframe";
/**
* Tags to remove from response.
*/
type HMPLDisallowedTags = HMPLDisallowedTag[];
/**
* Enabling sanitize method from DOMPurify.
*/
type HMPLSanitize = boolean;
/**
* An object that defines the properties of a request.
*/
interface HMPLRequestInfo {
src: string;
method?: string;
initId?: string | number;
after?: string;
repeat?: boolean;
memo?: boolean;
interval?: number;
allowedContentTypes?: HMPLContentTypes;
indicators?: HMPLIndicator[];
sanitize?: HMPLSanitize;
disallowedTags?: HMPLDisallowedTags;
autoBody?: boolean | HMPLAutoBodyOptions;
}
/**
* List of options for the autoBody property.
*/
interface HMPLAutoBodyOptions {
formData?: boolean;
}
/**
* Sets options for the compile function.
*/
interface HMPLCompileOptions {
memo?: boolean;
autoBody?: boolean | HMPLAutoBodyOptions;
allowedContentTypes?: HMPLContentTypes;
sanitize?: HMPLSanitize;
disallowedTags?: HMPLDisallowedTags;
}
/**
* A dictionary of parsed indicator templates.
*/
interface HMPLParsedIndicators {
[key: string]: HTMLTemplateElement;
}
/**
* Represents a compiled template, containing associated requests.
*/
interface HMPLTemplate {
requests: HMPLRequestsObject[];
}
/**
* Initializes a reference to a specific [HMPLRequestInit](https://hmpl-lang.github.io/types.html#hmplrequestinit) dictionary using id.
*/
interface HMPLIdentificationRequestInit {
value: HMPLRequestInit | HMPLRequestInitFunction;
id: string | number;
}
/**
* Represents a node and its associated data in the DOM.
*/
interface HMPLNodeObj {
id: number;
nodes: null | ChildNode[];
parentNode: null | ParentNode;
comment: Comment;
memo?: {
response: null | string;
isPending?: boolean;
nodes?: ChildNode[];
};
interval?: {
value: NodeJS.Timeout;
clearInterval: HMPLClearInterval;
};
}
/**
* Type for the full list of http codes, as well as for [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) states without fulfilled. Used in the [HMPLRequest](https://hmpl-lang.github.io/types.html#hmplrequest) object to indicate the status of the request.
*/
type HMPLRequestStatus = HMPLInitalStatus | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226;
/**
* The returned object for each request, if there are several in the template.
*/
interface HMPLRequest {
response: undefined | Element | null | ChildNode[];
status?: HMPLRequestStatus;
}
/**
* An object containing information about the response from the server to the request.
*/
interface HMPLInstance {
response: undefined | Element | null;
status?: HMPLRequestStatus;
requests?: HMPLRequest[];
}
/**
* Represents an element and its association with instance-specific data.
*/
interface HMPLElement {
el: Element;
id: number;
objNode?: HMPLNodeObj;
}
/**
* Contains data storage objects related to requests and nodes.
*/
interface HMPLData {
dataObjects: HMPLNodeObj[];
els: HMPLElement[];
currentId: number;
}
/**
* Type for interval clearing function.
*/
type HMPLClearInterval = () => void;
/**
* Type for the function that processes a request.
*/
type HMPLRequestFunction = (el: Element, options: HMPLRequestInitFunction | HMPLRequestInit | HMPLIdentificationRequestInit[], templateObject: HMPLInstance, data: HMPLData, mainEl?: Element, isArray?: boolean, reqObject?: HMPLRequest, isRequests?: boolean, currentHMPLElement?: HMPLElement, event?: Event, currentInterval?: NodeJS.Timeout | null) => void;
/**
* Type for a rendering function, which takes a request processor and produces a rendering operation.
*/
type HMPLRenderFunction = (requestFunction: HMPLRequestFunction) => (options?: HMPLRequestInitFunction | HMPLRequestInit | HMPLIdentificationRequestInit[]) => HMPLInstance;
/**
* Creates a template function.
*/
type HMPLCompile = (template: string, // Template string that defines how data will be rendered into HTML structure.
options?: HMPLCompileOptions) => HMPLTemplateFunction;
/**
* The function returned in response to the compile function. Creates template instances.
*/
type HMPLTemplateFunction = (options?: HMPLIdentificationRequestInit[] | HMPLRequestInit | HMPLRequestInitFunction) => HMPLInstance;
export { HMPLRequestInit, HMPLRequestInitFunction, HMPLInstanceContext, HMPLRequestsObject, HMPLRequestContext, HMPLInitalStatus, HMPLIndicatorTrigger, HMPLIndicator, HMPLContentTypes, HMPLRequestInfo, HMPLCompileOptions, HMPLParsedIndicators, HMPLTemplate, HMPLIdentificationRequestInit, HMPLNodeObj, HMPLRequestStatus, HMPLRequest, HMPLInstance, HMPLElement, HMPLData, HMPLRequestFunction, HMPLRenderFunction, HMPLCompile, HMPLTemplateFunction, HMPLAutoBodyOptions, HMPLDisallowedTag, HMPLDisallowedTags, HMPLSanitize, HMPLClearInterval, HMPLRequestGet, HMPLHeadersInit };