mockaton 0.9.3 → 0.9.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/Config.js +1 -1
- package/MockBroker.js +1 -1
- package/README.md +21 -15
- package/Route.js +1 -1
- package/_usage_example.js +2 -1
- package/mockBrokersCollection.js +2 -2
- package/package.json +1 -1
- package/utils/validate.js +19 -9
package/Config.js
CHANGED
|
@@ -9,7 +9,7 @@ export const Config = {
|
|
|
9
9
|
port: 0, // auto-assigned
|
|
10
10
|
delay: 1200, // milliseconds
|
|
11
11
|
cookies: {}, // defaults to the first kv
|
|
12
|
-
database: {},
|
|
12
|
+
database: {}, // for mjs transforms
|
|
13
13
|
skipOpen: false, // Prevents opening the dashboard in a browser
|
|
14
14
|
allowedExt: /\.(json|txt|md|mjs)$/ // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
15
15
|
}
|
package/MockBroker.js
CHANGED
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
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/
|
|
@@ -17,7 +16,7 @@ be used for downloading a tar of your XHR requests following that convention.
|
|
|
17
16
|
|
|
18
17
|
### Mock Variants
|
|
19
18
|
Each route can have different mocks and those variants could either be:
|
|
20
|
-
- a different response status code,
|
|
19
|
+
- a different response status code, or
|
|
21
20
|
- a comment on the filename, which is anything within parentheses.
|
|
22
21
|
|
|
23
22
|
Those variants can be manually selected in the dashboard
|
|
@@ -35,9 +34,9 @@ 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
|
-
-
|
|
37
|
+
- _200 - OK_,
|
|
38
|
+
- _204 - No Content_ of an empty list of friends, and
|
|
39
|
+
- _501 - Internal Server Error_
|
|
41
40
|
- 501 mocks get autogenerated for routes that have no 501’s.
|
|
42
41
|
|
|
43
42
|

|
|
@@ -193,8 +192,10 @@ with `.mjs` will process the mock before serving it.
|
|
|
193
192
|
|
|
194
193
|
For example, this handler will uppercase the mock body.
|
|
195
194
|
```js
|
|
196
|
-
export default capitalizeAllText(mockAsText, requestBody) {
|
|
197
|
-
|
|
195
|
+
export default function capitalizeAllText(mockAsText, requestBody, Config) {
|
|
196
|
+
Config.database.myCount ??= 0
|
|
197
|
+
Config.database.myCount++
|
|
198
|
+
return mockAsText.toUpperCase()
|
|
198
199
|
}
|
|
199
200
|
```
|
|
200
201
|
|
|
@@ -210,15 +211,17 @@ other words, only one transform per route is supported in demo mode.
|
|
|
210
211
|
```
|
|
211
212
|
PATCH http://localhost:2345/mockaton/edit
|
|
212
213
|
{
|
|
213
|
-
|
|
214
|
-
|
|
214
|
+
"file": "api/foo.200.GET.json"
|
|
215
|
+
"delayed": true // optional
|
|
215
216
|
}
|
|
216
217
|
```
|
|
217
218
|
|
|
218
219
|
### Bulk Selecting Mocks by Matching comments
|
|
219
220
|
```
|
|
220
221
|
PATCH http://localhost:2345/mockaton/bulk-select
|
|
221
|
-
{
|
|
222
|
+
{
|
|
223
|
+
"comment": "demo-a"
|
|
224
|
+
}
|
|
222
225
|
```
|
|
223
226
|
|
|
224
227
|
Many mocks can be changed at once. We do that by searching the
|
|
@@ -232,4 +235,7 @@ Similarly, if there’s no demo mock at all for
|
|
|
232
235
|
a route, the first dev mock (a-z) will be served.
|
|
233
236
|
|
|
234
237
|
|
|
235
|
-
|
|
238
|
+
### Reset
|
|
239
|
+
```
|
|
240
|
+
PATCH http://localhost:2345/mockaton/reset
|
|
241
|
+
```
|
package/Route.js
CHANGED
package/_usage_example.js
CHANGED
|
@@ -9,6 +9,7 @@ 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', { foo: 'bar' })
|
|
13
14
|
}
|
|
14
15
|
})
|
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,27 @@
|
|
|
1
|
+
const isTypeOf = example => value =>
|
|
2
|
+
Object.prototype.toString.call(value) ===
|
|
3
|
+
Object.prototype.toString.call(example)
|
|
4
|
+
|
|
5
|
+
const typeCheckers = new Map([
|
|
6
|
+
[Date, isTypeOf(new Date())],
|
|
7
|
+
[Array, isTypeOf([])],
|
|
8
|
+
[String, isTypeOf('')],
|
|
9
|
+
[Object, isTypeOf({})],
|
|
10
|
+
[Number, isTypeOf(1)],
|
|
11
|
+
[RegExp, isTypeOf(/a/)],
|
|
12
|
+
[Boolean, isTypeOf(true)],
|
|
13
|
+
[Function, isTypeOf(a => a)]
|
|
14
|
+
])
|
|
15
|
+
|
|
16
|
+
|
|
1
17
|
export function validate(obj, shape) {
|
|
2
18
|
for (const [field, value] of Object.entries(obj)) {
|
|
3
19
|
const validator = shape[field]
|
|
4
|
-
if (
|
|
5
|
-
if (!validator(value))
|
|
20
|
+
if (typeCheckers.has(validator)) {
|
|
21
|
+
if (!typeCheckers.get(validator)(value))
|
|
6
22
|
throw new TypeError(`${field} ${value}`)
|
|
7
23
|
}
|
|
8
|
-
else if (!
|
|
24
|
+
else if (!validator(value))
|
|
9
25
|
throw new TypeError(`${field} ${value}`)
|
|
10
26
|
}
|
|
11
27
|
}
|
|
12
|
-
|
|
13
|
-
function isTypeOf(example, value) {
|
|
14
|
-
return (
|
|
15
|
-
Object.prototype.toString.call(value) ===
|
|
16
|
-
Object.prototype.toString.call(example))
|
|
17
|
-
}
|