request-interceptor

1.0.1 • Public • Published

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

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

Readme

Keywords

Package Sidebar

Install

npm i request-interceptor

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • pringels