Keycloak
1export function stringifyQueryParams(params: Record<string, unknown>) {
2const searchParams = new URLSearchParams();
3
4for (const [key, value] of Object.entries(params)) {
5// Ignore undefined and null values.
6if (value === undefined || value === null) {
7continue;
8}
9
10// Ignore empty strings.
11if (typeof value === "string" && value.length === 0) {
12continue;
13}
14
15// Ignore empty arrays.
16if (Array.isArray(value) && value.length === 0) {
17continue;
18}
19
20// Append each entry of an array as a separate parameter, or the value itself otherwise.
21if (Array.isArray(value)) {
22value.forEach((item) => searchParams.append(key, item.toString()));
23} else {
24searchParams.append(key, value.toString());
25}
26}
27
28return searchParams.toString();
29}
30