You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bit4sat/web/src/api.js

81 lines
1.6 KiB
JavaScript

import 'babel-polyfill';
const endPoints = {
get upload () {
return '/api/upload'
}
}
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) {
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)
}
return res.json()
}
}
export default {
endPoints: endPoints,
Upload: Upload,
}