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

node-pdjs

v1.0.0-node.0.1.1

Published

NodeJS wrapper for the PagerDuty API

Downloads

3

Readme

PDJS (PagerDuty base JavaScript API) for Node

This is a simple JavaScript wrapper to the PagerDuty API

Making an API request

  1. Create a PDJSobj, with a subdomain and a token/api key

It was forked from https://github.com/eurica/pdjs and modified to also run in Node.js

Installation

npm install node-pdjs

In Node.js

var PDJSobj = require('node-pdjs');

In browser environment

Download pdjs.js file

<!-- Include jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> 
<!-- Include pdjs.js file -->
<script src="path/to/pdjs.js"></script>

Setup

    PDJS = new PDJSobj({
      token: "****************VDzA",
    })

This configuration uses version 2 of the PagerDuty REST and Events API as default. To request version 1, the api version needs to be specified on instantiation of the PDJSObj. The api_version parameter is optional.

PDJS = new PDJSobj({
  subdomain: 'webdemo',
  token: 'rcgtBVpizBZQjDzE3Hub',
  api_version: 'v1'
})
  1. Call an api request:
  • the res parameter is the URL path of the resource you want
  • the data parameter is optional

PDJS.api({ res: 'services', data: { limit: 20, }, success: function (data) { console.log(data) }, })

Making an API request

  1. Create a PDJSobj, with a token/api key
  2. Call an api request:
  • the res parameter is the URL path of the resource you want
  • the data parameter is optional
  1. Everything is asynchronous so you'll need:
  • a function to use on success
  • also optional is an error function

The res parameter may have an ID in it, here's the call to get the notes for incident PNCII2E

    PDJS.api({
      res: 'incidents/PNCII2E/notes',
      success: function (data) {
        alert(JSON.stringify(data))
      },
    })

POST and PUT requests are supported as well (although I can't give you any live examples, since the API key from webdemo is read-only, so go ahead and generate an API key from your own account)

For instance, here I'm adding a contact method for a user: [email protected], and then adding a notification rule to alert that email address after 900 minutes:

add_contact_method = function(user_id) {
  PDJS.api({
    res: 'users/'+user_id+'/contact_methods',
    type: 'POST',
    data: {
      contact_method: {
        type:'email',
        address:'[email protected]',
        label: 'Added from PDJS',
      }
    },
    success: function (data) {
      console.log('New contact method ID: ' + data.contact_method.id)
      add_notification_rule(user_id, data.contact_method.id, 900)
    }
  })
}

add_notification_rule = function(user_id, contact_method, start_delay_in_minutes) {
  PDJS.api({
    res: 'users/'+user_id+'/notification_rules',
    type: 'POST',
    data: {
      notification_rule: {
        contact_method_id: contact_method,
        start_delay_in_minutes: start_delay_in_minutes,
      }
    },
    success: function (data) {
      console.log(data)
      console.log('New notification rule ID: ' + data.notification_rule.id)
    }
  })
}

add_contact_method('PRJRF7T')

Triggering an incident (V2)

With Events API V2, alerts can be grouped with the same dedup_key.

PDJS.trigger({
  routing_key: '<v2 integration key>',
  event_action: 'trigger',
  data: {
    summary: 'Server on Fire',
    source: 'pdjs',
    severity: 'info'
    }
  })

Triggering an incident (V2)

The integration API has its own function as well

PDJS.trigger({
  service_key: '5eb2b9dae1b2480abf59f58c78ba06e7',
  description: 'Server on Fire',
  incident_key: (new Date()).toString(),
  details: {
    cause: 'PEBKAC'
  }
})

Again, you can specify a success function that will get a JavaScript object representing the incident:

{
  status: 'success',
  message: 'Event processed',
  incident_key: '8a803874eda340a09928f2631a39378d'
}

The api_all helper

There's also a helper method that will handle limits and offsets for lists longer than 100 elements:

PDJS.api_all({
  res: 'incidents',
  data: {
    since: '2013-08-01T09:53:17-07:00',
    until: '2013-08-14T09:53:17-07:00',
    status: 'resolved',
    fields: 'incident_number,status,created_on,service'
  },
  final_success: function(data) {
    console.log(data.total + ' objects!');
    console.log(data);
  },
  incremental_success: function(data) {
    console.log('Got data');
  }
})

It works the same, except you'll need to specify one or more of:

  • a function to run on final_success at the end
  • a function to run on each incremental_success

That's kind of nifty.

Examples

There's an examples directory:

To compile the base script:

This is written in CoffeeScript, so you're going to have to compile it to get JavaScript

coffee --output js/ --compile --watch --join pdjs.js coffee/ &

More info

This project is forked from https://github.com/PagerDuty/pdjs

It was originally written in CoffeeScript; however, this fork just updates the compiled pdjs.js file.

Are you using this? Let us know! @pagerduty or add it to our addons repo: https://github.com/PagerDuty/addons/

origin/master

You might notice that PDJS sends along some extra parameters, nothing scary, we use those to track QoS across our language-specific libraries.

Possibly also of interest is node-pagerduty to trigger PagerDuty incidents from node.js.