Comparing version 0.0.6 to 0.0.7
101
cot.js
/* | ||
Copyright (c) 2013 Will Conant, http://willconant.com/ | ||
Copyright (c) 2013, 2017 Will Conant, http://conant.xyz/ | ||
@@ -26,3 +26,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
var querystring = require('querystring'); | ||
var Q = require('q'); | ||
@@ -54,58 +53,56 @@ module.exports = Cot; | ||
jsonRequest: function(method, path, body) { | ||
var deferred = Q.defer(); | ||
var headers = {}; | ||
headers['accept'] = 'application/json'; | ||
headers['host'] = this.hostHeader; | ||
if (body) { | ||
headers['content-type'] = 'application/json'; | ||
} | ||
var request = this.http.request({ | ||
hostname: this.hostname, | ||
port: this.port, | ||
auth: this.auth, | ||
path: path, | ||
method: method, | ||
headers: headers | ||
}); | ||
request.on('error', deferred.reject.bind(deferred)); | ||
request.on('response', function(response) { | ||
response.setEncoding('utf8'); | ||
var buffer = ''; | ||
response.on('data', function(data) { | ||
buffer += data; | ||
return new Promise(function(resolve, reject) { | ||
var headers = {}; | ||
headers['accept'] = 'application/json'; | ||
headers['host'] = this.hostHeader; | ||
if (body) { | ||
headers['content-type'] = 'application/json'; | ||
} | ||
var request = this.http.request({ | ||
hostname: this.hostname, | ||
port: this.port, | ||
auth: this.auth, | ||
path: path, | ||
method: method, | ||
headers: headers | ||
}); | ||
response.on('error', deferred.reject.bind(deferred)); | ||
request.on('error', reject); | ||
response.on('end', function() { | ||
var myResponse = { | ||
statusCode: response.statusCode, | ||
unparsedBody: buffer | ||
}; | ||
request.on('response', function(response) { | ||
response.setEncoding('utf8'); | ||
if (response.headers['content-type'] === 'application/json') { | ||
try { | ||
myResponse.body = JSON.parse(buffer); | ||
} catch (err) { | ||
deferred.reject(err); | ||
return; | ||
var buffer = ''; | ||
response.on('data', function(data) { | ||
buffer += data; | ||
}); | ||
response.on('error', reject); | ||
response.on('end', function() { | ||
var myResponse = { | ||
statusCode: response.statusCode, | ||
unparsedBody: buffer | ||
}; | ||
if (response.headers['content-type'] === 'application/json') { | ||
try { | ||
myResponse.body = JSON.parse(buffer); | ||
} catch (err) { | ||
reject(err); | ||
return; | ||
} | ||
} | ||
} | ||
deferred.resolve(myResponse); | ||
resolve(myResponse); | ||
}); | ||
}); | ||
}); | ||
if (body) { | ||
request.end(JSON.stringify(body)); | ||
} else { | ||
request.end(); | ||
} | ||
return deferred.promise; | ||
if (body) { | ||
request.end(JSON.stringify(body)); | ||
} else { | ||
request.end(); | ||
} | ||
}.bind(this)) | ||
}, | ||
@@ -112,0 +109,0 @@ |
@@ -5,11 +5,9 @@ { | ||
"keywords": ["couch", "couchdb", "promise", "promises"], | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"main": "cot", | ||
"dependencies": { | ||
"q": "0.9.x" | ||
}, | ||
"dependencies": {}, | ||
"author": { | ||
"name": "Will Conant", | ||
"email": "will.conant@gmail.com", | ||
"url": "http://willconant.com" | ||
"url": "http://conant.xyz" | ||
}, | ||
@@ -16,0 +14,0 @@ "repository": { |
@@ -5,3 +5,3 @@ # What is Cot? # | ||
- It produces promises using the excellent Q module. | ||
- It provides a promise-based API. | ||
- It has clear method names that map almost directly to CouchDB's HTTP API. | ||
@@ -42,10 +42,12 @@ - It takes advantage of redundancies in the CouchDB API to map responses back to promises. | ||
}) | ||
.then(function(response) { | ||
// let's print out the rev because that would be cool | ||
console.log(response.rev); | ||
}) | ||
.fail(function(err) { | ||
// if anything goes wrong, we'll end up here | ||
console.log('errors are lame in examples'); | ||
}); | ||
.then( | ||
function(response) { | ||
// let's print out the rev because that would be cool | ||
console.log(response.rev); | ||
}, | ||
function(err) { | ||
// if anything goes wrong, we'll end up here | ||
console.log('errors are lame in examples'); | ||
} | ||
); | ||
@@ -52,0 +54,0 @@ |
@@ -38,3 +38,9 @@ var chai = require('chai'); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -61,3 +67,9 @@ | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -73,3 +85,9 @@ }); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -87,3 +105,9 @@ }); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -102,3 +126,9 @@ }); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -114,9 +144,9 @@ }); | ||
}) | ||
.then(function(response) { | ||
done(new Error('should not have resolved')); | ||
}) | ||
.fail(function() { | ||
done(); | ||
}) | ||
.done(); | ||
.then( | ||
function(response) { | ||
done(new Error('should not have resolved')); | ||
}, | ||
function(err) { | ||
done(); | ||
}) | ||
}); | ||
@@ -134,3 +164,7 @@ }); | ||
}) | ||
.delay(100) | ||
.then(function() { | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(resolve, 100); | ||
}); | ||
}) | ||
.then(function(response) { | ||
@@ -142,3 +176,9 @@ return db.get(doc._id); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -153,5 +193,11 @@ }); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -31,3 +31,9 @@ var chai = require('chai'); | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
@@ -45,5 +51,11 @@ | ||
}) | ||
.nodeify(done); | ||
.then( | ||
function(ret) { | ||
done(null, ret); | ||
}, | ||
function(err) { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
21670
0
517
247