mockaton 8.11.8 → 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 +5 -0
- package/index.d.ts +1 -0
- package/package.json +2 -2
- package/src/Api.js +6 -10
- package/src/ProxyRelay.js +8 -1
- package/src/config.js +2 -0
- package/src/utils/http-response.js +1 -6
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,
|
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",
|
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
|
|
@@ -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
|
}
|