-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathHttpStorageClient.js
51 lines (48 loc) · 1.15 KB
/
HttpStorageClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* global FormData */
import { sendRequest, forEach } from 'substance'
export default class HttpStorageClient {
constructor (apiUrl) {
this.apiUrl = apiUrl
}
/*
@returns a Promise for a raw archive, i.e. the data for a DocumentArchive.
*/
read (archiveId, cb) {
let url = this.apiUrl
if (archiveId) {
url = url + '/' + archiveId
}
return sendRequest({
method: 'GET',
url
}).then(response => {
cb(null, JSON.parse(response))
}).catch(err => {
cb(err)
})
}
write (archiveId, data, cb) {
let form = new FormData()
forEach(data.resources, (record, filePath) => {
if (record.encoding === 'blob') {
// removing the blob from the record and submitting it as extra part
form.append(record.id, record.data, filePath)
delete record.data
}
})
form.append('_archive', JSON.stringify(data))
let url = this.apiUrl
if (archiveId) {
url = url + '/' + archiveId
}
return sendRequest({
method: 'PUT',
url,
data: form
}).then(response => {
cb(null, response)
}).catch(err => {
cb(err)
})
}
}