UNPKG

fb-test-module

Version:

How to use: ``` import {store, getStore} from 'fb-test-module'; conf.DialogramApi = "API_BASE_URL"; conf.Platform = "mobile"; //use "web" if you are on webApp getStore(); //Inject store in your provider ``` and you'r readyt to go. # Base ## Act

88 lines (79 loc) 2.39 kB
import axios from 'axios'; import { fromJS } from 'immutable'; import { conf } from './Config'; import SessionHelper from './Session/Selector'; export function callApi({ method, url, data }) { if (conf.DialogramApi.length === 0) throw new Error('Api IP not set'); // console.log(SessionHelper.getSessionToken()) //console.log(`Bearer ${SessionHelper.getSessionToken()}`) return axios({ method: method, url: url, baseURL: conf.DialogramApi, data: data, headers: { 'Authorization': `Bearer ${SessionHelper.getSessionToken()}`, } }).then((response) => { return Promise.resolve(response); }, error => { console.error("Error ==>", error); return Promise.reject(new Error(error)); }).then(_json).then(_serializeResp); } function _json(response) { if (response.status !== 204) { return response.data || response.json(); } else { return {}; } } function _mapType(type) { if (type) { type = type.toLowerCase(); } switch (type) { case 'webfile': case 'weblink': case 'googlefile': case 'evernotefile': return 'media'; default: return type; } } function _formatArray(included) { const keyedIncluded = {}; included.forEach((data) => { const type = _mapType(data.type); const id = data.id; if (type) { if (!keyedIncluded[type]) { keyedIncluded[type] = {}; } keyedIncluded[type][id] = data; } else { keyedIncluded[id] = data; } }); return keyedIncluded; } function _serializeResp(json) { return new Promise((resolve) => { json.data = _formatArray(json.data); if (json.includes) { json.includes = _formatArray(json.includes); } const immutableData = fromJS(json); resolve(immutableData); }); } function _status(response) { if (response && response.status) { if (response.status >= 200 && response.status < 300) { return Promise.resolve(response); } } else { return Promise.reject({ status: 500, msg: "debug prod mod" }); } return Promise.reject(response); }