AllenYuan

auth - http

Apr 24th, 2020
1,226
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.   post: (url, body) =>
  19.     superagent.post(`${API_ROOT}${url}`).then(responseBody)
  20. };
  21.  
  22. // articles methods
  23. const Articles = {
  24.   // get promise for 10 articles
  25.   all: page =>
  26.     requests.get('/articles?limit=10')
  27. };
  28.  
  29. const Auth = {
  30.   // get promise for login success/failure
  31.   login: (email, password) =>
  32.     requests.post('/users/login', { user: { email, password } })
  33. };
  34.  
  35. export default {
  36.   Articles,
  37.   Auth
  38. };
Advertisement
Add Comment
Please, Sign In to add comment