Skip to content

Commit

Permalink
feat: json helper now also accepts callback
Browse files Browse the repository at this point in the history
Please check Readme doc for further information.
  • Loading branch information
muratcorlu committed Jun 4, 2021
1 parent 9b77acc commit 3b07b0f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ Connect-Api-Mocker also presents a bunch of helper functions to speed up writing
- `created()`: Sets status code as 201
- `success()`: Sets status code as 200
- `delay(duration)`: Delays the request by given duration(in ms).
- `json(data)`: Send given JSON object as response.
- `json(data|callback(req,res))`: Send given JSON object as response.
- `file(filePath)`: Responds with the content of file in given path(full path)
- `type(contentType)`: Sets content-type header.
- `end(body)`: Ends request and optionally sends the string output
Expand All @@ -388,6 +388,16 @@ const { delay, created, json } = require('connect-api-mocker/helpers');
module.exports = [delay(500), created(), json({success: true})];
```

`json` middleware also accepts a callback that has request and response objects as parameters:

``js
const { json } = require('connect-api-mocker/helpers');

module.exports = [json(req => ({
id: req.params.userId, success: true
}))];
```
Another example to return image as response:
```js
Expand Down Expand Up @@ -527,4 +537,4 @@ apiMocker('/api', {
[webpack-dev-server]: https://github.com/webpack/webpack-dev-server
[cra]: https://github.com/facebook/create-react-app
[customize-cra]: https://github.com/arackaf/customize-cra
[react-app-rewired]: https://github.com/timarney/react-app-rewired
[react-app-rewired]: https://github.com/timarney/react-app-rewired
6 changes: 5 additions & 1 deletion helpers/json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = data => (req, res) => {
let responseData = data;
if (typeof data === 'function') {
responseData = data(req, res);
}
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify(data));
res.end(JSON.stringify(responseData));
};
10 changes: 10 additions & 0 deletions test/mock-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe('Helpers', () => {
}, done);
});

it('json helper with callback', (done) => {
request(app)
.put('/api/users/1')
.expect('Content-Type', /json/)
.expect({
success: true,
id: '1'
}, done);
});

it('notFound', (done) => {
request(app)
.get('/api/users')
Expand Down
8 changes: 8 additions & 0 deletions test/mocks/helpers/users/__userId__/PUT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { json } = require('../../../../../helpers');

module.exports = [
json(req => ({
success: true,
id: req.params.userId
}))
];

0 comments on commit 3b07b0f

Please sign in to comment.