Permalink
Browse files

Simplify entry point to `clerk(uri)`

  • Loading branch information...
1 parent faf6f64 commit 7f161aeea7ecc6bebaa28a9d3d7a877cdd94754a @mikepb committed Apr 1, 2012
Showing with 99 additions and 68 deletions.
  1. +2 −1 Makefile
  2. +40 −27 clerk.js
  3. +1 −1 clerk.min.js
  4. +26 −19 lib/clerk.js
  5. +5 −3 lib/client.js
  6. +4 −2 lib/database.js
  7. +3 −1 lib/head.js
  8. +17 −13 lib/index.js
  9. +1 −1 lib/tail.js
View
@@ -23,7 +23,8 @@ build:
cat $(SRCJS) | sed -E \
-e '/\/\*!/,/\*\//d' \
-e "s/'(GET|HEAD|POST|PUT|DELETE|COPY)'/\1/g" \
- -e 's/exports\.(Client|Database|extend|request|_request|_response|asString|isString|isObject|isFunction|unpackArgs)( =( )(function))?/\4\3\1/g' \
+ -e 's/exports\.(Clerk|Client|Database|extend|request|_request|_response|asString|isString|isObject|isFunction|unpackArgs)( =( )(function))?/\4\3\1/g' \
+ -e 's/exports\.(Clerk|Client|Database) = (Clerk|Client|Database);//g' \
-e 's/Array.prototype.slice/__slice/g' \
| cat $(HEADJS) - $(TAILJS) > $(OUTJS)
uglifyjs $(OUTJS) > $(MINJS)
View
@@ -5,12 +5,14 @@
*/
;(function(
- exports,
+ module,
encodeURI,
encodeURIComponent,
decodeURIComponent
){
+var exports = module.exports || {};
+
var GET = 'GET'
, HEAD = 'HEAD'
, POST = 'POST'
@@ -20,36 +22,43 @@ var GET = 'GET'
var __slice = Array.prototype.slice;
-exports.version = '0.1.0pre';
-
-/**
- Restore global variable 'clerk' to original value and return the library as an object.
- */
-
-exports.noConflict = function() {
- window.clerk = exports._;
- return exports;
-};
-
/**
Create CouchDB client.
@param {String} uri Fully qualified URI.
*/
-exports.createClient = function(uri) {
- var match = /^(https?:\/\/)(?:([^@:]+):([^@]+)@)?(.*?)\/*$/.exec(uri);
- if (!match) throw new Error('Bad URI: ' + uri);
+module.exports = exports = function(uri) {
+ var client, match;
- var client = new Client(match[1] + match[4]);
- client.auth = {
- user: match[2] && decodeURIComponent(match[2]),
- pass: match[3] && decodeURIComponent(match[3])
- };
+ if (uri) {
+ match = /^(https?:\/\/)(?:([^@:]+):([^@]+)@)?(.*?)\/*$/.exec(uri);
+ if (match) uri = uri[1] + uri[4];
+ }
+
+ client = new Client(uri);
+
+ if (uri) {
+ client.auth = {
+ user: match[2] && decodeURIComponent(match[2]),
+ pass: match[3] && decodeURIComponent(match[3])
+ };
+ }
return client;
};
+exports.version = '0.1.0pre';
+
+/**
+ Restore global variable 'clerk' to original value and return the library as an object.
+ */
+
+exports.noConflict = function() {
+ window.clerk = exports._;
+ return exports;
+};
+
/**
Service request and parse JSON response.
@@ -200,7 +209,7 @@ function unpackArgs(args, objectBeforeQuery) {
*/
function Client(uri) {
- this.uri = uri;
+ this.uri = uri || 'http://127.0.0.1:5984';
};
Client.prototype = {
@@ -267,7 +276,7 @@ Client.prototype = {
uuids: function(count /* [query], [callback] */) {
var args = unpackArgs(arguments);
if (count > 1) args.q.count = count;
- this.request(GET, '_uuids', args.q, args.fn);
+ this.request(GET, '_uuids', args.q, args.f);
},
/**
@@ -439,6 +448,8 @@ Client.prototype = {
};
+Client = Client;
+
/**
Methods for CouchDB database.
@@ -572,7 +583,7 @@ Database.prototype = {
@param {ClientResponse} [callback.response] ClientResponse object.
*/
- head: function(id /* query, callback */) {
+ head: function(id /* [query], [callback] */) {
var args = unpackArgs(arguments);
this.request(HEAD, encodeURIComponent(id), args.q, function(err, body, status, headers, xhr) {
args.f && args.f(err, err ? body : {
@@ -597,7 +608,7 @@ Database.prototype = {
@param {ClientResponse} [callback.response] ClientResponse object.
*/
- put: function(/* [id], [rev], [doc], [query] callback */) {
+ put: function(/* [id], [rev], [doc], [query], [callback] */) {
var args = unpackDocArgs(arguments, 1);
this.request(PUT, args.id, args.q, args.doc, args.f);
},
@@ -936,7 +947,7 @@ Database.prototype = {
purge: function(revs /* [query], [callback] */) {
var args = unpackArgs(arguments);
- this.request(POST, '_purge', args.q, revs, args.fn);
+ this.request(POST, '_purge', args.q, revs, args.f);
},
/**
@@ -981,7 +992,7 @@ Database.prototype = {
};
function unpackDocArgs(args, withDoc) {
- // [id, rev], [doc], [query], callback
+ // [id, rev], [doc], [query], [callback]
args = __slice.call(args);
return {
@@ -1008,13 +1019,15 @@ function parseViewOptions(q, body) {
return body;
}
+Database = Database;
+
if (typeof window != 'undefined') {
exports._ = window.clerk;
window.clerk = exports;
}
})(
- typeof exports != 'undefined' ? exports : {},
+ typeof module != 'undefined' ? module : {},
encodeURI,
encodeURIComponent,
decodeURIComponent
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -16,36 +16,43 @@
limitations under the License.
*/
-exports.version = '0.1.0pre';
-
-/**
- Restore global variable 'clerk' to original value and return the library as an object.
- */
-
-exports.noConflict = function() {
- window.clerk = exports._;
- return exports;
-};
-
/**
Create CouchDB client.
@param {String} uri Fully qualified URI.
*/
-exports.createClient = function(uri) {
- var match = /^(https?:\/\/)(?:([^@:]+):([^@]+)@)?(.*?)\/*$/.exec(uri);
- if (!match) throw new Error('Bad URI: ' + uri);
+module.exports = exports = function(uri) {
+ var client, match;
- var client = new exports.Client(match[1] + match[4]);
- client.auth = {
- user: match[2] && decodeURIComponent(match[2]),
- pass: match[3] && decodeURIComponent(match[3])
- };
+ if (uri) {
+ match = /^(https?:\/\/)(?:([^@:]+):([^@]+)@)?(.*?)\/*$/.exec(uri);
+ if (match) uri = uri[1] + uri[4];
+ }
+
+ client = new exports.Client(uri);
+
+ if (uri) {
+ client.auth = {
+ user: match[2] && decodeURIComponent(match[2]),
+ pass: match[3] && decodeURIComponent(match[3])
+ };
+ }
return client;
};
+exports.version = '0.1.0pre';
+
+/**
+ Restore global variable 'clerk' to original value and return the library as an object.
+ */
+
+exports.noConflict = function() {
+ window.clerk = exports._;
+ return exports;
+};
+
/**
Service request and parse JSON response.
View
@@ -28,11 +28,11 @@
@see [CouchDB Wiki](http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference)
*/
-exports.Client = function(uri) {
- this.uri = uri;
+function Client(uri) {
+ this.uri = uri || 'http://127.0.0.1:5984';
};
-exports.Client.prototype = {
+Client.prototype = {
/**
Service request and parse JSON response.
@@ -267,3 +267,5 @@ exports.Client.prototype = {
}
};
+
+exports.Client = Client;
View
@@ -24,13 +24,13 @@
@param {String} options Database name.
*/
-exports.Database = function(client, name) {
+function Database(client, name) {
this.client = client;
this.name = name;
this.uri = client.uri + '/' + encodeURIComponent(name);
};
-exports.Database.prototype = {
+Database.prototype = {
/**
Service request and parse JSON response.
@@ -585,3 +585,5 @@ function parseViewOptions(q, body) {
}
return body;
}
+
+exports.Database = Database;
View
@@ -5,12 +5,14 @@
*/
;(function(
- exports,
+ module,
encodeURI,
encodeURIComponent,
decodeURIComponent
){
+var exports = module.exports || {};
+
var GET = 'GET'
, HEAD = 'HEAD'
, POST = 'POST'
View
@@ -21,25 +21,20 @@ var crypto = require('crypto')
, request = require('request')
, follow = require('follow');
-var client = require('./client')
+var clerk = require('./clerk')
+ , client = require('./client')
, database = require('./database');
-// import components
-exports = module.exports = require('./clerk');
-exports.extend(exports, client, database);
-exports.extend(client, exports);
-exports.extend(database, exports);
-delete exports.noConflict;
-
-// patch prototypes
-client.Client.prototype.request = database.Database.prototype.request = exports.request;
+var extend = clerk.extend;
-// request can handle auth in URI
-exports.createClient = function(uri) {
- uri = uri.replace(/\/*$/g, '');
+module.exports = exports = function(uri) {
+ uri = uri && uri.replace(/\/*$/g, '');
return new exports.Client(uri);
};
+// import modules
+extend(exports, clerk, client, database);
+
// request-based requests
exports._request = function(method, uri, query, body, headers, auth, callback) {
request({
@@ -56,6 +51,15 @@ exports._request = function(method, uri, query, body, headers, auth, callback) {
});
};
+// patch modules
+extend(clerk, exports);
+extend(client, exports);
+extend(database, exports);
+delete exports.noConflict;
+
+// patch prototypes
+client.Client.prototype.request = database.Database.prototype.request = clerk.request;
+
/**
Generate UUIDs.
View
@@ -5,7 +5,7 @@ if (typeof window != 'undefined') {
}
})(
- typeof exports != 'undefined' ? exports : {},
+ typeof module != 'undefined' ? module : {},
encodeURI,
encodeURIComponent,
decodeURIComponent

0 comments on commit 7f161ae

Please sign in to comment.