import axios from 'axios'; import { events } from './store'; export const apiServer = axios.create({ baseURL: '/api/', }); export async function boot() { return apiServer.get('/boot'); } export async function logIn(username, password) { const data = { name: username, password, }; return apiServer.post('/auth/login', data); } export async function logOut() { return apiServer.post('/auth/logout', {}); } export async function listChannels() { return apiServer.get('/channels'); } export async function createChannel(name) { return apiServer.post('/channels', { name }); } export async function postToChannel(channelId, message) { return apiServer.post(`/channels/${channelId}`, { message }); } export function subscribeToEvents() { const evtSource = new EventSource("/api/events"); // TODO: this should process all incoming events and store them. // TODO: eventually we'll need to handle expiring old info, so as not to use // infinite browser memory. evtSource.onmessage = (evt) => { const data = JSON.parse(evt.data); events.update((value) => [...value, data]); } }