mockaton 7.8.0 → 8.0.0
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 +26 -28
- package/index.d.ts +3 -3
- package/package.json +1 -4
- package/src/Config.js +5 -2
- package/src/MockDispatcherPlugins.js +2 -7
package/README.md
CHANGED
|
@@ -274,21 +274,20 @@ Config.extraMimes = {
|
|
|
274
274
|
}
|
|
275
275
|
```
|
|
276
276
|
|
|
277
|
-
### `plugins?:
|
|
277
|
+
### `plugins?: [filenameTester: RegExp, plugin: Plugin][]
|
|
278
278
|
```ts
|
|
279
279
|
type Plugin = (
|
|
280
280
|
filePath: string,
|
|
281
281
|
request: IncomingMessage,
|
|
282
282
|
response: OutgoingMessage
|
|
283
|
-
) => {
|
|
283
|
+
) => Promise<{
|
|
284
284
|
mime: string,
|
|
285
285
|
body: string | Uint8Array
|
|
286
|
-
}
|
|
286
|
+
}>
|
|
287
287
|
```
|
|
288
|
-
Plugins are for processing mocks before sending them.
|
|
289
|
-
of a filename. In other words, it’s not limited to the file extension.
|
|
288
|
+
Plugins are for processing mocks before sending them.
|
|
290
289
|
|
|
291
|
-
|
|
290
|
+
Note: don’t call `response.end()`
|
|
292
291
|
|
|
293
292
|
|
|
294
293
|
#### Plugin Examples
|
|
@@ -296,30 +295,29 @@ Node’s `request` and `response` are included but don’t call `response.end()`
|
|
|
296
295
|
npm install yaml
|
|
297
296
|
```
|
|
298
297
|
```js
|
|
299
|
-
import { readFileSync as read } from 'node:js'
|
|
300
298
|
import { parse } from 'yaml'
|
|
299
|
+
import { readFileSync } from 'node:js'
|
|
301
300
|
import { jsToJsonPlugin } from 'mockaton'
|
|
302
301
|
|
|
303
302
|
|
|
304
|
-
Config.plugins =
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
'.ts': jsToJsonPlugin // yes, it’s reused
|
|
303
|
+
Config.plugins = [
|
|
304
|
+
[/\.(js|ts)$/, jsToJsonPlugin], // Default
|
|
305
|
+
[/\.yaml$/, yamlToJsonPlugin],
|
|
306
|
+
[/foo\.GET\.200\.txt$/, capitalizePlugin], // e.g. GET /api/foo would be capitalized
|
|
307
|
+
]
|
|
308
|
+
|
|
309
|
+
function yamlToJsonPlugin(filePath) {
|
|
310
|
+
return {
|
|
311
|
+
mime: 'application/json',
|
|
312
|
+
body: JSON.stringify(parse(readFileSync(filePath, 'utf8')))
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function capitalizePlugin(filePath) {
|
|
317
|
+
return {
|
|
318
|
+
mime: 'application/text',
|
|
319
|
+
body: readFileSync(filePath, 'utf8').toUpperCase()
|
|
320
|
+
}
|
|
323
321
|
}
|
|
324
322
|
```
|
|
325
323
|
|
|
@@ -338,8 +336,8 @@ Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
|
338
336
|
```
|
|
339
337
|
|
|
340
338
|
### `onReady?: (dashboardUrl: string) => void`
|
|
341
|
-
This
|
|
342
|
-
|
|
339
|
+
This defaults to trying to open the dashboard
|
|
340
|
+
in your default browser in macOS and Windows.
|
|
343
341
|
|
|
344
342
|
If you don’t want to open a browser, pass a noop, such as
|
|
345
343
|
```js
|
package/index.d.ts
CHANGED
|
@@ -4,10 +4,10 @@ type Plugin = (
|
|
|
4
4
|
filePath: string,
|
|
5
5
|
request: IncomingMessage,
|
|
6
6
|
response: OutgoingMessage
|
|
7
|
-
) => {
|
|
7
|
+
) => Promise<{
|
|
8
8
|
mime: string,
|
|
9
9
|
body: string | Uint8Array
|
|
10
|
-
}
|
|
10
|
+
}>
|
|
11
11
|
|
|
12
12
|
interface Config {
|
|
13
13
|
mocksDir: string
|
|
@@ -24,7 +24,7 @@ interface Config {
|
|
|
24
24
|
extraHeaders?: string[]
|
|
25
25
|
extraMimes?: { [fileExt: string]: string }
|
|
26
26
|
|
|
27
|
-
plugins?:
|
|
27
|
+
plugins?: [filenameTester: RegExp, plugin: Plugin][]
|
|
28
28
|
|
|
29
29
|
corsAllowed?: boolean,
|
|
30
30
|
corsOrigins: string[]
|
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": "
|
|
5
|
+
"version": "8.0.0",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
8
8
|
"license": "MIT",
|
|
@@ -11,8 +11,5 @@
|
|
|
11
11
|
"test": "node --test",
|
|
12
12
|
"demo": "node _usage_example.js",
|
|
13
13
|
"demo:ts": "node --import=tsx _usage_example.js"
|
|
14
|
-
},
|
|
15
|
-
"optionalDependencies": {
|
|
16
|
-
"tsx": "4.19.1"
|
|
17
14
|
}
|
|
18
15
|
}
|
package/src/Config.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isDirectory } from './utils/fs.js'
|
|
2
2
|
import { openInBrowser } from './utils/openInBrowser.js'
|
|
3
|
+
import { jsToJsonPlugin } from './MockDispatcherPlugins.js'
|
|
3
4
|
import { StandardMethods } from './utils/http-request.js'
|
|
4
5
|
import { validate, is, optional } from './utils/validate.js'
|
|
5
6
|
|
|
@@ -19,7 +20,9 @@ export const Config = Object.seal({
|
|
|
19
20
|
extraHeaders: [],
|
|
20
21
|
extraMimes: {},
|
|
21
22
|
|
|
22
|
-
plugins:
|
|
23
|
+
plugins: [
|
|
24
|
+
[/\.(js|ts)$/, jsToJsonPlugin]
|
|
25
|
+
],
|
|
23
26
|
|
|
24
27
|
corsAllowed: false,
|
|
25
28
|
corsOrigins: ['*'],
|
|
@@ -50,7 +53,7 @@ export function setup(options) {
|
|
|
50
53
|
extraHeaders: val => Array.isArray(val) && val.length % 2 === 0,
|
|
51
54
|
extraMimes: is(Object),
|
|
52
55
|
|
|
53
|
-
plugins:
|
|
56
|
+
plugins: Array.isArray,
|
|
54
57
|
|
|
55
58
|
corsAllowed: is(Boolean),
|
|
56
59
|
corsOrigins: validateCorsAllowedOrigins,
|
|
@@ -3,14 +3,9 @@ import { mimeFor } from './utils/mime.js'
|
|
|
3
3
|
import { Config } from './Config.js'
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
const plugins = {
|
|
7
|
-
'.js': jsToJsonPlugin,
|
|
8
|
-
'.ts': jsToJsonPlugin
|
|
9
|
-
}
|
|
10
|
-
|
|
11
6
|
export async function preprocessPlugins(filePath, req, response) {
|
|
12
|
-
for (const [
|
|
13
|
-
if (
|
|
7
|
+
for (const [regex, plugin] of Config.plugins) // TESTME capitalizePlugin
|
|
8
|
+
if (regex.test(filePath))
|
|
14
9
|
return await plugin(filePath, req, response)
|
|
15
10
|
return defaultPlugin(filePath)
|
|
16
11
|
}
|