import { exactInteger } from './exact-json.ts'; // Side-effect-free URL-encoded form reading, shared by requests and published examples. /** A field set as the object's own, whatever its name (`__proto__`, `constructor` and `prototype` are data a caller * may name: Stripe's metadata keys are the caller's), never through what the object inherits. */ export function ownField(node: Record | unknown[], key: string, value: unknown): void { Object.defineProperty(node, key, { value, writable: true, enumerable: true, configurable: true }); } /** A field the object holds as its own, never one it inherits (`constructor` is every object's). */ const ownValue = (node: Record | unknown[], key: string): unknown => (Object.hasOwn(node, key) ? (node as Record)[key] : undefined); /** A form body's bracket notation (`a[b][0][c]=v`, `expand[]=x`) as nested objects and arrays. A form carries only * text: a field the operation's spec types as a number or a boolean (`scalars`, by bracket path, written by * twin-standard's formScalarsOf) is read as one when its text is that literal; every other field stays the text sent * (`metadata[order]=007`, `name=2024`). */ export function parseBracketForm(text: string, scalars?: Readonly>): Record { const out: Record = {}; for (const [rawKey, raw] of new URLSearchParams(text)) { const parts = rawKey.replace(/\]/g, '').split('['); const kind = scalars && scalarAt(scalars, parts); const value: unknown = kind === 'boolean' ? (raw === 'true' ? true : raw === 'false' ? false : raw) : kind === 'integer' ? (/^-?\d+$/.test(raw) ? exactInteger(raw) : raw) : kind === 'number' ? (/^-?\d+$/.test(raw) ? exactInteger(raw) : /^-?\d+\.\d+$/.test(raw) ? Number(raw) : raw) : raw; // every field is the node's own, read and written as such: a key naming an object's machinery (`__proto__[x]`, // `constructor[prototype][x]`) is a field of that name, never a walk into what every object inherits let node: Record | unknown[] = out; for (const [i, key] of parts.entries()) { // an array holds its items by index (`a[]`, `a[0]`): a named key under one (`a[]=1&a[length]=-1`) has nowhere to // go, as a key under text has none (`a=1&a[b]=2`): the first shape stands if (Array.isArray(node) && key !== '' && !/^\d+$/.test(key)) break; if (i === parts.length - 1) { if (key === '') { if (Array.isArray(node)) node.push(value); } else { const held = ownValue(node, key); if (held === undefined) ownField(node, key, value); else if (Array.isArray(held)) held.push(value); else ownField(node, key, [held, value]); } break; } const next = parts[i + 1]!; if (key === '') break; let child = ownValue(node, key); if (child === undefined) { child = next === '' || /^\d+$/.test(next) ? [] : {}; ownField(node, key, child); } if (child === null || typeof child !== 'object') break; node = child as Record | unknown[]; } } return out; } /** The type `scalars` gives a form key's parts: a segment is a property name, `[]` an array's item (an index or empty), * `*` a map's key. */ function scalarAt(scalars: Readonly>, parts: string[]): string | undefined { let paths = ['']; for (const [i, part] of parts.entries()) { const segs = part === '' || /^\d+$/.test(part) ? ['[]', '*'] : [part, '*']; paths = paths.flatMap((p) => segs.map((seg) => (i === 0 ? seg : `${p}.${seg}`))); if (paths.length > 64) paths = paths.filter((p) => Object.keys(scalars).some((k) => k === p || k.startsWith(`${p}.`))); } for (const p of paths) if (scalars[p]) return scalars[p]; return undefined; }