demonhide

Untitled

May 31st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fetchival from 'fetchival';
  2. import _ from 'lodash';
  3.  
  4. import * as sessionSelectors from 'services/session/selectors';
  5. import apiConfig from './config';
  6.  
  7. /*
  8.  * Get message exception
  9.  *
  10.  * @param {String} exception error
  11.  * @return error
  12. */
  13.  
  14. export const exceptionExtractError = (exception) => {
  15.   if (!exception.Error) return false;
  16.   let error = false;
  17.   const errorKeys = Object.keys(exception.Errors);
  18.   if (errorKeys.length > 0) {
  19.     error = exception.Errors[errorKeys[0]][0].message;
  20.   }
  21.   return error;
  22. };
  23.  
  24. /*
  25.  * Connect to api
  26.  *
  27.  * @param {String} endPoint end path of api url
  28.  * @param {Object} payload data want to send
  29.  * @param {String} method method use to send api
  30.  * @param {Object} headers header want to send
  31.  * @throw {e}
  32. */
  33.  
  34. export const fetchApi = (endPoint, payload = {}, method = 'get', headers = {}) => {
  35.   const accessToken = sessionSelectors.get().tokens && sessionSelectors.get().tokens.access.value;
  36.  
  37.   if (!accessToken) {
  38.     payload.client_id = apiConfig.clientId;
  39.     payload.client_secret = apiConfig.clientSecret;
  40.   }
  41.   return fetchival(`${apiConfig.url}${endPoint}`, {
  42.     headers: _.pickBy({
  43.       ...(accessToken ? {
  44.         Authorization: `Bearer ${accessToken}`,
  45.       } : {}),
  46.       ...headers,
  47.     }, item => !_.isEmpty(item)),
  48.   })[method.toLowerCase()](payload)
  49.     .catch((e) => {
  50.       throw e;
  51.     });
  52. };
  53.  
  54. /*
  55.  * Connect to api have image
  56.  *
  57.  * @param {String} endPont end path of api url
  58.  * @param {Object} payload data want to send
  59.  * @param {String} method method use to send api
  60.  * @param {Object} header header want to send
  61. */
  62.  
  63. export const fetchApiWithImage = (
  64.   endPoint,
  65.   payload = {},
  66.   method = 'post',
  67.   headers = { Accept: 'application/json', 'Content-Type': 'multipart/form-data' },
  68. ) => {
  69.   const accessToken = sessionSelectors.get().tokens && sessionSelectors.get().tokens.access.value;
  70.  
  71.  
  72.   return new Promise(
  73.     (resolve, reject) => {
  74.       const xhr = new XMLHttpRequest();
  75.       xhr.open(method, `${apiConfig.url}${endPoint}`);
  76.       xhr.responseType = 'json';
  77.  
  78.       const formdata = payload;
  79.       xhr.onreadystatechange = () => {
  80.         if (xhr.readyState === XMLHttpRequest.DONE) {
  81.           resolve(xhr.response);
  82.         }
  83.       };
  84.       xhr.setRequestHeader('Authorization', `Bearer ${accessToken}`);
  85.       xhr.send(formdata);
  86.     },
  87.   );
  88. };
Advertisement
Add Comment
Please, Sign In to add comment