[#204] fixed query string parsing
This commit is contained in:
@@ -600,108 +600,6 @@ export default class CommonHelper {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the query string (without "?") for the provided url.
|
||||
*
|
||||
* @param {String} [url]
|
||||
* @return {String}
|
||||
*/
|
||||
static getQueryString(url) {
|
||||
let queryStartPos = url.indexOf("?");
|
||||
|
||||
if (queryStartPos < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
let hashStartPos = url.indexOf("#");
|
||||
|
||||
return url.substring(queryStartPos + 1, (hashStartPos > queryStartPos ? hashStartPos : url.length));
|
||||
}
|
||||
|
||||
/**
|
||||
* Very simple and robust query params parser
|
||||
* (suppors encoded object and array params too).
|
||||
*
|
||||
* @param {String} url
|
||||
* @return {Object}
|
||||
*/
|
||||
static getQueryParams(url) {
|
||||
let result = {};
|
||||
let params = CommonHelper.getQueryString(url).split("&");
|
||||
|
||||
for (let i in params) {
|
||||
let parts = params[i].split("=");
|
||||
if (parts.length === 2) {
|
||||
let val = decodeURIComponent(parts[1]);
|
||||
|
||||
|
||||
if (val.startsWith("{") || val.startsWith("[")) {
|
||||
try {
|
||||
val = JSON.parse(val);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
result[decodeURIComponent(parts[0])] = val;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the query parameter of the provided url.
|
||||
*
|
||||
* @param {String} url
|
||||
* @param {Object} params
|
||||
* @param {Boolean} [extend]
|
||||
* @return {String}
|
||||
*/
|
||||
static setQueryParams(url, params, extend = true) {
|
||||
let oldQueryString = CommonHelper.getQueryString(url);
|
||||
let oldParams = extend && oldQueryString ? CommonHelper.getQueryParams(url) : {};
|
||||
let resultParams = Object.assign(oldParams, params);
|
||||
let newQueryString = "";
|
||||
|
||||
for (let param in resultParams) {
|
||||
if (CommonHelper.isEmpty(resultParams[param])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newQueryString) {
|
||||
newQueryString += "&";
|
||||
}
|
||||
|
||||
newQueryString += encodeURIComponent(param) + "=";
|
||||
|
||||
if (CommonHelper.isObject(resultParams[param])) {
|
||||
newQueryString += encodeURIComponent(JSON.stringify(resultParams[param]));
|
||||
} else {
|
||||
newQueryString += encodeURIComponent(resultParams[param]);
|
||||
}
|
||||
}
|
||||
newQueryString = newQueryString ? ("?" + newQueryString) : "";
|
||||
|
||||
// append the new query string to the url
|
||||
if (CommonHelper.isEmpty(oldQueryString)) {
|
||||
return url + newQueryString;
|
||||
}
|
||||
|
||||
// replace old query strung with the new one
|
||||
return url.replace("?" + oldQueryString, newQueryString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the current url query params.
|
||||
*
|
||||
* @param {Object} params
|
||||
*/
|
||||
static replaceClientQueryParams(params) {
|
||||
let url = CommonHelper.setQueryParams(window.location.href, params);
|
||||
|
||||
window.location.replace(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and returns the decoded jwt payload data.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user