Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
wrap arrays in an object with key of results (#31)
Browse files Browse the repository at this point in the history
* wrap arrays in an object with key of results

* update changelog and package

* add notes to the readme
  • Loading branch information
cvburgess authored Mar 7, 2018
1 parent 6991814 commit f848211
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.13.0

- Convert array responses into objects with results arrays

## 0.12.0

- Add Data component
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,19 @@ const App = () => (
- `store: Object` - _**Required**_
- `observeData: Function` - _**Required**_ Returns data from the cache or fetches it - see code for signature - must return an id for unsubscribing
- `unobserveData: Function` - _**Required**_ Accepts an id and stops observing it
## axiosStore
axiosStore is a light wrapper around axios that allows for caching get requests using `cache.js`.
**NOTE:** As of v0.13.0 all array responses will instead return an object with a results array.
Example:
```js
APIResponse = [ 0, 2, 4, 6, 8 ];

// gets turned into

formattedResponse = { results: [ 0, 2, 4, 6, 8 ] };
```
4 changes: 3 additions & 1 deletion lib/axiosStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ var axiosStore = function axiosStore(axiosInstance) {
return axiosInstance.get.apply(axiosInstance, arg);
}).then(function (_ref) {
var data = _ref.data;
return _extends({}, data, { __cacheKey: cacheKey });

var wrappedData = Array.isArray(data) ? { results: data } : data;
return _extends({}, wrappedData, { __cacheKey: cacheKey });
}).catch(function (error) {
if (_store2.default.get(cacheKey) === PLACEHOLDER) {
_store2.default.remove(cacheKey);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perch-data",
"version": "0.12.0",
"version": "0.13.0",
"description": "Utilities for managing data. Inspired by react-apollo.",
"main": "lib/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/axiosStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const axiosStore = axiosInstance => {
})
: createPlaceholder(cacheKey)
.then(() => axiosInstance.get(...arg))
.then(({ data }) => ({ ...data, __cacheKey: cacheKey }))
.then(({ data }) => {
const wrappedData = Array.isArray(data) ? { results: data } : data;
return { ...wrappedData, __cacheKey: cacheKey };
})
.catch(error => {
if (store.get(cacheKey) === PLACEHOLDER) {
store.remove(cacheKey);
Expand Down

0 comments on commit f848211

Please sign in to comment.