mockaton 8.2.17 → 8.2.19

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.
Files changed (2) hide show
  1. package/README.md +52 -51
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  ![NPM Version](https://img.shields.io/npm/l/mockaton)
5
5
 
6
6
 
7
- _Mockaton_ is a mock server for developing and testing frontend apps.
7
+ _Mockaton_ is a mock server for improving the frontend development and testing experience.
8
8
 
9
9
  With Mockaton, you don’t need to write code for wiring your mocks. Instead, it
10
10
  scans a given directory for filenames following a convention similar to the
@@ -12,17 +12,26 @@ URL paths. For example, the following file will be served on `/api/user/1234`
12
12
  ```
13
13
  my-mocks-dir/api/user/[user-id].GET.200.json
14
14
  ```
15
- Also, you don’t need to mock everything. Mockaton can request from your backend
16
- the routes you don’t have mocks for. See `Config.proxyFallback` below.
17
15
 
18
16
  By the way, [this browser
19
17
  extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
20
18
  can create a TAR of your requests following that convention.
21
19
 
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 `config.proxyFallback = 'http://mybackend'`
22
+
23
+ ## Multiple Mock Variants
24
+ Each route can have many mocks, which could either be:
25
+ - Different response __status code__. For example, for triggering errors.
26
+ - __Comment__ on the filename, which is anything within parentheses.
27
+ For example, `api/login(locked out user).POST.423.json`
28
+
29
+
22
30
  ## Dashboard UI
23
31
 
24
- In the dashboard, you can manually select which mock variant to serve for a particular
25
- route. So you can test different scenarios without changing code or the database state.
32
+ In the dashboard, you can select a mock variant for a particular
33
+ route, among other options. But there’s also a programmatic API,
34
+ which is handy for setting up tests (see **Commander API** below).
26
35
 
27
36
  <picture>
28
37
  <source media="(prefers-color-scheme: light)" srcset="./README-dashboard-light.png">
@@ -64,7 +73,7 @@ This demo uses the [sample-mocks/](./sample-mocks) directory of this repository.
64
73
 
65
74
  Experiment with the Dashboard:
66
75
 
67
- - Pick a mock variant from the _Mock dropdown_ (we’ll discuss them later)
76
+ - Pick a mock variant from the _Mock dropdown_
68
77
  - Toggle the 🕓 _Delay Responses_ button, (e.g. for testing spinners)
69
78
  - Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
70
79
 
@@ -91,7 +100,7 @@ allows for checking out long-lived branches with old API contracts.
91
100
  ### Deterministic Standalone Demo Server
92
101
  Perhaps you need to demo your app, but the ideal flow is too complex to
93
102
  simulate from the actual backend. In this case, compile your frontend app and
94
- put its built assets in `Config.staticDir`. Then, from the Mockaton dashboard
103
+ put its built assets in `config.staticDir`. Then, from the Mockaton dashboard
95
104
  you can "Bulk Select" mocks to simulate the complete states you want to demo.
96
105
  For bulk-selecting, you just need to add a comment to the mock
97
106
  filename, such as `(demo-part1)`, `(demo-part2)`.
@@ -99,8 +108,8 @@ filename, such as `(demo-part1)`, `(demo-part2)`.
99
108
 
100
109
  ## Motivation
101
110
  - Avoids spinning up and maintaining hefty backends when developing UIs.
102
- - For a deterministic and comprehensive backend state. For example, having all the possible
103
- state variants of a collection helps for spotting inadvertent bugs.
111
+ - For a deterministic, comprehensive, and consistent backend state. For example, having
112
+ a collection with all the possible state variants helps for spotting inadvertent bugs.
104
113
  - Sometimes, frontend progress is blocked waiting for some backend API. Similarly,
105
114
  it’s often delayed due to missing data or inconvenient contracts. Therefore,
106
115
  many meetings can be saved by prototyping frontend features with mocks, and
@@ -112,17 +121,7 @@ filename, such as `(demo-part1)`, `(demo-part2)`.
112
121
  - [Mock Server Worker](https://mswjs.io)
113
122
 
114
123
  ---
115
-
116
- ## Multiple Mock Variants
117
- Each route can have many mocks, which could either be:
118
- - Different response __status code__. For example, for triggering errors.
119
- - __Comment__ on the filename, which is anything within parentheses.
120
- - e.g. `api/login(locked out user).POST.423.json`
121
-
122
- Those alternatives can be manually selected on the dashboard, or
123
- programmatically (see **Commander API** section), for instance, for setting up tests.
124
-
125
- ### Default Mock for a Route
124
+ ## Default Mock for a Route
126
125
  You can add the comment: `(default)` to a filename.
127
126
  Otherwise, the first file in **alphabetical order** wins.
128
127
 
@@ -147,7 +146,8 @@ export default [{ foo: 'bar' }]
147
146
  Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
148
147
 
149
148
  ```js
150
- export default (request, response) => JSON.stringify({ foo: 'bar' })
149
+ export default (request, response) =>
150
+ JSON.stringify({ foo: 'bar' })
151
151
  ```
152
152
 
153
153
  Think of these functions as HTTP handlers, so you can
@@ -192,7 +192,7 @@ export default function listColors() {
192
192
  ---
193
193
 
194
194
  If you are wondering, what if I need to serve a static `.js`?
195
- Put it in your `Config.staticDir` without the mock filename convention.
195
+ Put it in your `config.staticDir` without the mock filename convention.
196
196
 
197
197
  ---
198
198
 
@@ -239,13 +239,13 @@ permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file)
239
239
  For instance, if you have `api/foo/bar` and
240
240
  `api/foo`. For the latter you have two options:
241
241
 
242
- **Option A.** Place it outside the directory:
242
+ **Option A.**
243
243
  ```
244
- api/foo/
245
244
  api/foo.GET.200.json
245
+ api/foo/bar.GET.200.json
246
246
  ```
247
247
 
248
- **Option B.** Omit the filename:
248
+ **Option B.** Omit the filename.
249
249
  ```text
250
250
  api/foo/.GET.200.json
251
251
  ```
@@ -270,12 +270,12 @@ Defaults to `/(\.DS_Store|~)$/`
270
270
 
271
271
  ### `delay?: number` 🕓
272
272
  The clock icon next to the mock selector is a checkbox for delaying a particular
273
- response. The delay is globally configurable via `Config.delay = 1200` (milliseconds).
273
+ response. The delay is globally configurable via `config.delay = 1200` (milliseconds).
274
274
 
275
275
 
276
276
  ### `proxyFallback?: string`
277
277
  Lets you specify a target server for serving routes you don’t have mocks for.
278
- For example, `Config.proxyFallback = 'http://example.com:8080'`
278
+ For example, `config.proxyFallback = 'http://example.com'`
279
279
 
280
280
 
281
281
  ### `staticDir?: string`
@@ -283,8 +283,8 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
283
283
  - Use Case 2: For a standalone demo server. For example,
284
284
  build your frontend bundle, and serve it from Mockaton.
285
285
 
286
- Files under `Config.staticDir` don’t use the filename convention.
287
- They take precedence over the `GET` mocks in `Config.mocksDir`.
286
+ Files under `config.staticDir` don’t use the filename convention.
287
+ They take precedence over the `GET` mocks in `config.mocksDir`.
288
288
  For example, if you have two files for `GET /foo/bar.jpg`
289
289
  ```
290
290
  my-static-dir/foo/bar.jpg
@@ -293,18 +293,11 @@ my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
293
293
 
294
294
 
295
295
  ### `cookies?: { [label: string]: string }`
296
- The selected cookie is sent in every response in the `Set-Cookie` header.
297
- The key is just a label used for selecting a particular cookie in the
298
- dashboard. In the dashboard, only one cookie can be selected. If you need more
299
- cookies you can inject additional cookies globally in `Config.extraHeaders`.
300
-
301
- By the way, there’s a `jwtCookie` helper, which has a hardcoded header and
302
- signature. In other words, it’s useful if you only care about its payload.
303
296
 
304
297
  ```js
305
298
  import { jwtCookie } from 'mockaton'
306
299
 
307
- Config.cookies = {
300
+ config.cookies = {
308
301
  'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
309
302
  'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
310
303
  'My JWT': jwtCookie('my-cookie', {
@@ -313,13 +306,21 @@ Config.cookies = {
313
306
  })
314
307
  }
315
308
  ```
309
+ The selected cookie is sent in every response in a `Set-Cookie` header.
310
+
311
+ By the way, the `jwtCookie` helper has a hardcoded header and signature.
312
+ In other words, it’s useful only if you care about the payload.
313
+
314
+ If you need to send more than one cookie,
315
+ inject them globally in `config.extraHeaders`.
316
+
316
317
 
317
318
 
318
319
  ### `extraHeaders?: string[]`
319
320
  Note it’s a unidimensional array. The header name goes at even indices.
320
321
 
321
322
  ```js
322
- Config.extraHeaders = [
323
+ config.extraHeaders = [
323
324
  'Server', 'Mockaton',
324
325
  'Set-Cookie', 'foo=FOO;Path=/;SameSite=strict',
325
326
  'Set-Cookie', 'bar=BAR;Path=/;SameSite=strict'
@@ -329,7 +330,7 @@ Config.extraHeaders = [
329
330
 
330
331
  ### `extraMimes?: { [fileExt: string]: string }`
331
332
  ```js
332
- Config.extraMimes = {
333
+ config.extraMimes = {
333
334
  jpe: 'application/jpeg'
334
335
  }
335
336
  ```
@@ -361,7 +362,7 @@ import { parse } from 'yaml'
361
362
  import { readFileSync } from 'node:js'
362
363
  import { jsToJsonPlugin } from 'mockaton'
363
364
 
364
- Config.plugins = [
365
+ config.plugins = [
365
366
  [/\.(js|ts)$/, jsToJsonPlugin], // Default
366
367
  [/\.yml$/, yamlToJsonPlugin],
367
368
  [/foo\.GET\.200\.txt$/, capitalizePlugin], // e.g. GET /api/foo would be capitalized
@@ -385,14 +386,14 @@ function capitalizePlugin(filePath) {
385
386
 
386
387
 
387
388
  ### `corsAllowed?: boolean`
388
- Defaults to `corsAllowed = false`. When `Config.corsAllowed === true`, these are the default options:
389
+ Defaults to `corsAllowed = false`. When `config.corsAllowed === true`, these are the default options:
389
390
  ```js
390
- Config.corsOrigins = ['*']
391
- Config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
392
- Config.corsHeaders = ['content-type']
393
- Config.corsCredentials = true
394
- Config.corsMaxAge = 0 // seconds to cache the preflight req
395
- Config.corsExposedHeaders = [] // headers you need to access in client-side JS
391
+ config.corsOrigins = ['*']
392
+ config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
393
+ config.corsHeaders = ['content-type']
394
+ config.corsCredentials = true
395
+ config.corsMaxAge = 0 // seconds to cache the preflight req
396
+ config.corsExposedHeaders = [] // headers you need to access in client-side JS
396
397
  ```
397
398
 
398
399
 
@@ -401,12 +402,12 @@ This defaults to trying to open the dashboard in your default browser in macOS a
401
402
  Windows. For a more cross-platform utility, you could `npm install open` and pass it.
402
403
  ```js
403
404
  import open from 'open'
404
- Config.onReady = open
405
+ config.onReady = open
405
406
  ```
406
407
 
407
408
  If you don’t want to open a browser, pass a noop:
408
409
  ```js
409
- Config.onReady = () => {}
410
+ config.onReady = () => {}
410
411
  ```
411
412
 
412
413
 
@@ -440,7 +441,7 @@ await mockaton.setRouteIsDelayed('GET', '/api/foo', true)
440
441
  ```
441
442
 
442
443
  ### Select a cookie
443
- In `Config.cookies`, each key is the label used for selecting it.
444
+ In `config.cookies`, each key is the label used for selecting it.
444
445
  ```js
445
446
  await mockaton.selectCookie('My Normal User')
446
447
  ```
@@ -454,7 +455,7 @@ Pass an empty string to disable it.
454
455
  ### Reset
455
456
  Re-initialize the collection. So if you added or removed mocks they will
456
457
  be considered. The selected mocks, cookies, and delays go back to default,
457
- but `Config.proxyFallback` and `Config.corsAllowed` are not affected.
458
+ but `config.proxyFallback` and `config.corsAllowed` are not affected.
458
459
  ```js
459
460
  await mockaton.reset()
460
461
  ```
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": "8.2.17",
5
+ "version": "8.2.19",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",