From 68ea1d16f9ca9bca4fab386c47035120d06b7e68 Mon Sep 17 00:00:00 2001 From: Hovhannes Babayan Date: Tue, 24 Feb 2015 20:38:09 +0400 Subject: [PATCH] single field can now be passed as string (i.e. '*' is now the same as ['*']) --- src/plugins/copy.js | 2 +- src/plugins/create.js | 2 +- src/plugins/edit.js | 2 +- src/plugins/execute.js | 1 + src/plugins/namedQuery.js | 2 +- src/plugins/request.js | 4 ++++ src/plugins/search.js | 2 +- test/plugins/execute.spec.js | 6 ++++++ test/plugins/request.spec.js | 24 ++++++++++++++++++++++++ 9 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/plugins/copy.js b/src/plugins/copy.js index 4066d1bf..d8f958cb 100644 --- a/src/plugins/copy.js +++ b/src/plugins/copy.js @@ -25,7 +25,7 @@ module.exports = function(Api) { * @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} * @param {String} objID ID of object to copy * @param {Object} updates Which fields to set on copied object. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. - * @param {Object} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. + * @param {String|String[]} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. * @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise */ Api.prototype.copy = function (objCode, objID, updates, fields) { diff --git a/src/plugins/create.js b/src/plugins/create.js index 235c5d29..35009be5 100644 --- a/src/plugins/create.js +++ b/src/plugins/create.js @@ -24,7 +24,7 @@ module.exports = function(Api) { * @memberOf Workfront.Api * @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} * @param {Object} params Values of fields to be set for the new object. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. - * @param {String[]} [fields] Which fields of newly created object to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. + * @param {String|String[]} [fields] Which fields of newly created object to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. * @returns {Promise} A promise which will resolved with the ID and any other specified fields of newly created object */ Api.prototype.create = function (objCode, params, fields) { diff --git a/src/plugins/edit.js b/src/plugins/edit.js index 23953e03..a676ff82 100644 --- a/src/plugins/edit.js +++ b/src/plugins/edit.js @@ -24,7 +24,7 @@ module.exports = function(Api) { * @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} * @param {String} objID ID of object to modify * @param {Object} updates Which fields to set. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. - * @param {Object} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. + * @param {String|String[]} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. * @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise */ Api.prototype.edit = function (objCode, objID, updates, fields) { diff --git a/src/plugins/execute.js b/src/plugins/execute.js index 68076de1..cbfe96c1 100644 --- a/src/plugins/execute.js +++ b/src/plugins/execute.js @@ -34,6 +34,7 @@ module.exports = function(Api) { endPoint += '/' + objID + '/' + action; } else { + actionArgs = actionArgs || {}; actionArgs['action'] = action; } return this.request(endPoint, actionArgs, null, Api.Methods.PUT); diff --git a/src/plugins/namedQuery.js b/src/plugins/namedQuery.js index b0ff1161..b99708ac 100644 --- a/src/plugins/namedQuery.js +++ b/src/plugins/namedQuery.js @@ -25,7 +25,7 @@ module.exports = function(Api) { * @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} * @param {String} query A query to execute. A list of allowed named queries are available within the {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} under "actions" for each object. * @param {Object} [queryArgs] Optional. Arguments for the action. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of valid arguments - * @param {Object} fields Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. + * @param {String|String[]} fields Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. * @returns {Promise} A promise which will resolved with received data if everything went ok and rejected with error info otherwise */ Api.prototype.namedQuery = function (objCode, query, queryArgs, fields) { diff --git a/src/plugins/request.js b/src/plugins/request.js index b977ddf7..f5c8471b 100644 --- a/src/plugins/request.js +++ b/src/plugins/request.js @@ -31,6 +31,10 @@ module.exports = function(Api) { params = params || {}; fields = fields || []; + if (typeof fields === 'string') { + fields = [fields]; + } + var options = {}; util._extend(options, this.httpOptions); options.method = method; diff --git a/src/plugins/search.js b/src/plugins/search.js index ee682f76..cecd2faf 100644 --- a/src/plugins/search.js +++ b/src/plugins/search.js @@ -24,7 +24,7 @@ module.exports = function(Api) { * @memberOf Workfront.Api * @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} * @param {Object} query An object with search criteria - * @param {Array} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. + * @param {String|String[]} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode. * @return {Promise} A promise which will resolved with search results if everything went ok and rejected otherwise */ Api.prototype.search = function (objCode, query, fields) { diff --git a/test/plugins/execute.spec.js b/test/plugins/execute.spec.js index f3bd63ea..1804ca39 100644 --- a/test/plugins/execute.spec.js +++ b/test/plugins/execute.spec.js @@ -50,4 +50,10 @@ describe('Api.execute() method', function() { expect(api.request).to.have.callCount(1); expect(api.request).to.have.been.calledWith('foo', {objID: 111, action: 'doSomething'}, null, "PUT"); }); + + it('should call request() with proper params (without args) when objID is omitted', function() { + api.execute('foo', null, 'doSomething'); + expect(api.request).to.have.callCount(1); + expect(api.request).to.have.been.calledWith('foo', {action: 'doSomething'}, null, "PUT"); + }); }); \ No newline at end of file diff --git a/test/plugins/request.spec.js b/test/plugins/request.spec.js index a07af5dc..74e0f370 100644 --- a/test/plugins/request.spec.js +++ b/test/plugins/request.spec.js @@ -48,6 +48,30 @@ describe('Api.request() method', function() { }); + it('should resolve returned promise with data if everything went ok when fields are specified as string', function(done) { + var url = 'http://foobar:8080', + path = '/test', + params = { + 'foo': 1, + 'bar': 'baz' + }, + fields = '*', + method = 'GET'; + + nock(url) + .get('/attask/api' + path + '?foo=1&bar=baz&fields=*') + .reply(200, { + data: { + 'got': 'ok' + } + }); + + var api = new Api({url: url}); + var promise = api.request(path, params, fields, method); + expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done); + }); + + it('should resolve returned promise with data if everything went ok when using POST', function(done) { var url = 'http://foobar:8080', path = '/test',