import type { QueryObject } from '../../helpers/search/search'; import { normalizeSearchParams } from '../../helpers/search/search'; import { isBrowser } from '../is-browser'; const BUILDER_SEARCHPARAMS_PREFIX = 'builder.'; const BUILDER_OPTIONS_PREFIX = 'options.'; /** * Receives a `URLSearchParams` object or a regular query object, and returns * the subset of query params that are relevant to the Builder SDK. * * Outputs a key-value object to be passed to `fetchOneEntry` or `fetchEntries` * functions as the `options` argument. * * NOTE: This function is generally not needed. Instead, it is recommended to use `isPreviewing()` * to check if the current page requires previewed content. * * @returns */ export const getBuilderSearchParams = (_options: QueryObject | URLSearchParams | undefined) => { if (!_options) { return {}; } const options = normalizeSearchParams(_options); const newOptions: QueryObject = {}; Object.keys(options).forEach(key => { if (key.startsWith(BUILDER_SEARCHPARAMS_PREFIX)) { const trimmedKey = key.replace(BUILDER_SEARCHPARAMS_PREFIX, '').replace(BUILDER_OPTIONS_PREFIX, ''); newOptions[trimmedKey] = options[key]; } }); return newOptions; }; export const getBuilderSearchParamsFromWindow = () => { if (!isBrowser()) { return {}; } const searchParams = new URLSearchParams(window.location.search); return getBuilderSearchParams(searchParams); }