mockaton 7.5.2 → 7.6.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
@@ -150,6 +150,8 @@ export default function optionalName(request, response) {
150
150
  }
151
151
  ```
152
152
 
153
+ If you need to serve a static `.js` file, put it in `Config.staticDir`.
154
+
153
155
 
154
156
  ## File Name Convention
155
157
 
@@ -203,6 +205,16 @@ api/foo/.GET.200.json
203
205
 
204
206
  ---
205
207
 
208
+ ## `Config.staticDir`
209
+ Files under `Config.staticDir` don’t use the filename convention.
210
+ Also, they take precedence over the `GET` mocks in `Config.mockDir`.
211
+
212
+ For example, if you have two files for `GET /foo/bar.jpg`
213
+ ```
214
+ my-static-dir/foo/bar.jpg
215
+ my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
216
+ ```
217
+
206
218
 
207
219
  ## `Config.proxyFallback`
208
220
  Lets you specify a target server for serving routes you don’t have mocks for.
@@ -359,4 +371,5 @@ await mockaton.reset()
359
371
 
360
372
  ## TODO
361
373
  - Refactor Tests
362
- - Dashboard. List `staticDir` and indicate if it’s overriding some mock.
374
+ - Dashboard. Indicate if some static it’s overriding a mock.
375
+ - jsonc, json5
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "A deterministic server-side for developing and testing frontend clients",
4
4
  "type": "module",
5
- "version": "7.5.2",
5
+ "version": "7.6.0",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Api.js CHANGED
@@ -8,6 +8,7 @@ import { cookie } from './cookie.js'
8
8
  import { Config } from './Config.js'
9
9
  import { DF, API } from './ApiConstants.js'
10
10
  import { parseJSON } from './utils/http-request.js'
11
+ import { listFilesRecursively } from './utils/fs.js'
11
12
  import * as mockBrokersCollection from './mockBrokersCollection.js'
12
13
  import { sendOK, sendBadRequest, sendJSON, sendFile } from './utils/http-response.js'
13
14
 
@@ -23,7 +24,8 @@ export const apiGetRequests = new Map([
23
24
  [API.mocks, listMockBrokers],
24
25
  [API.cookies, listCookies],
25
26
  [API.comments, listComments],
26
- [API.cors, getIsCorsAllowed]
27
+ [API.cors, getIsCorsAllowed],
28
+ [API.static, listStaticFiles]
27
29
  ])
28
30
 
29
31
  export const apiPatchRequests = new Map([
@@ -128,3 +130,17 @@ async function setCorsAllowed(req, response) {
128
130
  sendBadRequest(response, error)
129
131
  }
130
132
  }
133
+
134
+
135
+ // TESTME
136
+ async function listStaticFiles(req, response) {
137
+ try {
138
+ const files = Config.staticDir
139
+ ? listFilesRecursively(Config.staticDir).filter(f => !Config.ignore.test(f))
140
+ : []
141
+ sendJSON(response, files)
142
+ }
143
+ catch (error) {
144
+ sendBadRequest(response, error)
145
+ }
146
+ }
@@ -9,7 +9,8 @@ export const API = {
9
9
  reset: MOUNT + '/reset',
10
10
  cookies: MOUNT + '/cookies',
11
11
  fallback: MOUNT + '/fallback',
12
- cors: MOUNT + '/cors'
12
+ cors: MOUNT + '/cors',
13
+ static: MOUNT + '/static'
13
14
  }
14
15
 
15
16
  export const DF = { // Dashboard Fields (XHR)
package/src/Commander.js CHANGED
@@ -52,6 +52,10 @@ export class Commander {
52
52
  return this.#patch(API.cors, value)
53
53
  }
54
54
 
55
+ listStaticFiles() {
56
+ return this.#get(API.static)
57
+ }
58
+
55
59
 
56
60
  #get(api) {
57
61
  return fetch(this.#addr + api)
package/src/Dashboard.css CHANGED
@@ -88,9 +88,9 @@ menu {
88
88
  gap: 16px;
89
89
 
90
90
  img {
91
- width: 140px;
91
+ width: 130px;
92
92
  align-self: center;
93
- margin-right: 75px;
93
+ margin-right: 85px;
94
94
  }
95
95
 
96
96
  label {
@@ -311,6 +311,30 @@ main {
311
311
  font-weight: bold;
312
312
  }
313
313
 
314
+ .StaticFilesList {
315
+ margin-top: 40px;
316
+
317
+ summary {
318
+ margin-bottom: 8px;
319
+ cursor: pointer;
320
+ }
321
+
322
+ li {
323
+ list-style: none;
324
+ }
325
+
326
+ a {
327
+ color: var(--colorAccent);
328
+ text-decoration: none;
329
+ padding: 3px 0;
330
+ display: inline-block;
331
+
332
+ &:hover {
333
+ text-decoration: underline;
334
+ }
335
+ }
336
+ }
337
+
314
338
 
315
339
  /*
316
340
  * Prism
package/src/Dashboard.js CHANGED
@@ -14,6 +14,7 @@ const Strings = {
14
14
  mock: 'Mock',
15
15
  reset: 'Reset',
16
16
  select_one: 'Select One',
17
+ static: 'Static',
17
18
  title: 'Mockaton'
18
19
  }
19
20
 
@@ -25,6 +26,7 @@ const CSS = {
25
26
  PayloadViewer: 'PayloadViewer',
26
27
  PreviewLink: 'PreviewLink',
27
28
  ProgressBar: 'ProgressBar',
29
+ StaticFilesList: 'StaticFilesList',
28
30
 
29
31
  bold: 'bold',
30
32
  chosen: 'chosen',
@@ -42,20 +44,21 @@ function init() {
42
44
  mockaton.listMocks(),
43
45
  mockaton.listCookies(),
44
46
  mockaton.listComments(),
45
- mockaton.getCorsAllowed()
47
+ mockaton.getCorsAllowed(),
48
+ mockaton.listStaticFiles()
46
49
  ].map(api => api.then(response => response.ok && response.json())))
47
50
  .then(App)
48
51
  .catch(console.error)
49
52
  }
50
53
  init()
51
54
 
52
- function App([brokersByMethod, cookies, comments, corsAllowed]) {
55
+ function App([brokersByMethod, cookies, comments, corsAllowed, staticFiles]) {
53
56
  empty(document.body)
54
57
  createRoot(document.body).render(
55
- DevPanel(brokersByMethod, cookies, comments, corsAllowed))
58
+ DevPanel(brokersByMethod, cookies, comments, corsAllowed, staticFiles))
56
59
  }
57
60
 
58
- function DevPanel(brokersByMethod, cookies, comments, corsAllowed) {
61
+ function DevPanel(brokersByMethod, cookies, comments, corsAllowed, staticFiles) {
59
62
  document.title = Strings.title
60
63
  return (
61
64
  r('div', null,
@@ -71,7 +74,8 @@ function DevPanel(brokersByMethod, cookies, comments, corsAllowed) {
71
74
  r('div', { className: CSS.PayloadViewer },
72
75
  r('h2', { ref: refPayloadFile }, Strings.mock),
73
76
  r('pre', null,
74
- r('code', { ref: refPayloadViewer }, Strings.click_link_to_preview))))))
77
+ r('code', { ref: refPayloadViewer }, Strings.click_link_to_preview)))),
78
+ r(StaticFilesList, { staticFiles })))
75
79
  }
76
80
 
77
81
  function CookieSelector({ list }) {
@@ -140,6 +144,24 @@ function ResetButton() {
140
144
  )
141
145
  }
142
146
 
147
+ function StaticFilesList({ staticFiles }) {
148
+ console.log(staticFiles)
149
+ if (!staticFiles.length)
150
+ return null
151
+ return (
152
+ r('details', {
153
+ open: true,
154
+ className: CSS.StaticFilesList
155
+ },
156
+ r('summary', null, Strings.static),
157
+ r('ul', null,
158
+ staticFiles.map(f =>
159
+ r('li', null,
160
+ r('a', {
161
+ href: f,
162
+ target: '_blank'
163
+ }, f))))))
164
+ }
143
165
 
144
166
 
145
167
  function SectionByMethod({ method, brokers }) {
package/src/Mockaton.js CHANGED
@@ -46,13 +46,13 @@ async function onRequest(req, response) {
46
46
  if (isPreflight(req))
47
47
  sendNoContent(response)
48
48
 
49
- else if (method === 'GET' && apiGetRequests.has(url))
50
- apiGetRequests.get(url)(req, response)
51
-
52
49
  else if (method === 'PATCH' && apiPatchRequests.has(url))
53
50
  await apiPatchRequests.get(url)(req, response)
54
51
 
55
- else if (isStatic(req))
52
+ else if (method === 'GET' && apiGetRequests.has(url))
53
+ apiGetRequests.get(url)(req, response)
54
+
55
+ else if (method === 'GET' && isStatic(req))
56
56
  await dispatchStatic(req, response)
57
57
 
58
58
  else
@@ -5,11 +5,16 @@ import { sendFile, sendPartialContent, sendNotFound } from './utils/http-respons
5
5
 
6
6
 
7
7
  export function isStatic(req) {
8
- return Config.staticDir && resolvePath(req)
8
+ if (!Config.staticDir)
9
+ return false
10
+
11
+ const f = resolvePath(req.url)
12
+ return !Config.ignore.test(f) // TESTME
13
+ && Boolean(f)
9
14
  }
10
15
 
11
16
  export async function dispatchStatic(req, response) {
12
- const file = resolvePath(req)
17
+ const file = resolvePath(req.url)
13
18
  if (!file)
14
19
  sendNotFound(response)
15
20
  else if (req.headers.range)
@@ -18,8 +23,8 @@ export async function dispatchStatic(req, response) {
18
23
  sendFile(response, file)
19
24
  }
20
25
 
21
- function resolvePath(req) {
22
- let candidate = join(Config.staticDir, req.url)
26
+ function resolvePath(url) {
27
+ let candidate = join(Config.staticDir, url)
23
28
  if (isDirectory(candidate))
24
29
  candidate += '/index.html'
25
30
  if (isFile(candidate))