npm package: @kronos-integration/interceptor
Description: intercepects / modifies requests as they pass between endpoints
Latest version: 13.1.32

[![npm](https://img.shields.io/npm/v/@kronos-integration/interceptor.svg)](https://www.npmjs.com/package/@kronos-integration/interceptor)
[![License](https://img.shields.io/badge/License-0BSD-blue.svg)](https://spdx.org/licenses/0BSD.html)
[![Typed with TypeScript](https://flat.badgen.net/badge/icon/Typed?icon=typescript\&label\&labelColor=blue\&color=555555)](https://typescriptlang.org)
[![bundlejs](https://deno.bundlejs.com/?q=@kronos-integration/interceptor\&badge=detailed)](https://bundlejs.com/?q=@kronos-integration/interceptor)
[![downloads](http://img.shields.io/npm/dm/@kronos-integration/interceptor.svg?style=flat-square)](https://npmjs.org/package/@kronos-integration/interceptor)
[![GitHub Issues](https://img.shields.io/github/issues/Kronos-Integration/interceptor.svg?style=flat-square)](https://github.com/Kronos-Integration/interceptor/issues)
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FKronos-Integration%2Finterceptor%2Fbadge\&style=flat)](https://actions-badge.atrox.dev/Kronos-Integration/interceptor/goto)
[![Styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![Known Vulnerabilities](https://snyk.io/test/github/Kronos-Integration/interceptor/badge.svg)](https://snyk.io/test/github/Kronos-Integration/interceptor)

# @kronos-integration/interceptor

intercepects / modifies requests as they pass between endpoints

<!-- skip-example -->

```javascript
const { Interceptor } from '@kronos-integration/interceptor';

const endpoint = { get name() { return 'aName'; }, receive() {}};
const interceptor = new Interceptor();

const response = interceptor.receive(endpoint, arg1, arg2);
```

# API

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

### Table of Contents

*   [Endpoint](#endpoint)
*   [Interceptor](#interceptor)
    *   [Parameters](#parameters)
    *   [type](#type)
    *   [attributes](#attributes)
    *   [configure](#configure)
        *   [Parameters](#parameters-1)
    *   [toJSONWithOptions](#tojsonwithoptions)
        *   [Parameters](#parameters-2)
    *   [reset](#reset)
    *   [receive](#receive)
        *   [Parameters](#parameters-3)
    *   [attributes](#attributes-1)
*   [IntervalInterceptor](#intervalinterceptor)
    *   [Properties](#properties)
    *   [name](#name)
*   [LimitingInterceptor](#limitinginterceptor)
    *   [Parameters](#parameters-4)
    *   [name](#name-1)
*   [LoggingInterceptor](#logginginterceptor)
    *   [name](#name-2)
*   [StatsCollectorInterceptor](#statscollectorinterceptor)
    *   [receive](#receive-1)
        *   [Parameters](#parameters-5)
    *   [name](#name-3)
*   [TemplateInterceptor](#templateinterceptor)
    *   [name](#name-4)
*   [TimeoutInterceptor](#timeoutinterceptor)
    *   [Properties](#properties-1)
    *   [name](#name-5)
*   [rejectUnlessResolvedWithin](#rejectunlessresolvedwithin)
    *   [Parameters](#parameters-6)

## Endpoint

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

## Interceptor

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)?**&#x20;

### 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)**&#x20;

### attributes

Meta description of the configuration.

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

### configure

Takes attribute values from config parameters
and copies them over to the object.
Copying is done according to attributes.
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)?**&#x20;

### toJSONWithOptions

Deliver the json representation.

#### Parameters

*   `options` &#x20;

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

*   `endpoint` **[Endpoint](#endpoint)**&#x20;
*   `next` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;
*   `args` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<any>** the request from the leading interceptor

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>**&#x20;

### attributes

Meta description of the configuration

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

## IntervalInterceptor

**Extends Interceptor**

Only passes requests after inteval time has passed

### Properties

*   `interval` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;

### name

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

## 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` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**&#x20;

### name

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

## LoggingInterceptor

**Extends Interceptor**

logs args and result

### name

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

## StatsCollectorInterceptor

**Extends Interceptor**

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

### receive

Logs the time the requests takes

#### Parameters

*   `endpoint` &#x20;
*   `next` &#x20;
*   `args` **...any**&#x20;

### name

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

## TemplateInterceptor

**Extends Interceptor**

Map params into requests.

### name

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

## TimeoutInterceptor

**Extends Interceptor**

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

### Properties

*   `timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;

### name

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

## rejectUnlessResolvedWithin

Rejects promise when it is not resolved within given timeout.

### Parameters

*   `promise` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>**&#x20;
*   `timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** in miliseconds
*   `source` **[Interceptor](#interceptor)**&#x20;

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>**&#x20;

# install

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

```shell
npm install @kronos-integration/interceptor
```

# license

BSD-2-Clause