UNPKG

@sunney/flareutils

Version:

Small Utilities and little goodies that make developing with Cloudflare easier and faster.

49 lines (48 loc) 2.29 kB
/// <reference types="@cloudflare/workers-types" /> import type { AddOptions, TransformationOptionals } from "./types"; /** * Simple Workers-native templating engine. Utilizes the [*HTMLRewriter*](http://developers.cloudflare.com/workers/runtime-apis/html-rewriter) API, allowing templating operations to be streamed to the user, rather than being a blocking process. While it is not required, it is recommended that any resulting HTML be cached before being returned. * @template Environment The Environment object, provided by the Workers Runtime. * @template Optional Optional parameters, provided to your replacer function. */ export declare class Temra<Environment = unknown, Optionals = unknown> { /** * Default Prefix used for tag names. */ private readonly tagPrefix; /** * Whether comments should be removed. */ private readonly deleteComments; /** * Replacers applied to your HTML. */ private replacers; /** * Initializes a new Temra instance. * @param {string} prefix Default Prefix used for tag names. For example, if the prefix is "Temra", then the tag name "TemraName" will be read as "Name". * @param {boolean} deleteComments Whether comments should be removed. */ constructor(prefix?: string, deleteComments?: boolean); /** * Add a replacer to the Temra instance. * @param {string} selector Selector used to find elements. * @param {AddOptions} options Options used to configure the replacer. * @returns {Temra} Returns the Temra instance, for chaining. * @example ```ts * temra.add("username", () => "Jay Doe", {removeSelector: true}); * ``` */ add(selector: string, options: AddOptions<Environment, Optionals>): this; /** * Applies the currently added replacer functions to the provided HTML. * @param {Response} body HTML response that will be transformed. * @param {TransformationOptionals<E, O>} [optionals] Optional parameters to pass to the replacer function. * @returns Response * @example ```ts * return temra.transform(await fetch(req), {req, env}); * ``` */ transform(body: Response, optionals?: TransformationOptionals<Environment, Optionals>): Response; } export * from "./types";