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

@deskpro/deskpro-api-client-javascript

v2.0.1

Published

JavaScript client for use with the Deskpro API.

Downloads

68

Readme

Deskpro API JavaScript Client

JavaScript client for use with the Deskpro API.

Build Status

Installing

npm install --save @deskpro/deskpro-api-client-javascript

Basic Usage

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

// Create a new client with the URL to your Deskpro instance.
const client = new DeskproClient('http://deskpro-dev.com');

// Set the ID of the user to authenticate, and either the auth
// key or token.
// client.setAuthKey(1, 'dev-admin-code');
// client.setAuthToken(1, 'AWJ2BQ7WG589PQ6S862TCGY4');

client.get('/articles')
  .then((resp) => {
    console.log(resp.data);
    console.log(resp.meta);
  })
  .catch((err) => {
    console.error(err.message);
  });

Posting values

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

const body = {
  title: 'This is a title',
  content: 'This is the content',
  content_input_type: 'rte',
  status: 'published'
};

client.post('/articles', body)
  .then((resp) => {
    console.log(resp.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

Uploading files

Sending values using multipart/form-data, for instance to upload files, is done by passing an object with the property "multipart" to the post() or put() methods.

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const FormData = require('form-data');
const fs = require('fs');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

// Create a form with the file data to upload.
const fileStream = fs.createReadStream('test.gif');
const form = new FormData();
form.append('file', fileStream, 'test.gif');

// Create body data containing the property 'multipart', which
// contains the form data.
const body = {
  multipart: form
};

client.post('/blobs/temp', body)
  .then((resp) => {
    console.log(resp.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

Batch request

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

client.batch({
  one: {
    method: 'GET',
    url: '/articles/105'
  },
  two: '/articles/106'
}).then((resp) => {
    console.log(resp.one.data);
    console.log(resp.two.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

Interpolating URLs

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

const params = {
  id:       5,
  parentId: 101,
  limit:    25,
  offset:   100
};

// The params are interplated into the endpoint URL so it becomes:
// "/articles/101/5?limit=25&offset=100"
client.get('/articles/{parentId}/{id}', params);

Browser Usage

DeskproClient may also be used in the browser.

<!DOCTYPE>
<html>
  <head>
    <!-- Import DeskproClient from node_modules -->
    <script src="node_modules/@deskpro/deskpro-api-client-javascript/dist/index.js"></script>
    
    <!-- Or import it from CDN -->
    <!-- <script src="https://unpkg.com/@deskpro/[email protected]/dist/index.js"></script> -->
  </head>
  <body>
    <script>
      var client = new DeskproClient('http://deskpro-dev.com');
      client.setAuthKey(1, 'dev-admin-code');

      client.get('/articles')
          .then(function(resp) {
            console.log(resp.data);
          })
          .catch(function(err) {
            console.error(err.message);
          });
    </script>
  </body>
</html>

Default Headers

Use the setDefaultHeaders() method to set headers that get sent with each request.

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');
client.setDefaultHeaders({
  'X-Custom-Value': 'foo'
});

client.get('/articles')
  .then((resp) => {
    console.log(resp.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

Logging

Pass a function to the setLogger() method to enable request logging.

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');
client.setLogger(console.log);

client.get('/articles')
  .then((resp) => {
    console.log(resp.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

Axios

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

// @see https://github.com/axios/axios#global-axios-defaults
client.getHTTPClient().defaults.timeout = 2500;

client.get('/articles')
  .then((resp) => {
    console.log(resp.data);
  })
  .catch((err) => {
    console.error(err.message);
  });

For debugging purposes, the request, response, and exception generated by the last operation may be retrieved for inspection.

const DeskproClient = require('@deskpro/deskpro-api-client-javascript');

const client = new DeskproClient('http://deskpro-dev.com');
client.setAuthKey(1, 'dev-admin-code');

client.get('/articles')
  .then(() => {
    console.log(client.getLastHTTPRequest());
    console.log(client.getLastHTTPResponse());
    console.log(client.getLastHTTPRequestException());
  });

Testing

Tests are run using the npm "test" script.

npm test