Skip to content

Commit 11e0a1c

Browse files
authored
Merge pull request #109 from pipedrive/oauth
OAuth2 access token support
2 parents fb811bf + 19a5d7c commit 11e0a1c

8 files changed

+55
-26
lines changed

Diff for: README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ npm install pipedrive
1010

1111
## Roadmap & known issues
1212
- [Missing async/await promise support](https://github.com/pipedrive/client-nodejs/issues/81)
13-
- [Missing oauth 2.0 support](https://github.com/pipedrive/client-nodejs/issues/78)
1413

1514
## API Documentation
1615
The Pipedrive REST API documentation can be found at https://developers.pipedrive.com/v1
@@ -38,6 +37,12 @@ This Pipedrive API client is distributed under the MIT licence.
3837
- Run unit and integration tests
3938
- Make PR
4039

40+
## Options
41+
* `strictMode` - In strict mode `*_id` items in the responses are numeric IDs. Default is *false* in which case expanded
42+
objects are returned. Strict mode is recommended and is likely to be the default in the future.
43+
* `oauth` - whether the API token is to be used as OAuth bearer token instead of classic API key (default is *false*).
44+
When setting `oauth` to true your application code must take care of fetching, storing and refershing the tokens.
45+
4146
# How to use
4247
With a pre-set API token:
4348
```js
@@ -64,13 +69,15 @@ pipedrive.Deals.getAll({}, function(err, deals) {
6469
### Supported objects
6570

6671
* Activities
72+
* ActivityFields
6773
* ActivityTypes
6874
* Authorizations
6975
* Currencies
7076
* CompanyFeatures,
7177
* CompanySettings,
7278
* Deals
7379
* DealFields
80+
* EmailThreads
7481
* Files
7582
* Filters
7683
* Goals
@@ -87,6 +94,7 @@ pipedrive.Deals.getAll({}, function(err, deals) {
8794
* SearchResults
8895
* Stages
8996
* Users
97+
* userFields
9098
* Webhooks
9199

92100
### Supported operations for object collections

Diff for: lib/Pipedrive.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var defaults = {
2424
strictMode: false,
25-
apiHost: null,
26-
apiVersion: null
25+
oauth: false,
2726
};
2827

2928
options = _.extend({}, defaults, options);

Diff for: lib/apiUrl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
module.exports = function apiUrl(path, options, tokenNeeded) {
1212
var queryObj = {};
13-
if (tokenNeeded) {
13+
14+
if (!options.oauth && tokenNeeded) {
1415
queryObj.api_token = options.apiToken;
1516
}
1617
if (options.strictMode === true) {
@@ -19,4 +20,4 @@
1920

2021
return baseUri + '/' + path + (_.keys(queryObj).length > 0 ? '?' + qs.stringify(queryObj) : '');
2122
};
22-
})();
23+
})();

Diff for: lib/blueprint.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Scoped in API under /v1/{objectName} standard namespaces.
99
exports.apiObjects = [
1010
'activities',
11+
'activityFields',
1112
'activityTypes',
1213
'authorizations',
1314
'companyFeatures',
@@ -32,6 +33,7 @@ exports.apiObjects = [
3233
'searchResults',
3334
'stages',
3435
'users',
36+
'userFields',
3537
'webhooks'
3638
];
3739

Diff for: lib/rest.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
requestOptions;
3939

4040
this.options = options || {};
41-
this.options.method = this.options.method || method || 'GET';
41+
this.options.method = this.options.method || method || 'GET';
4242

4343
if(this.options.query) {
44-
query = typeof this.options.query == 'string' ? this.options.query : qs.stringify(this.options.query);
44+
query = typeof this.options.query == 'string' ? this.options.query : qs.stringify(this.options.query);
4545
url += (url.match(/\?/) ? '&' : '?') + query;
4646
}
4747

@@ -76,6 +76,10 @@
7676
headers['content-type'] = this.options.json ? 'application/json' : 'application/x-www-form-urlencoded';
7777
}
7878

79+
if (options.accessToken) {
80+
headers['Authorization'] = 'Bearer ' + options.accessToken;
81+
}
82+
7983
requestOptions = {
8084
method: method,
8185
maxResponseLength: 10 * 1024 * 1024,
@@ -124,4 +128,4 @@
124128

125129
util.inherits(Request, EventEmitter);
126130

127-
})();
131+
})();

Diff for: lib/restHandlers.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
this.options = _.extend({}, defaults, options || {});
1616

17+
this.resolveOptions = function (options) {
18+
options = options || {};
19+
if(this.options.oauth) {
20+
options.accessToken = this.options.apiToken;
21+
}
22+
return options;
23+
};
24+
1725
return this;
1826
}
1927

@@ -82,9 +90,9 @@
8290
// GET /items
8391
RestHandlers.prototype.listItems = function(object, params, callback) {
8492
var self = this,
85-
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
93+
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
8694
dataObject = object == 'authorizations' ? { multipart: false, data: paramsToSupply } : { query: qs.stringify(paramsToSupply) },
87-
req = rest[object == 'authorizations' ? 'post' : 'get'](apiUrl(object, this.options, false), dataObject);
95+
req = rest[object == 'authorizations' ? 'post' : 'get'](apiUrl(object, this.options, false), this.resolveOptions(dataObject));
8896

8997
req.on('complete', function(data, res) {
9098
self.genericResponse('GET', object, data, callback, req, res);
@@ -96,9 +104,9 @@
96104
// GET /items/find
97105
RestHandlers.prototype.findItems = function(object, params, callback) {
98106
var self = this,
99-
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
107+
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
100108
dataObject = { query: qs.stringify(paramsToSupply) },
101-
req = rest.get(apiUrl(object + '/find', this.options, false), dataObject);
109+
req = rest.get(apiUrl(object + '/find', this.options, false), this.resolveOptions(dataObject));
102110

103111
req.on('complete', function(data, res) {
104112
self.genericResponse('GET', object, data, callback, req, res);
@@ -110,9 +118,9 @@
110118
// GET /items/timeline
111119
RestHandlers.prototype.timelineItems = function(object, params, callback) {
112120
var self = this,
113-
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
121+
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
114122
dataObject = { query: qs.stringify(paramsToSupply) },
115-
req = rest.get(apiUrl(object + '/timeline', this.options, false), dataObject);
123+
req = rest.get(apiUrl(object + '/timeline', this.options, false), this.resolveOptions(dataObject));
116124

117125
req.on('complete', function(data, res) {
118126
self.genericResponse('GET', object, data, callback, req, res);
@@ -124,9 +132,9 @@
124132
// GET /searchResults/field
125133
RestHandlers.prototype.searchFields = function(object, params, callback) {
126134
var self = this,
127-
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
135+
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
128136
dataObject = { query: qs.stringify(paramsToSupply) },
129-
req = rest.get(apiUrl(object + '/field', this.options, false), dataObject);
137+
req = rest.get(apiUrl(object + '/field', this.options, false), this.resolveOptions(dataObject));
130138

131139
req.on('complete', function(data, res) {
132140
self.genericResponse('GET', object, data, callback, req, res);
@@ -138,8 +146,9 @@
138146
// GET /items/5
139147
RestHandlers.prototype.getItem = function(object, id, callback, params) {
140148
var self = this,
141-
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
142-
req = rest.get(apiUrl(object + '/' + id, this.options, false), { json: true, query: qs.stringify(paramsToSupply) });
149+
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
150+
dataObject = { json: true, query: qs.stringify(paramsToSupply) },
151+
req = rest.get(apiUrl(object + '/' + id, this.options, false), this.resolveOptions(dataObject));
143152

144153
req.on('complete', function(data, res) {
145154
self.genericResponse('GET', object, data, callback, req, res);
@@ -153,7 +162,8 @@
153162
var self = this,
154163
multipart_objects = ['files'],
155164
multipart = (_.indexOf(multipart_objects, object) != -1),
156-
req = rest.post(apiUrl(object, this.options, true), { json: true, multipart: multipart, data: params });
165+
dataObject = { json: true, multipart: multipart, data: params },
166+
req = rest.post(apiUrl(object, this.options, true), this.resolveOptions(dataObject));
157167

158168
req.on('complete', function(data, res) {
159169
self.genericResponse('POST', object, data, callback, req, res);
@@ -165,7 +175,8 @@
165175
// PUT /items/5
166176
RestHandlers.prototype.editItem = function(itemId, object, params, callback) {
167177
var self = this,
168-
req = rest.put(apiUrl(object + '/' + itemId, this.options, true), { json: true, multipart: false, data: params });
178+
dataObject = { json: true, multipart: false, data: params },
179+
req = rest.put(apiUrl(object + '/' + itemId, this.options, true), this.resolveOptions(dataObject));
169180

170181
req.on('complete', function(data, res) {
171182
self.genericResponse('PUT', object, data, callback, req, res);
@@ -177,7 +188,8 @@
177188
// DELETE /items/5
178189
RestHandlers.prototype.removeItem = function(itemId, object, params, callback) {
179190
var self = this,
180-
req = rest.del(apiUrl(itemId ? object + '/' + itemId : object, this.options, true), { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { id: itemId }) });
191+
dataObject = { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { id: itemId }) },
192+
req = rest.del(apiUrl(itemId ? object + '/' + itemId : object, this.options, true), this.resolveOptions(dataObject));
181193

182194
req.on('complete', function(data, res) {
183195
self.genericResponse('DELETE', object, data, (_.isFunction(params) ? params : callback), req, res);
@@ -189,7 +201,8 @@
189201
// DELETE /items
190202
RestHandlers.prototype.removeManyItems = function(itemIds, object, params, callback) {
191203
var self = this,
192-
req = rest.del(apiUrl(object, this.options, true), { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { ids: itemIds }) });
204+
dataObject = { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { ids: itemIds }) },
205+
req = rest.del(apiUrl(object, this.options, true), this.resolveOptions(dataObject));
193206

194207
req.on('complete', function(data, res) {
195208
self.genericResponse('DELETE', object, data, (_.isFunction(params) ? params : callback), req, res);
@@ -205,7 +218,8 @@
205218
return false;
206219
}
207220
var self = this,
208-
req = rest.post(apiUrl(object + '/' + whichId + '/merge', this.options, true), { json: true, multipart: false, data: { merge_with_id: withId } });
221+
dataObject = { json: true, multipart: false, data: { merge_with_id: withId } },
222+
req = rest.post(apiUrl(object + '/' + whichId + '/merge', this.options, true), this.resolveOptions(dataObject));
209223

210224
req.on('complete', function(data, res) {
211225
self.genericResponse('POST', object, data, callback, req, res);
@@ -220,7 +234,8 @@
220234
}
221235

222236
var self = this,
223-
req = rest.post(apiUrl(object + '/' + whichId + '/duplicate', this.options, true), { json: true, multipart: false, data: {} });
237+
dataObject = { json: true, multipart: false, data: {} },
238+
req = rest.post(apiUrl(object + '/' + whichId + '/duplicate', this.options, true), this.resolveOptions(dataObject));
224239

225240
req.on('complete', function(data, res) {
226241
self.genericResponse('POST', object, data, callback, req, res);

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pipedrive",
3-
"version": "9.1.1",
3+
"version": "9.2.0",
44
"description": "Pipedrive REST client for NodeJS",
55
"keywords": [
66
"pipedrive",

0 commit comments

Comments
 (0)