MockFileStoreApi.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  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 MockFileStoreApi
  14. const express = require('express')
  15. const bodyParser = require('body-parser')
  16. const app = express()
  17. module.exports = MockFileStoreApi = {
  18. files: {},
  19. run() {
  20. app.post('/project/:project_id/file/:file_id', (req, res, next) => {
  21. const chunks = []
  22. req.on('data', chunk => chunks.push(chunk))
  23. return req.on('end', () => {
  24. const content = Buffer.concat(chunks).toString()
  25. const { project_id, file_id } = req.params
  26. if (this.files[project_id] == null) {
  27. this.files[project_id] = {}
  28. }
  29. this.files[project_id][file_id] = { content }
  30. return res.sendStatus(200)
  31. })
  32. })
  33. app.get('/project/:project_id/file/:file_id', (req, res, next) => {
  34. const { project_id, file_id } = req.params
  35. const { content } = this.files[project_id][file_id]
  36. return res.send(content)
  37. })
  38. // handle file copying
  39. app.put(
  40. '/project/:project_id/file/:file_id',
  41. bodyParser.json(),
  42. (req, res, next) => {
  43. const { project_id, file_id } = req.params
  44. const { source } = req.body
  45. const { content } =
  46. this.files[source.project_id] != null
  47. ? this.files[source.project_id][source.file_id]
  48. : undefined
  49. if (content == null) {
  50. return res.sendStatus(500)
  51. } else {
  52. if (this.files[project_id] == null) {
  53. this.files[project_id] = {}
  54. }
  55. this.files[project_id][file_id] = { content }
  56. return res.sendStatus(200)
  57. }
  58. }
  59. )
  60. return app
  61. .listen(3009, error => {
  62. if (error != null) {
  63. throw error
  64. }
  65. })
  66. .on('error', error => {
  67. console.error('error starting MockFileStoreApi:', error.message)
  68. return process.exit(1)
  69. })
  70. }
  71. }
  72. MockFileStoreApi.run()