npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dispatch/dispatch-node-sdk

v0.0.4

Published

High- and low-level libraries for interacting with the Dispatch API

Downloads

12

Readme

Dispatch JavaScript SDK

High- and low-level libraries for interacting with the Dispatch API.

Installation

$ npm install --save dispatch-node-sdk

Usage

Client 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.

Instantiation

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');

Authentication

Set the Bearer Token

You can manually set the API bearer token if it came from an external source:

client.setBearerToken(bearerToken, refreshToken);

Log in with username/password

client.loginEmailPassword(email, password).then(token => {
  return client.identifyUser()
}).then(user => {
  console.log('Current user is', user);
}).catch(err => console.error('Failed to log in:', err));

Log in with phone + verification code

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!'));

Interacting with Data

By default, the SDK wraps each entity in a Model class, (very similar to a Backbone model), which exposes methods like refresh, set, get, and save.

Get a list of models

For example, get a list of unscheduled jobs:

client.getCollection('/v1/jobs', {
  status_eq: 'unscheduled'
}).then(jobs => {
  jobs.forEach(job => console.log('Got job ID ' + job.get('id')));
}).catch(err => alert('Error loading jobs!'));

Updating a model

Once you have a model, you can modify it and save it:

model.set('status', 'scheduled');

model.save().then(() => {
  alert('Saved!');
}).catch(err => alert('Error saving job!'));

Retrieving a single model

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.get('status')))
.catch(err => alert('Error loading job #1'));

Raw Client

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.length))
.catch(err => console.error(err));