2018-01-20 00:46:47 -08:00
|
|
|
import { encode, decode } from 'wherehows-web/utils/encode-decode-uri-component-with-space';
|
2017-03-24 20:50:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a url by appending a query pair (?key=value | &key=value) to a base url and
|
|
|
|
* encoding the query value in the pair
|
|
|
|
* @param {String} baseUrl the base or original url that will be appended with a query string
|
|
|
|
* @param {String} queryParam
|
|
|
|
* @param {String} queryValue
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-03-07 11:15:11 -08:00
|
|
|
export default (baseUrl: string, queryParam: string, queryValue: string): string => {
|
2017-03-24 20:50:42 -07:00
|
|
|
// If the query string already contains the initial question mark append
|
|
|
|
// kv-pair with ampersand
|
|
|
|
const separator = String(baseUrl).includes('?') ? '&' : '?';
|
|
|
|
|
|
|
|
// Malformed URL will cause decodeURIComponent to throw
|
|
|
|
// handle and encode queryValue in such instance
|
|
|
|
try {
|
|
|
|
// Check if queryValue is already encoded,
|
|
|
|
// otherwise encode queryValue before composing url
|
|
|
|
// e.g. if user directly enters query in location bar
|
|
|
|
if (decode(queryValue) === queryValue) {
|
|
|
|
queryValue = encode(queryValue);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof URIError) {
|
|
|
|
queryValue = encode(queryValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${baseUrl}${separator}${queryParam}=${queryValue}`;
|
|
|
|
};
|