UNPKG

@sigiljs/sigil

Version:

TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating

129 lines (128 loc) 4.09 kB
class s { #t = /* @__PURE__ */ Object.create(null); #e = /* @__PURE__ */ Object.create(null); constructor(t = []) { if (Array.isArray(t)) for (let e = 0; e < t.length; e += 2) this.append(t[e], t[e + 1]); else for (const e in t) { const i = t[e]; i !== void 0 && (Array.isArray(i) ? i.forEach((o) => this.append(e, o)) : this.append(e, i)); } } /* ---------------------- 🔙 100 % совместимость ----------------------- */ /** * Gets the link associated with the current map instance. * * @return link to the internal map. */ get link() { return this.#t; } /* ---------------------- Cookie-утилиты ----------------------- */ /** * Retrieves all stored cookies as key-value pairs. * * @return {Record<string, string>} an object containing cookie names as keys and their corresponding values. */ get cookies() { return { ...this.#e }; } /** * Retrieves the value of a cookie by its name. * * @param {string} name name of the cookie to retrieve. * @return {string | undefined} value of the cookie if found, or undefined if the cookie does not exist. */ getCookie(t) { return this.#e[t]; } /* ---------------------- Header-утилиты ----------------------- */ /** * Returns an iterator for traversing the key-value pair entries stored in the internal map. * Each key-value pair is returned as a two-element array. * * @return {IterableIterator<[string, string]>} an iterator that yields key-value pairs as arrays. */ *entries() { for (const t in this.#t) for (const e of this.#t[t]) yield [t, e]; } /** * Returns a JSON-like object of parameters, taking the first value for each key. * @returns object mapping each key to its first value. */ json() { return Object.fromEntries( Object.entries(this.#t).map(([t, e]) => [t, e[0]]) ); } /** * Checks if a given key exists in the internal map. * * @param name key to check for existence. It can either be a specific type parameter of `Schema` or any string. * @return {boolean} true if the key exists in the map, otherwise false. */ has(t) { return t.toString().toLowerCase() in this.#t; } /** * Retrieves the first value associated with a given key. * * @param name key whose associated value needs to be retrieved. * @return {string | null} first value associated with the specified key if it exists, otherwise returns null. */ get(t) { return this.#t[t.toString().toLowerCase()]?.[0] ?? null; } /** * Retrieves all values associated with the specified key from the internal map. * * @param name key whose associated values are to be retrieved. * @return {string[]} array of strings containing the values associated with the specified key. * Returns an empty array if no values are found. */ getAll(t) { return this.#t[t.toString().toLowerCase()] ?? []; } /** * Sets or replaces the header with the given name and value. * * @param name header name (typed if using a Schema). * @param value header value. * @returns instance for chaining. */ set(t, e) { const i = t.toString().toLowerCase(); return this.#t[i] = [e], i === "cookie" && this.#i(e), this; } /** * Appends a value to an existing header or creates it if absent. * * @param name header name. * @param value value to append. * @returns instance for chaining. */ append(t, e) { const i = t.toString().toLowerCase(); return (this.#t[i] ||= []).push(e), i === "cookie" && this.#i(e), this; } /** * Deletes a header by name. * * @param name header name to delete. * @returns instance for chaining. */ delete(t) { const e = t.toString().toLowerCase(); return delete this.#t[e], e === "cookie" && (this.#e = {}), this; } #i(t) { t.split(";").forEach((e) => { const [i, ...o] = e.trim().split("="); i && (this.#e[i] = decodeURIComponent(o.join("=") || "")); }); } } export { s as default };