mockaton 0.9.2 → 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 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
@@ -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,9 +1,8 @@
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/
@@ -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, (e.g. 200, 401), or
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
- - 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_
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
  ![](./README-dashboard-dropdown.png)
@@ -78,17 +77,15 @@ node my-mockaton.js
78
77
  ## Config Options
79
78
  ```ts
80
79
  interface Config {
81
- mocksDir: string
82
- staticDir?: string
83
- host?: string,
84
- port?: number
85
-
86
- cookies?(): object
87
-
88
- skipOpen?: boolean
89
- allowedExt?: RegExp
90
- delayMilliseconds?: number
91
- database?: object
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
92
89
  }
93
90
  ```
94
91
 
@@ -155,8 +152,6 @@ api/foo/?bar=[bar].GET.200.json
155
152
  api/foo/(my comment).GET.200.json
156
153
  ```
157
154
 
158
- ---
159
-
160
155
 
161
156
  ---
162
157
  ## Mock Precedence
@@ -197,8 +192,10 @@ with `.mjs` will process the mock before serving it.
197
192
 
198
193
  For example, this handler will uppercase the mock body.
199
194
  ```js
200
- export default capitalizeAllText(mockAsText, requestBody) {
201
- return mockAsText.toUpperCase();
195
+ export default function capitalizeAllText(mockAsText, requestBody, Config) {
196
+ Config.database.myCount ??= 0
197
+ Config.database.myCount++
198
+ return mockAsText.toUpperCase()
202
199
  }
203
200
  ```
204
201
 
@@ -214,15 +211,17 @@ other words, only one transform per route is supported in demo mode.
214
211
  ```
215
212
  PATCH http://localhost:2345/mockaton/edit
216
213
  {
217
- "file": "api/foo.200.GET.json"
218
- "delayed": true // optional
214
+ "file": "api/foo.200.GET.json"
215
+ "delayed": true // optional
219
216
  }
220
217
  ```
221
218
 
222
219
  ### Bulk Selecting Mocks by Matching comments
223
220
  ```
224
221
  PATCH http://localhost:2345/mockaton/bulk-select
225
- { "comment": "demo-a" }
222
+ {
223
+ "comment": "demo-a"
224
+ }
226
225
  ```
227
226
 
228
227
  Many mocks can be changed at once. We do that by searching the
@@ -236,4 +235,7 @@ Similarly, if there’s no demo mock at all for
236
235
  a route, the first dev mock (a-z) will be served.
237
236
 
238
237
 
239
-
238
+ ### Reset
239
+ ```
240
+ PATCH http://localhost:2345/mockaton/reset
241
+ ```
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,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
  })
@@ -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.2",
5
+ "version": "0.9.4",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
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 (isTypeOf(validator, Function) && validator !== Function) {
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 (!isTypeOf(validator, value))
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
- }