vault/ui/app/utils/query-param-string.js
Vault Automation 0c6c13dd38
license: update headers to IBM Corp. (#10229) (#10233)
* license: update headers to IBM Corp.
* `make proto`
* update offset because source file changed

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-10-21 15:20:20 -06:00

37 lines
1.2 KiB
JavaScript

/**
* Copyright IBM Corp. 2016, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
import { isEmptyValue } from 'core/helpers/is-empty-value';
/**
* queryParamString converts an object to a query param string with URL encoded keys and values.
* It does not include values that are considered invalid query params (below).
* @param {object} queryObject with key-value pairs of desired URL params
* @returns string like ?key=val1&key2=val2
*/
// we can't just rely on falsy values because "false" and "0" are valid query params
const INVALID_QP = [undefined, null, ''];
export default function queryParamString(queryObject) {
if (
!queryObject ||
isEmptyValue(queryObject) ||
typeof queryObject !== 'object' ||
Array.isArray(queryObject) ||
Object.values(queryObject).every((v) => INVALID_QP.includes(v))
)
return '';
return Object.keys(queryObject).reduce((prev, key) => {
const value = queryObject[key];
if (INVALID_QP.includes(value)) return prev;
const keyval = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
if (prev === '?') {
return `${prev}${keyval}`;
}
return `${prev}&${keyval}`;
}, '?');
}