1
0
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:
Tim Daubenschütz 2015-05-19 14:09:10 +02:00
parent adb4c72253
commit 9c58fa321c
4 changed files with 16 additions and 10 deletions

View File

@ -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);
});
}
};

View File

@ -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>
);

View File

@ -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());
}
};

View File

@ -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('');
}