Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from DamonOehlman/master

Various Updates
  • Loading branch information...
commit 62ff49aff8a2ca540e0b5a1e6307a624d20539c4 2 parents d2c5982 + 5cce412
@landeiro authored
Showing with 60 additions and 52 deletions.
  1. +60 −52 lib/PJsonCouch.js
View
112 lib/PJsonCouch.js
@@ -63,7 +63,7 @@ var Utils = {
smartQueryString: function (query) {
if (query === "") return "";
- var qstring = ""
+ var qstring = "";
for (key in query) {
if (qstring !== "") {
qstring += "&";
@@ -126,7 +126,7 @@ var Utils = {
try {
result = JSON.parse(result);
} catch (err) {
- result = Utils.setJSONError(option, result)
+ result = Utils.setJSONError(option, result);
};
if (typeof result !== "object") result = Utils.setJSONError(option, result);
@@ -156,6 +156,10 @@ var Utils = {
request: function (reqOptions, data, reqConfig, callBackFunction) {
var result = "";
data = data || "";
+
+ // add the content length header
+ reqOptions.headers['Content-Length'] = unescape(encodeURIComponent(data)).length;
+
var req = http.request(reqOptions, function (res) {
(reqOptions.resultEncoding) ? res.setEncoding(reqOptions.resultEncoding) : res.setEncoding('utf8');
res.on('data', function (chunk) {
@@ -189,26 +193,32 @@ var Utils = {
}
req.end();
}
-}
+};
var PJsonCouch = function (options) {
+
+ // initialise options defaults
+ options.headers = options.headers || {};
+ options.path = options.path || '';
if (this instanceof PJsonCouch) {
this.setDB = function (dbObj) {
myDB = dbObj.db;
- }
+ };
+
this.getDB = function () {
return {
db: myDB
};
- }
+ };
+
this.getSession = function () {
return {
AuthSession: mySession
};
- }
+ };
var myDB = options.db;
@@ -219,13 +229,13 @@ var PJsonCouch = function (options) {
debugWithHeaders: false,
debugOnError: false,
throwExceptionsOnError: false
- }
+ };
this.setDebugConfig = function (argCfg) {
for (var argKey in argCfg) {
if (config.hasOwnProperty(argKey)) config[argKey] = argCfg[argKey];
}
- }
+ };
this.getDebugConfig = function () {
return config;
@@ -234,7 +244,7 @@ var PJsonCouch = function (options) {
this.login = function (credentials, callBackFunction) {
var postOptions = this.buildRequest("POST", {
serverAction: "_session"
- }, options);
+ });
postOptions.headers = {};
postOptions.headers["Content-Type"] = "application/x-www-form-urlencoded";
// postOptions.headers["Referer"] = "http://127.0.0.1";
@@ -249,13 +259,12 @@ var PJsonCouch = function (options) {
callBackFunction(res);
}
});
-
- }
+ };
this.logout = function (callBackFunction) {
var reqOptions = this.buildRequest("DELETE", {
serverAction: "_session"
- }, options);
+ });
Utils.request(reqOptions, "", config, function (logoutResult) {
if (logoutResult.ok) {
mySession = "";
@@ -264,11 +273,10 @@ var PJsonCouch = function (options) {
callBackFunction(logoutResult);
}
});
-
- }
+ };
this.createDB = function (url, callBackFunction) {
- var reqOptions = this.buildRequest("PUT", url, options);
+ var reqOptions = this.buildRequest("PUT", url);
Utils.request(reqOptions, "", config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -277,7 +285,7 @@ var PJsonCouch = function (options) {
};
this.deleteDB = function (url, callBackFunction) {
- var reqOptions = this.buildRequest("DELETE", url, options);
+ var reqOptions = this.buildRequest("DELETE", url);
Utils.request(reqOptions, "", config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -287,7 +295,7 @@ var PJsonCouch = function (options) {
this.queryDB = function (url, callBackFunction) {
var method = (url.request) ? methodsWithRequest[url.action] : methods[url.action];
- var reqOptions = this.buildRequest(method, url, options);
+ var reqOptions = this.buildRequest(method, url);
Utils.request(reqOptions, JSON.stringify(url.request), config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -296,13 +304,16 @@ var PJsonCouch = function (options) {
};
this.saveDoc = function (url, callBackFunction) {
+ var putOptions;
+
if (typeof url.doc === "string") {
url.doc = JSON.parse(url.doc);
}
if (url.local) {
url.doc._id = url.doc._id.replace(/_local\//, "");
url.action = encodeURIComponent(url.doc._id);
- var putOptions = this.buildRequest("PUT", url, options);
+
+ putOptions = this.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -310,7 +321,8 @@ var PJsonCouch = function (options) {
});
} else if (url.doc._id) {
url.action = encodeURIComponent(url.doc._id);
- var putOptions = this.buildRequest("PUT", url, options);
+
+ putOptions = this.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -322,7 +334,7 @@ var PJsonCouch = function (options) {
action: "_uuids"
}, function (u) {
url.action = encodeURIComponent(u.uuids[0]);
- var putOptions = that.buildRequest("PUT", url, options);
+ var putOptions = that.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -345,7 +357,7 @@ var PJsonCouch = function (options) {
var content = url.attachment.content;
var encoding = url.attachment.content_encoding;
url.attachment = url.attachment.file;
- var putOptions = this.buildRequest("PUT", url, options);
+ var putOptions = this.buildRequest("PUT", url);
putOptions.requestEncoding = encoding;
putOptions.headers["Content-Type"] = type;
if (length) {
@@ -360,7 +372,7 @@ var PJsonCouch = function (options) {
}
});
- }
+ };
this.deleteDocAttachment = function (url, callBackFunction) {
if (url.rev) {
@@ -371,14 +383,14 @@ var PJsonCouch = function (options) {
url.id = encodeURIComponent(url.id);
}
url.attachment = url.attachment.file;
- var deleteOptions = this.buildRequest("DELETE", url, options);
+ var deleteOptions = this.buildRequest("DELETE", url);
Utils.request(deleteOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
}
});
- }
+ };
this.deleteDoc = function (url, callBackFunction) {
if (url.local) {
@@ -388,7 +400,7 @@ var PJsonCouch = function (options) {
} else {
url.doc._deleted = true;
}
- var postdeleteOptions = this.buildRequest((url.local) ? "DELETE" : "POST", url, options);
+ var postdeleteOptions = this.buildRequest((url.local) ? "DELETE" : "POST", url);
Utils.request(postdeleteOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -400,6 +412,7 @@ var PJsonCouch = function (options) {
if (url.id) {
url.id = encodeURIComponent(url.id);
}
+
var getOptions = this.buildRequest("GET", url, options);
Utils.request(getOptions, "", config, function (doc) {
if (callBackFunction) {
@@ -414,7 +427,7 @@ var PJsonCouch = function (options) {
}
var encoding = url.attachment.content_encoding;
url.attachment = url.attachment.file;
- var getOptions = this.buildRequest("GET", url, options);
+ var getOptions = this.buildRequest("GET", url);
getOptions.resultEncoding = encoding;
Utils.request(getOptions, "", config, function (doc) {
if (callBackFunction) callBackFunction(doc);
@@ -422,7 +435,7 @@ var PJsonCouch = function (options) {
};
this.infoDoc = function (url, callBackFunction) {
- var headOptions = this.buildRequest("HEAD", url, options);
+ var headOptions = this.buildRequest("HEAD", url);
Utils.request(headOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -444,7 +457,7 @@ var PJsonCouch = function (options) {
url.args.rev = url.source.rev;
}
}
- var copyOptions = this.buildRequest("COPY", url, options);
+ var copyOptions = this.buildRequest("COPY", url);
copyOptions.headers.destination = url.destination.id + ((url.destination.rev) ? "?rev=" + url.destination.rev : "");
Utils.request(copyOptions, "", config, function (result) {
if (callBackFunction) {
@@ -456,7 +469,7 @@ var PJsonCouch = function (options) {
this.saveBulkDocs = function (url, callBackFunction) {
var result = "";
url.action = '_bulk_docs';
- var postOptions = this.buildRequest("POST", url, options);
+ var postOptions = this.buildRequest("POST", url);
var extraParams = url.args;
var docs = {};
@@ -483,7 +496,7 @@ var PJsonCouch = function (options) {
data[idx] = url[idx];
}
}
- var serverOptions = this.buildRequest(methods[url.action], url, options);
+ var serverOptions = this.buildRequest(methods[url.action], url);
if (serverOptions.method !== "PUT" && serverOptions.method !== "POST") {
data = "";
}
@@ -497,7 +510,7 @@ var PJsonCouch = function (options) {
this.infoDesign = function (url, callBackFunction) {
url.action = "_info";
- var designOptions = this.buildRequest(methods[url.action], url, options);
+ var designOptions = this.buildRequest(methods[url.action], url);
Utils.request(designOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -507,7 +520,7 @@ var PJsonCouch = function (options) {
this.queryDesign = function (url, callBackFunction) {
url.stringifyRequest = true;
- var designOptions = this.buildRequest((url.request) ? "POST" : methods[url.action], url, options);
+ var designOptions = this.buildRequest((url.request) ? "POST" : methods[url.action], url);
Utils.request(designOptions, JSON.stringify(url.request), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -515,36 +528,31 @@ var PJsonCouch = function (options) {
});
};
- this.buildRequest = function (method, url, options) {
+ this.buildRequest = function (method, url) {
if (!url.db && !url.serverAction) url.db = myDB;
- var qsparams = Utils.smartQueryString(url.args);
- options.path = Utils.decodeGetData(url);
- options.method = method;
- options.path = (qsparams === "") ? options.path : options.path + "?" + qsparams;
- if (options.headers === undefined) {
- options.headers = {};
- }
+
+ var qsparams = Utils.smartQueryString(url.args),
+ reqOpts = Utils.clone(options);
+
+ reqOpts.path += Utils.decodeGetData(url);
+ reqOpts.method = method;
+ reqOpts.path = (qsparams === "") ? reqOpts.path : reqOpts.path + "?" + qsparams;
+
if (method !== "GET") {
- options.headers["Content-Type"] = "application/json";
+ reqOpts.headers["Content-Type"] = "application/json";
}
-
if (mySession !== "") {
-
- options.headers["Cookie"] = "AuthSession=" + mySession;
+ reqOpts.headers["Cookie"] = "AuthSession=" + mySession;
}
- options.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>"
+ reqOpts.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>";
- return Utils.clone(options);
- }
+ return reqOpts;
+ };
} else {
return new PJsonCouch(options);
}
-}
-
-
-
-
+};
module.exports = PJsonCouch;
Please sign in to comment.
Something went wrong with that request. Please try again.