mockaton 8.2.11 → 8.2.16
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 +41 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
<img src="src/mockaton-logo.svg" alt="Mockaton Logo" width="210" style="margin-top: 30px"/>
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
_Mockaton_ is an HTTP mock server developing and testing frontend apps.
|
|
8
|
+
|
|
9
|
+
With Mockaton, you don’t need to write code for wiring your mocks. Instead, it
|
|
10
|
+
scans a given directory for filenames following a convention similar to the
|
|
11
|
+
URL paths. For example, the following file will be served on `/api/user/1234`
|
|
9
12
|
```
|
|
10
13
|
my-mocks-dir/api/user/[user-id].GET.200.json
|
|
11
14
|
```
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
Also, you don’t need to mock everything. It can request from your backend
|
|
16
|
+
the routes you don’t have mocks for. See `Config.proxyFallback` below.
|
|
15
17
|
|
|
16
18
|
By the way, [this browser
|
|
17
|
-
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
18
|
-
create a TAR of your requests following that convention.
|
|
19
|
+
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
20
|
+
can create a TAR of your requests following that convention.
|
|
19
21
|
|
|
20
22
|
## Dashboard UI
|
|
21
23
|
|
|
24
|
+
In the dashboard, you can manually select which mock variant to serve for a particular
|
|
25
|
+
route. This is useful for testing different scenarios without changing code or the database state.
|
|
26
|
+
|
|
22
27
|
<picture>
|
|
23
28
|
<source media="(prefers-color-scheme: light)" srcset="./README-dashboard-light.png">
|
|
24
29
|
<source media="(prefers-color-scheme: dark)" srcset="./README-dashboard-dark.png">
|
|
@@ -37,8 +42,7 @@ Create a `my-mockaton.js` file
|
|
|
37
42
|
import { resolve } from 'node:path'
|
|
38
43
|
import { Mockaton } from 'mockaton'
|
|
39
44
|
|
|
40
|
-
|
|
41
|
-
// See the Config section below for more options
|
|
45
|
+
// See the Config section for more options
|
|
42
46
|
Mockaton({
|
|
43
47
|
mocksDir: resolve('my-mocks-dir'),
|
|
44
48
|
port: 2345
|
|
@@ -60,7 +64,7 @@ This demo uses the [sample-mocks/](./sample-mocks) directory of this repository.
|
|
|
60
64
|
|
|
61
65
|
Experiment with the Dashboard:
|
|
62
66
|
|
|
63
|
-
- Pick a mock variant from the
|
|
67
|
+
- Pick a mock variant from the _Mock dropdown_ (we’ll discuss them later)
|
|
64
68
|
- Toggle the 🕓 _Delay Responses_ button, (e.g. for testing spinners)
|
|
65
69
|
- Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
|
|
66
70
|
|
|
@@ -146,11 +150,11 @@ Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
|
146
150
|
export default (request, response) => JSON.stringify({ foo: 'bar' })
|
|
147
151
|
```
|
|
148
152
|
|
|
149
|
-
Think of these functions as HTTP handlers
|
|
150
|
-
|
|
153
|
+
Think of these functions as HTTP handlers, so you can
|
|
154
|
+
intercept requests. For example, for writing to a database.
|
|
151
155
|
|
|
152
156
|
<details>
|
|
153
|
-
<summary><b>See
|
|
157
|
+
<summary><b>See Intercepting Requests Examples</b></summary>
|
|
154
158
|
|
|
155
159
|
Imagine you have an initial list of colors, and
|
|
156
160
|
you want to concatenate newly added colors.
|
|
@@ -205,9 +209,10 @@ api/user.GET.200.json
|
|
|
205
209
|
|
|
206
210
|
|
|
207
211
|
### Dynamic Parameters
|
|
208
|
-
Anything within square brackets. For example
|
|
212
|
+
Anything within square brackets is always matched. For example, for this route
|
|
213
|
+
`/api/company/1234/user/5678`
|
|
209
214
|
<pre>
|
|
210
|
-
api/
|
|
215
|
+
api/company/<b>[id]</b>/user/<b>[uid]</b>.GET.200.json
|
|
211
216
|
</pre>
|
|
212
217
|
|
|
213
218
|
### Comments
|
|
@@ -220,18 +225,18 @@ api/foo.GET.200.json
|
|
|
220
225
|
</pre>
|
|
221
226
|
|
|
222
227
|
### Query String Params
|
|
228
|
+
The query string is ignored when routing to it. In other words, it’s only used for
|
|
229
|
+
documenting the URL contract.
|
|
223
230
|
<pre>
|
|
224
231
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
225
232
|
</pre>
|
|
226
233
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
[not permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
230
|
-
but since that’s part of the query string, it’s ignored anyway.
|
|
234
|
+
Speaking of which, in Windows, filenames containing "?" are [not
|
|
235
|
+
permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file), but since that’s part of the query string, it’s ignored anyway.
|
|
231
236
|
|
|
232
237
|
|
|
233
238
|
### Index-like route
|
|
234
|
-
For instance,
|
|
239
|
+
For instance, if you have `api/foo/bar` and
|
|
235
240
|
`api/foo`. For the latter you have two options:
|
|
236
241
|
|
|
237
242
|
**Option A.** Place it outside the directory:
|
|
@@ -274,18 +279,18 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
274
279
|
|
|
275
280
|
|
|
276
281
|
### `staticDir?: string`
|
|
282
|
+
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
283
|
+
- Use Case 2: For a standalone demo server. For example,
|
|
284
|
+
build your frontend bundle, and serve it from Mockaton.
|
|
285
|
+
|
|
277
286
|
Files under `Config.staticDir` don’t use the filename convention.
|
|
278
|
-
|
|
287
|
+
They take precedence over the `GET` mocks in `Config.mocksDir`.
|
|
279
288
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
280
289
|
```
|
|
281
290
|
my-static-dir/foo/bar.jpg
|
|
282
291
|
my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
283
292
|
```
|
|
284
293
|
|
|
285
|
-
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
286
|
-
- Use Case 2: For a standalone demo server. For example,
|
|
287
|
-
build your frontend bundle, and serve it from Mockaton.
|
|
288
|
-
|
|
289
294
|
|
|
290
295
|
### `cookies?: { [label: string]: string }`
|
|
291
296
|
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
@@ -346,7 +351,7 @@ Plugins are for processing mocks before sending them.
|
|
|
346
351
|
Note: don’t call `response.end()`
|
|
347
352
|
|
|
348
353
|
<details>
|
|
349
|
-
<summary><b> Plugin Examples </b></summary>
|
|
354
|
+
<summary><b> See Plugin Examples </b></summary>
|
|
350
355
|
|
|
351
356
|
```shell
|
|
352
357
|
npm install yaml
|
|
@@ -380,9 +385,7 @@ function capitalizePlugin(filePath) {
|
|
|
380
385
|
|
|
381
386
|
|
|
382
387
|
### `corsAllowed?: boolean`
|
|
383
|
-
Defaults to `corsAllowed = false`
|
|
384
|
-
|
|
385
|
-
When `Config.corsAllowed === true`, these are the default options:
|
|
388
|
+
Defaults to `corsAllowed = false`. When `Config.corsAllowed === true`, these are the default options:
|
|
386
389
|
```js
|
|
387
390
|
Config.corsOrigins = ['*']
|
|
388
391
|
Config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
|
|
@@ -394,20 +397,19 @@ Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
|
394
397
|
|
|
395
398
|
|
|
396
399
|
### `onReady?: (dashboardUrl: string) => void`
|
|
397
|
-
This defaults to trying to open the dashboard in your default browser in
|
|
398
|
-
|
|
399
|
-
|
|
400
|
+
This defaults to trying to open the dashboard in your default browser in macOS and
|
|
401
|
+
Windows. For a more cross-platform utility, you could `npm install open` and pass it.
|
|
400
402
|
```js
|
|
401
|
-
|
|
403
|
+
import open from 'open'
|
|
404
|
+
Config.onReady = open
|
|
402
405
|
```
|
|
403
406
|
|
|
404
|
-
|
|
407
|
+
If you don’t want to open a browser, pass a noop:
|
|
405
408
|
```js
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
Config.onReady = open
|
|
409
|
+
Config.onReady = () => {}
|
|
409
410
|
```
|
|
410
411
|
|
|
412
|
+
|
|
411
413
|
---
|
|
412
414
|
|
|
413
415
|
## Commander API
|
package/package.json
CHANGED