@tramvai/module-http-client 5.41.2 → 5.45.2
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.
- package/lib/papi/papiService.d.ts +1 -1
- package/lib/papi/papiService.es.js +14 -3
- package/lib/papi/papiService.js +14 -3
- package/lib/utils/comparePathWithPattern.d.ts +5 -0
- package/lib/utils/comparePathWithPattern.es.js +6 -0
- package/lib/utils/comparePathWithPattern.js +10 -0
- package/lib/utils/getPathParams.d.ts +4 -0
- package/lib/utils/getPathParams.es.js +3 -0
- package/lib/utils/getPathParams.js +7 -0
- package/lib/utils/index.d.ts +2 -0
- package/package.json +13 -13
- package/tests.js +17 -3
|
@@ -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,12 +15,19 @@ class PapiService extends BaseHttpClient {
|
|
|
11
15
|
this.papi = flatten(papi || []);
|
|
12
16
|
this.di = di;
|
|
13
17
|
}
|
|
14
|
-
async request({ path, query, body }) {
|
|
15
|
-
const
|
|
18
|
+
async request({ path, query, body, headers, }) {
|
|
19
|
+
const pathWithLeadingSlash = path?.startsWith('/') ? path : `/${path}`;
|
|
20
|
+
const papiRoute = find((papi) => comparePathWithPattern(pathWithLeadingSlash, getPapiParameters(papi).path), this.papi ?? []);
|
|
16
21
|
if (!papiRoute) {
|
|
17
22
|
throw new Error(`papi handler '${path}' not found`);
|
|
18
23
|
}
|
|
19
|
-
const req = {
|
|
24
|
+
const req = {
|
|
25
|
+
headers: { ...headers, host: 'localhost' },
|
|
26
|
+
cookies: {},
|
|
27
|
+
query,
|
|
28
|
+
body,
|
|
29
|
+
params: getPathParams(pathWithLeadingSlash, getPapiParameters(papiRoute).path),
|
|
30
|
+
};
|
|
20
31
|
const res = {};
|
|
21
32
|
const childDi = createChildContainer(this.di, [
|
|
22
33
|
{
|
package/lib/papi/papiService.js
CHANGED
|
@@ -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,12 +24,19 @@ 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 }) {
|
|
24
|
-
const
|
|
27
|
+
async request({ path, query, body, headers, }) {
|
|
28
|
+
const pathWithLeadingSlash = path?.startsWith('/') ? path : `/${path}`;
|
|
29
|
+
const papiRoute = find__default["default"]((papi$1) => comparePathWithPattern.comparePathWithPattern(pathWithLeadingSlash, papi.getPapiParameters(papi$1).path), this.papi ?? []);
|
|
25
30
|
if (!papiRoute) {
|
|
26
31
|
throw new Error(`papi handler '${path}' not found`);
|
|
27
32
|
}
|
|
28
|
-
const req = {
|
|
33
|
+
const req = {
|
|
34
|
+
headers: { ...headers, host: 'localhost' },
|
|
35
|
+
cookies: {},
|
|
36
|
+
query,
|
|
37
|
+
body,
|
|
38
|
+
params: getPathParams.getPathParams(pathWithLeadingSlash, papi.getPapiParameters(papiRoute).path),
|
|
39
|
+
};
|
|
29
40
|
const res = {};
|
|
30
41
|
const childDi = dippy.createChildContainer(this.di, [
|
|
31
42
|
{
|
|
@@ -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;
|
package/lib/utils/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-http-client",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.45.2",
|
|
4
4
|
"initialVersion": "0.58.99",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"watch": "tsc -w"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tramvai/http-client": "0.5.
|
|
29
|
-
"@tramvai/tinkoff-request-http-client-adapter": "0.12.
|
|
30
|
-
"@tramvai/tokens-http-client": "5.
|
|
31
|
-
"@tramvai/tokens-common": "5.
|
|
32
|
-
"@tramvai/tokens-server": "5.
|
|
33
|
-
"@tramvai/tokens-server-private": "5.
|
|
28
|
+
"@tramvai/http-client": "0.5.4",
|
|
29
|
+
"@tramvai/tinkoff-request-http-client-adapter": "0.12.108",
|
|
30
|
+
"@tramvai/tokens-http-client": "5.45.2",
|
|
31
|
+
"@tramvai/tokens-common": "5.45.2",
|
|
32
|
+
"@tramvai/tokens-server": "5.45.2",
|
|
33
|
+
"@tramvai/tokens-server-private": "5.45.2"
|
|
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": "5.
|
|
43
|
-
"@tramvai/module-common": "5.
|
|
44
|
-
"@tramvai/papi": "5.
|
|
45
|
-
"@tramvai/test-helpers": "5.
|
|
46
|
-
"@tramvai/test-unit": "5.
|
|
47
|
-
"@tramvai/test-mocks": "5.
|
|
42
|
+
"@tramvai/core": "5.45.2",
|
|
43
|
+
"@tramvai/module-common": "5.45.2",
|
|
44
|
+
"@tramvai/papi": "5.45.2",
|
|
45
|
+
"@tramvai/test-helpers": "5.45.2",
|
|
46
|
+
"@tramvai/test-unit": "5.45.2",
|
|
47
|
+
"@tramvai/test-mocks": "5.45.2",
|
|
48
48
|
"@tinkoff/dippy": "0.11.4",
|
|
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) => path.match(new RegExp(`^${pattern.replace(/:(\w+)/g, '(?<$1>[^\\/]+)')}$`))?.groups ?? {};
|
|
70
|
+
|
|
64
71
|
const createUserAgent = ({ appInfo, envManager, }) => {
|
|
65
72
|
const { appName } = appInfo;
|
|
66
73
|
const appVersion = envManager.get('APP_VERSION');
|
|
@@ -151,12 +158,19 @@ class PapiService extends httpClient.BaseHttpClient {
|
|
|
151
158
|
this.papi = flatten__default["default"](papi || []);
|
|
152
159
|
this.di = di;
|
|
153
160
|
}
|
|
154
|
-
async request({ path, query, body }) {
|
|
155
|
-
const
|
|
161
|
+
async request({ path, query, body, headers, }) {
|
|
162
|
+
const pathWithLeadingSlash = path?.startsWith('/') ? path : `/${path}`;
|
|
163
|
+
const papiRoute = find__default["default"]((papi$1) => comparePathWithPattern(pathWithLeadingSlash, papi.getPapiParameters(papi$1).path), this.papi ?? []);
|
|
156
164
|
if (!papiRoute) {
|
|
157
165
|
throw new Error(`papi handler '${path}' not found`);
|
|
158
166
|
}
|
|
159
|
-
const req = {
|
|
167
|
+
const req = {
|
|
168
|
+
headers: { ...headers, host: 'localhost' },
|
|
169
|
+
cookies: {},
|
|
170
|
+
query,
|
|
171
|
+
body,
|
|
172
|
+
params: getPathParams(pathWithLeadingSlash, papi.getPapiParameters(papiRoute).path),
|
|
173
|
+
};
|
|
160
174
|
const res = {};
|
|
161
175
|
const childDi = dippy.createChildContainer(this.di, [
|
|
162
176
|
{
|