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 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, // Prevents opening the dashboard in a browser
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
@@ -24,7 +24,7 @@ export class MockBroker {
24
24
  delay: 0
25
25
  }
26
26
 
27
- this.transforms = [] // *.js
27
+ this.transforms = [] // *.mjs
28
28
  this.currentTransform = ''
29
29
 
30
30
  this.register(file)
package/README.md CHANGED
@@ -1,26 +1,25 @@
1
- # Mockaton (Mock Server)
1
+ # Mockaton
2
+ _Mockaton_ is a mock server for developing and testing frontends.
2
3
 
3
- Mockaton scans `Config.mocksDir` for files
4
- following a specific file name convention, which is similar to the URLs.
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 [browser
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 tar of your XHR requests following that convention.
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 different mocks and those variants could either be:
20
- - a different response status code, (e.g. 200, 401), or
21
- - a comment on the filename, which is anything within parentheses.
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 variants can be manually selected in the dashboard
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
- - an HTTP status of _200 - OK_,
39
- - another one with _204 - No Content_ of an empty list of friends, and
40
- - a _501 - Internal Server Error_
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
  ![](./README-dashboard-dropdown.png)
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
  ![](./README-mocks-with-comments.png)
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 when developing UIs.
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
- mocksDir: string
82
- staticDir?: string
83
- host?: string,
84
- port?: number
85
- delay?: number
86
- cookies?(): object
87
- database?: object
88
- skipOpen?: boolean
89
- allowedExt?: RegExp
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', { foo: 'bar' })
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
- return mockAsText.toUpperCase();
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
- "file": "api/foo.200.GET.json"
214
- "delayed": true // optional
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
- { "comment": "demo-a" }
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
@@ -67,7 +67,7 @@ export class Route {
67
67
  }
68
68
 
69
69
 
70
- // Stars out (for regex) all the paths that are in angle brackets
70
+ // Stars out (for regex) all the paths that are in square brackets
71
71
  function disregardVariables(str) {
72
72
  return str.replace(/\[.*?]/g, '[^/]*')
73
73
  }
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
  })
@@ -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/<id>) don’t take precedence over exact paths (e.g.
51
- // /user/name). That’s because "<" chars are lower than alphanumeric ones.
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
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "A deterministic server-side for developing and testing frontend clients",
4
4
  "type": "module",
5
- "version": "0.9.3",
5
+ "version": "0.9.5",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
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
- const validator = shape[field]
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
- function isTypeOf(example, value) {
14
- return (
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)