UNPKG

@sigiljs/sigil

Version:

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

128 lines (127 loc) 3.32 kB
class i { /** * Read any known query parameter as a string or null. */ getAny = this.getKnown; /** Internal map of parameter names to array of values. */ #t = /* @__PURE__ */ Object.create(null); /** * Constructs a new IncomingSearchParams instance. * * @param init initial parameters */ constructor(t) { if (t) { if (typeof t == "string") { const r = t.charAt(0) === "?" ? t.slice(1) : t; for (const s of r.split("&")) { if (!s) continue; const [e, n = ""] = s.split("=", 2); this.append(decodeURIComponent(e), decodeURIComponent(n)); } return; } if (Array.isArray(t)) { for (const [r, s] of t) this.append(r, s); return; } if (t instanceof URLSearchParams) { for (const [r, s] of t) this.append(r, s); return; } for (const r in t) this.append(r, t[r]); } } /** * 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, r]) => [t, r[0]]) ); } /** * Retrieves the first value for a known parameter or null if missing. * * @param key parameter key to retrieve. * @returns first parameter value or null. */ getKnown(t) { const r = this.#t[this.#r(String(t))]; return (r && r[0]) ?? null; } /** * Retrieves all values for a known parameter key. * * @param key parameter key to retrieve. * @returns array of all values for the key. */ getAll(t) { return this.#t[this.#r(String(t))] ?? []; } /** * Checks if a known parameter key is present. * * @param key parameter key to check. * @returns true if the key exists. */ has(t) { return this.#r(String(t)) in this.#t; } /** * Sets a parameter to a single value, replacing existing values. * * @param key parameter key to set. * @param value value to assign. * @returns instance for chaining. */ set(t, r) { return this.#t[this.#r(String(t))] = [String(r)], this; } /** * Appends a value to a parameter key, preserving existing values. * * @param key parameter key to append to. * @param value value to append. * @returns instance for chaining. */ append(t, r) { const s = this.#r(String(t)), e = this.#t[s]; return e ? e.push(String(r)) : this.#t[s] = [String(r)], this; } /** * Deletes all values for a parameter key. * * @param key parameter key to delete. * @returns instance for chaining. */ delete(t) { return delete this.#t[this.#r(String(t))], this; } /** * Returns a simple object of parameters taking the first value for each key. * * @returns plain object of key->firstValue. */ getObject() { const t = {}; for (const r in this.#t) t[r] = this.#t[r][0]; return t; } /** * Iterator over [key, value] pairs for each parameter value. * Works like URLSearchParams[Symbol.iterator]. */ *[Symbol.iterator]() { for (const t in this.#t) for (const r of this.#t[t]) yield [t, r]; } /** Internal helper to normalize key names. */ #r(t) { return t.toString(); } } export { i as default };