MockWebApi.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  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 MockWebApi
  14. const express = require('express')
  15. const bodyParser = require('body-parser')
  16. const app = express()
  17. const MAX_REQUEST_SIZE = 2 * (2 * 1024 * 1024 + 64 * 1024)
  18. module.exports = MockWebApi = {
  19. docs: {},
  20. clearDocs() {
  21. return (this.docs = {})
  22. },
  23. insertDoc(project_id, doc_id, doc) {
  24. if (doc.version == null) {
  25. doc.version = 0
  26. }
  27. if (doc.lines == null) {
  28. doc.lines = []
  29. }
  30. doc.pathname = '/a/b/c.tex'
  31. return (this.docs[`${project_id}:${doc_id}`] = doc)
  32. },
  33. setDocument(
  34. project_id,
  35. doc_id,
  36. lines,
  37. version,
  38. ranges,
  39. lastUpdatedAt,
  40. lastUpdatedBy,
  41. callback
  42. ) {
  43. if (callback == null) {
  44. callback = function () {}
  45. }
  46. const doc =
  47. this.docs[`${project_id}:${doc_id}`] ||
  48. (this.docs[`${project_id}:${doc_id}`] = {})
  49. doc.lines = lines
  50. doc.version = version
  51. doc.ranges = ranges
  52. doc.pathname = '/a/b/c.tex'
  53. doc.lastUpdatedAt = lastUpdatedAt
  54. doc.lastUpdatedBy = lastUpdatedBy
  55. return callback(null)
  56. },
  57. getDocument(project_id, doc_id, callback) {
  58. if (callback == null) {
  59. callback = function () {}
  60. }
  61. return callback(null, this.docs[`${project_id}:${doc_id}`])
  62. },
  63. run() {
  64. app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
  65. return this.getDocument(
  66. req.params.project_id,
  67. req.params.doc_id,
  68. (error, doc) => {
  69. if (error != null) {
  70. return res.sendStatus(500)
  71. } else if (doc != null) {
  72. return res.send(JSON.stringify(doc))
  73. } else {
  74. return res.sendStatus(404)
  75. }
  76. }
  77. )
  78. })
  79. app.post(
  80. '/project/:project_id/doc/:doc_id',
  81. bodyParser.json({ limit: MAX_REQUEST_SIZE }),
  82. (req, res, next) => {
  83. return MockWebApi.setDocument(
  84. req.params.project_id,
  85. req.params.doc_id,
  86. req.body.lines,
  87. req.body.version,
  88. req.body.ranges,
  89. req.body.lastUpdatedAt,
  90. req.body.lastUpdatedBy,
  91. error => {
  92. if (error != null) {
  93. return res.sendStatus(500)
  94. } else {
  95. return res.json({ rev: '123' })
  96. }
  97. }
  98. )
  99. }
  100. )
  101. return app
  102. .listen(3000, error => {
  103. if (error != null) {
  104. throw error
  105. }
  106. })
  107. .on('error', error => {
  108. console.error('error starting MockWebApi:', error.message)
  109. return process.exit(1)
  110. })
  111. },
  112. }
  113. MockWebApi.run()