# @tinkoff/request-core

> Request library extendable by plugins

Latest version **0.10.0** (published 2023-12-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install @tinkoff/request-core
pnpm add @tinkoff/request-core
yarn add @tinkoff/request-core
bun add @tinkoff/request-core
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.10.0 |
| Published | 2023-12-06 |
| First published | 2018-10-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 27 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 62 |
| Author | Oleg Sorokotyaga |
| Maintainers | yeahga, sradyukov, hondasmx, makar_l, tinkoffbank, dmitry-korolev, rouland, ishivan, ytsareva, shoom3301, waterplea, meskill, marsibarsi, zig-green, super_oleg, dersizes, alexkvak |
| Keywords | request |

## Links

- npm: https://www.npmjs.com/package/@tinkoff/request-core
- Repository: https://github.com/Tinkoff/tinkoff-request
- Homepage: https://github.com/Tinkoff/tinkoff-request#readme
- Issues: https://github.com/Tinkoff/tinkoff-request/issues
- npm.io page: https://npm.io/package/@tinkoff/request-core

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.1.3
- [@tinkoff/utils](https://npm.io/package/@tinkoff/utils.md) ^2.0.0

## Recent versions

- 0.10.0 (latest) — 2023-12-06
- 0.9.3 — 2023-08-02
- 0.9.2 — 2022-04-28
- 0.9.1 — 2022-03-12
- 0.9.0 — 2022-03-03
- 0.9.0-alpha.0 — 2022-03-03
- 0.8.9 — 2021-08-18
- 0.8.8 — 2020-12-02
- 0.8.7 — 2020-06-18
- 0.8.6 — 2020-04-07
- 0.8.5 — 2020-03-03
- 0.8.4 — 2020-02-17
- 0.8.3 — 2019-11-19
- 0.8.2 — 2019-10-22
- 0.8.1 — 2019-08-17
- … 7 more at https://npm.io/package/@tinkoff/request-core/versions

## README

# @tinkoff/request
Modular lightweight request library extendable by plugins.

[Documentation](https://tinkoff.github.io/tinkoff-request/)

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/tinkoff-request-playground-0t1wrs?file=/src/index.ts)

## Example of usage
```javascript
import request from '@tinkoff/request-core';
import log from '@tinkoff/request-plugin-log';
import deduplicateCache from '@tinkoff/request-plugin-cache-deduplicate';
import memoryCache from '@tinkoff/request-plugin-cache-memory';
import persistentCache from '@tinkoff/request-plugin-cache-persistent';
import fallbackCache from '@tinkoff/request-plugin-cache-fallback';
import validate from '@tinkoff/request-plugin-validate';
import http from '@tinkoff/request-plugin-protocol-http';

const makeRequest = request([
    // The order of plugins is important
    log(), // log-plugin is first as we want it always execute
    memoryCache({ allowStale: true }), // passing parameters for specific plugin, see plugin docs
    persistentCache(),
    fallbackCache(), // fallbackCache is the last as it executed only for errored requests
    deduplicateCache(), // plugins for cache are coming from simple one to complex as if simple cache has cached value - it will be returned and the others plugins won't be called
    validate({
        validator: ({ response }) => {
            if (response.type === 'json') { return null; }
            return new Error('NOT json format');
        }
    }), // validate is placed exactly before plugin for actual request since there is no point to validate values from caches
    http() // on the last place the plugin to make actual request, it will be executed only if no plugin before changed the flow of request
]);

makeRequest({
    url: 'https://config.mysite.ru/resources?name=example'
})
    .then(result => console.log(result))
    .catch(error => console.error(error))
```

## Plugins
Plugins can inject to the request flow to change its execution or adjust request\response data.

### List of plugins
1. `protocol/http` - base plugin to make request with http\https
1. `protocol/jsonp` - plugin to make jsonp requests
1. `cache/deduplicate` - deduplicate identical requests
1. `cache/memory` - caches responses into memory
1. `cache/etag` - [!server only] cache based on etag http-header
1. `cache/fallback` - [!server only] stores response data to disk and returns from cache only for errored requests
1. `cache/persistent` - [!browser only] caching data at IndexedDB to keep cache among page reloads
1. `log` - logs the data of request execution
1. `batch` - groups several request into single one
1. `validate` - validate response or restore on errors
1. `transform-url` - transforms url string for every request
1. `circuit-breaker` - [!server only] fault protections

---
_Source: https://npm.io/package/@tinkoff/request-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
