Modular lightweight request library extendable by plugins.
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 can inject to the request flow to change its execution or adjust request\response data.
protocol/http
- base plugin to make request with http\httpsprotocol/jsonp
- plugin to make jsonp requestscache/deduplicate
- deduplicate identical requestscache/memory
- caches responses into memorycache/etag
- [!server only] cache based on etag http-headercache/fallback
- [!server only] stores response data to disk and returns from cache only for errored requestscache/persistent
- [!browser only] caching data at IndexedDB to keep cache among page reloadslog
- logs the data of request executionbatch
- groups several request into single onevalidate
- validate response or restore on errorstransform-url
- transforms url string for every requestcircuit-breaker
- [!server only] fault protections