Guest User

Untitled

a guest
Nov 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import superagent from 'superagent'
  2. import Promise from 'bluebird'
  3.  
  4. // standard superagent get request:
  5. const getRequst = (url, params) => {
  6. return new Promise((resolve, reject) => {
  7. superagent.get(url)
  8. .query(params)
  9. .set('Accept', 'application/json')
  10. .end((err, response) => {
  11. if (err){
  12. reject(err)
  13. return
  14. }
  15.  
  16. const payload = response.body || response.text
  17. resolve(payload)
  18. })
  19. })
  20. }
  21.  
  22. const postRequst = (url, body) => {
  23. return new Promise((resolve, reject) => {
  24. superagent.post(url)
  25. .send(body)
  26. .set('Accept', 'application/json')
  27. .end((err, response) => {
  28. if (err){
  29. reject(err)
  30. return
  31. }
  32.  
  33. const payload = response.body || response.text
  34. resolve(payload)
  35. })
  36. })
  37. }
  38.  
  39. export default {
  40. post: (url, body, actionType) => {
  41. return dispatch => postRequst(url, body)
  42. .then(data => {
  43. // console.log('DATA: ' + JSON.stringify(data))
  44. if (actionType != null){
  45. dispatch({
  46. type: actionType,
  47. data: data
  48. })
  49. }
  50.  
  51. return data
  52. })
  53. .catch(err => {
  54. throw err
  55. })
  56. },
  57.  
  58. get: (url, params, actionType) => {
  59. return dispatch => getRequst(url, params)
  60. .then(data => {
  61. // console.log('DATA: ' + JSON.stringify(data))
  62. if (actionType != null){
  63. dispatch({
  64. type: actionType,
  65. data: data
  66. })
  67. }
  68.  
  69. return data
  70. })
  71. .catch(err => {
  72. throw err
  73. })
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment