dispatch-node-sdk
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -21,2 +21,6 @@ 'use strict'; | ||
var _request = require('./request'); | ||
var _request2 = _interopRequireDefault(_request); | ||
var _job = require('./entities/job'); | ||
@@ -30,2 +34,4 @@ | ||
var _errors = require('./errors'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -50,3 +56,5 @@ | ||
jobOffers: new _Collection2.default(this, endpoints.JOB_OFFERS), | ||
hybridJobs: new _Collection2.default(this, endpoints.HYBRID_JOBS) | ||
hybridJobs: new _Collection2.default(this, endpoints.HYBRID_JOBS), | ||
customers: new _Collection2.default(this, endpoints.CUSTOMERS), | ||
organizations: new _Collection2.default(this, endpoints.ORGANIZATIONS) | ||
}; | ||
@@ -130,2 +138,18 @@ } | ||
/** | ||
* Log in with a limited-use auth token retrieved via /v1/auth_tokens | ||
* @param {String} authToken | ||
* @return {Promise} | ||
*/ | ||
}, { | ||
key: 'loginAuthToken', | ||
value: function loginAuthToken(authToken) { | ||
var _this2 = this; | ||
return this.getNoAuthClient().post('/v1/auth_tokens/' + authToken + '/exchange').then(function (response) { | ||
return _this2.handleBearerToken(response.access_token, response.refresh_token); | ||
}); | ||
} | ||
/** | ||
* Request a verification code to be sent to the given phone number | ||
@@ -153,6 +177,6 @@ * | ||
value: function loginPhoneNumber(phoneNumber, verificationCode) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
return this.getNoAuthClient().post('/v1/phone_numbers/' + phoneNumber + '/verification_codes/' + verificationCode).then(function (response) { | ||
return _this2.handleBearerToken(response.access_token, response.refresh_token); | ||
return _this3.handleBearerToken(response.access_token, response.refresh_token); | ||
}); | ||
@@ -169,10 +193,17 @@ } | ||
value: function identifyUser() { | ||
return this.getAuthClient().get('/v1/me').then(function (response) { | ||
return this.doAuthenticatedRequest('GET', '/v1/me').then(function (response) { | ||
return response[Object.keys(response)[0]]; | ||
}); | ||
} | ||
/** | ||
* Exchange a refresh token for another bearer token | ||
* @param {String} refreshToken | ||
* @return {Promise} | ||
*/ | ||
}, { | ||
key: 'exchangeToken', | ||
value: function exchangeToken() { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -187,3 +218,3 @@ var refreshToken = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | ||
}).then(function (response) { | ||
return _this3.handleBearerToken(response.access_token, response.refresh_token); | ||
return _this4.handleBearerToken(response.access_token, response.refresh_token); | ||
}); | ||
@@ -213,4 +244,3 @@ } | ||
var client = this.getAuthClient(); | ||
return client.get(fullPath).then(function (response) { | ||
return this.doAuthenticatedRequest('GET', fullPath).then(function (response) { | ||
if (opts.raw === true) { | ||
@@ -225,2 +255,40 @@ return response; | ||
/** | ||
* Perform a request on the authenticated raw client, with support | ||
* for automatically attempting a token refresh and replaying the request after | ||
* the refresh is successful, or failing after that attempt or if there is | ||
* no refresh token | ||
* @param {String} method HTTP verb | ||
* @param {String} endpoint | ||
* @param {Object} body | ||
* @param {Object} options | ||
* @return {Promise} | ||
*/ | ||
}, { | ||
key: 'doAuthenticatedRequest', | ||
value: function doAuthenticatedRequest(method, endpoint) { | ||
var _this5 = this; | ||
var body = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; | ||
var options = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
var req = new _request2.default(method, endpoint, body, options); | ||
return new Promise(function (resolve, reject) { | ||
req.do(_this5.getAuthClient()).then(resolve).catch(function (err) { | ||
if (err instanceof _errors.UnauthorizedError) { | ||
if (_this5.refreshToken) { | ||
_this5.exchangeToken().then(function () { | ||
return req.do(_this5.getAuthClient()); | ||
}).then(resolve).catch(reject); | ||
} else { | ||
reject(err); | ||
} | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Retrieve a single entity by ID. Promise resolves with the raw data | ||
@@ -236,4 +304,8 @@ * | ||
value: function getModel(endpoint, id) { | ||
return this.getAuthClient().get(endpoint + '?filter[id_eq]=' + id).then(function (response) { | ||
return response[Object.keys(response)[0]][0]; | ||
return this.doAuthenticatedRequest('GET', endpoint + '?filter[id_eq]=' + id).then(function (response) { | ||
var model = response[Object.keys(response)[0]][0]; | ||
if (!model) { | ||
throw new Error('Not found'); | ||
} | ||
return model; | ||
}); | ||
@@ -240,0 +312,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -27,3 +27,3 @@ Object.defineProperty(exports, "__esModule", { | ||
_createClass(Collection, [{ | ||
key: "get", | ||
key: 'get', | ||
value: function get() { | ||
@@ -36,3 +36,3 @@ var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
}, { | ||
key: "getOne", | ||
key: 'getOne', | ||
value: function getOne(id) { | ||
@@ -42,20 +42,20 @@ return this.client.getModel(this.endpoint, id); | ||
}, { | ||
key: "create", | ||
key: 'create', | ||
value: function create(data) { | ||
return this.client.getAuthClient().post(this.endpoint, data); | ||
return this.client.doAuthenticatedRequest('POST', this.endpoint, data); | ||
} | ||
}, { | ||
key: "getSingleEndpoint", | ||
key: 'getSingleEndpoint', | ||
value: function getSingleEndpoint(id) { | ||
return this.endpoint + "/" + id; | ||
return this.endpoint + '/' + id; | ||
} | ||
}, { | ||
key: "update", | ||
key: 'update', | ||
value: function update(id, data) { | ||
return this.client.getAuthClient().patch(this.getSingleEndpoint(id), data); | ||
return this.client.doAuthenticatedRequest('PATCH', this.getSingleEndpoint(id), data); | ||
} | ||
}, { | ||
key: "delete", | ||
key: 'delete', | ||
value: function _delete(id) { | ||
return this.client.getAuthClient().delete(this.getSingleEndpoint(id)); | ||
return this.client.doAuthenticatedRequest('DELETE', this.getSingleEndpoint(id)); | ||
} | ||
@@ -62,0 +62,0 @@ }]); |
@@ -50,3 +50,3 @@ 'use strict'; | ||
addNote: function addNote(text) { | ||
return client.getAuthClient().post(endpoints.ATTACHMENTS, { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
entity_type: 'Job', | ||
@@ -53,0 +53,0 @@ entity_id: id, |
@@ -20,2 +20,4 @@ 'use strict'; | ||
var _errors = require('./errors'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -29,2 +31,3 @@ | ||
/** | ||
@@ -166,2 +169,5 @@ * Low-level client for interacting with the API. | ||
}); | ||
} else if (response.status === 401) { | ||
// 401 needs different behavior than other 400s because that would indicate an invalid bearer token | ||
return Promise.reject(new _errors.UnauthorizedError()); | ||
} else if (response.status >= 400 && response.status < 500) { | ||
@@ -168,0 +174,0 @@ return response.json().then(function (json) { |
{ | ||
"name": "dispatch-node-sdk", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "High- and low-level libraries for interacting with the Dispatch API", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index.js", |
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
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
76146
37
1931