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 +14 -9
- package/index.d.ts +9 -5
- package/package.json +1 -1
- package/src/Config.js +18 -10
- package/src/MockBroker.js +2 -13
- package/src/MockDispatcher.js +1 -1
- package/src/Route.js +1 -2
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
|
-
|
|
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
|
|
84
|
-
default JSON content type. But don’t call
|
|
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
|
-
|
|
87
|
-
|
|
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?:
|
|
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
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/MockDispatcher.js
CHANGED
package/src/Route.js
CHANGED
|
@@ -14,8 +14,7 @@ export class Route {
|
|
|
14
14
|
#urlRegex
|
|
15
15
|
|
|
16
16
|
constructor(file) {
|
|
17
|
-
const { urlMask
|
|
18
|
-
this.method = method
|
|
17
|
+
const { urlMask } = Route.parseFilename(file)
|
|
19
18
|
this.#urlRegex = new RegExp('^' + disregardVariables(removeQueryStringAndFragment(urlMask)) + '/*$')
|
|
20
19
|
}
|
|
21
20
|
|