mockaton 9.1.1 → 9.1.2

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "HTTP Mock Server",
4
4
  "type": "module",
5
- "version": "9.1.1",
5
+ "version": "9.1.2",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Api.js CHANGED
@@ -27,6 +27,7 @@ export const apiGetRequests = new Map([
27
27
 
28
28
  [API.state, getState],
29
29
  [API.syncVersion, longPollClientSyncVersion],
30
+ [API.throws, () => { throw new Error('Test500') }]
30
31
  ])
31
32
 
32
33
  export const apiPatchRequests = new Map([
@@ -14,7 +14,8 @@ export const API = {
14
14
  select: MOUNT + '/select',
15
15
  state: MOUNT + '/state',
16
16
  staticStatus: MOUNT + '/static-status',
17
- syncVersion: MOUNT + '/sync-version'
17
+ syncVersion: MOUNT + '/sync-version',
18
+ throws: MOUNT + '/throws',
18
19
  }
19
20
 
20
21
  export const DF = { // Dashboard Fields (XHR)
package/src/Mockaton.js CHANGED
@@ -14,15 +14,8 @@ import { apiPatchRequests, apiGetRequests } from './Api.js'
14
14
  import { sendNoContent, sendInternalServerError, sendUnprocessableContent } from './utils/http-response.js'
15
15
 
16
16
 
17
- process.on('unhandledRejection', error => { throw error })
18
-
19
17
  export function Mockaton(options) {
20
- const error = setup(options)
21
- if (error) {
22
- log.error(error)
23
- process.exitCode = 1
24
- return
25
- }
18
+ setup(options)
26
19
 
27
20
  mockBrokerCollection.init()
28
21
  staticCollection.init()
@@ -31,12 +24,7 @@ export function Mockaton(options) {
31
24
 
32
25
  const server = createServer(onRequest)
33
26
 
34
- server.listen(config.port, config.host, function (error) {
35
- if (error) {
36
- log.error(error)
37
- process.exit(1)
38
- return
39
- }
27
+ server.listen(config.port, config.host, function () {
40
28
  const { address, port } = this.address()
41
29
  const url = `http://${address}:${port}`
42
30
  log.info('Listening', url)
@@ -44,11 +32,6 @@ export function Mockaton(options) {
44
32
  config.onReady(url + API.dashboard)
45
33
  })
46
34
 
47
- server.on('error', error => {
48
- log.error(error.message)
49
- process.exit(1)
50
- })
51
-
52
35
  return server
53
36
  }
54
37
 
package/src/cli.js CHANGED
@@ -8,26 +8,35 @@ import { Mockaton } from '../index.js'
8
8
  import pkgJSON from '../package.json' with { type: 'json' }
9
9
 
10
10
 
11
- const args = parseArgs({
12
- options: {
13
- config: { short: 'c', type: 'string' },
11
+ process.on('unhandledRejection', error => { throw error })
14
12
 
15
- port: { short: 'p', type: 'string' },
16
- host: { short: 'H', type: 'string' },
13
+ let args
14
+ try {
15
+ args = parseArgs({
16
+ options: {
17
+ config: { short: 'c', type: 'string' },
17
18
 
18
- 'mocks-dir': { short: 'm', type: 'string' },
19
- 'static-dir': { short: 's', type: 'string' },
19
+ port: { short: 'p', type: 'string' },
20
+ host: { short: 'H', type: 'string' },
20
21
 
21
- quiet: { short: 'q', type: 'boolean' },
22
- 'no-open': { short: 'n', type: 'boolean' },
23
-
24
- help: { short: 'h', type: 'boolean' },
25
- version: { short: 'v', type: 'boolean' },
26
- }
27
- }).values
22
+ 'mocks-dir': { short: 'm', type: 'string' },
23
+ 'static-dir': { short: 's', type: 'string' },
24
+
25
+ quiet: { short: 'q', type: 'boolean' },
26
+ 'no-open': { short: 'n', type: 'boolean' },
28
27
 
28
+ help: { short: 'h', type: 'boolean' },
29
+ version: { short: 'v', type: 'boolean' }
30
+ }
31
+ }).values
32
+ }
33
+ catch (error) {
34
+ console.error(error.message)
35
+ process.exit(1)
36
+ }
29
37
 
30
- if (args.version)
38
+
39
+ if (args.version)
31
40
  console.log(pkgJSON.version)
32
41
 
33
42
  else if (args.help)
@@ -73,5 +82,14 @@ else {
73
82
  if (args.quiet) opts.logLevel = 'quiet'
74
83
  if (args['no-open']) opts.onReady = () => {}
75
84
 
76
- Mockaton(opts)
85
+ try {
86
+ Mockaton(opts).on('error', error => {
87
+ console.error(error.message)
88
+ process.exit(1)
89
+ })
90
+ }
91
+ catch (err) {
92
+ console.error(err?.message || err)
93
+ process.exit(1)
94
+ }
77
95
  }
package/src/config.js CHANGED
@@ -22,9 +22,9 @@ const schema = {
22
22
 
23
23
  host: ['127.0.0.1', is(String)],
24
24
  port: [0, port => Number.isInteger(port) && port >= 0 && port < 2 ** 16], // 0 means auto-assigned
25
-
25
+
26
26
  logLevel: ['normal', val => ['normal', 'quiet'].includes(val)],
27
-
27
+
28
28
  delay: [1200, ms => Number.isInteger(ms) && ms >= 0],
29
29
  delayJitter: [0, percent => percent >= 0 && percent <= 3],
30
30
 
@@ -43,13 +43,13 @@ const schema = {
43
43
  corsExposedHeaders: [[], Array.isArray],
44
44
  corsCredentials: [true, is(Boolean)],
45
45
  corsMaxAge: [0, is(Number)],
46
-
46
+
47
47
  plugins: [
48
48
  [
49
49
  [/\.(js|ts)$/, jsToJsonPlugin]
50
50
  ], Array.isArray],
51
51
 
52
- onReady: [await openInBrowser, is(Function)],
52
+ onReady: [await openInBrowser, is(Function)]
53
53
  }
54
54
 
55
55
 
@@ -77,15 +77,10 @@ export function setup(options) {
77
77
 
78
78
  if (!options.staticDir && !isDirectory(defaults.staticDir))
79
79
  options.staticDir = ''
80
-
81
- try {
82
- Object.assign(config, options)
83
- validate(config, ConfigValidator)
84
- log.setLevel(config.logLevel)
85
- }
86
- catch (err) {
87
- return err.message
88
- }
80
+
81
+ Object.assign(config, options)
82
+ validate(config, ConfigValidator)
83
+ log.setLevel(config.logLevel)
89
84
  }
90
85
 
91
86
 
@@ -35,7 +35,7 @@ export function sendUnprocessableContent(response, error) {
35
35
  }
36
36
 
37
37
  export function sendInternalServerError(response, error) {
38
- log.error(error)
38
+ log.error(error?.message || error, error?.stack || undefined)
39
39
  response.statusCode = 500
40
40
  response.end()
41
41
  }