Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored options passing for the `buildRequest` function, tweaked c…

…ode to fix JSONlint warnings
  • Loading branch information...
commit 59b627b9e202d8afd769878c9aeba7e4b1ee898d 1 parent e4f942c
@DamonOehlman DamonOehlman authored
Showing with 41 additions and 41 deletions.
  1. +41 −41 lib/PJsonCouch.js
View
82 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);
@@ -189,7 +189,7 @@ var Utils = {
}
req.end();
}
-}
+};
var PJsonCouch = function (options) {
@@ -198,17 +198,19 @@ var PJsonCouch = function (options) {
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 +221,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 +236,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 +251,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 +265,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 +277,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 +287,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 +296,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 +313,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 +326,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 +349,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 +364,7 @@ var PJsonCouch = function (options) {
}
});
- }
+ };
this.deleteDocAttachment = function (url, callBackFunction) {
if (url.rev) {
@@ -371,14 +375,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 +392,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,7 +404,7 @@ var PJsonCouch = function (options) {
if (url.id) {
url.id = encodeURIComponent(url.id);
}
- var getOptions = this.buildRequest("GET", encodeURIComponent(url), options);
+ var getOptions = this.buildRequest("GET", encodeURIComponent(url));
Utils.request(getOptions, "", config, function (doc) {
if (callBackFunction) {
callBackFunction(doc);
@@ -414,7 +418,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 +426,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 +448,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 +460,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 +487,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 +501,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 +511,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,7 +519,7 @@ 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);
@@ -533,17 +537,13 @@ var PJsonCouch = function (options) {
options.headers["Cookie"] = "AuthSession=" + mySession;
}
- options.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>"
+ options.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>";
return Utils.clone(options);
- }
+ };
} else {
return new PJsonCouch(options);
}
-}
-
-
-
-
+};
module.exports = PJsonCouch;
Please sign in to comment.
Something went wrong with that request. Please try again.