import 'babel-polyfill'; let apiPort = '8880' if (process.env.API_PORT !== undefined) { apiPort = process.env.API_PORT; } function getEndpoint (path) { return new URL(path, apiEndpoint).toString() } const endPoints = { get upload () { return getEndpoint('upload') } } const apiEndpoint = function() { let currentLoc = self.location; let endpoint = new URL(currentLoc); endpoint.port = apiPort; return endpoint.toString() }() class Upload { constructor(files) { this.files = Array.from(files) this.timestamp = new Date() } get payload() { return { files: this.files, timestamp: this.timestamp, } } async create() { let req = new Request(endPoints.upload, { method: 'POST', body: JSON.stringify(this.payload) }) //let result = await fetch(req); let res = await fetch(req) .catch((e) => { console.error(e) }) if (!res.ok) { throw(`${res.status}: ` + (await res.json()).error) } return res.json() } } export default { endpoint: apiEndpoint, endPoints: endPoints, Upload: Upload }