mockaton 8.2.18 → 8.2.20
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 +47 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
|
18
18
|
can create a TAR of your requests following that convention.
|
|
19
19
|
|
|
20
20
|
Nonetheless, you don’t need to mock all your APIs. Mockaton can request from your backend
|
|
21
|
-
the routes you don’t have mocks for. That’s done with `
|
|
21
|
+
the routes you don’t have mocks for. That’s done with `config.proxyFallback = 'http://mybackend'`
|
|
22
22
|
|
|
23
23
|
## Multiple Mock Variants
|
|
24
24
|
Each route can have many mocks, which could either be:
|
|
@@ -30,8 +30,8 @@ Each route can have many mocks, which could either be:
|
|
|
30
30
|
## Dashboard UI
|
|
31
31
|
|
|
32
32
|
In the dashboard, you can select a mock variant for a particular
|
|
33
|
-
route
|
|
34
|
-
|
|
33
|
+
route, among other options. But there’s also a programmatic API,
|
|
34
|
+
which is handy for setting up tests (see **Commander API** below).
|
|
35
35
|
|
|
36
36
|
<picture>
|
|
37
37
|
<source media="(prefers-color-scheme: light)" srcset="./README-dashboard-light.png">
|
|
@@ -66,10 +66,12 @@ node --import=tsx my-mockaton.js
|
|
|
66
66
|
## Running the Demo Example
|
|
67
67
|
This demo uses the [sample-mocks/](./sample-mocks) directory of this repository.
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
```sh
|
|
70
|
+
git clone https://github.com/ericfortis/mockaton.git
|
|
71
|
+
cd mockaton
|
|
72
|
+
npm install tsx
|
|
73
|
+
npm run demo:ts
|
|
74
|
+
```
|
|
73
75
|
|
|
74
76
|
Experiment with the Dashboard:
|
|
75
77
|
|
|
@@ -100,7 +102,7 @@ allows for checking out long-lived branches with old API contracts.
|
|
|
100
102
|
### Deterministic Standalone Demo Server
|
|
101
103
|
Perhaps you need to demo your app, but the ideal flow is too complex to
|
|
102
104
|
simulate from the actual backend. In this case, compile your frontend app and
|
|
103
|
-
put its built assets in `
|
|
105
|
+
put its built assets in `config.staticDir`. Then, from the Mockaton dashboard
|
|
104
106
|
you can "Bulk Select" mocks to simulate the complete states you want to demo.
|
|
105
107
|
For bulk-selecting, you just need to add a comment to the mock
|
|
106
108
|
filename, such as `(demo-part1)`, `(demo-part2)`.
|
|
@@ -108,8 +110,8 @@ filename, such as `(demo-part1)`, `(demo-part2)`.
|
|
|
108
110
|
|
|
109
111
|
## Motivation
|
|
110
112
|
- Avoids spinning up and maintaining hefty backends when developing UIs.
|
|
111
|
-
- For a deterministic and
|
|
112
|
-
|
|
113
|
+
- For a deterministic, comprehensive, and consistent backend state. For example, having
|
|
114
|
+
a collection with all the possible state variants helps for spotting inadvertent bugs.
|
|
113
115
|
- Sometimes, frontend progress is blocked waiting for some backend API. Similarly,
|
|
114
116
|
it’s often delayed due to missing data or inconvenient contracts. Therefore,
|
|
115
117
|
many meetings can be saved by prototyping frontend features with mocks, and
|
|
@@ -146,7 +148,8 @@ export default [{ foo: 'bar' }]
|
|
|
146
148
|
Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
147
149
|
|
|
148
150
|
```js
|
|
149
|
-
export default (request, response) =>
|
|
151
|
+
export default (request, response) =>
|
|
152
|
+
JSON.stringify({ foo: 'bar' })
|
|
150
153
|
```
|
|
151
154
|
|
|
152
155
|
Think of these functions as HTTP handlers, so you can
|
|
@@ -191,7 +194,7 @@ export default function listColors() {
|
|
|
191
194
|
---
|
|
192
195
|
|
|
193
196
|
If you are wondering, what if I need to serve a static `.js`?
|
|
194
|
-
Put it in your `
|
|
197
|
+
Put it in your `config.staticDir` without the mock filename convention.
|
|
195
198
|
|
|
196
199
|
---
|
|
197
200
|
|
|
@@ -236,17 +239,18 @@ permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file)
|
|
|
236
239
|
|
|
237
240
|
### Index-like route
|
|
238
241
|
For instance, if you have `api/foo/bar` and
|
|
239
|
-
`api/foo
|
|
242
|
+
`api/foo`, you have two options:
|
|
240
243
|
|
|
241
|
-
**Option A
|
|
244
|
+
**Option A:**
|
|
242
245
|
```
|
|
243
|
-
api/foo/
|
|
244
246
|
api/foo.GET.200.json
|
|
247
|
+
api/foo/bar.GET.200.json
|
|
245
248
|
```
|
|
246
249
|
|
|
247
|
-
**Option B
|
|
250
|
+
**Option B:** Omit the filename.
|
|
248
251
|
```text
|
|
249
252
|
api/foo/.GET.200.json
|
|
253
|
+
api/foo/bar.GET.200.json
|
|
250
254
|
```
|
|
251
255
|
|
|
252
256
|
---
|
|
@@ -269,12 +273,12 @@ Defaults to `/(\.DS_Store|~)$/`
|
|
|
269
273
|
|
|
270
274
|
### `delay?: number` 🕓
|
|
271
275
|
The clock icon next to the mock selector is a checkbox for delaying a particular
|
|
272
|
-
response. The delay is globally configurable via `
|
|
276
|
+
response. The delay is globally configurable via `config.delay = 1200` (milliseconds).
|
|
273
277
|
|
|
274
278
|
|
|
275
279
|
### `proxyFallback?: string`
|
|
276
280
|
Lets you specify a target server for serving routes you don’t have mocks for.
|
|
277
|
-
For example, `
|
|
281
|
+
For example, `config.proxyFallback = 'http://example.com'`
|
|
278
282
|
|
|
279
283
|
|
|
280
284
|
### `staticDir?: string`
|
|
@@ -282,8 +286,8 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
282
286
|
- Use Case 2: For a standalone demo server. For example,
|
|
283
287
|
build your frontend bundle, and serve it from Mockaton.
|
|
284
288
|
|
|
285
|
-
Files under `
|
|
286
|
-
They take precedence over the `GET` mocks in `
|
|
289
|
+
Files under `config.staticDir` don’t use the filename convention.
|
|
290
|
+
They take precedence over the `GET` mocks in `config.mocksDir`.
|
|
287
291
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
288
292
|
```
|
|
289
293
|
my-static-dir/foo/bar.jpg
|
|
@@ -292,18 +296,11 @@ my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
|
292
296
|
|
|
293
297
|
|
|
294
298
|
### `cookies?: { [label: string]: string }`
|
|
295
|
-
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
296
|
-
The key is just a label used for selecting a particular cookie in the
|
|
297
|
-
dashboard. In the dashboard, only one cookie can be selected. If you need more
|
|
298
|
-
cookies you can inject additional cookies globally in `Config.extraHeaders`.
|
|
299
|
-
|
|
300
|
-
By the way, there’s a `jwtCookie` helper, which has a hardcoded header and
|
|
301
|
-
signature. In other words, it’s useful if you only care about its payload.
|
|
302
299
|
|
|
303
300
|
```js
|
|
304
301
|
import { jwtCookie } from 'mockaton'
|
|
305
302
|
|
|
306
|
-
|
|
303
|
+
config.cookies = {
|
|
307
304
|
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
308
305
|
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
309
306
|
'My JWT': jwtCookie('my-cookie', {
|
|
@@ -312,13 +309,21 @@ Config.cookies = {
|
|
|
312
309
|
})
|
|
313
310
|
}
|
|
314
311
|
```
|
|
312
|
+
The selected cookie is sent in every response in a `Set-Cookie` header.
|
|
313
|
+
|
|
314
|
+
By the way, the `jwtCookie` helper has a hardcoded header and signature.
|
|
315
|
+
In other words, it’s useful only if you care about the payload.
|
|
316
|
+
|
|
317
|
+
If you need to send more than one cookie,
|
|
318
|
+
inject them globally in `config.extraHeaders`.
|
|
319
|
+
|
|
315
320
|
|
|
316
321
|
|
|
317
322
|
### `extraHeaders?: string[]`
|
|
318
323
|
Note it’s a unidimensional array. The header name goes at even indices.
|
|
319
324
|
|
|
320
325
|
```js
|
|
321
|
-
|
|
326
|
+
config.extraHeaders = [
|
|
322
327
|
'Server', 'Mockaton',
|
|
323
328
|
'Set-Cookie', 'foo=FOO;Path=/;SameSite=strict',
|
|
324
329
|
'Set-Cookie', 'bar=BAR;Path=/;SameSite=strict'
|
|
@@ -328,7 +333,7 @@ Config.extraHeaders = [
|
|
|
328
333
|
|
|
329
334
|
### `extraMimes?: { [fileExt: string]: string }`
|
|
330
335
|
```js
|
|
331
|
-
|
|
336
|
+
config.extraMimes = {
|
|
332
337
|
jpe: 'application/jpeg'
|
|
333
338
|
}
|
|
334
339
|
```
|
|
@@ -360,7 +365,7 @@ import { parse } from 'yaml'
|
|
|
360
365
|
import { readFileSync } from 'node:js'
|
|
361
366
|
import { jsToJsonPlugin } from 'mockaton'
|
|
362
367
|
|
|
363
|
-
|
|
368
|
+
config.plugins = [
|
|
364
369
|
[/\.(js|ts)$/, jsToJsonPlugin], // Default
|
|
365
370
|
[/\.yml$/, yamlToJsonPlugin],
|
|
366
371
|
[/foo\.GET\.200\.txt$/, capitalizePlugin], // e.g. GET /api/foo would be capitalized
|
|
@@ -384,14 +389,14 @@ function capitalizePlugin(filePath) {
|
|
|
384
389
|
|
|
385
390
|
|
|
386
391
|
### `corsAllowed?: boolean`
|
|
387
|
-
Defaults to `corsAllowed = false`. When `
|
|
392
|
+
Defaults to `corsAllowed = false`. When `config.corsAllowed === true`, these are the default options:
|
|
388
393
|
```js
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
394
|
+
config.corsOrigins = ['*']
|
|
395
|
+
config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
|
|
396
|
+
config.corsHeaders = ['content-type']
|
|
397
|
+
config.corsCredentials = true
|
|
398
|
+
config.corsMaxAge = 0 // seconds to cache the preflight req
|
|
399
|
+
config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
395
400
|
```
|
|
396
401
|
|
|
397
402
|
|
|
@@ -400,12 +405,12 @@ This defaults to trying to open the dashboard in your default browser in macOS a
|
|
|
400
405
|
Windows. For a more cross-platform utility, you could `npm install open` and pass it.
|
|
401
406
|
```js
|
|
402
407
|
import open from 'open'
|
|
403
|
-
|
|
408
|
+
config.onReady = open
|
|
404
409
|
```
|
|
405
410
|
|
|
406
411
|
If you don’t want to open a browser, pass a noop:
|
|
407
412
|
```js
|
|
408
|
-
|
|
413
|
+
config.onReady = () => {}
|
|
409
414
|
```
|
|
410
415
|
|
|
411
416
|
|
|
@@ -439,7 +444,7 @@ await mockaton.setRouteIsDelayed('GET', '/api/foo', true)
|
|
|
439
444
|
```
|
|
440
445
|
|
|
441
446
|
### Select a cookie
|
|
442
|
-
In `
|
|
447
|
+
In `config.cookies`, each key is the label used for selecting it.
|
|
443
448
|
```js
|
|
444
449
|
await mockaton.selectCookie('My Normal User')
|
|
445
450
|
```
|
|
@@ -453,7 +458,7 @@ Pass an empty string to disable it.
|
|
|
453
458
|
### Reset
|
|
454
459
|
Re-initialize the collection. So if you added or removed mocks they will
|
|
455
460
|
be considered. The selected mocks, cookies, and delays go back to default,
|
|
456
|
-
but `
|
|
461
|
+
but `config.proxyFallback` and `config.corsAllowed` are not affected.
|
|
457
462
|
```js
|
|
458
463
|
await mockaton.reset()
|
|
459
464
|
```
|
package/package.json
CHANGED