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

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