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.

37 lines
1.3 KiB

8 years ago
  1. const users = require('./users.json')
  2. /* responses for /users */
  3. const userResponses = [
  4. {
  5. route: '/users',
  6. responses: [
  7. /* Respond with 400 Bad Request for PUT and DELETE - inappropriate on a collection */
  8. { request: { method: 'PUT' }, response: { status: 400 } },
  9. { request: { method: 'DELETE' }, response: { status: 400 } },
  10. {
  11. /* for GET requests return a subset of data, optionally filtered on 'minAge' and 'nationality' */
  12. request: { method: 'GET' },
  13. response: function (ctx) {
  14. ctx.body = users.filter(user => {
  15. const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)
  16. const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
  17. return meetsMinAge && requiredNationality
  18. })
  19. }
  20. },
  21. {
  22. /* for POST requests, create a new user and return the path to the new resource */
  23. request: { method: 'POST' },
  24. response: function (ctx) {
  25. const newUser = ctx.request.body
  26. users.push(newUser)
  27. newUser.id = users.length
  28. ctx.status = 201
  29. ctx.response.set('Location', `/users/${newUser.id}`)
  30. }
  31. }
  32. ]
  33. }
  34. ]
  35. module.exports = userResponses