UNPKG

@dossierhq/core

Version:

The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.

38 lines 1.45 kB
/// <reference types="./UrlQueryUtils.d.ts" /> export function encodeObjectToURLSearchParams(params, options) { const payload = new URLSearchParams(); encodeURLSearchParams(payload, params, options); return payload; } export function encodeURLSearchParams(urlSearchParams, params, options) { const removeEmptyObjects = !options?.keepEmptyObjects; if (params) { for (const [key, value] of Object.entries(params)) { if (value === null || value === undefined || (removeEmptyObjects && typeof value === 'object' && value && Object.keys(value).length === 0)) { continue; } const encoded = JSON.stringify(value); urlSearchParams.set(key, encoded); } } } export function decodeURLSearchParamsParam(urlSearchParams, name) { if (!urlSearchParams) { return undefined; } const isURLSearchParams = 'get' in urlSearchParams && typeof urlSearchParams.get === 'function'; const encoded = isURLSearchParams ? urlSearchParams.get(name) : urlSearchParams[name]; if (encoded === undefined || encoded === null) { return undefined; } if (typeof encoded !== 'string') { throw new Error(`Expected string value for URL search param ${name}`); } return JSON.parse(encoded); } //# sourceMappingURL=UrlQueryUtils.js.map