2018-03-25 18:03:08 +00:00
|
|
|
// License: LGPL-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
const flyd = require('flyd')
|
|
|
|
const R = require('ramda')
|
|
|
|
|
|
|
|
// TODO make this use flyd-ajax
|
|
|
|
// Using the bare-bones XMLHttpRequest API so we can post form data and upload the image
|
|
|
|
// Returns a flyd stream
|
|
|
|
|
|
|
|
module.exports = R.curryN(2, (url, object) => {
|
|
|
|
var stream = flyd.stream()
|
|
|
|
var req = new XMLHttpRequest()
|
|
|
|
var formData = new FormData()
|
|
|
|
R.mapObjIndexed((val, key) => {
|
|
|
|
if(val.constructor === Object) val = JSON.stringify(val)
|
|
|
|
formData.append(key, val)
|
|
|
|
}, object)
|
|
|
|
req.open("POST", url)
|
|
|
|
// req.setRequestHeader('X-CSRF-Token', window._csrf)
|
|
|
|
req.send(formData)
|
|
|
|
req.onload = ev => {
|
|
|
|
var body = {}
|
|
|
|
try { body = JSON.parse(req.response) } catch(e) { }
|
|
|
|
stream( {status: req.status, body: body } )
|
|
|
|
}
|
|
|
|
return stream
|
|
|
|
})
|
|
|
|
|