| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- const AbstractMockApi = require('./AbstractMockApi')
- class MockFilestoreApi extends AbstractMockApi {
- reset() {
- this.files = {}
- }
- applyRoutes() {
- this.app.post('/project/:projectId/file/:fileId', (req, res) => {
- const chunks = []
- req.on('data', chunk => chunks.push(chunk))
- req.on('end', () => {
- const content = Buffer.concat(chunks).toString()
- const { projectId, fileId } = req.params
- if (!this.files[projectId]) {
- this.files[projectId] = {}
- }
- this.files[projectId][fileId] = { content }
- res.sendStatus(200)
- })
- })
- this.app.get('/project/:projectId/file/:fileId', (req, res) => {
- const { projectId, fileId } = req.params
- const { content } = this.files[projectId][fileId]
- res.send(content)
- })
- // handle file copying
- this.app.put('/project/:projectId/file/:fileId', (req, res) => {
- const { projectId, fileId } = req.params
- const { source } = req.body
- const { content } =
- this.files[source.project_id] &&
- this.files[source.project_id][source.file_id]
- if (!content) {
- res.sendStatus(500)
- } else {
- if (!this.files[projectId]) {
- this.files[projectId] = {}
- }
- this.files[projectId][fileId] = { content }
- res.sendStatus(200)
- }
- })
- this.app.delete('/project/:projectId', (req, res) => {
- const { projectId } = req.params
- delete this.files[projectId]
- res.sendStatus(204)
- })
- }
- getFile(projectId, fileId) {
- return (
- this.files[projectId] &&
- this.files[projectId][fileId] &&
- this.files[projectId][fileId].content
- )
- }
- }
- module.exports = MockFilestoreApi
- // type hint for the inherited `instance` method
- /**
- * @function instance
- * @memberOf MockFilestoreApi
- * @static
- * @returns {MockFilestoreApi}
- */
|