This is an automated email from the ASF dual-hosted git repository.
glynnbird pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git
The following commit(s) were added to refs/heads/master by this push:
new 5828bba Only return Promise from `headDoc` when no callback is
specified. (#130)
5828bba is described below
commit 5828bbadbc5295acb04a653f92e0f060ff5e0859
Author: Sam Smith <[email protected]>
AuthorDate: Wed Nov 14 07:04:12 2018 +0000
Only return Promise from `headDoc` when no callback is specified. (#130)
---
lib/nano.js | 34 ++++++++++++++++++++--------------
tests/unit/document/head.js | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 14 deletions(-)
diff --git a/lib/nano.js b/lib/nano.js
index 572c9d9..2fa4408 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -504,25 +504,31 @@ module.exports = exports = function dbScope (cfg) {
//
http://docs.couchdb.org/en/latest/api/document/common.html#head--db-docid
function headDoc (docName, callback) {
- // this function doesn't pass on the Promise from relax because it needs
- // to return the headers when resolving the Promise
- return new Promise(function (resolve, reject) {
+ if (callback) {
relax({
db: dbName,
doc: docName,
method: 'HEAD',
qs: {}
- }, function (err, body, headers) {
- if (callback) {
- callback(err, body, headers)
- }
- if (err) {
- reject(err)
- } else {
- resolve(headers)
- }
- })
- })
+ }, callback);
+ } else {
+ // this function doesn't pass on the Promise from relax because it
needs
+ // to return the headers when resolving the Promise
+ return new Promise(function (resolve, reject) {
+ relax({
+ db: dbName,
+ doc: docName,
+ method: 'HEAD',
+ qs: {}
+ }, function (err, body, headers) {
+ if (err) {
+ reject(err);
+ } else {
+ resolve(headers);
+ }
+ });
+ });
+ }
}
//
http://docs.couchdb.org/en/latest/api/document/common.html#copy--db-docid
diff --git a/tests/unit/document/head.js b/tests/unit/document/head.js
new file mode 100644
index 0000000..9108b74
--- /dev/null
+++ b/tests/unit/document/head.js
@@ -0,0 +1,36 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy
of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
under
+// the License.
+
+'use strict';
+
+const helpers = require('../../helpers/unit');
+const test = require('tape');
+const debug = require('debug')('nano/tests/unit/shared/error');
+
+const cli = helpers.mockClientDb(debug);
+const db = cli.use('foo');
+
+test('it should return a promise when no callback is specified', function
(assert) {
+ var p = db.head('doc');
+ p.then((headers) => {
+ assert.equal(headers.statusCode, 200);
+ assert.end();
+ });
+});
+
+test('it should not return a promise when a callback is specified', function
(assert) {
+ var p = db.head('doc', function(err, body, headers) {
+ assert.equal(headers.statusCode, 200);
+ assert.end();
+ });
+ assert.equal(typeof p, 'undefined');
+});