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

dts-api-client

v6.2.0

Published

DTS API client for web browsers.

Downloads

2,306

Readme

DTS API client

Installation

npm i --save dts-api-client

Package can be consumed in Webpack and Node.js environments.

Webpack setup

If you need to use the upload client in a Webpack application, use the following configuration:

config.resolve = {
  alias: {
    'aws-sdk': 'aws-sdk/dist/aws-sdk.min.js'
  }
};

Ignore the packages that you don't need completely to reduce the footprint:

config.plugins = [
  new webpack.IgnorePlugin(/^(aws-sdk)$/), //If you don't use the upload client
  new webpack.IgnorePlugin(/^(jquery)$/),  //If you don't use the legacy client
  new webpack.IgnorePlugin(/^(socketcluster-client)$/),  //If you don't use the socket client
];

Configuration

If you don't use some of the clients (e.g. the upload client), set the related configuration section to a false key.

Not set configuration properties will use the default configuration.

import DtsApi from 'dts-api-client';

/* Application configuration */
const application = {
    "client_id": "d04ad813dac443b28a490ddca27b9178",
    "security": "oauth",
    "redirect_uri": "https://web.snap.menu/"
};

/* Client configuration */
const client = new DtsApi.Client({
    /* All client configs and keys are optional */

    //API client configuration
    api: {
        host: 'api.managesnap.com',
        secure: true,
        path: '/frontend/',
        application: application //Optional, defaults to { security: 'oauth' }                
    },

    //Alternative API configuration (with Swagger object):
    /*
    api: {
      spec: require('./swagger.json')
    },
    */

    //CDN client configuration
    cdn: {
        host: 'cdn.snap.menu',
        secure: false,
        path: '/',
        application: application
    },

    //Socket client configuration
    socket: {
        host: 'socket.managesnap.com',
        secure: true,
        path: '/snap/',
        application: application
    },

    //Partial configuration example:
    //Legacy client configuration
    legacy: {
        host: 'api-jwt.managesnap.com'
        //See? No secure or path here
    },

    //Exclude client example:
    //Upload client configuration
    upload: false
});

/* Manager configuration */
const manager = new DtsApi.Manager([
    application
])

Usage Example

class Application {
  constructor(mainApplication, client, manager) {
    this._mainApplication = mainApplication;
    this._client = client;
    this._manager = manager;
  }

  //An OAuth callback is received (i.e. check current URL on application launch)
  oauthCallback(application, data) {
    //Create new token to save
    let token = {
      access_token: data.access_token,
      refresh_token: data.refresh_token,
      expires_in: data.expires_in
    };

    this._saveToken(token, application);
  }

  //Watch for API token events
  watchAuthentication() {
    this._manager.tokenExpiring.add((token, application) => this._refreshToken(token, application));
    this._manager.tokenExpired.add((token, application) => this._refreshToken(token, application));
  }

  _refreshToken(token, application) {
    return this.client.api.request('oauth2', 'grantToken', {
      grant_type: 'refresh_token',
      client_id: application.client_id,
      refresh_token: token.refresh_token
    })
    .then(token => {
      console.log('OAuth token refreshed', token);
      this._saveToken(token, application);
    })
    .catch(e => {
      console.error('Unable to refresh an OAuth token.', e);

      if (this._manager.isAccessValid(application)) {
        console.warn('Falling back to an existing OAuth token.');
        return Promise.resolve();
      }

      return Promise.reject(e);
    });
  }

  _saveToken(token, application) {
    this._client.authorize(application, token);
    this._manager.saveToken(application, token);
  }

  //Enable WebSocket connections and listen to client events
  configureWebSocket() {
    this._client.socket.isEnabled = true; //Explicitly indicate when you need a WebSocket connection

    console.log('WebSocket is connected:', this._client.socket.isConnected);
    this._client.socket.isConnectedChanged.add(isConnected => console.log('WebSocket is connected:', isConnected));
    this._client.socket.onError.add(err => console.error('WebSocket error:', err));
  }

  //Restore authentication on application startup
  startup() {
    return this._manager.applications.reduce((promise, application) => {
      let token = this._manager.getToken(application); //Read active token

      //Check if refresh token has not expired
      if (!this._manager.isAccessValid(application)) {
        //No refresh token found
        if (!token || !token.refresh_token) {
          return promise;
        }

        return promise.then(() => {
          console.log(`Refreshing access for '${application.client_id}'...`);

          return this.refreshAccess(application)
          .then(() => console.log(`Access for '${application.client_id}' has been refreshed.`))
          .catch(e => {
            console.warn(`Unable to refresh access for '${application.client_id}'`, e);
            return Promise.resolve();
          });
        });
      }

      // Optional: authenticate Raygun with the current API user
      //
      // if (application === this._mainApplication) {
      //   let metadata = this._manager.getTokenMetadata(token);
      //   Raygun.setUser(metadata.location, false, undefined, undefined, undefined, metadata.device);
      // }

      return promise.then(() => this._client.authorize(application, token));
    }, Promise.resolve());
  }

  //Check if API token is valid
  get isAccessValid() {
    let token = this._manager.getToken(this._mainApplication);

    if (!token || !token.refresh_token) {
      return false;
    }

    return this._manager.isAccessValid(this._mainApplication);
  }

  //Get an OAuth authorization URL to redirect when an API token is not valid
  authorize() {
    return this._client.api.getAuthorizationUrl(this._mainApplication)
    .then(url => window.location = url);
  }

  //Call API
  loadLocationInfo(id) {
    return this._client.api.request('asset', 'assetLocationRead', { token: id });
  }

  //Load data from CDN
  loadData() {
    return this._client.cdn.load('deployment/847429c4-bb61-40b5-b3fa-a4a50151030c/4a69171b-9b85-416f-af2e-71739ed865e2.json');
  }

  //Load data from CDN as a binary blob
  loadBinary() {
    return this._client.cdn.loadBinary('version/2b8cd287-d93c-455e-a1ef-a4b100f06550_470_410.jpg');
  }

  //Subscribe to a WebSocket topic
  listenToTopic() {
    this._client.socket.subscribe('location_847429c4-bb61-40b5-b3fa-a4a50151030c', message => {
      console.log(message);
    });
  }

  //Send a WebSocket message to a topic
  publishToTopic(message) {
    this._client.socket.publish('location_847429c4-bb61-40b5-b3fa-a4a50151030c', message);
  }

  //Send a WebSocket event
  sendEvent(message) {
    this._client.socket.emit('some_event', message);
  }
}