@@ -367,7 +367,7 @@ Connect-Api-Mocker also presents a bunch of helper functions to speed up writing
367
367
- ` created() ` : Sets status code as 201
368
368
- ` success() ` : Sets status code as 200
369
369
- ` delay(duration) ` : Delays the request by given duration(in ms).
370
- - ` json(data) ` : Send given JSON object as response.
370
+ - ` json(data|callback(req,res) ) ` : Send given JSON object as response.
371
371
- ` file(filePath) ` : Responds with the content of file in given path(full path)
372
372
- ` type(contentType) ` : Sets content-type header.
373
373
- ` end(body) ` : Ends request and optionally sends the string output
@@ -388,6 +388,16 @@ const { delay, created, json } = require('connect-api-mocker/helpers');
388
388
module .exports = [delay (500 ), created (), json ({success: true })];
389
389
```
390
390
391
+ ` json ` middleware also accepts a callback that has request and response objects as parameters:
392
+
393
+ ``js
394
+ const { json } = require('connect-api-mocker/helpers');
395
+
396
+ module.exports = [ json(req => ({
397
+ id: req.params.userId, success: true
398
+ }))] ;
399
+ ```
400
+
391
401
Another example to return image as response:
392
402
393
403
```js
@@ -527,4 +537,4 @@ apiMocker('/api', {
527
537
[webpack-dev-server]: https://github.com/webpack/webpack-dev-server
528
538
[cra]: https://github.com/facebook/create-react-app
529
539
[customize-cra]: https://github.com/arackaf/customize-cra
530
- [react-app-rewired]: https://github.com/timarney/react-app-rewired
540
+ [react-app-rewired]: https://github.com/timarney/react-app-rewired
0 commit comments