-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (29 loc) · 904 Bytes
/
index.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
var request = require('request')
module.exports = function (couch, id, cb) {
// we need the prev doc. hack attack
request({url: couch + '/' + id + '?revs=true&open_revs=all'}, function (err, resp, body) {
if (err) return cb(err)
// total hacky
// the prev url returns a mess chunked mime type sections
var _json
try {
_json = JSON.parse(body.split('\n')[3])
} catch (e) {
return cb(new Error('could not parse revisions of document'))
}
if (!_json._revisions || !_json._revisions.start || !_json._revisions.ids) {
return cb(new Error('strange response from couch'))
}
var _rev = (_json._revisions.start - 1) + '-' + _json._revisions.ids[1]
var _opts = {
url: couch + '/' + id,
qs: {
rev: _rev
},
json: true
}
request(_opts, function (err, resp, _prev_doc) {
cb(err, _prev_doc)
})
})
}