dispatch-node-sdk
Advanced tools
Comparing version 0.0.27 to 0.0.28
@@ -53,2 +53,6 @@ 'use strict'; | ||
var _configuration = require('./configuration'); | ||
var _configuration2 = _interopRequireDefault(_configuration); | ||
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; } } | ||
@@ -60,2 +64,13 @@ | ||
/** | ||
* Class Dispatch is the high-level abstracted client for *front-end use only*. | ||
* This client assumes that there is only ever one logged in user at a time ( | ||
* its bearer token is a singleton) and thus is not suited for back-end use. | ||
* | ||
* This class automatically handles things like: | ||
* * Refreshing bearer tokens | ||
* * Parsing API responses | ||
* * Storying analytics params in sessions to attach to all API calls | ||
*/ | ||
var Dispatch = function () { | ||
@@ -71,5 +86,2 @@ function Dispatch(clientID, clientSecret) { | ||
// Determine files api url from host | ||
this.filesAPIHost = this.host.replace('api', 'files-api'); | ||
this.session = null; | ||
@@ -88,4 +100,15 @@ this.entities = { | ||
}; | ||
this.auxiliaryClients = {}; | ||
this.configuration = new _configuration2.default(this); | ||
} | ||
/** | ||
* Lazy load an unauthenticated client. For use with logging in | ||
* | ||
* @return {RawClient} | ||
*/ | ||
_createClass(Dispatch, [{ | ||
@@ -103,2 +126,9 @@ key: 'getNoAuthClient', | ||
} | ||
/** | ||
* Lazy load an authenticated client using the dispatch client's bearer token | ||
* | ||
* @return {RawClient} | ||
*/ | ||
}, { | ||
@@ -117,15 +147,46 @@ key: 'getAuthClient', | ||
} | ||
/** | ||
* Determine the host of a service using our naming convention | ||
* | ||
* @param {String} serviceName | ||
* @return {String} | ||
*/ | ||
}, { | ||
key: 'getFilesAPIClient', | ||
value: function getFilesAPIClient() { | ||
if (!this.filesAPIClient) { | ||
this.filesAPIClient = new _rawClient2.default({ | ||
key: 'determineAuxiliaryAPIHost', | ||
value: function determineAuxiliaryAPIHost(serviceName) { | ||
return this.host.replace('api', serviceName); | ||
} | ||
/** | ||
* Lazy load a client for an auxiliary service that uses bearer-token authentication. | ||
* E.g. "config", "files-api", "integrations-orchestrator" | ||
* | ||
* @param {String} serviceName | ||
* @return {RawClient} | ||
*/ | ||
}, { | ||
key: 'getAuxiliaryClient', | ||
value: function getAuxiliaryClient(serviceName) { | ||
if (!this.auxiliaryClients[serviceName]) { | ||
this.auxiliaryClients[serviceName] = new _rawClient2.default({ | ||
authMode: _rawClient.AUTH_MODE_BEARER, | ||
bearerToken: this.bearerToken, | ||
host: this.filesAPIHost | ||
host: this.determineAuxiliaryAPIHost(serviceName) | ||
}); | ||
} | ||
return this.filesAPIClient; | ||
return this.auxiliaryClients[serviceName]; | ||
} | ||
/** | ||
* Load and authenticate a client using the application's credentials. This is used | ||
* for making verification code and other requests that require an authenticated application | ||
* rather than an unauthenticated user | ||
* . | ||
* @return {Promise<RawClient>} | ||
*/ | ||
}, { | ||
@@ -152,2 +213,12 @@ key: 'getAppClient', | ||
} | ||
/** | ||
* Save a retrieved bearer token/refresh token combo and reset all existing authenticated | ||
* clients that were using the previous bearer token | ||
* | ||
* @param {String} bearerToken | ||
* @param {String} refreshToken | ||
* @return {String} Bearer token, for chaining/promise resolution | ||
*/ | ||
}, { | ||
@@ -163,2 +234,5 @@ key: 'handleBearerToken', | ||
// Reset all aux clients for the same reason | ||
this.auxiliaryClients = {}; | ||
return this.bearerToken; | ||
@@ -168,3 +242,4 @@ } | ||
/** | ||
* Set the bearer token on the SDK | ||
* Set the bearer token on the SDK. For now this just is an alias for handleBearerToken | ||
* | ||
* @param {String} bearerToken Bearer token | ||
@@ -206,2 +281,3 @@ * @param {String} refreshToken Optional refresh token | ||
* Log in with a limited-use auth token retrieved via /v1/auth_tokens | ||
* | ||
* @param {String} authToken | ||
@@ -248,2 +324,3 @@ * @return {Promise} | ||
* Log the user in with their phone number and verification code | ||
* | ||
* @param {String} phoneNumber Phone number formatted like +15555555555 | ||
@@ -268,2 +345,3 @@ * @param {String} verificationCode The 4-digit verification code | ||
* Get the user info based on the bearer token retrieved via log in | ||
* | ||
* @return {Promise} | ||
@@ -356,4 +434,4 @@ */ | ||
var client = void 0; | ||
if (options.file) { | ||
client = this.getFilesAPIClient(); | ||
if (options.client) { | ||
client = this.getAuxiliaryClient(options.client); | ||
} else { | ||
@@ -418,3 +496,3 @@ client = this.getAuthClient(); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
file: true, | ||
client: 'files-api', | ||
headers: {} | ||
@@ -455,7 +533,7 @@ }); | ||
* Search an organization's entities. | ||
* @param {[String]} options.organizationID The organization's id. | ||
* @param {[String]} options.query The search text. | ||
* @param {[Array]} options.filter The entities to filter by. | ||
* @param {[Number]} options.maxResultsPerModel The maximum results to show per model. | ||
* @return {Promise} A promise with the results. | ||
* @param {String} options.organizationID The organization's id. | ||
* @param {String} options.query The search text. | ||
* @param {Array} options.filter The entities to filter by. | ||
* @param {Number} options.maxResultsPerModel The maximum results to show per model. | ||
* @return {Promise<Object>} A promise with the results. | ||
*/ | ||
@@ -462,0 +540,0 @@ |
@@ -13,3 +13,14 @@ import _ from 'underscore'; | ||
import { dataURItoBlob } from './fileHelpers'; | ||
import Configuration from './configuration'; | ||
/** | ||
* Class Dispatch is the high-level abstracted client for *front-end use only*. | ||
* This client assumes that there is only ever one logged in user at a time ( | ||
* its bearer token is a singleton) and thus is not suited for back-end use. | ||
* | ||
* This class automatically handles things like: | ||
* * Refreshing bearer tokens | ||
* * Parsing API responses | ||
* * Storying analytics params in sessions to attach to all API calls | ||
*/ | ||
export default class Dispatch { | ||
@@ -21,5 +32,2 @@ constructor(clientID, clientSecret, host = 'https://api.dispatch.me') { | ||
// Determine files api url from host | ||
this.filesAPIHost = this.host.replace('api', 'files-api'); | ||
this.session = null; | ||
@@ -38,4 +46,13 @@ this.entities = { | ||
}; | ||
this.auxiliaryClients = {}; | ||
this.configuration = new Configuration(this); | ||
} | ||
/** | ||
* Lazy load an unauthenticated client. For use with logging in | ||
* | ||
* @return {RawClient} | ||
*/ | ||
getNoAuthClient() { | ||
@@ -52,2 +69,7 @@ if (!this.noAuthClient) { | ||
/** | ||
* Lazy load an authenticated client using the dispatch client's bearer token | ||
* | ||
* @return {RawClient} | ||
*/ | ||
getAuthClient() { | ||
@@ -65,14 +87,38 @@ if (!this.authClient) { | ||
getFilesAPIClient() { | ||
if (!this.filesAPIClient) { | ||
this.filesAPIClient = new RawClient({ | ||
/** | ||
* Determine the host of a service using our naming convention | ||
* | ||
* @param {String} serviceName | ||
* @return {String} | ||
*/ | ||
determineAuxiliaryAPIHost(serviceName) { | ||
return this.host.replace('api', serviceName); | ||
} | ||
/** | ||
* Lazy load a client for an auxiliary service that uses bearer-token authentication. | ||
* E.g. "config", "files-api", "integrations-orchestrator" | ||
* | ||
* @param {String} serviceName | ||
* @return {RawClient} | ||
*/ | ||
getAuxiliaryClient(serviceName) { | ||
if (!this.auxiliaryClients[serviceName]) { | ||
this.auxiliaryClients[serviceName] = new RawClient({ | ||
authMode: AUTH_MODE_BEARER, | ||
bearerToken: this.bearerToken, | ||
host: this.filesAPIHost, | ||
host: this.determineAuxiliaryAPIHost(serviceName), | ||
}); | ||
} | ||
return this.filesAPIClient; | ||
return this.auxiliaryClients[serviceName]; | ||
} | ||
/** | ||
* Load and authenticate a client using the application's credentials. This is used | ||
* for making verification code and other requests that require an authenticated application | ||
* rather than an unauthenticated user | ||
* . | ||
* @return {Promise<RawClient>} | ||
*/ | ||
getAppClient() { | ||
@@ -96,2 +142,10 @@ if (!this.appClient) { | ||
/** | ||
* Save a retrieved bearer token/refresh token combo and reset all existing authenticated | ||
* clients that were using the previous bearer token | ||
* | ||
* @param {String} bearerToken | ||
* @param {String} refreshToken | ||
* @return {String} Bearer token, for chaining/promise resolution | ||
*/ | ||
handleBearerToken(bearerToken, refreshToken) { | ||
@@ -105,2 +159,5 @@ this.bearerToken = bearerToken; | ||
// Reset all aux clients for the same reason | ||
this.auxiliaryClients = {}; | ||
return this.bearerToken; | ||
@@ -110,3 +167,4 @@ } | ||
/** | ||
* Set the bearer token on the SDK | ||
* Set the bearer token on the SDK. For now this just is an alias for handleBearerToken | ||
* | ||
* @param {String} bearerToken Bearer token | ||
@@ -139,2 +197,3 @@ * @param {String} refreshToken Optional refresh token | ||
* Log in with a limited-use auth token retrieved via /v1/auth_tokens | ||
* | ||
* @param {String} authToken | ||
@@ -167,2 +226,3 @@ * @return {Promise} | ||
* Log the user in with their phone number and verification code | ||
* | ||
* @param {String} phoneNumber Phone number formatted like +15555555555 | ||
@@ -179,2 +239,3 @@ * @param {String} verificationCode The 4-digit verification code | ||
* Get the user info based on the bearer token retrieved via log in | ||
* | ||
* @return {Promise} | ||
@@ -241,4 +302,4 @@ */ | ||
let client; | ||
if (options.file) { | ||
client = this.getFilesAPIClient(); | ||
if (options.client) { | ||
client = this.getAuxiliaryClient(options.client); | ||
} else { | ||
@@ -297,3 +358,3 @@ client = this.getAuthClient(); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
file: true, | ||
client: 'files-api', | ||
headers: {}, | ||
@@ -329,7 +390,7 @@ }); | ||
* Search an organization's entities. | ||
* @param {[String]} options.organizationID The organization's id. | ||
* @param {[String]} options.query The search text. | ||
* @param {[Array]} options.filter The entities to filter by. | ||
* @param {[Number]} options.maxResultsPerModel The maximum results to show per model. | ||
* @return {Promise} A promise with the results. | ||
* @param {String} options.organizationID The organization's id. | ||
* @param {String} options.query The search text. | ||
* @param {Array} options.filter The entities to filter by. | ||
* @param {Number} options.maxResultsPerModel The maximum results to show per model. | ||
* @return {Promise<Object>} A promise with the results. | ||
*/ | ||
@@ -336,0 +397,0 @@ search({ organizationID, query, filter, maxResultsPerModel }) { |
@@ -261,3 +261,3 @@ import nock from 'nock'; | ||
client.doAuthenticatedRequest('GET', '/', null, { | ||
file: true, | ||
client: 'files-api', | ||
}); | ||
@@ -264,0 +264,0 @@ expect(scope.isDone()).toEqual(true); |
{ | ||
"name": "dispatch-node-sdk", | ||
"version": "0.0.27", | ||
"version": "0.0.28", | ||
"description": "High- and low-level libraries for interacting with the Dispatch API", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index.js", |
@@ -124,2 +124,17 @@ Dispatch JavaScript SDK | ||
## Auxiliary Services | ||
The Dispatch client has built-in support for interacting with auxiliary Dispatch APIs such as the Files API or Configuration Service. You should not have to use this directly as there are abstracted functions like `uploadFile` and `configuration.getForEntity` that make use of it, but if you need to, you can do the following: | ||
This will make a POST request to `https://my-service-name.dispatch.me`: | ||
```javascript | ||
myClient.getAuxiliaryClient('my-service-name').post('/foo', {body}).then(...); | ||
``` | ||
This will do the same POST request but also handle automatic token refreshing if necessary: | ||
```javascript | ||
myClient.doAuthenticatedRequest('POST', '/foo', {body}, { | ||
client: 'my-service-name', | ||
}).then(...); | ||
``` | ||
## Raw Client | ||
@@ -126,0 +141,0 @@ Use the low-level raw client on the server-side for shared-key authentication: |
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
121892
45
3137
160