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

npo-api-interceptor

v1.9.0

Published

Request Interceptor for using the NPO API with Axios or AngularJS's $http service. Calculates and adds the necessary authorization headers to the request. The NPO API Interceptor can be used both in the browser and in Node.js.

Downloads

21

Readme

NPO API Interceptor

Request Interceptor for using the NPO API with Axios, AngularJS's $http service or even jQuery.ajax. Calculates and adds the necessary authorization headers to the request. The NPO API Interceptor can be used both in the browser and in Node.js.

Installation

Install via npm:

npm install --save npo-api-interceptor

or Yarn:

yarn add npo-api-interceptor

If you don't use a module bundler like Webpack or Browserify in your project, a browser build is available at lib/npoapiinterceptor.js. This build makes the NPO API Interceptor available on the global npoApiInterceptor variable.

As this depends on jsSHA, you need to include that dependency yourself.

Usage with a module bundler

After installation (see above) you can import the interceptor and use it. The interceptor takes at least an API key and secret and returns a function (the actual interceptor) that Axios or AngularJS will call when performing requests. Example:

import axios from 'axios'
import npoApiInterceptor from 'npo-api-interceptor'

axios.interceptors.request.use(npoApiInterceptor({
  key: '<your-key>',
  secret: '<your-secret>'
}))

Usage without a module bundler

After installation (see above) the interceptor is available on the global npoApiInterceptor variable. The interceptor takes at least an API key and secret and returns a function (the actual interceptor) that Axios or AngularJS will call when performing requests. Example:

axios.interceptors.request.use(npoApiInterceptor({
  key: '<your-key>',
  secret: '<your-secret>'
}))

Usage with Angular

The NPO API Interceptor can be provided as an interceptor to the $http service. Example using an anonymous factory:

$httpProvider.interceptors.push(function() {
  return {
    request: npoApiInterceptor({
      key: '<your-key>',
      secret: '<your-secret>'
    })
  };
});

Usage with jQuery.ajax

Even though jQuery.ajax() doesn't have the concept op request interceptors, the NPO API Interceptor can be used to add the necessary headers to the request. But you need to be prepared to jump through a couple of hoops. Example of a Find media (POST /media) request:

var interceptor = window.npoApiInterceptor({
  key: '<your-key>',
  secret: '<your-secret>'
});

// An object of the URL parameters you will use:
var params = {
  profile: 'eo',
  max: '100'
};

var url = 'https://rs.poms.omroep.nl/v1/api/media/';

var config = {
  type: 'POST',
  // Add params as query string to the URL
  url: url + '?' + jQuery.params(params),
  // Add params property for NPO API Interceptor, jQuery.ajax doesn't use it
  params: params,
  data: JSON.stringify({
    searches: {
      types: 'SERIES'
    }
  }),
  dataType: 'json'
};

interceptor(config).then(function(config) {
  // Wrap jQuery.ajax in a Promise to return a real Promise instead of a Promise-like jqXHR object
  return new Promise(function(resolve, reject) {
    jQuery.ajax(config).done(resolve).fail(reject);
  });
});

Usage in Node.js

When using the NPO API Interceptor server-side, the required Origin header isn't present on API requests. Therefore, you should specify an origin in the interceptor config:

axios.interceptors.request.use(npoApiInterceptor({
  key: '<your-key>',
  secret: '<your-secret>',
  origin: 'https://www.example.com'
}))

Note that this origin should be whitelisted to access the NPO API.

Browser support

The NPO API Interceptor depends on one ES2015 feature: Promises. A polyfills is not included, you need to polyfill it in your project, depending on your browser support level.

Development

If you want, you can use nvm to manage multiple Node.js versions on your machine.

We use JavaScript Standard Style to format the code. Numerous text editor plugins are available, so you can set up your editor to format the JS code for you.

We use Babel and Rollup to transpile and bundle the code. The source code is in src/ and multiple target bundles are built in lib/.

We use Yarn, but npm can also be used. For publishing to the Yarn and npm registries, we currently use npm, because Yarn publish resulted in invalid tar files.

To get started:

  1. Clone the repository.
  2. Run nvm to switch to the right Node.js version: nvm use.
  3. Install the dependencies: yarn install.

To publish a new version:

  1. Create a new version number: npm version major|minor|patch. See SemVer. This will run the build command and add the built files to the new version.
  2. Publish to the registry: npm publish. The lint and build commands will be run automatically, to make sure we're always publishing the latest source and adhere to the style guide.