mockaton 0.9.3 → 0.9.5
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/Config.js +7 -12
- package/MockBroker.js +1 -1
- package/README.md +42 -35
- package/Route.js +1 -1
- package/_usage_example.js +5 -1
- package/mockBrokersCollection.js +2 -2
- package/package.json +1 -1
- package/utils/validate.js +4 -13
package/Config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, lstatSync } from 'node:fs'
|
|
2
|
-
import { validate } from './utils/validate.js'
|
|
2
|
+
import { validate, is, optional } from './utils/validate.js'
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
export const Config = {
|
|
@@ -10,7 +10,7 @@ export const Config = {
|
|
|
10
10
|
delay: 1200, // milliseconds
|
|
11
11
|
cookies: {}, // defaults to the first kv
|
|
12
12
|
database: {},
|
|
13
|
-
skipOpen: false,
|
|
13
|
+
skipOpen: false,
|
|
14
14
|
allowedExt: /\.(json|txt|md|mjs)$/ // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -19,21 +19,16 @@ export function setup(options) {
|
|
|
19
19
|
validate(Config, {
|
|
20
20
|
mocksDir: isDirectory,
|
|
21
21
|
staticDir: optional(isDirectory),
|
|
22
|
-
host: String,
|
|
22
|
+
host: is(String),
|
|
23
23
|
port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
|
|
24
24
|
delay: ms => Number.isInteger(ms) && ms > 0,
|
|
25
|
-
cookies: Object,
|
|
26
|
-
database: Object,
|
|
27
|
-
skipOpen: Boolean,
|
|
28
|
-
allowedExt: RegExp
|
|
25
|
+
cookies: is(Object),
|
|
26
|
+
database: is(Object),
|
|
27
|
+
skipOpen: is(Boolean),
|
|
28
|
+
allowedExt: is(RegExp)
|
|
29
29
|
})
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
function optional(tester) {
|
|
34
|
-
return val => !val || tester(val)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
32
|
function isDirectory(dir) {
|
|
38
33
|
return existsSync(dir) && lstatSync(dir).isDirectory()
|
|
39
34
|
}
|
package/MockBroker.js
CHANGED
package/README.md
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
# Mockaton
|
|
1
|
+
# Mockaton
|
|
2
|
+
_Mockaton_ is a mock server for developing and testing frontends.
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
For example, the following file will be served for `/api/user/1234`
|
|
4
|
+
It scans `Config.mocksDir` for files following a specific file name convention, which is
|
|
5
|
+
similar to the URLs. For example, the following file will be served for `/api/user/1234`
|
|
7
6
|
```
|
|
8
7
|
api/
|
|
9
8
|
api/user/
|
|
10
9
|
api/user/[user-id].GET.200.json
|
|
11
10
|
```
|
|
12
11
|
|
|
13
|
-
By the way, this
|
|
12
|
+
By the way, [this browser
|
|
14
13
|
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests) can
|
|
15
|
-
be used for downloading a
|
|
14
|
+
be used for downloading a TAR of your XHR requests following that convention.
|
|
16
15
|
|
|
17
16
|
|
|
18
17
|
### Mock Variants
|
|
19
|
-
Each route can have
|
|
20
|
-
-
|
|
21
|
-
-
|
|
18
|
+
Each route can have many mocks, which could either be:
|
|
19
|
+
- Different response status code. For example, for testing error responses.
|
|
20
|
+
- Comment on the filename, which is anything within parentheses.
|
|
22
21
|
|
|
23
|
-
Those
|
|
22
|
+
Those alternatives can be manually selected in the dashboard
|
|
24
23
|
UI, or programmatically, for instance, for setting up tests.
|
|
25
24
|
|
|
26
25
|
|
|
@@ -35,10 +34,10 @@ exploring its [sample-mocks/](./sample-mocks) directory. Then run
|
|
|
35
34
|
### Mock Variants of Status Code
|
|
36
35
|
The **sample-mocks/** directory has three mock alternatives for serving
|
|
37
36
|
`/api/user/friends`:
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
- 501 mocks get autogenerated for routes that have no 501’s.
|
|
37
|
+
- _200 - OK_
|
|
38
|
+
- _204 - No Content_ with an empty list of friends
|
|
39
|
+
- _501 - Internal Server Error_
|
|
40
|
+
- BTW, 501 mocks get autogenerated for routes that have no 501’s.
|
|
42
41
|
|
|
43
42
|

|
|
44
43
|
|
|
@@ -46,11 +45,9 @@ The **sample-mocks/** directory has three mock alternatives for serving
|
|
|
46
45
|
Comments are anything within parentheses, including them.
|
|
47
46
|

|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
## Delay
|
|
48
|
+
## Delay 🕓
|
|
52
49
|
The clock icon next to the mock selector dropdown is a checkbox for delaying a
|
|
53
|
-
particular response. They are handy for testing spinners
|
|
50
|
+
particular response. They are handy for testing spinners.
|
|
54
51
|
|
|
55
52
|
The milliseconds for the delay is globally configurable via `Config.delay = 1200`
|
|
56
53
|
|
|
@@ -78,15 +75,15 @@ node my-mockaton.js
|
|
|
78
75
|
## Config Options
|
|
79
76
|
```ts
|
|
80
77
|
interface Config {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
78
|
+
mocksDir: string
|
|
79
|
+
staticDir?: string
|
|
80
|
+
host?: string, // 'localhost'
|
|
81
|
+
port?: number // 0 auto-assigned
|
|
82
|
+
delay?: number // 1200 ms
|
|
83
|
+
cookies?(): object
|
|
84
|
+
database?: object // for "Transforms"
|
|
85
|
+
skipOpen?: boolean // Prevents opening the dashboard in a browser
|
|
86
|
+
allowedExt?: RegExp // /\.(json|txt|md|mjs)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
90
87
|
}
|
|
91
88
|
```
|
|
92
89
|
|
|
@@ -97,7 +94,10 @@ import { jwtCookie } from 'mockaton'
|
|
|
97
94
|
Config.cookies = {
|
|
98
95
|
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
99
96
|
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
100
|
-
'My JWT': jwtCookie('my-cookie', {
|
|
97
|
+
'My JWT': jwtCookie('my-cookie', {
|
|
98
|
+
email: 'john.doe@example.com',
|
|
99
|
+
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
|
100
|
+
})
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
@@ -193,8 +193,10 @@ with `.mjs` will process the mock before serving it.
|
|
|
193
193
|
|
|
194
194
|
For example, this handler will uppercase the mock body.
|
|
195
195
|
```js
|
|
196
|
-
export default capitalizeAllText(mockAsText, requestBody) {
|
|
197
|
-
|
|
196
|
+
export default function capitalizeAllText(mockAsText, requestBody, Config) {
|
|
197
|
+
Config.database.myCount ??= 0
|
|
198
|
+
Config.database.myCount++
|
|
199
|
+
return mockAsText.toUpperCase()
|
|
198
200
|
}
|
|
199
201
|
```
|
|
200
202
|
|
|
@@ -210,15 +212,17 @@ other words, only one transform per route is supported in demo mode.
|
|
|
210
212
|
```
|
|
211
213
|
PATCH http://localhost:2345/mockaton/edit
|
|
212
214
|
{
|
|
213
|
-
|
|
214
|
-
|
|
215
|
+
"file": "api/foo.200.GET.json"
|
|
216
|
+
"delayed": true // optional
|
|
215
217
|
}
|
|
216
218
|
```
|
|
217
219
|
|
|
218
220
|
### Bulk Selecting Mocks by Matching comments
|
|
219
221
|
```
|
|
220
222
|
PATCH http://localhost:2345/mockaton/bulk-select
|
|
221
|
-
{
|
|
223
|
+
{
|
|
224
|
+
"comment": "demo-a"
|
|
225
|
+
}
|
|
222
226
|
```
|
|
223
227
|
|
|
224
228
|
Many mocks can be changed at once. We do that by searching the
|
|
@@ -232,4 +236,7 @@ Similarly, if there’s no demo mock at all for
|
|
|
232
236
|
a route, the first dev mock (a-z) will be served.
|
|
233
237
|
|
|
234
238
|
|
|
235
|
-
|
|
239
|
+
### Reset
|
|
240
|
+
```
|
|
241
|
+
PATCH http://localhost:2345/mockaton/reset
|
|
242
|
+
```
|
package/Route.js
CHANGED
package/_usage_example.js
CHANGED
|
@@ -9,6 +9,10 @@ Mockaton({
|
|
|
9
9
|
staticDir: resolve('sample-static'),
|
|
10
10
|
cookies: {
|
|
11
11
|
'Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
12
|
-
'Normal User': 'my-cookie=0;Path=/;SameSite=strict'
|
|
12
|
+
'Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
13
|
+
'My JWT': jwtCookie('my-cookie', {
|
|
14
|
+
email: 'john.doe@example.com',
|
|
15
|
+
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
|
16
|
+
})
|
|
13
17
|
}
|
|
14
18
|
})
|
package/mockBrokersCollection.js
CHANGED
|
@@ -47,8 +47,8 @@ export const getBrokerByFilename = file => {
|
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
// Searching the routes in reverse order so dynamic params (e.g.
|
|
50
|
-
// /user
|
|
51
|
-
// /user/name). That’s because "
|
|
50
|
+
// /user/[id]) don’t take precedence over exact paths (e.g.
|
|
51
|
+
// /user/name). That’s because "[]" chars are lower than alphanumeric ones.
|
|
52
52
|
// BTW, `urlMasks` always start with "/", so there’s no need to
|
|
53
53
|
// worry about the primacy of array-like keys when iterating.
|
|
54
54
|
export function findMatchingBroker(method, url) {
|
package/package.json
CHANGED
package/utils/validate.js
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
export function validate(obj, shape) {
|
|
2
|
-
for (const [field, value] of Object.entries(obj))
|
|
3
|
-
|
|
4
|
-
if (isTypeOf(validator, Function) && validator !== Function) {
|
|
5
|
-
if (!validator(value))
|
|
6
|
-
throw new TypeError(`${field} ${value}`)
|
|
7
|
-
}
|
|
8
|
-
else if (!isTypeOf(validator, value))
|
|
2
|
+
for (const [field, value] of Object.entries(obj))
|
|
3
|
+
if (!shape[field](value))
|
|
9
4
|
throw new TypeError(`${field} ${value}`)
|
|
10
|
-
}
|
|
11
5
|
}
|
|
12
6
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Object.prototype.toString.call(value) ===
|
|
16
|
-
Object.prototype.toString.call(example))
|
|
17
|
-
}
|
|
7
|
+
export const is = ctor => val => val.constructor === ctor
|
|
8
|
+
export const optional = tester => val => !val || tester(val)
|