@tramvai/tinkoff-request-http-client-adapter 0.9.229 → 0.9.233

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@ import { createAgent } from './agent/createAgent.browser.browser.js';
12
12
 
13
13
  const defaultAgent = createAgent();
14
14
  function createTinkoffRequest(options) {
15
- const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, ...defaults } = options;
15
+ const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, interceptors, ...defaults } = options;
16
16
  const log = logger && logger(`${name}:initialization`);
17
17
  const plugins = [];
18
18
  plugins.push({
@@ -12,7 +12,7 @@ import { createAgent } from './agent/createAgent.es.js';
12
12
 
13
13
  const defaultAgent = createAgent();
14
14
  function createTinkoffRequest(options) {
15
- const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, ...defaults } = options;
15
+ const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, interceptors, ...defaults } = options;
16
16
  const log = logger && logger(`${name}:initialization`);
17
17
  const plugins = [];
18
18
  plugins.push({
@@ -29,7 +29,7 @@ var retry__default = /*#__PURE__*/_interopDefaultLegacy(retry);
29
29
 
30
30
  const defaultAgent = createAgent.createAgent();
31
31
  function createTinkoffRequest(options) {
32
- const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, ...defaults } = options;
32
+ const { logger, name, disableCache, enableCircuitBreaker, createCache, cacheTime = 30000, defaultTimeout, validator, errorValidator, errorModificator, circuitBreakerOptions = {}, getCacheKey, lruOptions = { max: 1000, maxAge: cacheTime }, agent, querySerializer, retryOptions, interceptors, ...defaults } = options;
33
33
  const log = logger && logger(`${name}:initialization`);
34
34
  const plugins = [];
35
35
  plugins.push({
@@ -5,49 +5,62 @@ import { mergeOptions } from './mergeOptions.browser.js';
5
5
  class HttpClientAdapter extends BaseHttpClient {
6
6
  constructor({ options, makeRequest, }) {
7
7
  super();
8
+ // eslint-disable-next-line sort-class-members/sort-class-members
9
+ this._processMakeRequest = async (reqAfterInterceptors, { modifyRequest, modifyResponse, modifyError, }) => {
10
+ const { method, body, requestType, ...adaptedReq } = modifyRequest
11
+ ? modifyRequest(reqAfterInterceptors)
12
+ : reqAfterInterceptors;
13
+ if (method) {
14
+ adaptedReq.httpMethod = method;
15
+ }
16
+ if (body) {
17
+ adaptedReq.payload = body;
18
+ }
19
+ if (requestType) {
20
+ adaptedReq.type = requestType;
21
+ }
22
+ const res = this.makeRequest(adaptedReq);
23
+ try {
24
+ const payload = await res;
25
+ const status = getStatus(res);
26
+ const headers = getHeaders(res);
27
+ const resToModify = {
28
+ payload,
29
+ status,
30
+ headers,
31
+ };
32
+ return modifyResponse ? modifyResponse(resToModify) : resToModify;
33
+ }
34
+ catch (error) {
35
+ const meta = res.getExternalMeta();
36
+ const status = getStatus(res);
37
+ const headers = getHeaders(res);
38
+ // Useful for logging
39
+ const errorWithMeta = Object.assign(error, {
40
+ __meta: meta,
41
+ status,
42
+ headers,
43
+ });
44
+ throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
45
+ }
46
+ };
8
47
  this.options = options;
9
48
  this.makeRequest = makeRequest;
10
49
  }
11
- async request(req) {
50
+ request(req) {
12
51
  // применяем дефолтные опции до вызова modifyRequest на объекте запроса
13
52
  const optionsWithDefaults = mergeOptions(this.options, req);
14
- const { modifyRequest, modifyResponse, modifyError, ...reqWithDefaults } = optionsWithDefaults;
15
- const { method, body, requestType, ...adaptedReq } = modifyRequest
16
- ? modifyRequest(reqWithDefaults)
17
- : reqWithDefaults;
18
- if (method) {
19
- adaptedReq.httpMethod = method;
20
- }
21
- if (body) {
22
- adaptedReq.payload = body;
23
- }
24
- if (requestType) {
25
- adaptedReq.type = requestType;
26
- }
27
- const res = this.makeRequest(adaptedReq);
28
- try {
29
- const payload = await res;
30
- const status = getStatus(res);
31
- const headers = getHeaders(res);
32
- const resToModify = {
33
- payload,
34
- status,
35
- headers,
36
- };
37
- return modifyResponse ? modifyResponse(resToModify) : resToModify;
38
- }
39
- catch (error) {
40
- const meta = res.getExternalMeta();
41
- const status = getStatus(res);
42
- const headers = getHeaders(res);
43
- // Useful for logging
44
- const errorWithMeta = Object.assign(error, {
45
- __meta: meta,
46
- status,
47
- headers,
53
+ const { modifyRequest, modifyResponse, modifyError, interceptors, ...reqWithDefaults } = optionsWithDefaults;
54
+ let _next = (_req) => this._processMakeRequest(_req, { modifyRequest, modifyResponse, modifyError });
55
+ if (interceptors) {
56
+ Array.from(interceptors)
57
+ .reverse()
58
+ .forEach((interceptor) => {
59
+ const _prevNext = _next;
60
+ _next = (_req) => interceptor(_req, _prevNext);
48
61
  });
49
- throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
50
62
  }
63
+ return _next(reqWithDefaults);
51
64
  }
52
65
  fork(forkOptions = {}, mergeOptionsConfig = {}) {
53
66
  return new HttpClientAdapter({
@@ -12,4 +12,5 @@ export declare class HttpClientAdapter extends BaseHttpClient implements HttpCli
12
12
  fork(forkOptions?: HttpClientRequest, mergeOptionsConfig?: {
13
13
  replace?: boolean;
14
14
  }): HttpClientAdapter;
15
+ private _processMakeRequest;
15
16
  }
@@ -5,49 +5,62 @@ import { mergeOptions } from './mergeOptions.es.js';
5
5
  class HttpClientAdapter extends BaseHttpClient {
6
6
  constructor({ options, makeRequest, }) {
7
7
  super();
8
+ // eslint-disable-next-line sort-class-members/sort-class-members
9
+ this._processMakeRequest = async (reqAfterInterceptors, { modifyRequest, modifyResponse, modifyError, }) => {
10
+ const { method, body, requestType, ...adaptedReq } = modifyRequest
11
+ ? modifyRequest(reqAfterInterceptors)
12
+ : reqAfterInterceptors;
13
+ if (method) {
14
+ adaptedReq.httpMethod = method;
15
+ }
16
+ if (body) {
17
+ adaptedReq.payload = body;
18
+ }
19
+ if (requestType) {
20
+ adaptedReq.type = requestType;
21
+ }
22
+ const res = this.makeRequest(adaptedReq);
23
+ try {
24
+ const payload = await res;
25
+ const status = getStatus(res);
26
+ const headers = getHeaders(res);
27
+ const resToModify = {
28
+ payload,
29
+ status,
30
+ headers,
31
+ };
32
+ return modifyResponse ? modifyResponse(resToModify) : resToModify;
33
+ }
34
+ catch (error) {
35
+ const meta = res.getExternalMeta();
36
+ const status = getStatus(res);
37
+ const headers = getHeaders(res);
38
+ // Useful for logging
39
+ const errorWithMeta = Object.assign(error, {
40
+ __meta: meta,
41
+ status,
42
+ headers,
43
+ });
44
+ throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
45
+ }
46
+ };
8
47
  this.options = options;
9
48
  this.makeRequest = makeRequest;
10
49
  }
11
- async request(req) {
50
+ request(req) {
12
51
  // применяем дефолтные опции до вызова modifyRequest на объекте запроса
13
52
  const optionsWithDefaults = mergeOptions(this.options, req);
14
- const { modifyRequest, modifyResponse, modifyError, ...reqWithDefaults } = optionsWithDefaults;
15
- const { method, body, requestType, ...adaptedReq } = modifyRequest
16
- ? modifyRequest(reqWithDefaults)
17
- : reqWithDefaults;
18
- if (method) {
19
- adaptedReq.httpMethod = method;
20
- }
21
- if (body) {
22
- adaptedReq.payload = body;
23
- }
24
- if (requestType) {
25
- adaptedReq.type = requestType;
26
- }
27
- const res = this.makeRequest(adaptedReq);
28
- try {
29
- const payload = await res;
30
- const status = getStatus(res);
31
- const headers = getHeaders(res);
32
- const resToModify = {
33
- payload,
34
- status,
35
- headers,
36
- };
37
- return modifyResponse ? modifyResponse(resToModify) : resToModify;
38
- }
39
- catch (error) {
40
- const meta = res.getExternalMeta();
41
- const status = getStatus(res);
42
- const headers = getHeaders(res);
43
- // Useful for logging
44
- const errorWithMeta = Object.assign(error, {
45
- __meta: meta,
46
- status,
47
- headers,
53
+ const { modifyRequest, modifyResponse, modifyError, interceptors, ...reqWithDefaults } = optionsWithDefaults;
54
+ let _next = (_req) => this._processMakeRequest(_req, { modifyRequest, modifyResponse, modifyError });
55
+ if (interceptors) {
56
+ Array.from(interceptors)
57
+ .reverse()
58
+ .forEach((interceptor) => {
59
+ const _prevNext = _next;
60
+ _next = (_req) => interceptor(_req, _prevNext);
48
61
  });
49
- throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
50
62
  }
63
+ return _next(reqWithDefaults);
51
64
  }
52
65
  fork(forkOptions = {}, mergeOptionsConfig = {}) {
53
66
  return new HttpClientAdapter({
@@ -9,49 +9,62 @@ var mergeOptions = require('./mergeOptions.js');
9
9
  class HttpClientAdapter extends httpClient.BaseHttpClient {
10
10
  constructor({ options, makeRequest, }) {
11
11
  super();
12
+ // eslint-disable-next-line sort-class-members/sort-class-members
13
+ this._processMakeRequest = async (reqAfterInterceptors, { modifyRequest, modifyResponse, modifyError, }) => {
14
+ const { method, body, requestType, ...adaptedReq } = modifyRequest
15
+ ? modifyRequest(reqAfterInterceptors)
16
+ : reqAfterInterceptors;
17
+ if (method) {
18
+ adaptedReq.httpMethod = method;
19
+ }
20
+ if (body) {
21
+ adaptedReq.payload = body;
22
+ }
23
+ if (requestType) {
24
+ adaptedReq.type = requestType;
25
+ }
26
+ const res = this.makeRequest(adaptedReq);
27
+ try {
28
+ const payload = await res;
29
+ const status = http.getStatus(res);
30
+ const headers = http.getHeaders(res);
31
+ const resToModify = {
32
+ payload,
33
+ status,
34
+ headers,
35
+ };
36
+ return modifyResponse ? modifyResponse(resToModify) : resToModify;
37
+ }
38
+ catch (error) {
39
+ const meta = res.getExternalMeta();
40
+ const status = http.getStatus(res);
41
+ const headers = http.getHeaders(res);
42
+ // Useful for logging
43
+ const errorWithMeta = Object.assign(error, {
44
+ __meta: meta,
45
+ status,
46
+ headers,
47
+ });
48
+ throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
49
+ }
50
+ };
12
51
  this.options = options;
13
52
  this.makeRequest = makeRequest;
14
53
  }
15
- async request(req) {
54
+ request(req) {
16
55
  // применяем дефолтные опции до вызова modifyRequest на объекте запроса
17
56
  const optionsWithDefaults = mergeOptions.mergeOptions(this.options, req);
18
- const { modifyRequest, modifyResponse, modifyError, ...reqWithDefaults } = optionsWithDefaults;
19
- const { method, body, requestType, ...adaptedReq } = modifyRequest
20
- ? modifyRequest(reqWithDefaults)
21
- : reqWithDefaults;
22
- if (method) {
23
- adaptedReq.httpMethod = method;
24
- }
25
- if (body) {
26
- adaptedReq.payload = body;
27
- }
28
- if (requestType) {
29
- adaptedReq.type = requestType;
30
- }
31
- const res = this.makeRequest(adaptedReq);
32
- try {
33
- const payload = await res;
34
- const status = http.getStatus(res);
35
- const headers = http.getHeaders(res);
36
- const resToModify = {
37
- payload,
38
- status,
39
- headers,
40
- };
41
- return modifyResponse ? modifyResponse(resToModify) : resToModify;
42
- }
43
- catch (error) {
44
- const meta = res.getExternalMeta();
45
- const status = http.getStatus(res);
46
- const headers = http.getHeaders(res);
47
- // Useful for logging
48
- const errorWithMeta = Object.assign(error, {
49
- __meta: meta,
50
- status,
51
- headers,
57
+ const { modifyRequest, modifyResponse, modifyError, interceptors, ...reqWithDefaults } = optionsWithDefaults;
58
+ let _next = (_req) => this._processMakeRequest(_req, { modifyRequest, modifyResponse, modifyError });
59
+ if (interceptors) {
60
+ Array.from(interceptors)
61
+ .reverse()
62
+ .forEach((interceptor) => {
63
+ const _prevNext = _next;
64
+ _next = (_req) => interceptor(_req, _prevNext);
52
65
  });
53
- throw modifyError ? modifyError(errorWithMeta, adaptedReq) : errorWithMeta;
54
66
  }
67
+ return _next(reqWithDefaults);
55
68
  }
56
69
  fork(forkOptions = {}, mergeOptionsConfig = {}) {
57
70
  return new HttpClientAdapter({
@@ -23,6 +23,7 @@ function mergeOptions(options, nextOptions, config) {
23
23
  ...options.headers,
24
24
  ...nextOptions.headers,
25
25
  },
26
+ interceptors: [...(options.interceptors || []), ...(nextOptions.interceptors || [])],
26
27
  };
27
28
  const composeModifier = (modifier) => {
28
29
  if (options[modifier] && nextOptions[modifier]) {
@@ -23,6 +23,7 @@ function mergeOptions(options, nextOptions, config) {
23
23
  ...options.headers,
24
24
  ...nextOptions.headers,
25
25
  },
26
+ interceptors: [...(options.interceptors || []), ...(nextOptions.interceptors || [])],
26
27
  };
27
28
  const composeModifier = (modifier) => {
28
29
  if (options[modifier] && nextOptions[modifier]) {
@@ -31,6 +31,7 @@ function mergeOptions(options, nextOptions, config) {
31
31
  ...options.headers,
32
32
  ...nextOptions.headers,
33
33
  },
34
+ interceptors: [...(options.interceptors || []), ...(nextOptions.interceptors || [])],
34
35
  };
35
36
  const composeModifier = (modifier) => {
36
37
  if (options[modifier] && nextOptions[modifier]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/tinkoff-request-http-client-adapter",
3
- "version": "0.9.229",
3
+ "version": "0.9.233",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "browser": {
@@ -30,8 +30,8 @@
30
30
  "@tinkoff/request-plugin-validate": "^0.9.2",
31
31
  "@tinkoff/request-plugin-retry": "^0.2.3",
32
32
  "@tinkoff/utils": "^2.1.2",
33
- "@tramvai/http-client": "0.2.8",
34
- "@tramvai/tokens-common": "2.90.0",
33
+ "@tramvai/http-client": "0.2.9",
34
+ "@tramvai/tokens-common": "2.92.0",
35
35
  "tslib": "^2.4.0"
36
36
  },
37
37
  "sideEffects": false,