homebox/frontend/lib/api/base/urls.ts

29 lines
797 B
TypeScript
Raw Normal View History

2022-09-04 02:42:03 +00:00
const parts = {
host: 'http://localhost.com',
prefix: '/api/v1',
};
2022-08-31 00:06:57 +00:00
2022-09-04 02:42:03 +00:00
export function OverrideParts(host: string, prefix: string) {
parts.host = host;
parts.prefix = prefix;
}
export type QueryValue = string | string[] | number | number[] | boolean | null | undefined;
2022-08-31 00:06:57 +00:00
2022-09-04 02:42:03 +00:00
export function UrlBuilder(rest: string, params: Record<string, QueryValue> = {}): string {
const url = new URL(parts.prefix + rest, parts.host);
2022-08-31 00:06:57 +00:00
2022-09-04 02:42:03 +00:00
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value) {
url.searchParams.append(key, String(item));
}
} else {
url.searchParams.append(key, String(value));
}
}
2022-08-31 00:06:57 +00:00
2022-09-04 02:42:03 +00:00
// we return the path only, without the base URL
return url.toString().replace('http://localhost.com', '');
2022-08-31 00:06:57 +00:00
}