mockaton 8.11.7 → 8.12.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 +7 -5
- package/index.d.ts +1 -0
- package/package.json +3 -2
- package/src/Api.js +6 -10
- package/src/ProxyRelay.js +8 -1
- package/src/config.js +3 -1
- package/src/utils/http-response.js +1 -6
- package/src/utils/openInBrowser.js +10 -1
package/README.md
CHANGED
|
@@ -367,6 +367,11 @@ Mockaton’s predefined list. For that, you can add it to <code>config.extraMime
|
|
|
367
367
|
</details>
|
|
368
368
|
|
|
369
369
|
|
|
370
|
+
### `formatCollectedJSON?: boolean`
|
|
371
|
+
Defaults to `true`. Saves the mock with the formatting output
|
|
372
|
+
of `JSON.stringify(data, null, ' ')` (two spaces indentation).
|
|
373
|
+
|
|
374
|
+
|
|
370
375
|
### `staticDir?: string`
|
|
371
376
|
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
372
377
|
- Use Case 2: For a standalone demo server. For example,
|
|
@@ -497,11 +502,8 @@ config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
|
497
502
|
|
|
498
503
|
### `onReady?: (dashboardUrl: string) => void`
|
|
499
504
|
By default, it will open the dashboard in your default browser on macOS and
|
|
500
|
-
Windows. But for a more cross-platform utility
|
|
501
|
-
|
|
502
|
-
import open from 'open'
|
|
503
|
-
config.onReady = open
|
|
504
|
-
```
|
|
505
|
+
Windows. But for a more cross-platform utility you could `npm install open` and
|
|
506
|
+
Mockaton will use that implementation instead.
|
|
505
507
|
|
|
506
508
|
If you don’t want to open a browser, pass a noop:
|
|
507
509
|
```js
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mockaton",
|
|
3
|
-
"description": "A deterministic server-side for developing and testing
|
|
3
|
+
"description": "A deterministic server-side for developing and testing APIs",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.
|
|
5
|
+
"version": "8.12.0",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
8
8
|
"license": "MIT",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"outdated": "npm outdated --parseable | awk -F: '{ printf \"npm i %-30s ;# %s\\n\", $4, $2 }'"
|
|
16
16
|
},
|
|
17
17
|
"optionalDependencies": {
|
|
18
|
+
"open": "^10.0.0",
|
|
18
19
|
"pixaton": ">=1.0.2",
|
|
19
20
|
"puppeteer": ">=24.1.1"
|
|
20
21
|
}
|
package/src/Api.js
CHANGED
|
@@ -11,7 +11,7 @@ import { parseJSON } from './utils/http-request.js'
|
|
|
11
11
|
import { listFilesRecursively } from './utils/fs.js'
|
|
12
12
|
import * as mockBrokersCollection from './mockBrokersCollection.js'
|
|
13
13
|
import { DF, API, LONG_POLL_SERVER_TIMEOUT } from './ApiConstants.js'
|
|
14
|
-
import { sendOK, sendJSON, sendUnprocessableContent,
|
|
14
|
+
import { sendOK, sendJSON, sendUnprocessableContent, sendFile } from './utils/http-response.js'
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
const dashboardAssets = [
|
|
@@ -25,8 +25,7 @@ const dashboardAssets = [
|
|
|
25
25
|
|
|
26
26
|
export const apiGetRequests = new Map([
|
|
27
27
|
[API.dashboard, serveDashboard],
|
|
28
|
-
...dashboardAssets.map(f =>
|
|
29
|
-
[API.dashboard + f, serveDashboardAsset]),
|
|
28
|
+
...dashboardAssets.map(f => [API.dashboard + f, serveDashboardAsset(f)]),
|
|
30
29
|
[API.cors, getIsCorsAllowed],
|
|
31
30
|
[API.static, listStaticFiles],
|
|
32
31
|
[API.mocks, listMockBrokers],
|
|
@@ -54,15 +53,12 @@ export const apiPatchRequests = new Map([
|
|
|
54
53
|
/* === GET === */
|
|
55
54
|
|
|
56
55
|
function serveDashboard(_, response) {
|
|
57
|
-
|
|
56
|
+
sendFile(response, join(import.meta.dirname, 'Dashboard.html'))
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
function serveDashboardAsset(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
sendDashboardFile(response, join(import.meta.dirname, f))
|
|
64
|
-
else
|
|
65
|
-
sendForbidden(response)
|
|
59
|
+
function serveDashboardAsset(f) {
|
|
60
|
+
return (req, response) =>
|
|
61
|
+
sendFile(response, join(import.meta.dirname, f))
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
function listCookies(_, response) { sendJSON(response, cookie.list()) }
|
package/src/ProxyRelay.js
CHANGED
|
@@ -39,6 +39,13 @@ export async function proxy(req, response, delay) {
|
|
|
39
39
|
let filename = makeMockFilename(req.url, req.method, proxyResponse.status, ext)
|
|
40
40
|
if (isFile(join(config.mocksDir, filename))) // TESTME
|
|
41
41
|
filename = makeMockFilename(req.url + `(${randomUUID()})`, req.method, proxyResponse.status, ext)
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
let data = body
|
|
44
|
+
if (config.formatCollectedJSON && ext === 'json') // TESTME
|
|
45
|
+
try {
|
|
46
|
+
data = JSON.string(JSON.parse(body), null, ' ')
|
|
47
|
+
}
|
|
48
|
+
catch {}
|
|
49
|
+
write(join(config.mocksDir, filename), data)
|
|
43
50
|
}
|
|
44
51
|
}
|
package/src/config.js
CHANGED
|
@@ -16,6 +16,7 @@ export const config = Object.seal({
|
|
|
16
16
|
port: 0, // auto-assigned
|
|
17
17
|
proxyFallback: '', // e.g. http://localhost:9999
|
|
18
18
|
collectProxied: false,
|
|
19
|
+
formatCollectedJSON: true,
|
|
19
20
|
|
|
20
21
|
delay: 1200, // milliseconds
|
|
21
22
|
cookies: {}, // defaults to the first kv
|
|
@@ -34,7 +35,7 @@ export const config = Object.seal({
|
|
|
34
35
|
corsCredentials: true,
|
|
35
36
|
corsMaxAge: 0,
|
|
36
37
|
|
|
37
|
-
onReady: openInBrowser
|
|
38
|
+
onReady: await openInBrowser
|
|
38
39
|
})
|
|
39
40
|
|
|
40
41
|
|
|
@@ -49,6 +50,7 @@ export function setup(options) {
|
|
|
49
50
|
port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
|
|
50
51
|
proxyFallback: optional(URL.canParse),
|
|
51
52
|
collectProxied: is(Boolean),
|
|
53
|
+
formatCollectedJSON: is(Boolean),
|
|
52
54
|
|
|
53
55
|
delay: ms => Number.isInteger(ms) && ms > 0,
|
|
54
56
|
cookies: is(Object),
|
|
@@ -17,12 +17,7 @@ export function sendJSON(response, payload) {
|
|
|
17
17
|
response.end(JSON.stringify(payload))
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export function
|
|
21
|
-
response.statusCode = 403
|
|
22
|
-
response.end()
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function sendDashboardFile(response, file) {
|
|
20
|
+
export function sendFile(response, file) {
|
|
26
21
|
response.setHeader('Content-Type', mimeFor(file))
|
|
27
22
|
response.end(readFileSync(file, 'utf8'))
|
|
28
23
|
}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { exec } from 'node:child_process'
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export const openInBrowser = (async () => {
|
|
5
|
+
try {
|
|
6
|
+
return (await import('open')).default
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
return _openInBrowser
|
|
10
|
+
}
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
function _openInBrowser(address) {
|
|
5
14
|
switch (process.platform) {
|
|
6
15
|
case 'darwin':
|
|
7
16
|
exec(`open ${address}`)
|