mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 09:23:13 +01:00
add encoding and camel to snake case conversion to FetchApiUtils
This commit is contained in:
parent
adb4c72253
commit
9c58fa321c
@ -10,15 +10,11 @@ class ArtworkListActions {
|
||||
|
||||
fetchArtworkList() {
|
||||
ArtworkFetcher.fetch()
|
||||
/*.then((res) => {
|
||||
return res.json();
|
||||
})*/
|
||||
.then((res) => {
|
||||
this.actions.updateArtworkList(res.pieces);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
console.error('OMG cannot retrieve the artworks');
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -10,8 +10,11 @@ class AscribeApp extends React.Component {
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<h1>ascribe all the things!</h1>
|
||||
<Link to="artworks">artworks</Link>
|
||||
<header>ascribe</header>
|
||||
<navigation>
|
||||
<Link to="artworks">artworks</Link>
|
||||
</navigation>
|
||||
|
||||
<RouteHandler />
|
||||
</div>
|
||||
);
|
||||
|
@ -7,14 +7,14 @@ var ArtworkListFetcher = {
|
||||
fetch(page=1, pageSize=10) {
|
||||
let params = FetchApiUtils.argsToQueryParams({
|
||||
page,
|
||||
'page_size': pageSize // this is kind of a bummer...
|
||||
pageSize
|
||||
});
|
||||
|
||||
return fetch(AppConstants.baseUrl + 'pieces/' + params, {
|
||||
headers: {
|
||||
'Authorization': 'Basic ZGltaUBtYWlsaW5hdG9yLmNvbTowMDAwMDAwMDAw'
|
||||
}
|
||||
}).then((res) => { return res.json(); });
|
||||
}).then((res) => res.json());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -9,6 +9,11 @@ let FetchApiUtils = {
|
||||
* }
|
||||
*
|
||||
* 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!
|
||||
*
|
||||
* @param {[type]}
|
||||
* @return {[type]}
|
||||
@ -25,7 +30,9 @@ let FetchApiUtils = {
|
||||
s += '&';
|
||||
}
|
||||
|
||||
return s + key + '=' + obj[key];
|
||||
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
|
||||
|
||||
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
||||
})
|
||||
.join('');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user