@tramvai/module-http-client 2.72.3 → 2.72.4
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/README.md +3 -209
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
The module provides the application with a factory of HTTP clients, a basic service for working with various APIs and a service for working with `papi`.
|
|
4
4
|
|
|
5
|
+
Link to complete HTTP clients documentation - https://tramvai.dev/docs/features/data-fetching/http-client/
|
|
6
|
+
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
You need to install `@tramvai/module-http-client`
|
|
@@ -22,214 +24,6 @@ createApp({
|
|
|
22
24
|
});
|
|
23
25
|
```
|
|
24
26
|
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
The `http-client` module adds functionality to the application related to API requests. Available providers allow you to create new services to work with any API and create more specific services with preset settings for specific APIs.
|
|
27
|
+
## Explanation
|
|
28
28
|
|
|
29
29
|
The module implements interfaces from the library [@tramvai/http-client](references/libs/http-client.md) using a special library - adapter [@tramvai/tinkoff-request-http-client-adapter](references/libs/tinkoff-request-http-client-adapter.md), running on top of [@tinkoff/request](https://tinkoff.github.io/tinkoff-request/).
|
|
30
|
-
|
|
31
|
-
## Concepts
|
|
32
|
-
|
|
33
|
-
### HTTP client
|
|
34
|
-
|
|
35
|
-
HTTP client - implementation of the `HttpClient` interface, created via the `HTTP_CLIENT_FACTORY` token. HTTP client accepts general settings, some of which will be used as defult values for all requests. The HTTP client does not provide an opportunity to add additional methods for requests, and to perform side actions when the request is completed or failed.
|
|
36
|
-
|
|
37
|
-
### Services for working with API
|
|
38
|
-
|
|
39
|
-
The API service inherits from the `ApiService` class, which is exported from `@tramvai/http-client`. The API service takes an HTTP client in its constructor and uses it for requests. The API service implements all methods for requests from the `HttpClient` interface, but allows you to modify them. For example, you can replace the implementation of the `request` method by adding an error message to the `catch` request via an HTTP client - this logic will automatically work for all other methods - `get`, `put`, `post`, `delete`. In the API service, you can add custom methods for requests to certain API endpoints, and specify only the necessary parameters in them, and type responses.
|
|
40
|
-
|
|
41
|
-
Additional reasons to create API services - if you need to use several different HTTP clients to work with a specific API, or you need the ability to add a convenient abstraction on top of the basic methods for sending requests.
|
|
42
|
-
|
|
43
|
-
## Usage
|
|
44
|
-
|
|
45
|
-
### Create a new HTTP client
|
|
46
|
-
|
|
47
|
-
Each new HTTP client must directly or indirectly inherit `HTTP_CLIENT_FACTORY`.
|
|
48
|
-
|
|
49
|
-
New HTTP clients / API services should not be created with `scope: Scope.SINGLETON`, because each request is supplemented with default parameters specific to each user, for example - passing the `X-Real-Ip` header from the request to the application in all requests to the API.
|
|
50
|
-
|
|
51
|
-
#### Basic HTTP client
|
|
52
|
-
|
|
53
|
-
The `HTTP_CLIENT_FACTORY` token - provides a factory for creating new HTTP clients. The options are preinstalled with a logger and a cache factory.
|
|
54
|
-
|
|
55
|
-
##### Peculiarities
|
|
56
|
-
|
|
57
|
-
- For all requests to the API, headers are added from the list returned by the `API_CLIENT_PASS_HEADERS` token, and `X-Real-Ip` from the current request to the application
|
|
58
|
-
|
|
59
|
-
**Token interface:**
|
|
60
|
-
|
|
61
|
-
```tsx
|
|
62
|
-
type HTTP_CLIENT_FACTORY = (options: HttpClientFactoryOptions) => HttpClient;
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
**Token use:**
|
|
66
|
-
|
|
67
|
-
```tsx
|
|
68
|
-
import { Scope, provide } from '@tramvai/core';
|
|
69
|
-
import { ENV_MANAGER_TOKEN } from '@tramvai/tokens-common';
|
|
70
|
-
import { HTTP_CLIENT_FACTORY } from '@tramvai/tokens-http-client';
|
|
71
|
-
|
|
72
|
-
const provider = provide({
|
|
73
|
-
provide: 'WHATEVER_API_HTTP_CLIENT',
|
|
74
|
-
useFactory: ({
|
|
75
|
-
factory,
|
|
76
|
-
envManager,
|
|
77
|
-
}: {
|
|
78
|
-
factory: typeof HTTP_CLIENT_FACTORY;
|
|
79
|
-
envManager: typeof ENV_MANAGER_TOKEN;
|
|
80
|
-
}) => {
|
|
81
|
-
return factory({
|
|
82
|
-
name: 'whatever-api',
|
|
83
|
-
baseUrl: envManager.get('WHATEVER_API'),
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
deps: {
|
|
87
|
-
factory: HTTP_CLIENT_FACTORY,
|
|
88
|
-
envManager: ENV_MANAGER_TOKEN,
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### Using existing HTTP clients
|
|
94
|
-
|
|
95
|
-
Most HTTP clients implement additional logic for requests, and inherit from `ApiService`. Thus, each service has methods `get`, `post`, `put`, `delete` and `request`, but there may be specific methods.
|
|
96
|
-
|
|
97
|
-
#### Common HTTP client
|
|
98
|
-
|
|
99
|
-
The `HTTP_CLIENT` token provides a basic client for sending requests to any URLs, request caching is disabled.
|
|
100
|
-
|
|
101
|
-
**Token use:**
|
|
102
|
-
|
|
103
|
-
```tsx
|
|
104
|
-
import { declareAction } from '@tramvai/core';
|
|
105
|
-
import { HTTP_CLIENT } from '@tramvai/tokens-http-client';
|
|
106
|
-
|
|
107
|
-
export const fetchAction = declareAction({
|
|
108
|
-
name: 'fetch',
|
|
109
|
-
async fn() {
|
|
110
|
-
const { payload, headers, status } = await this.deps.httpClient.get(
|
|
111
|
-
'https://www.domain.com/api/endpoint'
|
|
112
|
-
);
|
|
113
|
-
return payload;
|
|
114
|
-
},
|
|
115
|
-
deps: {
|
|
116
|
-
httpClient: HTTP_CLIENT,
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Adding custom data to requests
|
|
122
|
-
|
|
123
|
-
Let's consider a case using the abstract service `WHATEVER_API_SERVICE` as an example. Let's say we want to add an `X-Real-Ip` header to every request:
|
|
124
|
-
|
|
125
|
-
```tsx
|
|
126
|
-
import { provide } from '@tramvai/core';
|
|
127
|
-
import { HttpClientRequest, HttpClient } from '@tramvai/http-client';
|
|
128
|
-
import { REQUEST_MANAGER_TOKEN } from '@tramvai/tokens-common';
|
|
129
|
-
|
|
130
|
-
const provider = provide({
|
|
131
|
-
provide: 'WHATEVER_API_SERVICE',
|
|
132
|
-
useFactory: ({
|
|
133
|
-
factory,
|
|
134
|
-
requestManager,
|
|
135
|
-
envManager,
|
|
136
|
-
}: {
|
|
137
|
-
factory: typeof HTTP_CLIENT_FACTORY;
|
|
138
|
-
requestManager: typeof REQUEST_MANAGER_TOKEN;
|
|
139
|
-
envManager: typeof ENV_MANAGER_TOKEN;
|
|
140
|
-
}) => {
|
|
141
|
-
return factory({
|
|
142
|
-
name: 'whatever-api',
|
|
143
|
-
baseUrl: envManager.get('WHATEVER_API'),
|
|
144
|
-
modifyRequest: (request: HttpClientRequest) => {
|
|
145
|
-
return {
|
|
146
|
-
...request,
|
|
147
|
-
headers: {
|
|
148
|
-
...request.headers,
|
|
149
|
-
'X-real-ip': requestManager.getClientIp(),
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
deps: {
|
|
156
|
-
factory: HTTP_CLIENT_FACTORY,
|
|
157
|
-
requestManager: REQUEST_MANAGER_TOKEN,
|
|
158
|
-
envManager: ENV_MANAGER_TOKEN,
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
## How to
|
|
164
|
-
|
|
165
|
-
### How to disable HTTP request caching?
|
|
166
|
-
|
|
167
|
-
To disable caching for all HTTP clients, pass the env variable `HTTP_CLIENT_CACHE_DISABLED: true` to the application
|
|
168
|
-
|
|
169
|
-
### Testing
|
|
170
|
-
|
|
171
|
-
#### Testing your api clients
|
|
172
|
-
|
|
173
|
-
If you have a module or providers that define api-clients, then it will be convenient to use special utilities in order to test them separately
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
import { testApi } from '@tramvai/module-http-client/tests';
|
|
177
|
-
import { CustomModule } from './module';
|
|
178
|
-
|
|
179
|
-
describe('testApi', () => {
|
|
180
|
-
it('test', async () => {
|
|
181
|
-
const { di, fetchMock, mockJsonResponse } = testApi({
|
|
182
|
-
modules: [CustomModule],
|
|
183
|
-
env: {
|
|
184
|
-
TEST_API: 'testApi',
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
const httpClient: typeof HTTP_CLIENT = di.get('CUSTOM_HTTP_CLIENT') as any;
|
|
188
|
-
|
|
189
|
-
mockJsonResponse({ a: 'aaa' });
|
|
190
|
-
|
|
191
|
-
const { payload } = await httpClient.get('test');
|
|
192
|
-
|
|
193
|
-
expect(payload).toEqual({ a: 'aaa' });
|
|
194
|
-
expect(fetchMock).toHaveBeenCalledWith('http://testApi/test', expect.anything());
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
### Logging
|
|
200
|
-
|
|
201
|
-
By default, `@tinkoff/request` will log every failed requests with level `error`. You can disable logging by pass `{ silent: true }` parameter to request parameters. Useful meta information about request will be available in `error.__meta` property.
|
|
202
|
-
|
|
203
|
-
Example:
|
|
204
|
-
|
|
205
|
-
```ts
|
|
206
|
-
const log = logger('request:test');
|
|
207
|
-
|
|
208
|
-
httpClient.request({ path: 'test', silent: true }).catch((error) => {
|
|
209
|
-
log.info(error);
|
|
210
|
-
});
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### Debug
|
|
214
|
-
|
|
215
|
-
You can show all the default logs of http clients by providing these env variables:
|
|
216
|
-
|
|
217
|
-
```bash
|
|
218
|
-
LOG_ENABLE=request*
|
|
219
|
-
LOG_LEVEL=trace
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
If the built-in http clients logs are not enough, you can enable NodeJS debugging of the `request` module this way:
|
|
223
|
-
|
|
224
|
-
```bash
|
|
225
|
-
NODE_DEBUG=request tramvai start<appName>
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## Exported tokens
|
|
229
|
-
|
|
230
|
-
[link](references/tokens/http-client.md)
|
|
231
|
-
|
|
232
|
-
## Environment Variables
|
|
233
|
-
|
|
234
|
-
- `HTTP_CLIENT_CACHE_DISABLED` - disable caching for all HTTP clients
|
|
235
|
-
- `HTTP_CLIENT_CIRCUIT_BREAKER_DISABLED` - disable plugin https://tinkoff.github.io/tinkoff-request/docs/plugins/circuit-breaker.html
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-http-client",
|
|
3
|
-
"version": "2.72.
|
|
3
|
+
"version": "2.72.4",
|
|
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.2.
|
|
29
|
-
"@tramvai/tinkoff-request-http-client-adapter": "0.9.
|
|
30
|
-
"@tramvai/tokens-http-client": "2.72.
|
|
31
|
-
"@tramvai/tokens-common": "2.72.
|
|
32
|
-
"@tramvai/tokens-server": "2.72.
|
|
33
|
-
"@tramvai/tokens-server-private": "2.72.
|
|
28
|
+
"@tramvai/http-client": "0.2.7",
|
|
29
|
+
"@tramvai/tinkoff-request-http-client-adapter": "0.9.182",
|
|
30
|
+
"@tramvai/tokens-http-client": "2.72.4",
|
|
31
|
+
"@tramvai/tokens-common": "2.72.4",
|
|
32
|
+
"@tramvai/tokens-server": "2.72.4",
|
|
33
|
+
"@tramvai/tokens-server-private": "2.72.4"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@tinkoff/request-core": "^0.9.2",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@tinkoff/utils": "^2.1.2",
|
|
42
|
-
"@tramvai/core": "2.72.
|
|
43
|
-
"@tramvai/module-common": "2.72.
|
|
44
|
-
"@tramvai/papi": "2.72.
|
|
45
|
-
"@tramvai/test-helpers": "2.72.
|
|
46
|
-
"@tramvai/test-unit": "2.72.
|
|
47
|
-
"@tramvai/test-mocks": "2.72.
|
|
42
|
+
"@tramvai/core": "2.72.4",
|
|
43
|
+
"@tramvai/module-common": "2.72.4",
|
|
44
|
+
"@tramvai/papi": "2.72.4",
|
|
45
|
+
"@tramvai/test-helpers": "2.72.4",
|
|
46
|
+
"@tramvai/test-unit": "2.72.4",
|
|
47
|
+
"@tramvai/test-mocks": "2.72.4",
|
|
48
48
|
"@tinkoff/dippy": "0.8.13",
|
|
49
49
|
"node-fetch": "^2.6.1",
|
|
50
50
|
"tslib": "^2.4.0"
|