MockFilestoreApi.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const AbstractMockApi = require('./AbstractMockApi')
  2. class MockFilestoreApi extends AbstractMockApi {
  3. reset() {
  4. this.files = {}
  5. }
  6. applyRoutes() {
  7. this.app.post('/project/:projectId/file/:fileId', (req, res) => {
  8. const chunks = []
  9. req.on('data', chunk => chunks.push(chunk))
  10. req.on('end', () => {
  11. const content = Buffer.concat(chunks).toString()
  12. const { projectId, fileId } = req.params
  13. if (!this.files[projectId]) {
  14. this.files[projectId] = {}
  15. }
  16. this.files[projectId][fileId] = { content }
  17. res.sendStatus(200)
  18. })
  19. })
  20. this.app.get('/project/:projectId/file/:fileId', (req, res) => {
  21. const { projectId, fileId } = req.params
  22. const { content } = this.files[projectId][fileId]
  23. res.send(content)
  24. })
  25. // handle file copying
  26. this.app.put('/project/:projectId/file/:fileId', (req, res) => {
  27. const { projectId, fileId } = req.params
  28. const { source } = req.body
  29. const { content } =
  30. this.files[source.project_id] &&
  31. this.files[source.project_id][source.file_id]
  32. if (!content) {
  33. res.sendStatus(500)
  34. } else {
  35. if (!this.files[projectId]) {
  36. this.files[projectId] = {}
  37. }
  38. this.files[projectId][fileId] = { content }
  39. res.sendStatus(200)
  40. }
  41. })
  42. this.app.delete('/project/:projectId', (req, res) => {
  43. const { projectId } = req.params
  44. delete this.files[projectId]
  45. res.sendStatus(204)
  46. })
  47. }
  48. getFile(projectId, fileId) {
  49. return (
  50. this.files[projectId] &&
  51. this.files[projectId][fileId] &&
  52. this.files[projectId][fileId].content
  53. )
  54. }
  55. }
  56. module.exports = MockFilestoreApi
  57. // type hint for the inherited `instance` method
  58. /**
  59. * @function instance
  60. * @memberOf MockFilestoreApi
  61. * @static
  62. * @returns {MockFilestoreApi}
  63. */