ampache_react/app/utils/url.js
Phyks (Lucas Verney) d8a7d4f66a Rework webplayer
Full rework of webplayer. Webplayer is back to its previous working
state, and ready for further improvements.
2016-08-10 23:50:23 +02:00

53 lines
1.3 KiB
JavaScript

/**
* 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.
*/
export function assembleURLAndParams(endpoint, params) {
let url = endpoint + "?";
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("&");
}
/**
* Clean an endpoint URL.
*
* Adds the protocol prefix if not specified, remove trailing slash
*
* @param An URL
* @return The cleaned URL
*/
export function cleanURL(endpoint) {
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;
}