1.0.1 • Published 7 years ago

request-interceptor v1.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

request-interceptor

A library that delays async XHR and Fetch requests until an event is triggered.

Installation

npm install request-interceptor

Usage

Import the module into your application's entry point (EG main.js)

ES6:

import RequestInterceptor from 'request-interceptor'

ES5:

var RequestInterceptor = require('request-interceptor');

Initilise the interceptor to start delaying requests:

RequestInterceptor({});

NOTE: Ensure you initialise the interceptor before any XHR or Fetch requests are triggered by your application code.

Options

The interceptor accepts the following optional arguments:


readyEvent The event it should listen for. Standard or custom events are supported. default = DOMContentLoaded

whiteList An array of strings. Any request url containing at least one of these strings will be passed through. default = []

allowCurrentHost An array of strings. Any request url containing at least one of these strings will be passed through. default = false


Basic Example

RequestInterceptor({
	readyEvent: 'readystatechange',
	whiteList: ['customhost.com', 'analytics.google.com'],
	allowCurrentHost: true
});

Example using a Custom Event

RequestInterceptor({
	readyEvent: 'customReadyEvent',
});

const event = new CustomEvent("customReadyEvent");
window.dispatchEvent(event);

TODO

  • Domain/URL Whitelisting
  • Allow through requests on current host
  • Add support for the Fetch API
  • Add support for Custom Events
  • Write tests
  • Verify browser support