mockaton 8.2.7 → 8.2.8
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 +43 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,8 +63,8 @@ Experiment with the Dashboard:
|
|
|
63
63
|
- Toggle the 🕓 Clock button, which _Delays_ responses (e.g. for testing spinners)
|
|
64
64
|
- Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
|
|
65
65
|
|
|
66
|
-
Finally, edit a mock file. You don’t need to restart Mockaton for that.
|
|
67
|
-
_Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
66
|
+
Finally, edit a mock file in your IDE. You don’t need to restart Mockaton for that.
|
|
67
|
+
The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
68
68
|
|
|
69
69
|
|
|
70
70
|
## Use Cases
|
|
@@ -111,49 +111,66 @@ api/user(default).GET.200.json
|
|
|
111
111
|
---
|
|
112
112
|
|
|
113
113
|
## You can write JSON mocks in JavaScript or TypeScript
|
|
114
|
+
For example, `api/foo.GET.200.js`
|
|
115
|
+
|
|
114
116
|
|
|
115
117
|
**Option A:** An Object, Array, or String is sent as JSON.
|
|
116
118
|
|
|
117
|
-
`api/foo.GET.200.js`
|
|
118
119
|
```js
|
|
119
|
-
export default [
|
|
120
|
-
{ id: 0 }
|
|
121
|
-
]
|
|
120
|
+
export default [{ foo: 'bar' }]
|
|
122
121
|
```
|
|
123
122
|
|
|
124
123
|
**Option B:** Function
|
|
125
124
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
export default (request, response) => JSON.stringify({ foo: 'bar' })
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Think of these functions as HTTP handlers. You can read or write to a
|
|
132
|
+
database, or pull data from a backend.
|
|
129
133
|
|
|
130
|
-
|
|
134
|
+
<details>
|
|
135
|
+
<summary><b>See More Examples</b></summary>
|
|
131
136
|
|
|
132
|
-
|
|
133
|
-
|
|
137
|
+
For example, imagine you have an initial list of
|
|
138
|
+
colors, and you want to concatenate newly added colors.
|
|
134
139
|
|
|
140
|
+
`api/colors.POST.201.js`
|
|
135
141
|
```js
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
+
import { parseJSON } from 'mockaton' // body-parser alike
|
|
143
|
+
|
|
144
|
+
export default async function insertColor(request, response) {
|
|
145
|
+
const color = await parseJSON(request)
|
|
146
|
+
globalThis.newColorsDatabase ??= []
|
|
147
|
+
globalThis.newColorsDatabase.push(color)
|
|
148
|
+
|
|
149
|
+
// These two lines are not needed but you can change them
|
|
150
|
+
// response.statusCode = 201 // default derived from filename
|
|
151
|
+
// response.setHeader('Content-Type', 'application/json') // unconditional default
|
|
152
|
+
|
|
153
|
+
return JSON.stringify({ msg: 'CREATED' })
|
|
142
154
|
}
|
|
143
155
|
```
|
|
144
156
|
|
|
145
|
-
|
|
157
|
+
`api/colors.GET.200.js`
|
|
146
158
|
```js
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
import colors from './colors.json' with { type: 'json' }
|
|
150
|
-
import { parseJSON } from 'mockaton' // body-parser alike
|
|
159
|
+
import colorsFixture from './colors.json' with { type: 'json' }
|
|
151
160
|
|
|
152
|
-
export default
|
|
153
|
-
|
|
154
|
-
|
|
161
|
+
export default function listColors() {
|
|
162
|
+
return JSON.stringify([
|
|
163
|
+
...colorsFixture,
|
|
164
|
+
...(globalThis.newColorsDatabase || [])
|
|
165
|
+
])
|
|
155
166
|
}
|
|
156
167
|
```
|
|
168
|
+
</details>
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
If you are wondering, what if I need to serve a static `.js`?
|
|
173
|
+
Put it in your `Config.staticDir` without the mock filename convention.
|
|
157
174
|
|
|
158
175
|
---
|
|
159
176
|
|
|
@@ -268,7 +285,6 @@ signature. In other words, it’s useful if you only care about its payload.
|
|
|
268
285
|
```js
|
|
269
286
|
import { jwtCookie } from 'mockaton'
|
|
270
287
|
|
|
271
|
-
|
|
272
288
|
Config.cookies = {
|
|
273
289
|
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
274
290
|
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
@@ -322,7 +338,6 @@ import { parse } from 'yaml'
|
|
|
322
338
|
import { readFileSync } from 'node:js'
|
|
323
339
|
import { jsToJsonPlugin } from 'mockaton'
|
|
324
340
|
|
|
325
|
-
|
|
326
341
|
Config.plugins = [
|
|
327
342
|
[/\.(js|ts)$/, jsToJsonPlugin], // Default
|
|
328
343
|
[/\.yml$/, yamlToJsonPlugin],
|
|
@@ -371,7 +386,6 @@ For a more cross-platform utility, you could `npm install open` and pass it.
|
|
|
371
386
|
```js
|
|
372
387
|
import open from 'open'
|
|
373
388
|
|
|
374
|
-
|
|
375
389
|
Config.onReady = open
|
|
376
390
|
```
|
|
377
391
|
|
|
@@ -383,7 +397,6 @@ All of its methods return their `fetch` response promise.
|
|
|
383
397
|
```js
|
|
384
398
|
import { Commander } from 'mockaton'
|
|
385
399
|
|
|
386
|
-
|
|
387
400
|
const myMockatonAddr = 'http://localhost:2345'
|
|
388
401
|
const mockaton = new Commander(myMockatonAddr)
|
|
389
402
|
```
|
package/package.json
CHANGED