mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 01:25:17 +01:00
8d73e4a77b
editionnote
72 lines
1.6 KiB
JavaScript
72 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
import { sanitize } from './general_utils';
|
|
|
|
// TODO: Create Unittests that test all functions
|
|
|
|
/**
|
|
* 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.
|
|
* The return looks like this:
|
|
*
|
|
* ?page=1&page_size=10
|
|
*
|
|
* CamelCase gets converted to snake_case!
|
|
*
|
|
*/
|
|
export function argsToQueryParams(obj) {
|
|
|
|
obj = sanitize(obj);
|
|
|
|
return Object
|
|
.keys(obj)
|
|
.map((key, i) => {
|
|
let s = '';
|
|
|
|
if(i === 0) {
|
|
s += '?';
|
|
} else {
|
|
s += '&';
|
|
}
|
|
|
|
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
|
|
|
|
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
|
})
|
|
.join('');
|
|
}
|
|
|
|
/**
|
|
* Takes a string and a boolean and generates a string query parameter for
|
|
* an API call.
|
|
*/
|
|
export function generateOrderingQueryParams(orderBy, orderAsc) {
|
|
let interpolation = '';
|
|
|
|
if(!orderAsc) {
|
|
interpolation += '-';
|
|
}
|
|
|
|
return interpolation + orderBy;
|
|
}
|
|
|
|
export function status(response) {
|
|
if (response.status >= 200 && response.status < 300) {
|
|
return response;
|
|
}
|
|
throw new Error(response.json());
|
|
}
|
|
|
|
export function getCookie(name) {
|
|
let value = '; ' + document.cookie;
|
|
let parts = value.split('; ' + name + '=');
|
|
if (parts.length === 2) {
|
|
return parts.pop().split(';').shift();
|
|
}
|
|
} |