dispatch-node-sdk
Advanced tools
Comparing version 0.0.29 to 0.0.30
@@ -17,2 +17,6 @@ 'use strict'; | ||
var _nodeUuid = require('node-uuid'); | ||
var _nodeUuid2 = _interopRequireDefault(_nodeUuid); | ||
var _rawClient = require('./rawClient'); | ||
@@ -402,2 +406,27 @@ | ||
/** | ||
* Append analyitcs tags and a transaction ID to the headers. | ||
* @param {Object} options A set of options for a request. | ||
* @return {Object} The new set of options. | ||
*/ | ||
}, { | ||
key: 'appendAnalyticsHeaders', | ||
value: function appendAnalyticsHeaders() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var analytics = Object.assign({}, this.session, options.analytics); | ||
var analyticsTags = _underscore2.default.map(_underscore2.default.pairs(analytics), function (param) { | ||
return param.join(':'); | ||
}).join(','); | ||
var transactionId = options.transactionID || _nodeUuid2.default.v4(); | ||
var headers = Object.assign({}, options.headers, { | ||
'X-Analytics-Tags': analyticsTags, | ||
'X-Transaction-ID': transactionId | ||
}); | ||
return Object.assign({}, options, { headers: headers }); | ||
} | ||
/** | ||
* Perform a request on the authenticated raw client, with support | ||
@@ -422,3 +451,3 @@ * for automatically attempting a token refresh and replaying the request after | ||
var req = new _request2.default(method, this.urlWithSession(endpoint), body, options); | ||
var req = new _request2.default(method, endpoint, body, this.appendAnalyticsHeaders(options)); | ||
@@ -480,2 +509,3 @@ var client = void 0; | ||
var type = arguments.length <= 2 || arguments[2] === undefined ? 'square_photo' : arguments[2]; | ||
var options = arguments[3]; | ||
@@ -487,6 +517,9 @@ var formData = new _formData2.default(); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, Object.assign({ | ||
client: 'files-api', | ||
headers: {} | ||
}); | ||
headers: { | ||
'Content-Type': undefined, | ||
Accept: undefined | ||
} | ||
}, options)); | ||
} | ||
@@ -507,17 +540,2 @@ | ||
} | ||
}, { | ||
key: 'urlWithSession', | ||
value: function urlWithSession(url) { | ||
if (this.session) { | ||
var queryStr = queryString.stringify({ | ||
event_params: this.session | ||
}); | ||
if (url.indexOf('?') >= 0) { | ||
url = url + '&' + queryStr; | ||
} else { | ||
url = url + '?' + queryStr; | ||
} | ||
} | ||
return url; | ||
} | ||
@@ -524,0 +542,0 @@ /** |
@@ -10,6 +10,6 @@ 'use strict'; | ||
return { | ||
update: function update(text) { | ||
update: function update(text, options) { | ||
return client.doAuthenticatedRequest('PATCH', _endpoints.ATTACHMENTS + '/' + id, { | ||
description: text | ||
}).then(function (response) { | ||
}, options).then(function (response) { | ||
return response.attachment; | ||
@@ -19,4 +19,4 @@ }); | ||
delete: function _delete() { | ||
return client.doAuthenticatedRequest('DELETE', _endpoints.ATTACHMENTS + '/' + id).then(function () { | ||
delete: function _delete(options) { | ||
return client.doAuthenticatedRequest('DELETE', _endpoints.ATTACHMENTS + '/' + id, null, options).then(function () { | ||
// endpoint returns no content on successful request | ||
@@ -23,0 +23,0 @@ // return id of successfully deleted item b/c its useful for resolve callbacks |
@@ -41,4 +41,4 @@ 'use strict'; | ||
key: 'create', | ||
value: function create(data) { | ||
return this.client.doAuthenticatedRequest('POST', this.endpoint, data); | ||
value: function create(data, options) { | ||
return this.client.doAuthenticatedRequest('POST', this.endpoint, data, options); | ||
} | ||
@@ -52,4 +52,4 @@ }, { | ||
key: 'update', | ||
value: function update(id, data) { | ||
return this.client.doAuthenticatedRequest('PATCH', this.getSingleEndpoint(id), data).then(function (response) { | ||
value: function update(id, data, options) { | ||
return this.client.doAuthenticatedRequest('PATCH', this.getSingleEndpoint(id), data, options).then(function (response) { | ||
return response[Object.keys(response)[0]]; | ||
@@ -60,4 +60,4 @@ }); | ||
key: 'delete', | ||
value: function _delete(id) { | ||
return this.client.doAuthenticatedRequest('DELETE', this.getSingleEndpoint(id)); | ||
value: function _delete(id, options) { | ||
return this.client.doAuthenticatedRequest('DELETE', this.getSingleEndpoint(id), null, options); | ||
} | ||
@@ -64,0 +64,0 @@ }]); |
@@ -49,8 +49,10 @@ 'use strict'; | ||
addNote: function addNote(text) { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
addNote: function addNote(text, options) { | ||
var body = { | ||
entity_type: 'Job', | ||
entity_id: id, | ||
description: text | ||
}).then(function (response) { | ||
}; | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, body, options).then(function (response) { | ||
return response.attachment; | ||
@@ -60,4 +62,4 @@ }); | ||
addPhoto: function addPhoto(fileToken, name, description) { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
addPhoto: function addPhoto(fileToken, name, description, options) { | ||
var body = { | ||
entity_type: 'Job', | ||
@@ -68,3 +70,5 @@ entity_id: id, | ||
file_token: fileToken | ||
}).then(function (response) { | ||
}; | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, body, options).then(function (response) { | ||
return response.attachment; | ||
@@ -76,6 +80,7 @@ }); | ||
var data = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments[1]; | ||
return client.doAuthenticatedRequest('POST', endpoints.APPOINTMENTS, Object.assign({}, data, { | ||
job_id: id | ||
})); | ||
var body = Object.assign({}, data, { job_id: id }); | ||
return client.doAuthenticatedRequest('POST', endpoints.APPOINTMENTS, body, options); | ||
}, | ||
@@ -82,0 +87,0 @@ |
@@ -130,6 +130,6 @@ 'use strict'; | ||
var headers = options.headers || { | ||
var headers = Object.assign({ | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}; | ||
}, options.headers); | ||
@@ -136,0 +136,0 @@ switch (this.authMode) { |
import _ from 'underscore'; | ||
import moment from 'moment'; | ||
import UUID from 'node-uuid'; | ||
import RawClient, { AUTH_MODE_NONE, AUTH_MODE_BEARER } from './rawClient'; | ||
@@ -278,2 +279,20 @@ import * as endpoints from './endpoints'; | ||
/** | ||
* Append analyitcs tags and a transaction ID to the headers. | ||
* @param {Object} options A set of options for a request. | ||
* @return {Object} The new set of options. | ||
*/ | ||
appendAnalyticsHeaders(options = {}) { | ||
const analytics = Object.assign({}, this.session, options.analytics); | ||
const analyticsTags = _.map(_.pairs(analytics), param => param.join(':')).join(','); | ||
const transactionId = options.transactionID || UUID.v4(); | ||
const headers = Object.assign({}, options.headers, { | ||
'X-Analytics-Tags': analyticsTags, | ||
'X-Transaction-ID': transactionId, | ||
}); | ||
return Object.assign({}, options, { headers }); | ||
} | ||
/** | ||
* Perform a request on the authenticated raw client, with support | ||
@@ -290,3 +309,3 @@ * for automatically attempting a token refresh and replaying the request after | ||
doAuthenticatedRequest(method, endpoint, body = null, options = {}) { | ||
const req = new Request(method, this.urlWithSession(endpoint), body, options); | ||
const req = new Request(method, endpoint, body, this.appendAnalyticsHeaders(options)); | ||
@@ -342,3 +361,3 @@ let client; | ||
*/ | ||
uploadFile(file, name, type = 'square_photo') { | ||
uploadFile(file, name, type = 'square_photo', options) { | ||
const formData = new FormData(); | ||
@@ -349,6 +368,9 @@ formData.append('file', dataURItoBlob(file), name); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, Object.assign({ | ||
client: 'files-api', | ||
headers: {}, | ||
}); | ||
headers: { | ||
'Content-Type': undefined, | ||
Accept: undefined, | ||
}, | ||
}, options)); | ||
} | ||
@@ -366,16 +388,2 @@ | ||
urlWithSession(url) { | ||
if (this.session) { | ||
const queryStr = queryString.stringify({ | ||
event_params: this.session, | ||
}); | ||
if (url.indexOf('?') >= 0) { | ||
url = `${url}&${queryStr}`; | ||
} else { | ||
url = `${url}?${queryStr}`; | ||
} | ||
} | ||
return url; | ||
} | ||
/** | ||
@@ -382,0 +390,0 @@ * Search an organization's entities. |
@@ -173,30 +173,2 @@ import nock from 'nock'; | ||
describe('session', () => { | ||
it('should add session params to url when there is a query string present', () => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
client.setSession({ | ||
foo: 'bar', | ||
baz: 'bing', | ||
}); | ||
const scope = nock('https://api.dispatch.me').get('/test?already=there&event_params[foo]=bar&event_params[baz]=bing').reply(200); | ||
client.doAuthenticatedRequest('GET', '/test?already=there'); | ||
expect(scope.isDone()).toEqual(true); | ||
}); | ||
it('should add session params to url when there is no query string present', () => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
client.setSession({ | ||
foo: 'bar', | ||
baz: 'bing', | ||
}); | ||
const scope = nock('https://api.dispatch.me').get('/test?event_params[foo]=bar&event_params[baz]=bing').reply(200); | ||
client.doAuthenticatedRequest('GET', '/test'); | ||
expect(scope.isDone()).toEqual(true); | ||
}); | ||
}); | ||
describe('wrapped authenticated request', () => { | ||
@@ -266,2 +238,70 @@ it('should try to refresh the bearer token when doing an authenticated request', done => { | ||
}); | ||
it('should append a transaction ID to the headers of an authenticated request', done => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
nock('https://api.dispatch.me').get('/').times(1).reply(function () { | ||
expect(this.req.headers['x-transaction-id']).toExist(); | ||
done(); | ||
}); | ||
nock('https://api.dispatch.me').post('/oauth/token', { | ||
grant_type: 'refresh_token', | ||
refresh_token: testRefreshToken, | ||
client_id: testClientID, | ||
client_secret: testClientSecret, | ||
}).reply(200, { | ||
access_token: 'new access token', | ||
refresh_token: 'new refresh token', | ||
}); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
client.doAuthenticatedRequest('GET', '/'); | ||
}); | ||
it('should append an analytics tags to the headers of an authenticated request', done => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
nock('https://api.dispatch.me').get('/').times(1).reply(function () { | ||
expect(this.req.headers['x-analytics-tags']).toEqual('foo:bar,bar:foo'); | ||
done(); | ||
}); | ||
nock('https://api.dispatch.me').post('/oauth/token', { | ||
grant_type: 'refresh_token', | ||
refresh_token: testRefreshToken, | ||
client_id: testClientID, | ||
client_secret: testClientSecret, | ||
}).reply(200, { | ||
access_token: 'new access token', | ||
refresh_token: 'new refresh token', | ||
}); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
client.doAuthenticatedRequest('GET', '/', null, { analytics: { foo: 'bar', bar: 'foo' } }); | ||
}); | ||
it('should append session params to the analytics tags to the headers of an authenticated request', done => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
nock('https://api.dispatch.me').get('/').times(1).reply(function () { | ||
expect(this.req.headers['x-analytics-tags']).toEqual('baz:bing,foo:bar,bar:foo'); | ||
done(); | ||
}); | ||
nock('https://api.dispatch.me').post('/oauth/token', { | ||
grant_type: 'refresh_token', | ||
refresh_token: testRefreshToken, | ||
client_id: testClientID, | ||
client_secret: testClientSecret, | ||
}).reply(200, { | ||
access_token: 'new access token', | ||
refresh_token: 'new refresh token', | ||
}); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
client.setSession({ baz: 'bing' }); | ||
client.doAuthenticatedRequest('GET', '/', null, { analytics: { foo: 'bar', bar: 'foo' } }); | ||
}); | ||
}); | ||
@@ -268,0 +308,0 @@ |
@@ -6,6 +6,6 @@ import { ATTACHMENTS } from '../endpoints'; | ||
return { | ||
update: (text) => { | ||
update: (text, options) => { | ||
return client.doAuthenticatedRequest('PATCH', `${ATTACHMENTS}/${id}`, { | ||
description: text, | ||
}).then(response => { | ||
}, options).then(response => { | ||
return response.attachment; | ||
@@ -15,4 +15,4 @@ }); | ||
delete: () => { | ||
return client.doAuthenticatedRequest('DELETE', `${ATTACHMENTS}/${id}`) | ||
delete: (options) => { | ||
return client.doAuthenticatedRequest('DELETE', `${ATTACHMENTS}/${id}`, null, options) | ||
.then(() => { | ||
@@ -19,0 +19,0 @@ // endpoint returns no content on successful request |
@@ -21,4 +21,4 @@ /** | ||
create(data) { | ||
return this.client.doAuthenticatedRequest('POST', this.endpoint, data); | ||
create(data, options) { | ||
return this.client.doAuthenticatedRequest('POST', this.endpoint, data, options); | ||
} | ||
@@ -30,4 +30,4 @@ | ||
update(id, data) { | ||
return this.client.doAuthenticatedRequest('PATCH', this.getSingleEndpoint(id), data) | ||
update(id, data, options) { | ||
return this.client.doAuthenticatedRequest('PATCH', this.getSingleEndpoint(id), data, options) | ||
.then(response => { | ||
@@ -38,5 +38,5 @@ return response[Object.keys(response)[0]]; | ||
delete(id) { | ||
return this.client.doAuthenticatedRequest('DELETE', this.getSingleEndpoint(id)); | ||
delete(id, options) { | ||
return this.client.doAuthenticatedRequest('DELETE', this.getSingleEndpoint(id), null, options); | ||
} | ||
} |
@@ -45,12 +45,14 @@ import * as endpoints from '../endpoints'; | ||
addNote: (text) => { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
addNote: (text, options) => { | ||
const body = { | ||
entity_type: 'Job', | ||
entity_id: id, | ||
description: text, | ||
}).then(response => response.attachment); | ||
}; | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, body, options).then(response => response.attachment); | ||
}, | ||
addPhoto: (fileToken, name, description) => { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
addPhoto: (fileToken, name, description, options) => { | ||
const body = { | ||
entity_type: 'Job', | ||
@@ -61,9 +63,11 @@ entity_id: id, | ||
file_token: fileToken, | ||
}).then(response => response.attachment); | ||
}; | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, body, options).then(response => response.attachment); | ||
}, | ||
createAppointment: (data = {}) => { | ||
return client.doAuthenticatedRequest('POST', endpoints.APPOINTMENTS, Object.assign({}, data, { | ||
job_id: id, | ||
})); | ||
createAppointment: (data = {}, options) => { | ||
const body = Object.assign({}, data, { job_id: id }); | ||
return client.doAuthenticatedRequest('POST', endpoints.APPOINTMENTS, body, options); | ||
}, | ||
@@ -70,0 +74,0 @@ |
@@ -90,6 +90,6 @@ import { ensure, oneOf } from 'simplecheck'; | ||
const headers = options.headers || { | ||
const headers = Object.assign({ | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}; | ||
}, options.headers); | ||
@@ -96,0 +96,0 @@ switch (this.authMode) { |
{ | ||
"name": "dispatch-node-sdk", | ||
"version": "0.0.29", | ||
"version": "0.0.30", | ||
"description": "High- and low-level libraries for interacting with the Dispatch API", | ||
@@ -39,2 +39,3 @@ "main": "dist/lib/index.js", | ||
"moment": "^2.13.0", | ||
"node-uuid": "^1.4.7", | ||
"simplecheck": "^0.1.2", | ||
@@ -41,0 +42,0 @@ "underscore": "^1.8.3" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125313
3190
6
2
+ Addednode-uuid@^1.4.7
+ Addednode-uuid@1.4.8(transitive)