AllenYuan

http - client

Apr 22nd, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import superagentPromise from 'superagent-promise';
  2. import _superagent from 'superagent';
  3.  
  4. // http client
  5. const superagent = superagentPromise(_superagent, global.Promise);
  6.  
  7. // root api url
  8. const API_ROOT = 'https://conduit.productionready.io/api';
  9.  
  10. // callback to extract the body out of a response
  11. const responseBody = res => res.body;
  12.  
  13. // http method wrapppers
  14. // make a request to the given url and parse out the body
  15. const requests = {
  16.   get: url =>
  17.     superagent.get(`${API_ROOT}${url}`).then(responseBody)
  18. };
  19.  
  20. // articles methods
  21. const Articles = {
  22.   // get promise for 10 articles
  23.   all: page =>
  24.     requests.get('/articles?limit=10')
  25. };
  26.  
  27. export default {
  28.   Articles
  29. };
Advertisement
Add Comment
Please, Sign In to add comment