2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Collection of helper functions to deal with URLs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assemble a base URL and its GET parameters.
|
|
|
|
*
|
|
|
|
* @param endpoint Base URL.
|
|
|
|
* @param params An object of GET params and their values.
|
|
|
|
*
|
|
|
|
* @return A string with the full URL with GET params.
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
export function assembleURLAndParams(endpoint, params) {
|
2016-08-05 00:00:25 +02:00
|
|
|
let url = endpoint + "?";
|
2016-07-07 23:23:18 +02:00
|
|
|
Object.keys(params).forEach(
|
|
|
|
key => {
|
|
|
|
if (Array.isArray(params[key])) {
|
|
|
|
params[key].forEach(value => url += key + "[]=" + value + "&");
|
|
|
|
} else {
|
|
|
|
url += key + "=" + params[key] + "&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return url.rstrip("&");
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean an endpoint URL.
|
|
|
|
*
|
|
|
|
* Adds the protocol prefix if not specified, remove trailing slash
|
|
|
|
*
|
|
|
|
* @param An URL
|
|
|
|
* @return The cleaned URL
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
export function cleanURL(endpoint) {
|
2016-08-10 21:36:11 +02:00
|
|
|
if (
|
|
|
|
!endpoint.startsWith("//") &&
|
|
|
|
!endpoint.startsWith("http://") &&
|
|
|
|
!endpoint.startsWith("https://"))
|
|
|
|
{
|
|
|
|
// Handle endpoints of the form "ampache.example.com"
|
|
|
|
// Append same protocol as currently in use, to avoid mixed content.
|
|
|
|
endpoint = window.location.protocol + "//" + endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove trailing slash
|
|
|
|
endpoint = endpoint.replace(/\/$/, "");
|
|
|
|
|
|
|
|
return endpoint;
|
|
|
|
}
|