MockDocUpdaterApi.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-undef,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let MockDocUpdaterApi
  15. const express = require('express')
  16. const bodyParser = require('body-parser')
  17. const app = express()
  18. app.use(bodyParser.json())
  19. module.exports = MockDocUpdaterApi = {
  20. docs: {},
  21. getDoc(project_id, doc_id, callback) {
  22. if (callback == null) {
  23. callback = function (error) {}
  24. }
  25. return callback(null, this.docs[doc_id])
  26. },
  27. setDoc(project_id, doc_id, lines, user_id, undoing, callback) {
  28. if (callback == null) {
  29. callback = function (error) {}
  30. }
  31. if (!this.docs[doc_id]) {
  32. this.docs[doc_id] = {}
  33. }
  34. this.docs[doc_id].lines = lines
  35. return callback()
  36. },
  37. run() {
  38. app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
  39. return this.getDoc(
  40. req.params.project_id,
  41. req.params.doc_id,
  42. (error, doc) => {
  43. if (error != null) {
  44. res.sendStatus(500)
  45. }
  46. if (doc == null) {
  47. return res.sendStatus(404)
  48. } else {
  49. return res.send(JSON.stringify(doc))
  50. }
  51. }
  52. )
  53. })
  54. app.post('/project/:project_id/doc/:doc_id', (req, res, next) => {
  55. return this.setDoc(
  56. req.params.project_id,
  57. req.params.doc_id,
  58. req.body.lines,
  59. req.body.user_id,
  60. req.body.undoing,
  61. (errr, doc) => {
  62. if (typeof error !== 'undefined' && error !== null) {
  63. return res.sendStatus(500)
  64. } else {
  65. return res.sendStatus(204)
  66. }
  67. }
  68. )
  69. })
  70. return app
  71. .listen(3003, error => {
  72. if (error != null) {
  73. throw error
  74. }
  75. })
  76. .on('error', error => {
  77. console.error('error starting MockDocUpdaterApi:', error.message)
  78. return process.exit(1)
  79. })
  80. },
  81. }
  82. MockDocUpdaterApi.run()