@tramvai/module-mocker 1.53.1 → 1.53.6
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 +24 -26
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# @tramvai/module-mocker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Module uses library [@tinkoff/mocker](references/libs/mocker.md) to add a mocker functionality to the `tramvai` app. Mocker will be available as [papi](references/modules/server.md#papi) route with path `/mocker`.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
First, install `@tramvai/module-mocker`:
|
|
8
8
|
|
|
9
|
-
```bash
|
|
10
|
-
|
|
9
|
+
```bash npm2yarn
|
|
10
|
+
npm install @tramvai/module-mocker
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Then, add your first mock to a new file `mocks/my-api.js`. In this file add export of object literal with the field `api` that should be specified as a name of environment variable for the API url that should be mocked:
|
|
14
14
|
|
|
15
15
|
```tsx
|
|
16
16
|
module.exports = {
|
|
@@ -23,7 +23,7 @@ module.exports = {
|
|
|
23
23
|
result: {
|
|
24
24
|
type: 'json',
|
|
25
25
|
value: {
|
|
26
|
-
a: 'b'
|
|
26
|
+
a: 'b',
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
29
|
},
|
|
@@ -32,7 +32,7 @@ module.exports = {
|
|
|
32
32
|
};
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
Add module to the project:
|
|
36
36
|
|
|
37
37
|
```tsx
|
|
38
38
|
import { createApp } from '@tramvai/core';
|
|
@@ -40,46 +40,44 @@ import { MockerModule } from '@tramvai/module-module';
|
|
|
40
40
|
|
|
41
41
|
createApp({
|
|
42
42
|
name: 'tincoin',
|
|
43
|
-
modules: [
|
|
43
|
+
modules: [MockerModule],
|
|
44
44
|
});
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
Run app with env `MOCKER_ENABLED`, e.g.:
|
|
48
48
|
|
|
49
49
|
```bash
|
|
50
50
|
MOCKER_ENABLED="true" tramvai start tincoin
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
After that, all of the requests to `MY_API` in browser and on server will be automatically sent to mocker. In case mocker doesn't have a suitable mock the request, the request will be proxied to the original API.
|
|
54
54
|
|
|
55
55
|
## Explanation
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
Модуль подключает middleware мокера на `papi` роуте `/mocker`, и заменяет все `env` переменные мокируемых API ссылками на `papi`, подходящие для серверного и для клиентского кода.
|
|
57
|
+
Most of the mocker features are described in the [lib documentation](references/libs/mocker.md#Explanation).
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
Module adds mocker middleware to `papi` route `/mocker` and replaces all of the env variables that were defined in mocks by links to the `papi`. After that all of the request to the original API are routed first to mocker that accepts requests from the client and the server side.
|
|
62
60
|
|
|
63
|
-
|
|
61
|
+
By default, all of the API that were defined mocks are mocked, but it might be overridden.
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
Mocker us enabled only when env variable `MOCKER_ENABLED` is defined.
|
|
66
64
|
|
|
67
|
-
|
|
65
|
+
### Env variables replacement
|
|
68
66
|
|
|
69
|
-
|
|
67
|
+
Let's say app has env variable `MY_API: https://www.my-api.com/` and for that api some mock is defined.
|
|
70
68
|
|
|
71
|
-
|
|
69
|
+
The module can work locally, on dynamic stand, in test/stage environments. But this flexibility leads to the following problems when resolving path to the `papi` endpoint:
|
|
72
70
|
|
|
73
|
-
1.
|
|
71
|
+
1. On server we should execute requests with absolute path. In this case we know that app is always available at `localhost` that mean we can replace API env variables by urls like `http://localhost:3000/tincoin/papi/mocker/MY_API/`
|
|
72
|
+
2. On client test stands we do not known the domain of the app. In this case we should make requests by relative urls that mean we can replace API env variables by urls like `/tincoin/papi/mocker/MY_API/`
|
|
74
73
|
|
|
75
|
-
|
|
74
|
+
Thanks to this env replacement we can redirect all of the request to the APIs to our mocker first automatically.
|
|
76
75
|
|
|
77
76
|
## How to
|
|
78
77
|
|
|
79
|
-
###
|
|
78
|
+
### Mock only specific API
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
Это поведение можно переопределить, передавая список API для мокирования при инициализации модуля:
|
|
80
|
+
By default, all of the API that has corresponding mock will be mocked. It might be overridden by passing list of the APIs to mock when initializing module:
|
|
83
81
|
|
|
84
82
|
```tsx
|
|
85
83
|
MockerModule.forRoot({
|
|
@@ -89,6 +87,6 @@ MockerModule.forRoot({
|
|
|
89
87
|
});
|
|
90
88
|
```
|
|
91
89
|
|
|
92
|
-
##
|
|
90
|
+
## Exported tokens
|
|
93
91
|
|
|
94
92
|
@inline src/tokens.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-mocker",
|
|
3
|
-
"version": "1.53.
|
|
3
|
+
"version": "1.53.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/server.js",
|
|
6
6
|
"browser": "./lib/browser.js",
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
"build-for-publish": "true"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@tinkoff/mocker": "1.5.
|
|
23
|
+
"@tinkoff/mocker": "1.5.5",
|
|
24
24
|
"tslib": "^2.0.3"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@tramvai/core": "1.53.
|
|
28
|
-
"@tramvai/module-common": "1.53.
|
|
29
|
-
"@tramvai/papi": "1.53.
|
|
30
|
-
"@tramvai/tokens-server": "1.53.
|
|
31
|
-
"@tinkoff/dippy": "0.7.
|
|
27
|
+
"@tramvai/core": "1.53.6",
|
|
28
|
+
"@tramvai/module-common": "1.53.6",
|
|
29
|
+
"@tramvai/papi": "1.53.6",
|
|
30
|
+
"@tramvai/tokens-server": "1.53.6",
|
|
31
|
+
"@tinkoff/dippy": "0.7.37"
|
|
32
32
|
},
|
|
33
33
|
"license": "Apache-2.0"
|
|
34
34
|
}
|