1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #7148: Handle array values when constructing API URLs

This commit is contained in:
thatmattlove
2021-09-02 08:58:55 -07:00
parent caa2813d0d
commit 113358f2de
13 changed files with 65 additions and 41 deletions

View File

@ -1,6 +1,8 @@
import Cookie from 'cookie';
import queryString from 'query-string';
import type { Stringifiable } from 'query-string';
type Method = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
type ReqData = URLSearchParams | Dict | undefined | unknown;
type SelectedOption = { name: string; options: string[] };
@ -117,6 +119,30 @@ function getCsrfToken(): string {
return value;
}
/**
* Constrict an object from a URL query param string, with all values as an array.
*
* @param params URL search query string.
* @returns Constructed query object.
*/
function queryParamsToObject(params: string): Record<string, Stringifiable[]> {
const result = {} as Record<string, Stringifiable[]>;
const searchParams = new URLSearchParams(params);
for (const [key, originalValue] of searchParams.entries()) {
let value = [] as Stringifiable[];
if (key in result) {
value = [...value, ...result[key]];
}
if (Array.isArray(originalValue)) {
value = [...value, ...originalValue];
} else {
value = [...value, originalValue];
}
result[key] = value;
}
return result;
}
/**
* Build a NetBox URL that includes `settings.BASE_PATH` and enforces leading and trailing slashes.
*
@ -153,7 +179,7 @@ function buildUrl(destination: string): string {
}
const url = combined.join('/');
// Construct an object from the URL search params so it can be re-serialized with the new URL.
const query = Object.fromEntries(new URLSearchParams(search).entries());
const query = queryParamsToObject(search);
return queryString.stringifyUrl({ url, query });
}