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(filesMetadata, fileObjects) { this.filesMetadata = Array.from(filesMetadata) this.fileObjects = fileObjects this.timestamp = new Date() } get uploadMetadata() { return { files: this.filesMetadata, timestamp: this.timestamp, } } async create() { let req = new Request(endPoints.upload , { method: 'POST', body: JSON.stringify(this.uploadMetadata) }) //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().then(data => { let { result: { id: id } } = data this.uploadId = id return data }) } async send(){ console.log(`Sending with id ${this.uploadId}`) let formData = new FormData(); for (let file of this.fileObjects) { console.log(file) formData.append('upload[]', file) } let req = new Request(endPoints.upload + this.uploadId, { method: 'PUT', body: formData }) let res = await fetch(req) .catch((e) => {console.error(e)}) if (!res.ok){ throw(`${res.status}: ` + (await res.json()).error) } } } export default { endpoint: apiEndpoint, endPoints: endPoints, Upload: Upload }