2015-05-19 17:01:28 +02:00
|
|
|
import GeneralUtils from './general_utils';
|
|
|
|
|
2015-05-20 16:44:45 +02:00
|
|
|
|
2015-05-19 17:01:28 +02:00
|
|
|
// TODO: Create Unittests that test all functions
|
2015-05-19 13:45:19 +02:00
|
|
|
let FetchApiUtils = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a key-value object of this form:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* 'page': 1,
|
|
|
|
* 'pageSize': 10
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* and converts it to a query-parameter, which you can append to your URL.
|
2015-05-19 14:09:10 +02:00
|
|
|
* The return looks like this:
|
|
|
|
*
|
|
|
|
* ?page=1&page_size=10
|
|
|
|
*
|
|
|
|
* CamelCase gets converted to snake_case!
|
2015-05-19 13:45:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
argsToQueryParams(obj) {
|
2015-05-19 17:01:28 +02:00
|
|
|
|
|
|
|
obj = GeneralUtils.sanitize(obj);
|
|
|
|
|
2015-05-19 13:45:19 +02:00
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.map((key, i) => {
|
|
|
|
let s = '';
|
|
|
|
|
|
|
|
if(i === 0) {
|
|
|
|
s += '?';
|
|
|
|
} else {
|
|
|
|
s += '&';
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:09:10 +02:00
|
|
|
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
|
|
|
|
|
|
|
|
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
2015-05-19 13:45:19 +02:00
|
|
|
})
|
|
|
|
.join('');
|
2015-05-21 12:12:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a string and a boolean and generates a string query parameter for
|
|
|
|
* an API call.
|
|
|
|
*/
|
|
|
|
generateOrderingQueryParams(orderBy, orderAsc) {
|
|
|
|
let interpolation = '';
|
|
|
|
|
|
|
|
if(!orderAsc) {
|
|
|
|
interpolation += '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
return interpolation + orderBy;
|
2015-05-19 13:45:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-20 16:44:45 +02:00
|
|
|
export default FetchApiUtils;
|