MockDocUpdaterApi.js 2.2 KB

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