mockaton 6.3.0 → 6.3.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/README.md CHANGED
@@ -44,16 +44,20 @@ node my-mockaton.js
44
44
  ```ts
45
45
  interface Config {
46
46
  mocksDir: string
47
+ ignore?: RegExp // defaults to /(.DS_Store|~)$/
48
+
47
49
  staticDir?: string
50
+
48
51
  host?: string, // defaults to 'localhost'
49
52
  port?: number // defaults to 0, which means auto-assigned
50
- delay?: number // defaults to 1200 (ms)
51
- ignore?: RegExp // defaults to /(.DS_Store|~)$/
52
- onReady?: (dashboardUrl: string) => void // defaults to trying to open macOS default browser. pass a noop to prevent opening the dashboard
53
- cookies?: object
54
53
  proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
55
- extraMimes?: object
54
+
55
+ delay?: number // defaults to 1200 (ms)
56
+ cookies?: { [label: string]: string }
57
+ extraMimes?: { [fileExt: string]: string }
56
58
  extraHeaders?: []
59
+
60
+ onReady?: (dashboardUrl: string) => void // defaults to trying to open macOS default browser. pass a noop to prevent opening the dashboard
57
61
  }
58
62
  ```
59
63
 
@@ -80,11 +84,12 @@ export default [
80
84
  ]
81
85
  ```
82
86
 
83
- Or, export default a function. In it, you can override the response status and the
84
- default JSON content type. But don’t call `response.end()`, just return a string.
87
+ Or, export default a function. In it, you can override the
88
+ response status and the default JSON content type. But don’t call
89
+ `response.end()`, just return a `string`, `Buffer`, or `Uint8Array`.
85
90
 
86
- In a sense, you can think of this is an HTTP handler. So you can read or
87
- write to a database, or pull data from a backend. The `request` is of type
91
+ Think of this as an HTTP handler. You can read or write to a
92
+ database, or pull data from a backend. The `request` is of type
88
93
  [IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage), and the
89
94
  `response` a [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse).
90
95
  ```js
package/index.d.ts CHANGED
@@ -2,16 +2,20 @@ import { Server } from 'node:http';
2
2
 
3
3
  interface Config {
4
4
  mocksDir: string
5
+ ignore?: RegExp
6
+
5
7
  staticDir?: string
8
+
6
9
  host?: string,
7
10
  port?: number
8
- ignore?: RegExp
9
- delay?: number
10
- onReady?: (address: string) => void
11
- cookies?: object
12
11
  proxyFallback?: string
12
+
13
+ delay?: number
14
+ cookies?: { [label: string]: string }
13
15
  extraHeaders?: [string, string][]
14
- extraMimes?: object
16
+ extraMimes?: { [fileExt: string]: string }
17
+
18
+ onReady?: (address: string) => void
15
19
  }
16
20
 
17
21
  export function Mockaton(options: Config): Server
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": "6.3.0",
5
+ "version": "6.3.4",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Config.js CHANGED
@@ -3,34 +3,42 @@ import { validate, is, optional } from './utils/validate.js'
3
3
  import { isDirectory } from './utils/fs.js'
4
4
 
5
5
 
6
- export const Config = {
6
+ export const Config = Object.seal({
7
7
  mocksDir: '',
8
+ ignore: /(\.DS_Store|~)$/,
9
+
8
10
  staticDir: '',
11
+
9
12
  host: '127.0.0.1',
10
13
  port: 0, // auto-assigned
11
- ignore: /(\.DS_Store|~)$/,
14
+ proxyFallback: '', // e.g. http://localhost:9999
15
+
12
16
  delay: 1200, // milliseconds
13
17
  cookies: {}, // defaults to the first kv
14
- onReady: openInBrowser,
15
- proxyFallback: '', // e.g. http://localhost:9999
16
18
  extraHeaders: [],
17
- extraMimes: {}
18
- }
19
+ extraMimes: {},
20
+
21
+ onReady: openInBrowser
22
+ })
19
23
 
20
24
  export function setup(options) {
21
25
  Object.assign(Config, options)
22
26
  validate(Config, {
23
27
  mocksDir: isDirectory,
28
+ ignore: is(RegExp),
29
+
24
30
  staticDir: optional(isDirectory),
31
+
25
32
  host: is(String),
26
33
  port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
27
- ignore: is(RegExp),
34
+ proxyFallback: optional(URL.canParse),
35
+
28
36
  delay: ms => Number.isInteger(ms) && ms > 0,
29
37
  cookies: is(Object),
30
- onReady: is(Function),
31
- proxyFallback: optional(URL.canParse),
32
38
  extraHeaders: Array.isArray,
33
- extraMimes: is(Object)
39
+ extraMimes: is(Object),
40
+
41
+ onReady: is(Function)
34
42
  })
35
43
  }
36
44
 
package/src/MockBroker.js CHANGED
@@ -1,7 +1,5 @@
1
- import { join } from 'node:path'
2
1
  import { Route } from './Route.js'
3
2
  import { Config } from './Config.js'
4
- import { isDirectory } from './utils/fs.js'
5
3
  import { DEFAULT_500_COMMENT } from './ApiConstants.js'
6
4
 
7
5
 
@@ -12,7 +10,6 @@ export class MockBroker {
12
10
 
13
11
  constructor(file) {
14
12
  this.#route = new Route(file)
15
- this.method = this.#route.method
16
13
  this.mocks = []
17
14
  this.currentMock = {
18
15
  file: '',
@@ -61,19 +58,11 @@ export class MockBroker {
61
58
  if (!this.#has500())
62
59
  this.#registerTemp500()
63
60
  }
64
-
65
61
  #has500() {
66
- return this.mocks.some(mock =>
67
- Route.parseFilename(mock).status === 500)
62
+ return this.mocks.some(mock => Route.parseFilename(mock).status === 500)
68
63
  }
69
-
70
64
  #registerTemp500() {
71
65
  const { urlMask, method } = Route.parseFilename(this.mocks[0])
72
- let mask = urlMask
73
- if (isDirectory(join(Config.mocksDir, urlMask)))
74
- mask = urlMask + '/'
75
- mask = mask.replace(/^\//, '') // remove initial slash
76
- const file = `${mask}${DEFAULT_500_COMMENT}.${method}.500.txt`
77
- this.register(file)
66
+ this.register(`${urlMask}${DEFAULT_500_COMMENT}.${method}.500.txt`)
78
67
  }
79
68
  }
@@ -22,7 +22,7 @@ export async function dispatchMock(req, response) {
22
22
 
23
23
  try {
24
24
  const { file, status, delay } = broker
25
- console.log('\n', req.url, '→\n ', file)
25
+ console.log(req.url, ' ', file)
26
26
 
27
27
  let mockText
28
28
  if (file.endsWith('.js')) {
package/src/Route.js CHANGED
@@ -14,8 +14,7 @@ export class Route {
14
14
  #urlRegex
15
15
 
16
16
  constructor(file) {
17
- const { urlMask, method } = Route.parseFilename(file)
18
- this.method = method
17
+ const { urlMask } = Route.parseFilename(file)
19
18
  this.#urlRegex = new RegExp('^' + disregardVariables(removeQueryStringAndFragment(urlMask)) + '/*$')
20
19
  }
21
20