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.
Install via npm:
npm install --save npo-api-interceptor
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
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:
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:
Usage with Angular
The NPO API Interceptor can be provided as an interceptor to the
$http service. Example using an anonymous factory:
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;// An object of the URL parameters you will use:var params =profile: 'eo'max: '100';var url = '';var config =type: 'POST'// Add params as query string to the URLurl: url + '?' + jQuery// Add params property for NPO API Interceptor, jQuery.ajax doesn't use itparams: paramsdata: JSONdataType: 'json';;
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:
Note that this origin should be whitelisted to access the NPO API.
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.
If you want, you can use nvm to manage multiple Node.js versions on your machine.
To get started:
- Clone the repository.
- Run nvm to switch to the right Node.js version:
- Install the dependencies:
To publish a new version:
- Create a new version number:
npm version major|minor|patch. See SemVer. This will run the
buildcommand and add the built files to the new version.
- Publish to the registry:
npm publish. The
buildcommands will be run automatically, to make sure we're always publishing the latest source and adhere to the style guide.