Skip to content

Commit

Permalink
Add async/simpleFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
thibmaek committed Nov 24, 2018
1 parent 6568389 commit 243e476
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
],
"sort-keys": [
"error"
]
],
},
"globals": {
"RequestOptions": true
}
}
1 change: 1 addition & 0 deletions ACKNOWLEDGMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Following people, users, repos, resources and projects have contributed towards
* @bendc gists (https://gist.github.com/bendc)
* https://github.com/timothyverhaeghe/pelicanjs
* https://lodash.com/
* https://twitter.com/wesbos/status/1063515277911052290

Sorry if I forgot you…
3 changes: 2 additions & 1 deletion esm/async/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as map } from './mapPromise';
export { default as forEach } from './forEach';
export { default as map } from './mapPromise';
export { default as simpleFetch } from './simpleFetch';
export { default as sleep } from './sleep';
export { default as throttle } from './throttle';
14 changes: 14 additions & 0 deletions esm/async/simpleFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
*
* Safe fetch which assumes JSON and returns it.
*/
export default async (url, options = {}) => {
const response = await fetch(url, {
headers: {
Accept: `application/json, text/plain, */*`,
'Content-Type': `application/json`,
},
...options,
});
return response.json();
};
3 changes: 2 additions & 1 deletion src/async/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as map } from './mapPromise';
export { default as forEach } from './forEach';
export { default as map } from './mapPromise';
export { default as simpleFetch } from './simpleFetch';
export { default as sleep } from './sleep';
export { default as throttle } from './throttle';
14 changes: 14 additions & 0 deletions src/async/simpleFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @flow
* Safe fetch which assumes JSON and returns it.
*/
export default async (url: string, options: RequestOptions = {}) => {
const response = await fetch(url, {
headers: {
Accept: `application/json, text/plain, */*`,
'Content-Type': `application/json`,
},
...options,
});
return response.json();
};

0 comments on commit 243e476

Please sign in to comment.