[![npm](https://img.shields.io/npm/v/kronos-interceptor.svg)](https://www.npmjs.com/package/kronos-interceptor)
[![Greenkeeper](https://badges.greenkeeper.io/Kronos-Integration/kronos-interceptor.svg)](https://greenkeeper.io/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/Kronos-Integration/kronos-interceptor)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Build Status](https://secure.travis-ci.org/Kronos-Integration/kronos-interceptor.png)](http://travis-ci.org/Kronos-Integration/kronos-interceptor)
[![codecov.io](http://codecov.io/github/Kronos-Integration/kronos-interceptor/coverage.svg?branch=master)](http://codecov.io/github/Kronos-Integration/kronos-interceptor?branch=master)
[![Coverage Status](https://coveralls.io/repos/Kronos-Integration/kronos-interceptor/badge.svg)](https://coveralls.io/r/Kronos-Integration/kronos-interceptor)
[![Known Vulnerabilities](https://snyk.io/test/github/Kronos-Integration/kronos-interceptor/badge.svg)](https://snyk.io/test/github/Kronos-Integration/kronos-interceptor)
[![GitHub Issues](https://img.shields.io/github/issues/Kronos-Integration/kronos-interceptor.svg?style=flat-square)](https://github.com/Kronos-Integration/kronos-interceptor/issues)
[![Stories in Ready](https://badge.waffle.io/Kronos-Integration/kronos-interceptor.svg?label=ready&title=Ready)](http://waffle.io/Kronos-Integration/kronos-interceptor)
[![Dependency Status](https://david-dm.org/Kronos-Integration/kronos-interceptor.svg)](https://david-dm.org/Kronos-Integration/kronos-interceptor)
[![devDependency Status](https://david-dm.org/Kronos-Integration/kronos-interceptor/dev-status.svg)](https://david-dm.org/Kronos-Integration/kronos-interceptor#info=devDependencies)
[![docs](http://inch-ci.org/github/Kronos-Integration/kronos-interceptor.svg?branch=master)](http://inch-ci.org/github/Kronos-Integration/kronos-interceptor)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![downloads](http://img.shields.io/npm/dm/kronos-interceptor.svg?style=flat-square)](https://npmjs.org/package/kronos-interceptor)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

# kronos-interceptor

introspects / modifies requests as they pass between endpoints

<!-- skip-example -->

```javascript
const ki = require('kronos-interceptor');

const endpoint = { get name() { return 'aName'; }, receive() {}};
const interceptor1 = new ki.Interceptor({},endpoint);
const interceptor2 = new ki.Interceptor({},endpoint);

interceptor1.connected = interceptor2

const request = {};
const promise = interceptor1.receive(request);
```

# API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [rejectingReceiver](#rejectingreceiver)
    -   [Parameters](#parameters)
-   [ConnectorMixin](#connectormixin)
    -   [Parameters](#parameters-1)
-   [Interceptor](#interceptor)
    -   [Parameters](#parameters-2)
    -   [logger](#logger)
    -   [type](#type)
    -   [configurationAttributes](#configurationattributes)
    -   [configure](#configure)
        -   [Parameters](#parameters-3)
    -   [toJSON](#tojson)
    -   [reset](#reset)
    -   [receive](#receive)
        -   [Parameters](#parameters-4)
    -   [configurationAttributes](#configurationattributes-1)
-   [TimeoutInterceptor](#timeoutinterceptor)
    -   [name](#name)
-   [rejectUnlessResolvedWithin](#rejectunlessresolvedwithin)
    -   [Parameters](#parameters-5)
-   [StatsCollectorInterceptor](#statscollectorinterceptor)
    -   [receive](#receive-1)
        -   [Parameters](#parameters-6)
    -   [name](#name-1)
-   [LimitingInterceptor](#limitinginterceptor)
    -   [Parameters](#parameters-7)
    -   [name](#name-2)

## rejectingReceiver

rejecting receiver used to signal a not present connection
when used always delivers a rejecting promise

### Parameters

-   `request`  

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)>** 

## ConnectorMixin

Mixin to make endpoints/interceptors connectable
Forms a single linked list

### Parameters

-   `superclass`  

## Interceptor

**Extends ConnectorMixin(class {})**

Base interceptor. The base class for all the interceptors
Calls configure() and reset().

### Parameters

-   `config` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The interceptor configuration object.
-   `endpoint` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the endpoint object this interceptor will be attached to.

### logger

use endpoint owner as logger

### type

The instance method returning the type.
Defaults to the constructors name (class name)

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 

### configurationAttributes

Meta description of the configuration

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 

### configure

Takes attribute values from config parameters
and copies them over to the object.
Copying is done according to configurationAttributes
Which means we loop over all configuration attributes
and then for each attribute decide if we use the default, call a setter function
or simply assign the attribute value

#### Parameters

-   `config` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 

### toJSON

Deliver the json representation

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** json representation

### reset

forget all accumulated information

### receive

The receive method. This method receives the request from the leading interceptor and calls the
trailing interceptor

#### Parameters

-   `request` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the request from the leading interceptor
-   `oldRequest` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the oldRequest from the leading interceptor.
           This is a special case. As some interceptors are in charge of copying and creating the
           request objects, the step will call the interceptor chain with the both requests.
           At some point of the interceptor chain only the request itself will survive.
           But all interceptors designed to be inserted early in the interceptor chain of a sending
           endpoint should pass both requests to the next interceptor.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** 

### configurationAttributes

Meta description of the configuration

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 

## TimeoutInterceptor

**Extends Interceptor**

Rejects a request if it does not resolve in a given time

### name

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 'timeout'

## rejectUnlessResolvedWithin

### Parameters

-   `promise` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** 
-   `timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** in miliseconds
-   `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** 

## StatsCollectorInterceptor

**Extends Interceptor**

Interceptor to collect processing time, number of processed and
failed requests

### receive

Logs the time the requests takes

#### Parameters

-   `request`  
-   `oldRequest`  

### name

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 'collect-request-stats'

## LimitingInterceptor

**Extends Interceptor**

Limits the number of concurrent requests.
Requests can be delayed or rejected.
Sample config:
[
 { count: 20 },
 { count: 10, delay:  100 },
 { count:  5, delay:   10 }
]
 1 -  4 : no delay
 5 -  9 : 10ms delay
10 - 19 : 100ms delay
20      : reject
default is to reject when more than 10 requests are on the way

### Parameters

-   `config`  
-   `endpoint`  

### name

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 'request-limit'

# install

With [npm](http://npmjs.org) do:

```shell
npm install kronos-interceptor
```

# license

BSD-2-Clause
