import axios from 'axios'; import cookies from 'react-cookies'; const apiBaseUrl = 'https://api.juick.com'; const client = axios.create({ baseURL: apiBaseUrl }); client.interceptors.request.use(config => { config.params = Object.assign(config.params || {}, { hash: cookies.load('hash') }); return config; }); export function me(username = '', password = '') { return new Promise((resolve, reject) => { client.get('/me', { headers: username ? { 'Authorization': 'Basic ' + window.btoa(unescape(encodeURIComponent(username + ':' + password))) } : {} }).then(response => { let visitor = response.data; cookies.save('hash', visitor.hash, { path: '/' }); resolve(visitor); }).catch(reason => { cookies.remove('hash', { path: '/'}); reject(reason); }); }); } export function info(username) { return client.get(`/users?uname=${username}`); } export function getChats() { return client.get('/groups_pms'); } export function getChat(userName) { return client.get('/pm', { params: { 'uname': userName } }); } export function pm(userName, body) { let form = new FormData(); form.set('uname', userName); form.set('body', body); return client.post('/pm', form); } export function getMessages(path, params) { return client.get(path, { params: params }); } export function post(body, attach) { let form = new FormData(); form.append('attach', attach); form.append('body', body); return client.post('/post', form); } export function comment(mid, rid, body, attach) { let form = new FormData(); form.append('mid', mid); form.append('rid', rid); form.append('body', body); form.append('attach', attach); return client.post('/comment', form); } export function updateAvatar(newAvatar) { let form = new FormData(); form.append('avatar', newAvatar); return client.post('/me/upload', form); } function socialLink(network) { return `${apiBaseUrl}/_${network}login?state=${window.location.protocol}//${window.location.host}${window.location.pathname}`; } export function facebookLink() { return socialLink('fb'); } export function vkLink() { return socialLink('vk'); } export function markReadTracker(msg, visitor) { return `${apiBaseUrl}/thread/mark_read/${msg.mid}-${msg.rid || 0}.gif?hash=${visitor.hash}`; }