2022-09-25 19:43:47 +03:00
|
|
|
export function formatApiCall(url, args) {
|
2022-09-07 16:53:24 +03:00
|
|
|
const find = /\{.*?\}/g;
|
2022-09-04 21:58:42 +03:00
|
|
|
const replace = (match) => {
|
|
|
|
const key = match.replace(/\{|\}/g, "");
|
2022-10-07 17:12:29 -07:00
|
|
|
return args[key] || "";
|
2022-09-04 21:58:42 +03:00
|
|
|
};
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return url.replace(/\/+$/, "").replace(find, replace).replace(find, replace);
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
|
2024-06-02 20:11:03 -07:00
|
|
|
export function getURLSearchParams(widget, endpoint) {
|
2022-09-04 21:58:42 +03:00
|
|
|
const params = new URLSearchParams({
|
|
|
|
type: widget.type,
|
|
|
|
group: widget.service_group,
|
|
|
|
service: widget.service_name,
|
|
|
|
});
|
2024-06-02 20:11:03 -07:00
|
|
|
if (endpoint) {
|
|
|
|
params.append("endpoint", endpoint);
|
2022-09-25 16:15:47 -07:00
|
|
|
}
|
2024-06-02 20:11:03 -07:00
|
|
|
return params;
|
2022-09-25 16:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatProxyUrl(widget, endpoint, queryParams) {
|
|
|
|
const params = getURLSearchParams(widget, endpoint);
|
|
|
|
if (queryParams) {
|
|
|
|
params.append("query", JSON.stringify(queryParams));
|
2022-09-25 14:31:41 -07:00
|
|
|
}
|
2023-10-18 11:44:26 -07:00
|
|
|
return `/api/services/proxy?${params.toString()}`;
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
2022-09-25 19:43:47 +03:00
|
|
|
|
|
|
|
export function asJson(data) {
|
|
|
|
if (data?.length > 0) {
|
|
|
|
const json = JSON.parse(data.toString());
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function jsonArrayTransform(data, transform) {
|
|
|
|
const json = asJson(data);
|
|
|
|
if (json instanceof Array) {
|
|
|
|
return transform(json);
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function jsonArrayFilter(data, filter) {
|
|
|
|
return jsonArrayTransform(data, (items) => items.filter(filter));
|
|
|
|
}
|
2023-02-15 14:46:31 -08:00
|
|
|
|
|
|
|
export function sanitizeErrorURL(errorURL) {
|
|
|
|
// Dont display sensitive params on frontend
|
|
|
|
const url = new URL(errorURL);
|
2024-04-29 17:18:55 -07:00
|
|
|
["apikey", "api_key", "token", "t", "access_token", "auth"].forEach((key) => {
|
2023-10-17 23:26:55 -07:00
|
|
|
if (url.searchParams.has(key)) url.searchParams.set(key, "***");
|
2024-06-02 20:11:03 -07:00
|
|
|
if (url.hash.includes(key)) url.hash = url.hash.replace(new RegExp(`${key}=[^&]+`), `${key}=***`);
|
2023-02-15 14:46:31 -08:00
|
|
|
});
|
|
|
|
return url.toString();
|
2023-10-17 23:26:55 -07:00
|
|
|
}
|