You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1014 B

9 years ago
  1. const users = require('./users.json')
  2. /* responses for /users/:id */
  3. const mockResponses = [
  4. /* don't support POST here */
  5. { request: { method: 'POST' }, response: { status: 400 } },
  6. /* for GET requests, return a particular user */
  7. {
  8. request: { method: 'GET' },
  9. response: function (ctx, id) {
  10. ctx.body = users.find(user => user.id === Number(id))
  11. }
  12. },
  13. /* for PUT requests, update the record */
  14. {
  15. request: { method: 'PUT' },
  16. response: function (ctx, id) {
  17. const updatedUser = ctx.request.body
  18. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  19. users.splice(existingUserIndex, 1, updatedUser)
  20. ctx.status = 200
  21. }
  22. },
  23. /* DELETE request: remove the record */
  24. {
  25. request: { method: 'DELETE' },
  26. response: function (ctx, id) {
  27. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  28. users.splice(existingUserIndex, 1)
  29. ctx.status = 200
  30. }
  31. }
  32. ]
  33. module.exports = mockResponses