mockaton 0.9.4 → 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 = {
@@ -9,8 +9,8 @@ export const Config = {
9
9
  port: 0, // auto-assigned
10
10
  delay: 1200, // milliseconds
11
11
  cookies: {}, // defaults to the first kv
12
- database: {}, // for mjs transforms
13
- skipOpen: false, // Prevents opening the dashboard in a browser
12
+ database: {},
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/README.md CHANGED
@@ -9,17 +9,17 @@ api/user/
9
9
  api/user/[user-id].GET.200.json
10
10
  ```
11
11
 
12
- By the way, this [browser
12
+ By the way, [this browser
13
13
  extension](https://github.com/ericfortis/devtools-ext-tar-http-requests) can
14
- 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.
15
15
 
16
16
 
17
17
  ### Mock Variants
18
- Each route can have different mocks and those variants could either be:
19
- - a different response status code, or
20
- - 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.
21
21
 
22
- Those variants can be manually selected in the dashboard
22
+ Those alternatives can be manually selected in the dashboard
23
23
  UI, or programmatically, for instance, for setting up tests.
24
24
 
25
25
 
@@ -34,10 +34,10 @@ exploring its [sample-mocks/](./sample-mocks) directory. Then run
34
34
  ### Mock Variants of Status Code
35
35
  The **sample-mocks/** directory has three mock alternatives for serving
36
36
  `/api/user/friends`:
37
- - _200 - OK_,
38
- - _204 - No Content_ of an empty list of friends, and
37
+ - _200 - OK_
38
+ - _204 - No Content_ with an empty list of friends
39
39
  - _501 - Internal Server Error_
40
- - 501 mocks get autogenerated for routes that have no 501’s.
40
+ - BTW, 501 mocks get autogenerated for routes that have no 501’s.
41
41
 
42
42
  ![](./README-dashboard-dropdown.png)
43
43
 
@@ -45,11 +45,9 @@ The **sample-mocks/** directory has three mock alternatives for serving
45
45
  Comments are anything within parentheses, including them.
46
46
  ![](./README-mocks-with-comments.png)
47
47
 
48
- ---
49
-
50
- ## Delay
48
+ ## Delay 🕓
51
49
  The clock icon next to the mock selector dropdown is a checkbox for delaying a
52
- particular response. They are handy for testing spinners when developing UIs.
50
+ particular response. They are handy for testing spinners.
53
51
 
54
52
  The milliseconds for the delay is globally configurable via `Config.delay = 1200`
55
53
 
@@ -77,15 +75,15 @@ node my-mockaton.js
77
75
  ## Config Options
78
76
  ```ts
79
77
  interface Config {
80
- mocksDir: string
81
- staticDir?: string
82
- host?: string,
83
- port?: number
84
- delay?: number
85
- cookies?(): object
86
- database?: object
87
- skipOpen?: boolean
88
- 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 ~)
89
87
  }
90
88
  ```
91
89
 
@@ -96,7 +94,10 @@ import { jwtCookie } from 'mockaton'
96
94
  Config.cookies = {
97
95
  'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
98
96
  'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
99
- '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
+ })
100
101
  }
101
102
  ```
102
103
 
package/_usage_example.js CHANGED
@@ -10,6 +10,9 @@ Mockaton({
10
10
  cookies: {
11
11
  'Admin User': 'my-cookie=1;Path=/;SameSite=strict',
12
12
  'Normal User': 'my-cookie=0;Path=/;SameSite=strict',
13
- 'My JWT': jwtCookie('my-cookie', { foo: 'bar' })
13
+ 'My JWT': jwtCookie('my-cookie', {
14
+ email: 'john.doe@example.com',
15
+ picture: 'https://cdn.auth0.com/avatars/jd.png'
16
+ })
14
17
  }
15
18
  })
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.4",
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,27 +1,8 @@
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
-
17
1
  export function validate(obj, shape) {
18
- for (const [field, value] of Object.entries(obj)) {
19
- const validator = shape[field]
20
- if (typeCheckers.has(validator)) {
21
- if (!typeCheckers.get(validator)(value))
22
- throw new TypeError(`${field} ${value}`)
23
- }
24
- else if (!validator(value))
2
+ for (const [field, value] of Object.entries(obj))
3
+ if (!shape[field](value))
25
4
  throw new TypeError(`${field} ${value}`)
26
- }
27
5
  }
6
+
7
+ export const is = ctor => val => val.constructor === ctor
8
+ export const optional = tester => val => !val || tester(val)