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 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
@@ -18,6 +18,7 @@ interface Config {
18
18
  port?: number
19
19
  proxyFallback?: string
20
20
  collectProxied?: boolean
21
+ formatCollectedJSON?: boolean
21
22
 
22
23
  delay?: number
23
24
  cookies?: { [label: string]: string }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "mockaton",
3
- "description": "A deterministic server-side for developing and testing frontend clients",
3
+ "description": "A deterministic server-side for developing and testing APIs",
4
4
  "type": "module",
5
- "version": "8.11.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, sendDashboardFile, sendForbidden } from './utils/http-response.js'
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
- sendDashboardFile(response, join(import.meta.dirname, 'Dashboard.html'))
56
+ sendFile(response, join(import.meta.dirname, 'Dashboard.html'))
58
57
  }
59
58
 
60
- function serveDashboardAsset(req, response) {
61
- const f = req.url.replace(API.dashboard, '')
62
- if (dashboardAssets.includes(f))
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
- write(join(config.mocksDir, filename), body)
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 sendForbidden(response) {
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
  }