mockaton 8.26.1 → 9.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+
4
+ ## 9.0.0 (9/13/25)
5
+ - **Breaking change**: Commander GET APIs have been consolidated into `commander.getState()`. These were undocumented APIs, so likely you are not affected.
6
+ - `--no-open` new cli flag. Prevents opening dashboard in a browser. (No-ops `config.onReady`)
package/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
  ![NPM Version](https://img.shields.io/npm/l/mockaton)
5
5
  [![Test](https://github.com/ericfortis/mockaton/actions/workflows/test.yml/badge.svg)](https://github.com/ericfortis/mockaton/actions/workflows/test.yml)
6
6
  [![CodeQL](https://github.com/ericfortis/mockaton/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/ericfortis/mockaton/actions/workflows/github-code-scanning/codeql)
7
+ [![codecov](https://codecov.io/github/ericfortis/mockaton/graph/badge.svg?token=90NYLMMG1J)](https://codecov.io/github/ericfortis/mockaton)
8
+
7
9
 
8
10
  An HTTP mock server for simulating APIs with minimal setup
9
11
  — ideal for testing difficult to reproduce states.
@@ -55,9 +57,9 @@ Nonetheless, there’s a programmatic API, which is handy
55
57
  for setting up tests (see **Commander API** section below).
56
58
 
57
59
  <picture>
58
- <source media="(prefers-color-scheme: light)" srcset="pixaton-tests/macos/pic-for-readme.vp880x768.light.gold.png">
59
- <source media="(prefers-color-scheme: dark)" srcset="pixaton-tests/macos/pic-for-readme.vp880x768.dark.gold.png">
60
- <img alt="Mockaton Dashboard" src="pixaton-tests/macos/pic-for-readme.vp880x768.light.gold.png">
60
+ <source media="(prefers-color-scheme: light)" srcset="pixaton-tests/macos/pic-for-readme.vp810x768.light.gold.png">
61
+ <source media="(prefers-color-scheme: dark)" srcset="pixaton-tests/macos/pic-for-readme.vp810x768.dark.gold.png">
62
+ <img alt="Mockaton Dashboard" src="pixaton-tests/macos/pic-for-readme.vp810x768.light.gold.png">
61
63
  </picture>
62
64
 
63
65
 
@@ -116,20 +118,60 @@ They will be saved in your `config.mocksDir` following the filename convention.
116
118
 
117
119
  <br/>
118
120
 
121
+ ## Privacy and Security
122
+ - Zero dependencies (no runtime and no build packages).
123
+ - Does not write to disk. Except when you select ✅ **Save Mocks** for scraping mocks from a backend.
124
+ - Does not initiate network connections (no logs, no telemetry).
125
+ - Does not hijack your HTTP client.
126
+ - Auditable. Organized and small &mdash; under 4 KLoC (50% is UI and tests).
127
+
119
128
 
120
129
  ## Basic Usage
121
- Mockaton is a Node.js program available as an NPM module. After you have
122
- Node.js, you can create a sample mock in the default mocks directory, which
123
- is `./mockaton-mocks`. Then run it with `npx`, which installs it if needed.
130
+ 1. Install Node.js, which comes with `npm` and `npx`
131
+
132
+ 2. Create a sample mock in the default mocks directory (`./mockaton-mocks`)
124
133
  ```sh
125
134
  mkdir -p mockaton-mocks/api/
126
135
  echo "[1,2,3]" > mockaton-mocks/api/foo.GET.200.json
127
-
136
+ ```
137
+ 3. Run Mockaton (npx installs it if needed)
138
+ ```shell
128
139
  npx mockaton --port 2345
129
140
  ```
130
141
 
142
+ <details>
143
+ <summary>Or, if you have a package.json</summary>
144
+
145
+ ```shell
146
+ npm install mockaton --save-dev
147
+ ```
148
+
149
+ ```json
150
+ {
151
+ "scripts": {
152
+ "mockaton": "mockaton --port 2345"
153
+ }
154
+ }
155
+ ```
156
+ </details>
157
+
158
+
159
+ <details>
160
+ <summary>Or, if want an executable without using NPM?</summary>
161
+
162
+ Clone this repo, and make a link to `src/cli.js`
163
+ ```shell
164
+ git clone https://github.com/ericfortis/mockaton.git
165
+ ln -s `realpath mockaton/src/cli.js` ~/bin/mockaton # some dir in your $PATH
166
+ ```
167
+
168
+
169
+ </details>
170
+
171
+
172
+
131
173
  ## CLI Options
132
- CLI options override their counterparts in `mockaton.config.js`
174
+ The CLI options override their counterparts in `mockaton.config.js`
133
175
 
134
176
  ```txt
135
177
  -c, --config <file> (default: ./mockaton.config.js)
@@ -141,13 +183,15 @@ CLI options override their counterparts in `mockaton.config.js`
141
183
  -p, --port <port> (default: 0) which means auto-assigned
142
184
 
143
185
  -q, --quiet Errors only
186
+ --no-open Don’t open dashboard in a browser (noops onReady callback)
187
+
144
188
  -h, --help Show this help
145
189
  -v, --version Show version
146
190
  ```
147
191
 
148
192
 
149
193
  ## mockaton.config.js (Optional)
150
- As an overview, these are the default options:
194
+ As an overview, these are the defaults:
151
195
  ```js
152
196
  import {
153
197
  defineConfig,
@@ -163,24 +207,20 @@ export default defineConfig({
163
207
 
164
208
  host: '127.0.0.1',
165
209
  port: 0,
210
+
211
+ logLevel: 'normal',
212
+
213
+ delay: 1200,
214
+ delayJitter: 0,
166
215
 
167
216
  proxyFallback: '',
168
217
  collectProxied: false,
169
218
  formatCollectedJSON: true,
170
219
 
171
- delay: 1200,
172
- delayJitter: 0,
173
-
174
220
  cookies: {},
175
-
176
221
  extraHeaders: [],
177
-
178
222
  extraMimes: {},
179
223
 
180
- plugins: [
181
- [/\.(js|ts)$/, jsToJsonPlugin]
182
- ],
183
-
184
224
  corsAllowed: true,
185
225
  corsOrigins: ['*'],
186
226
  corsMethods: SUPPORTED_METHODS,
@@ -188,10 +228,13 @@ export default defineConfig({
188
228
  corsExposedHeaders: [],
189
229
  corsCredentials: true,
190
230
  corsMaxAge: 0,
231
+
232
+
233
+ plugins: [
234
+ [/\.(js|ts)$/, jsToJsonPlugin]
235
+ ],
191
236
 
192
237
  onReady: await openInBrowser,
193
-
194
- logLevel: 'normal'
195
238
  })
196
239
  ```
197
240
 
@@ -331,7 +374,8 @@ config.extraHeaders = [
331
374
  ### `extraMimes?: { [fileExt: string]: string }`
332
375
  ```js
333
376
  config.extraMimes = {
334
- jpe: 'application/jpeg'
377
+ jpe: 'application/jpeg',
378
+ html: 'text/html; charset=utf-8' // overrides built-in
335
379
  }
336
380
  ```
337
381
  Those extra media types take precedence over the built-in
@@ -709,6 +753,14 @@ with many partial matches, their first mock in alphabetical order wins.
709
753
  ### Set route is delayed flag
710
754
  ```js
711
755
  await mockaton.setRouteIsDelayed('GET', '/api/foo', true)
756
+ // or
757
+ await mockaton.setStaticRouteIsDelayed('/api/foo', true)
758
+ ```
759
+ <br/>
760
+
761
+ ### Set static route status
762
+ ```js
763
+ await mockaton.setStaticRouteStatus('/api/foo', 404)
712
764
  ```
713
765
 
714
766
  ### Set route is proxied flag
@@ -739,6 +791,23 @@ await mockaton.setCollectProxied(true)
739
791
 
740
792
  <br/>
741
793
 
794
+ ### Set global delay value
795
+ ```js
796
+ await mockaton.setGlobalDelsy(1200) // ms
797
+ ```
798
+
799
+ <br/>
800
+
801
+
802
+ ### Set CORS allowed
803
+ ```js
804
+ await mockaton.setCorsAllowed(true)
805
+ ```
806
+
807
+ <br/>
808
+
809
+
810
+
742
811
  ### Reset
743
812
  Re-initialize the collection. The selected mocks, cookies, and delays go back to
744
813
  default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not affected.
package/index.d.ts CHANGED
@@ -16,22 +16,20 @@ interface Config {
16
16
 
17
17
  host?: string,
18
18
  port?: number
19
+
20
+ logLevel?: 'normal' | 'quiet'
21
+
22
+ delay?: number
23
+ delayJitter?: number
19
24
 
20
25
  proxyFallback?: string
21
26
  collectProxied?: boolean
22
27
  formatCollectedJSON?: boolean
23
28
 
24
- delay?: number
25
- delayJitter?: number
26
-
27
29
  cookies?: { [label: string]: string }
28
-
29
30
  extraHeaders?: string[]
30
-
31
31
  extraMimes?: { [fileExt: string]: string }
32
32
 
33
- plugins?: [filenameTester: RegExp, plugin: Plugin][]
34
-
35
33
  corsAllowed?: boolean,
36
34
  corsOrigins?: string[]
37
35
  corsMethods?: string[]
@@ -39,15 +37,16 @@ interface Config {
39
37
  corsExposedHeaders?: string[]
40
38
  corsCredentials?: boolean
41
39
  corsMaxAge?: number
40
+
41
+
42
+ plugins?: [filenameTester: RegExp, plugin: Plugin][]
42
43
 
43
44
  onReady?: (address: string) => void
44
-
45
- logLevel?: 'normal' | 'quiet'
46
45
  }
47
46
 
48
47
 
49
48
  export function Mockaton(options: Partial<Config>): Server | undefined
50
- export function defineConfig(options: Partial<Config>): Config
49
+ export function defineConfig(options: Partial<Config>): Partial<Config>
51
50
 
52
51
  export const jsToJsonPlugin: Plugin
53
52
 
@@ -86,3 +85,17 @@ export type ClientStaticBrokers = {
86
85
  }
87
86
 
88
87
 
88
+ export interface State {
89
+ brokersByMethod: ClientBrokersByMethod
90
+ staticBrokers: ClientStaticBrokers
91
+
92
+ cookies: [label:string, selected:boolean][]
93
+ comments: string[]
94
+
95
+ delay: number
96
+
97
+ collectProxied: boolean
98
+ proxyFallback: string
99
+
100
+ corsAllowed: boolean
101
+ }
package/index.js CHANGED
@@ -2,9 +2,7 @@ export { Mockaton } from './src/Mockaton.js'
2
2
  export { Commander } from './src/ApiCommander.js'
3
3
 
4
4
  export { jwtCookie } from './src/utils/jwt.js'
5
- export { openInBrowser } from './src/utils/openInBrowser.js'
6
5
  export { jsToJsonPlugin } from './src/MockDispatcher.js'
7
- export { parseJSON, SUPPORTED_METHODS } from './src/utils/http-request.js'
6
+ export { parseJSON } from './src/utils/http-request.js'
8
7
 
9
- /** @param {Partial<Config>} opts */
10
8
  export const defineConfig = opts => opts