mockaton 2.3.1 → 3.0.0

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/README.md CHANGED
@@ -44,17 +44,17 @@ node my-mockaton.js
44
44
  ## Config Options
45
45
  ```ts
46
46
  interface Config {
47
- mocksDir: string
48
- staticDir?: string
49
- host?: string, // defaults to 'localhost'
50
- port?: number // defaults to 0, which means auto-assigned
51
- delay?: number // defaults to 1200 (ms)
52
- cookies?: object
53
- skipOpen?: boolean // Prevents opening the dashboard in a browser
54
- proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
55
- allowedExt?: RegExp // /\.(json|txt|md|js)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
56
- generate500?: boolean // autogenerates an Internal Server Error empty mock for routes that have no 500
57
- extraHeaders?: []
47
+ mocksDir: string
48
+ staticDir?: string
49
+ host?: string, // defaults to 'localhost'
50
+ port?: number // defaults to 0, which means auto-assigned
51
+ delay?: number // defaults to 1200 (ms)
52
+ open?: (dashboardUrl: string) => void // pass a noop to prevent opening the dashboard
53
+ cookies?: object
54
+ proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
55
+ allowedExt?: RegExp // /\.(json|txt|md|js)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
56
+ generate500?: boolean // autogenerates an Internal Server Error empty mock for routes that have no 500
57
+ extraHeaders?: []
58
58
  }
59
59
  ```
60
60
 
package/Tests.js CHANGED
@@ -106,7 +106,7 @@ writeStatic('another-entry/index.html', '<h1>Another</h1>')
106
106
  const server = Mockaton({
107
107
  mocksDir: tmpDir,
108
108
  staticDir: staticTmpDir,
109
- skipOpen: true,
109
+ open: () => {},
110
110
  cookies: {
111
111
  userA: 'CookieA',
112
112
  userB: 'CookieB'
package/index.d.ts CHANGED
@@ -6,8 +6,8 @@ interface Config {
6
6
  host?: string,
7
7
  port?: number
8
8
  delay?: number
9
+ open?: (address: string) => void
9
10
  cookies?: object
10
- skipOpen?: boolean
11
11
  proxyFallback?: string
12
12
  allowedExt?: RegExp
13
13
  generate500?: boolean,
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": "2.3.1",
5
+ "version": "3.0.0",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Config.js CHANGED
@@ -1,5 +1,5 @@
1
- import { existsSync as exists, lstatSync } from 'node:fs'
2
- import { validate, is, optional } from './utils/validate.js'
1
+ import { openInBrowser } from './utils/openInBrowser.js'
2
+ import { validate, is, optional, isDirectory } from './utils/validate.js'
3
3
 
4
4
 
5
5
  export const Config = {
@@ -8,8 +8,8 @@ export const Config = {
8
8
  host: '127.0.0.1',
9
9
  port: 0, // auto-assigned
10
10
  delay: 1200, // milliseconds
11
+ open: openInBrowser,
11
12
  cookies: {}, // defaults to the first kv
12
- skipOpen: false,
13
13
  proxyFallback: '', // e.g. http://localhost:9999
14
14
  allowedExt: /\.(json|txt|md|js)$/, // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
15
15
  generate500: false,
@@ -24,8 +24,8 @@ export function setup(options) {
24
24
  host: is(String),
25
25
  port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
26
26
  delay: ms => Number.isInteger(ms) && ms > 0,
27
+ open: is(Function),
27
28
  cookies: is(Object),
28
- skipOpen: is(Boolean),
29
29
  proxyFallback: optional(URL.canParse),
30
30
  allowedExt: is(RegExp),
31
31
  generate500: is(Boolean),
@@ -33,8 +33,4 @@ export function setup(options) {
33
33
  })
34
34
  }
35
35
 
36
- function isDirectory(dir) {
37
- return exists(dir) && lstatSync(dir).isDirectory()
38
- }
39
-
40
36
 
package/src/Mockaton.js CHANGED
@@ -36,7 +36,7 @@ export function Mockaton(options) {
36
36
  console.log('Dashboard', url + API.dashboard)
37
37
  if (error)
38
38
  console.error(error)
39
- else if (!Config.skipOpen)
40
- exec(`open ${url + API.dashboard}`)
39
+ else
40
+ Config.open(url + API.dashboard)
41
41
  })
42
42
  }
@@ -0,0 +1,8 @@
1
+ import { exec } from 'node:child_process'
2
+
3
+
4
+ export function openInBrowser(address) {
5
+ if (process.platform === 'darwin')
6
+ exec(`open ${address}`)
7
+ }
8
+
@@ -1,3 +1,6 @@
1
+ import { existsSync as exists, lstatSync } from 'node:fs'
2
+
3
+
1
4
  export function validate(obj, shape) {
2
5
  for (const [field, value] of Object.entries(obj))
3
6
  if (!shape[field](value))
@@ -6,3 +9,4 @@ export function validate(obj, shape) {
6
9
 
7
10
  export const is = ctor => val => val.constructor === ctor
8
11
  export const optional = tester => val => !val || tester(val)
12
+ export const isDirectory = dir => exists(dir) && lstatSync(dir).isDirectory()