Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
dispatch-node-sdk
Advanced tools
High- and low-level libraries for interacting with the Dispatch API.
There are now two maintained versions of Dispatch Javascript SDK.
All versions will be remained publish, but our goal is two have at most 2 maintained versions at a time.
The versions are version 1 and version 2 and can be found on the branches trunk/1.X.X
and trunk/2.X.X
respectively.
When making changes to a version, please branch off of the proper trunk and then submit a PR into that trunk.
If the change needs to go into both versions, please submit a PR for each version and then each version will be incremented and published separately.
All API endpoints point to the v1 endpoints of the Dispatch API.
All API endpoints point to the v1 endpoints of the Dispatch API with the exception of the jobs endpoint. The jobs endpoint now points to the v2 endpoint. The search endpoint now points to the v2 endpoint. Also, the endpoints for job offers and hybdrid jobs have been removed.
$ npm install --save dispatch-node-sdk
The client SDK is meant for use on the browser. It assumes that there is only one active bearer token at a time - for server-level use, please use the raw Client
.
Create a new instance of the client with your client_id
and client_secret
.
import Dispatch from 'dispatch-node-sdk';
const dispatchClient = new Dispatch(clientID, clientSecret, 'https://api.dispatch.me');
You can manually set the API bearer token if it came from an external source:
client.setBearerToken(bearerToken, refreshToken);
client.loginEmailPassword(email, password).then(() => {
return client.identifyUser();
}).then(user => {
console.log('Current user is', user);
}).catch(err => console.error('Failed to log in:', err));
client.requestVerificationCode('+15555555555').then(() => {
alert('Verification code will be sent to your phone!');
}).catch(err => alert('Error getting verification code'));
// Later...
client.loginPhoneNumber('+15555555555', verificationCode).then(token => {
alert('Got bearer token: ' + token);
}).catch(err => alert('Error logging in!'));
client.loginAuthToken(token).then(() => {
return client.identifyUser();
}).then(user => {
console.log('Current user is', user);
}).catch(err => console.error('Failed to log in:', err));
You can use the getCollection
method directly, like this:
client.getCollection('/v1/jobs', {
filter: {
status_eq: 'unscheduled',
}
}).then(jobs => {
jobs.forEach(job => console.log('Got job ID ' + job.get('id')));
}).catch(err => alert('Error loading jobs!'));
Or, use the defined collection, like this:
client.entities.jobs.get({
filter: {
status_eq: 'unscheduled',
}
});
You can also get meta data (such as pagination, etc.) using getCollectionWithMeta
:
client.getCollectionWithMeta('/v1/jobs', {
filter: {
status_eq: 'unscheduled',
}
}).then(response => {
...
})
where response has the following structure:
response: {
data: [...],
meta: {...},
}
if there are more then one sources of data returned from the API that are not meta:
response: {
data: {
source1: [...],
source2: [...],
...
},
meta: {...},
}
client.entities.jobs.create({
title: 'New job!',
}).then(...);
Sometimes you may want to just get a single model instead of an entire collection. For example, to retrieve job #1:
client.getModel('/v1/jobs', 1)
.then(job => alert('Job 1 has status: ' + job.status))
.catch(err => alert('Error loading job #1'));
Similarly:
client.entities.jobs.getOne(1).then(...)
Once you have a model, you can modify it and save it:
model.status = 'new status';
client.entities.jobs.update(1, model).then(...);
Some entities have additional abstracted convenience functions. For example:
client.entities.job(123).addNote('note text').then(...)
Get one customer by id:
client.entities.customer(:id)
Update one customer by id and properties:
client.entities.customer(:id).update(: properties)
Get all attachments for a customer by id:
client.entities.customer(:id).getAttachments()
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
:
myClient.getAuxiliaryClient('my-service-name').post('/foo', {body}).then(...);
This will do the same POST request but also handle automatic token refreshing if necessary:
myClient.doAuthenticatedRequest('POST', '/foo', {body}, {
client: 'my-service-name',
}).then(...);
Use the low-level raw client on the server-side for shared-key authentication:
import { RawClient, AUTH_MODE_HMAC } from 'dispatch-node-sdk';
const client = new RawClient({
authMode: AUTH_MODE_HMAC,
hmacCredentials: {
userID: 10,
userType: 'user',
secret: '<secret key>',
},
host: 'https://api-sandbox.dispatch.me',
});
client.get('/v1/jobs')
.then(jobs => console.log('Got %d jobs', jobs.jobs.length))
.catch(err => console.error(err));
These functions are available on client.entities.organization(id).
client.entities.organization(5).addCustomer({
first_name: 'Ron',
last_name: 'Swanson',
email: 'rswan@pr.com',
home_address: {
street_1: '2475 Mountain Avenue',
street_2: '',
postal_code: '50265',
city: 'West Des Moines',
state: 'IA',
},
phone_number: '+17069203204',
phone_numbers: {
'+17069203204': {
type: 'Mobile',
number: '+17069203204',
primary: true,
},
},
}).then(result => {
// result is the created customer object returned from the API
});
Checks whether a date falls during an organization's defined work hours. The organization must have a timezone
defined for this to work. Otherwise it always returns false
.
const date = new Date();
client.entities.organization(5).dateIsInWorkHours(date).then(result => {
// result is true or false
});
These functions are available on client.entities.user(id).
Checks whether a date falls during a user's defined work hours, or during that user's organization's defined work hours if the user does not have work hours defined. Either the user or the organization must have a timezone
defined for this to work. Otherwise it always returns false
.
const date = new Date();
client.entities.user(5).dateIsInWorkHours(date).then(result => {
// result is true or false
});
FAQs
High- and low-level libraries for interacting with the Dispatch API
The npm package dispatch-node-sdk receives a total of 156 weekly downloads. As such, dispatch-node-sdk popularity was classified as not popular.
We found that dispatch-node-sdk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 23 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.