-
Notifications
You must be signed in to change notification settings - Fork 2
/
supercouch.min.js
9 lines (9 loc) · 4.42 KB
/
supercouch.min.js
1
2
3
4
5
6
7
8
9
/*!
* SuperCouch
* Copyright (c) 2012 Jake Luer <[email protected]>
* MIT Licensed
*
* @website http://supercou.ch
* @issues https://github.com/qualiancy/supercouch/issues
*/
var supercouch=function(e){var t={};return t.exports=function(e){function r(e){return"[object Function]"===n.call(e)}function i(e){return e===Object(e)}function u(e,t){if(e&&t)for(var n in t)e[n]=t[n];return e}function a(e,t){if(e&&t)for(var n in t)"undefined"==typeof e[n]&&(e[n]=t[n]);return e}function f(e){e=e||{},this.reqOpts=e}function l(){var e=this.reqOpts;e.path.join("/").split("/").filter(function(e){return!e.trim.length});var t=e.path.length===1&&e.path[0]=="/"?"":e.path.join("/");return e.base+"/"+t}function c(e,t){"object"==typeof e&&(t=e,e="http://localhost:5984"),t=t||{},this.reqOpts={base:e||"http://localhost:5984"}}function h(e){e=e||{},this.reqOpts=e}function p(e){e=e||{},this.message=e.reason||e.message,this.code="E"+(e.error.toUpperCase()||"COUCHERR"),this._raw=e,Error.captureStackTrace&&Error.captureStackTrace(this,arguments.callee)}var t=function(e){return new c(e)};t.version="0.2.4";var n=Object.prototype.toString,s=Array.isArray||function(e){return"[object Array]"===n.call(e)},o=Array.prototype.indexOf||function(e,t){for(var n=t||0,r=this.length;n<r;n++)if(this[n]===e)return n;return-1};return f.prototype.send=function(e,t){var n=this.reqOpts.body||(this.reqOpts.body={});return i(e)?this.reqOpts.body=u(e,n):this.reqOpts.body[e]=t,this},f.prototype.query=function(e,t){var n=this.reqOpts.qs||(this.reqOpts.qs={});return i(e)?this.reqOpts.qs=u(e,n):this.reqOpts.qs[e]=t,this},f.prototype.end=function(t){var n=this,i=this.reqOpts,s=l.call(this),o=i.method.toUpperCase(),u;if("GET"===o)u=e.get(s);else if("POST"===o)u=e.post(s);else if("PUT"===o)u=e.put(s);else if("DELETE"===o)u=e.del(s);else{if("HEAD"!==o)return t(new Error("Unsuppored request method"));u=e.head(s)}i.qs&&u.query(i.qs);if(i.body){var a=i.body.map,f=i.body.reduce;r(a)&&(i.body.map=""+a),r(f)&&(i.body.reduce=""+f),u.send(i.body)}u.end(function(n){var s,u=null;if(o==="HEAD"){var a=r(i.validate)?i.validate(n):n.status!==404;return t(null,a)}try{s=JSON.parse(n.text)}catch(f){u=f}!u&&s.error&&(u=new p(s));if(u)return t(u);var a=r(i.validate)?i.validate(s):s;t(null,a)})},c.prototype.request=function(e,t,n){var i=u({method:e,path:[t]},this.reqOpts),s=new f(i);return r(n)&&s.end(n),s},c.prototype.action=function(e,t,n){var i={"all dbs":"GET","active tasks":"GET",uuids:"GET",stats:"GET",log:"GET",replicate:"POST",restart:"POST"};if(!i[e])throw new Error("Couch action `"+e+"` not valid");r(t)&&(n=t,data={});var s=i[e],o="_"+e.split(" ").join("_"),a=u({method:s,path:[o],body:t},this.reqOpts),l=new f(a);return r(n)&&l.end(n),l},c.prototype.dbAdd=function(e,t){var n=u({method:"PUT",path:[e]},this.reqOpts),i=new f(n);return r(t)&&i.end(t),i},c.prototype.dbInfo=function(e,t){var n=u({method:"GET",path:[e]},this.reqOpts),i=new f(n);return r(t)&&i.end(t),i},c.prototype.dbExists=function(e,t){var n=u({method:"GET",path:["_all_dbs"],validate:function(t){return o.call(t,e)>-1}},this.reqOpts),i=new f(n);return r(t)&&i.end(t),i},c.prototype.dbDel=function(e,t){var n=u({method:"DELETE",path:[e]},this.reqOpts),i=new f(n);return r(t)&&i.end(t),i},c.prototype.db=function(e){var t=u({path:[e]},this.reqOpts),n=new h(t);return n},h.prototype.action=function(e,t,n){var i={changes:"GET",compact:"POST","view cleanup":"POST","temp view":"POST","ensure full commit":"POST",purge:"POST"};if(!i[e])throw new Error("Couch action `"+e+"` not valid");r(t)&&(n=t,data={});var s=i[e],o="_"+e.split(" ").join("_"),a=u({method:s,body:t},this.reqOpts);a.path.push(o);var l=new f(a);return r(n)&&l.end(n),l},h.prototype.insert=function(e,t){r(e)&&(t=e,e={});var n=u({method:e._id?"PUT":"POST",body:e},this.reqOpts);e._id&&n.path.push(e._id);var i=new f(n);return r(t)&&i.end(t),i},h.prototype.get=function(e,t,n){r(t)&&(n=t,t=null);var i=u({method:"GET"},this.reqOpts);i.path.push(e);var s=new f(i);return t&&s.query("rev",t),r(n)&&s.end(n),s},h.prototype.update=function(e,t,n,s){i(t)&&(s=n,n=t,t=null);var o=u({method:"PUT",body:n||{}},this.reqOpts);o.path.push(e),o.body._id=e,t&&(o.body._rev=t);var a=new f(o);return r(s)&&a.end(s),a},h.prototype.remove=function(e,t,n){r(t)&&(n=t,t=null);var i=u({method:"DELETE"},this.reqOpts);i.path.push(e);var s=new f(i);return t&&s.query("rev",t),r(n)&&s.end(n),s},p.prototype=Object.create(Error.prototype),p.prototype.name="CouchError",p.prototype.constructor=p,t.CouchError=p,t},t.exports(e)}(superagent)