MockWebApi.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. let MockWebApi
  10. const express = require('express')
  11. const app = express()
  12. module.exports = MockWebApi = {
  13. users: {},
  14. projects: {},
  15. getUserInfo(userId, callback) {
  16. if (callback == null) {
  17. callback = function () {}
  18. }
  19. return callback(null, this.users[userId] || null)
  20. },
  21. getProjectDetails(projectId, callback) {
  22. if (callback == null) {
  23. callback = function () {}
  24. }
  25. return callback(null, this.projects[projectId])
  26. },
  27. run() {
  28. app.get('/user/:user_id/personal_info', (req, res, next) => {
  29. return this.getUserInfo(req.params.user_id, (error, user) => {
  30. if (error != null) {
  31. res.sendStatus(500)
  32. }
  33. if (user == null) {
  34. return res.sendStatus(404)
  35. } else {
  36. return res.send(JSON.stringify(user))
  37. }
  38. })
  39. })
  40. app.get('/project/:project_id/details', (req, res, next) => {
  41. return this.getProjectDetails(req.params.project_id, (error, project) => {
  42. if (error != null) {
  43. res.sendStatus(500)
  44. }
  45. if (project == null) {
  46. return res.sendStatus(404)
  47. } else {
  48. return res.send(JSON.stringify(project))
  49. }
  50. })
  51. })
  52. return app
  53. .listen(3000, error => {
  54. if (error != null) {
  55. throw error
  56. }
  57. })
  58. .on('error', error => {
  59. console.error('error starting MockWebApiServer:', error.message)
  60. return process.exit(1)
  61. })
  62. },
  63. }
  64. MockWebApi.run()