@tramvai/module-http-client 4.41.79 → 4.41.93

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.
@@ -11,6 +11,6 @@ export declare class PapiService extends BaseHttpClient {
11
11
  papi: Deps['papi'];
12
12
  di: Deps['di'];
13
13
  constructor({ papi, di }: Deps);
14
- request<R = any>({ path, query, body }: HttpClientRequest): Promise<HttpClientResponse<R>>;
14
+ request<R = any>({ path, query, body, headers, }: HttpClientRequest): Promise<HttpClientResponse<R>>;
15
15
  }
16
16
  //# sourceMappingURL=papiService.d.ts.map
@@ -4,6 +4,10 @@ import { BaseHttpClient } from '@tramvai/http-client';
4
4
  import { createChildContainer } from '@tinkoff/dippy';
5
5
  import { getPapiParameters } from '@tramvai/papi';
6
6
  import { FASTIFY_REQUEST, FASTIFY_RESPONSE, PAPI_EXECUTOR } from '@tramvai/tokens-server-private';
7
+ import '@tinkoff/utils/function/identity';
8
+ import '@tinkoff/utils/object/pick';
9
+ import { comparePathWithPattern } from '../utils/comparePathWithPattern.es.js';
10
+ import { getPathParams } from '../utils/getPathParams.es.js';
7
11
 
8
12
  class PapiService extends BaseHttpClient {
9
13
  constructor({ papi, di }) {
@@ -11,13 +15,20 @@ class PapiService extends BaseHttpClient {
11
15
  this.papi = flatten(papi || []);
12
16
  this.di = di;
13
17
  }
14
- async request({ path, query, body }) {
18
+ async request({ path, query, body, headers, }) {
15
19
  var _a;
16
- const papiRoute = find((papi) => getPapiParameters(papi).path === `/${path}`, (_a = this.papi) !== null && _a !== void 0 ? _a : []);
20
+ const pathWithLeadingSlash = (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path : `/${path}`;
21
+ const papiRoute = find((papi) => comparePathWithPattern(pathWithLeadingSlash, getPapiParameters(papi).path), (_a = this.papi) !== null && _a !== void 0 ? _a : []);
17
22
  if (!papiRoute) {
18
23
  throw new Error(`papi handler '${path}' not found`);
19
24
  }
20
- const req = { headers: { host: 'localhost' }, cookies: {}, query, body };
25
+ const req = {
26
+ headers: { ...headers, host: 'localhost' },
27
+ cookies: {},
28
+ query,
29
+ body,
30
+ params: getPathParams(pathWithLeadingSlash, getPapiParameters(papiRoute).path),
31
+ };
21
32
  const res = {};
22
33
  const childDi = createChildContainer(this.di, [
23
34
  {
@@ -8,6 +8,10 @@ var httpClient = require('@tramvai/http-client');
8
8
  var dippy = require('@tinkoff/dippy');
9
9
  var papi = require('@tramvai/papi');
10
10
  var tokensServerPrivate = require('@tramvai/tokens-server-private');
11
+ require('@tinkoff/utils/function/identity');
12
+ require('@tinkoff/utils/object/pick');
13
+ var comparePathWithPattern = require('../utils/comparePathWithPattern.js');
14
+ var getPathParams = require('../utils/getPathParams.js');
11
15
 
12
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
17
 
@@ -20,13 +24,20 @@ class PapiService extends httpClient.BaseHttpClient {
20
24
  this.papi = flatten__default["default"](papi || []);
21
25
  this.di = di;
22
26
  }
23
- async request({ path, query, body }) {
27
+ async request({ path, query, body, headers, }) {
24
28
  var _a;
25
- const papiRoute = find__default["default"]((papi$1) => papi.getPapiParameters(papi$1).path === `/${path}`, (_a = this.papi) !== null && _a !== void 0 ? _a : []);
29
+ const pathWithLeadingSlash = (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path : `/${path}`;
30
+ const papiRoute = find__default["default"]((papi$1) => comparePathWithPattern.comparePathWithPattern(pathWithLeadingSlash, papi.getPapiParameters(papi$1).path), (_a = this.papi) !== null && _a !== void 0 ? _a : []);
26
31
  if (!papiRoute) {
27
32
  throw new Error(`papi handler '${path}' not found`);
28
33
  }
29
- const req = { headers: { host: 'localhost' }, cookies: {}, query, body };
34
+ const req = {
35
+ headers: { ...headers, host: 'localhost' },
36
+ cookies: {},
37
+ query,
38
+ body,
39
+ params: getPathParams.getPathParams(pathWithLeadingSlash, papi.getPapiParameters(papiRoute).path),
40
+ };
30
41
  const res = {};
31
42
  const childDi = dippy.createChildContainer(this.di, [
32
43
  {
@@ -0,0 +1,5 @@
1
+ /** Compares real path with pattern path
2
+ * @example comparePathWithPattern('/example/mock/12345', '/example/:type/:id') => true
3
+ */
4
+ export declare const comparePathWithPattern: (path: string, pattern: string) => boolean;
5
+ //# sourceMappingURL=comparePathWithPattern.d.ts.map
@@ -0,0 +1,6 @@
1
+ /** Compares real path with pattern path
2
+ * @example comparePathWithPattern('/example/mock/12345', '/example/:type/:id') => true
3
+ */
4
+ const comparePathWithPattern = (path, pattern) => new RegExp(`^${pattern.replace(/:(\w+)/g, '([^\\/]+)')}$`).test(path);
5
+
6
+ export { comparePathWithPattern };
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /** Compares real path with pattern path
6
+ * @example comparePathWithPattern('/example/mock/12345', '/example/:type/:id') => true
7
+ */
8
+ const comparePathWithPattern = (path, pattern) => new RegExp(`^${pattern.replace(/:(\w+)/g, '([^\\/]+)')}$`).test(path);
9
+
10
+ exports.comparePathWithPattern = comparePathWithPattern;
@@ -0,0 +1,4 @@
1
+ export declare const getPathParams: (path: string, pattern: string) => {
2
+ [key: string]: string;
3
+ };
4
+ //# sourceMappingURL=getPathParams.d.ts.map
@@ -0,0 +1,3 @@
1
+ const getPathParams = (path, pattern) => { var _a, _b; return (_b = (_a = path.match(new RegExp(`^${pattern.replace(/:(\w+)/g, '(?<$1>[^\\/]+)')}$`))) === null || _a === void 0 ? void 0 : _a.groups) !== null && _b !== void 0 ? _b : {}; };
2
+
3
+ export { getPathParams };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const getPathParams = (path, pattern) => { var _a, _b; return (_b = (_a = path.match(new RegExp(`^${pattern.replace(/:(\w+)/g, '(?<$1>[^\\/]+)')}$`))) === null || _a === void 0 ? void 0 : _a.groups) !== null && _b !== void 0 ? _b : {}; };
6
+
7
+ exports.getPathParams = getPathParams;
@@ -1,2 +1,4 @@
1
1
  export * from './fillHeaders';
2
+ export * from './comparePathWithPattern';
3
+ export * from './getPathParams';
2
4
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-http-client",
3
- "version": "4.41.79",
3
+ "version": "4.41.93",
4
4
  "initialVersion": "0.58.99",
5
5
  "description": "",
6
6
  "main": "lib/index.js",
@@ -26,11 +26,11 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@tramvai/http-client": "0.4.3",
29
- "@tramvai/tinkoff-request-http-client-adapter": "0.11.214",
30
- "@tramvai/tokens-http-client": "4.41.79",
31
- "@tramvai/tokens-common": "4.41.79",
32
- "@tramvai/tokens-server": "4.41.79",
33
- "@tramvai/tokens-server-private": "4.41.79"
29
+ "@tramvai/tinkoff-request-http-client-adapter": "0.11.228",
30
+ "@tramvai/tokens-http-client": "4.41.93",
31
+ "@tramvai/tokens-common": "4.41.93",
32
+ "@tramvai/tokens-server": "4.41.93",
33
+ "@tramvai/tokens-server-private": "4.41.93"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@tinkoff/request-core": "^0.10.0",
@@ -39,12 +39,12 @@
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@tinkoff/utils": "^2.1.2",
42
- "@tramvai/core": "4.41.79",
43
- "@tramvai/module-common": "4.41.79",
44
- "@tramvai/papi": "4.41.79",
45
- "@tramvai/test-helpers": "4.41.79",
46
- "@tramvai/test-unit": "4.41.79",
47
- "@tramvai/test-mocks": "4.41.79",
42
+ "@tramvai/core": "4.41.93",
43
+ "@tramvai/module-common": "4.41.93",
44
+ "@tramvai/papi": "4.41.93",
45
+ "@tramvai/test-helpers": "4.41.93",
46
+ "@tramvai/test-unit": "4.41.93",
47
+ "@tramvai/test-mocks": "4.41.93",
48
48
  "@tinkoff/dippy": "0.10.11",
49
49
  "node-fetch": "^2.6.1",
50
50
  "tslib": "^2.4.0"
package/tests.js CHANGED
@@ -61,6 +61,13 @@ const fillHeaders = ({ requestManager, headersList, }) => {
61
61
  };
62
62
  };
63
63
 
64
+ /** Compares real path with pattern path
65
+ * @example comparePathWithPattern('/example/mock/12345', '/example/:type/:id') => true
66
+ */
67
+ const comparePathWithPattern = (path, pattern) => new RegExp(`^${pattern.replace(/:(\w+)/g, '([^\\/]+)')}$`).test(path);
68
+
69
+ const getPathParams = (path, pattern) => { var _a, _b; return (_b = (_a = path.match(new RegExp(`^${pattern.replace(/:(\w+)/g, '(?<$1>[^\\/]+)')}$`))) === null || _a === void 0 ? void 0 : _a.groups) !== null && _b !== void 0 ? _b : {}; };
70
+
64
71
  const createUserAgent = ({ appInfo, envManager, }) => {
65
72
  const { appName } = appInfo;
66
73
  const appVersion = envManager.get('APP_VERSION');
@@ -152,13 +159,20 @@ class PapiService extends httpClient.BaseHttpClient {
152
159
  this.papi = flatten__default["default"](papi || []);
153
160
  this.di = di;
154
161
  }
155
- async request({ path, query, body }) {
162
+ async request({ path, query, body, headers, }) {
156
163
  var _a;
157
- const papiRoute = find__default["default"]((papi$1) => papi.getPapiParameters(papi$1).path === `/${path}`, (_a = this.papi) !== null && _a !== void 0 ? _a : []);
164
+ const pathWithLeadingSlash = (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path : `/${path}`;
165
+ const papiRoute = find__default["default"]((papi$1) => comparePathWithPattern(pathWithLeadingSlash, papi.getPapiParameters(papi$1).path), (_a = this.papi) !== null && _a !== void 0 ? _a : []);
158
166
  if (!papiRoute) {
159
167
  throw new Error(`papi handler '${path}' not found`);
160
168
  }
161
- const req = { headers: { host: 'localhost' }, cookies: {}, query, body };
169
+ const req = {
170
+ headers: { ...headers, host: 'localhost' },
171
+ cookies: {},
172
+ query,
173
+ body,
174
+ params: getPathParams(pathWithLeadingSlash, papi.getPapiParameters(papiRoute).path),
175
+ };
162
176
  const res = {};
163
177
  const childDi = dippy.createChildContainer(this.di, [
164
178
  {