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.

41 lines
1.1 KiB

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. name: 'GET user',
  9. request: { method: 'GET' },
  10. response: function (ctx, id) {
  11. ctx.body = users.find(user => user.id === Number(id))
  12. }
  13. },
  14. /* for PUT requests, update the record */
  15. {
  16. name: 'PUT user',
  17. request: { method: 'PUT' },
  18. response: function (ctx, id) {
  19. const updatedUser = ctx.request.body
  20. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  21. users.splice(existingUserIndex, 1, updatedUser)
  22. ctx.status = 200
  23. }
  24. },
  25. /* DELETE request: remove the record */
  26. {
  27. name: 'DELETE user',
  28. request: { method: 'DELETE' },
  29. response: function (ctx, id) {
  30. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  31. users.splice(existingUserIndex, 1)
  32. ctx.status = 200
  33. }
  34. }
  35. ]
  36. module.exports = mockResponses