mockaton 8.2.6 → 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.
Files changed (2) hide show
  1. package/README.md +43 -29
  2. 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. The
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,48 +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
- Think of this as an HTTP handler. You can read or write to a
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
127
132
  database, or pull data from a backend.
128
133
 
129
- Don’t call `response.end()`, just return a `string | Buffer | Uint8Array`.
134
+ <details>
135
+ <summary><b>See More Examples</b></summary>
130
136
 
137
+ For example, imagine you have an initial list of
138
+ colors, and you want to concatenate newly added colors.
139
+
140
+ `api/colors.POST.201.js`
131
141
  ```js
132
- export default function requestCounter(request, response) {
133
- globalThis.myDatabase ??= { count: 0 }
134
- globalThis.myDatabase.count++
135
- return JSON.stringify({
136
- count: globalThis.myDatabase.count
137
- })
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' })
138
154
  }
139
155
  ```
140
156
 
141
- If you need to serve a static `.js` file, put it in your
142
- `Config.staticDir` without the mock filename convention.
143
-
157
+ `api/colors.GET.200.js`
144
158
  ```js
145
- // This example will echo the request body concatenated with another fixture.
146
- // api/color.POST.201.js
147
-
148
- import colors from './colors.json' with { type: 'json' }
149
- import { parseJSON } from 'mockaton' // body-parser alike
159
+ import colorsFixture from './colors.json' with { type: 'json' }
150
160
 
151
- export default async function concatColor(request, response) {
152
- const newColor = await parseJSON(request)
153
- return JSON.stringify(colors.concat(newColor))
161
+ export default function listColors() {
162
+ return JSON.stringify([
163
+ ...colorsFixture,
164
+ ...(globalThis.newColorsDatabase || [])
165
+ ])
154
166
  }
155
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.
156
174
 
157
175
  ---
158
176
 
@@ -267,7 +285,6 @@ signature. In other words, it’s useful if you only care about its payload.
267
285
  ```js
268
286
  import { jwtCookie } from 'mockaton'
269
287
 
270
-
271
288
  Config.cookies = {
272
289
  'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
273
290
  'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
@@ -321,7 +338,6 @@ import { parse } from 'yaml'
321
338
  import { readFileSync } from 'node:js'
322
339
  import { jsToJsonPlugin } from 'mockaton'
323
340
 
324
-
325
341
  Config.plugins = [
326
342
  [/\.(js|ts)$/, jsToJsonPlugin], // Default
327
343
  [/\.yml$/, yamlToJsonPlugin],
@@ -370,7 +386,6 @@ For a more cross-platform utility, you could `npm install open` and pass it.
370
386
  ```js
371
387
  import open from 'open'
372
388
 
373
-
374
389
  Config.onReady = open
375
390
  ```
376
391
 
@@ -382,7 +397,6 @@ All of its methods return their `fetch` response promise.
382
397
  ```js
383
398
  import { Commander } from 'mockaton'
384
399
 
385
-
386
400
  const myMockatonAddr = 'http://localhost:2345'
387
401
  const mockaton = new Commander(myMockatonAddr)
388
402
  ```
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.6",
5
+ "version": "8.2.8",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",