| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /* eslint-disable
- camelcase,
- max-len,
- no-return-assign,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- let MockProjectHistoryApi
- const _ = require('lodash')
- const express = require('express')
- const bodyParser = require('body-parser')
- const app = express()
- const { ObjectId } = require('mongojs')
- module.exports = MockProjectHistoryApi = {
- docs: {},
- oldFiles: {},
- projectVersions: {},
- labels: {},
- projectSnapshots: {},
- addOldFile(project_id, version, pathname, content) {
- return (this.oldFiles[`${project_id}:${version}:${pathname}`] = content)
- },
- addProjectSnapshot(project_id, version, snapshot) {
- return (this.projectSnapshots[`${project_id}:${version}`] = snapshot)
- },
- setProjectVersion(project_id, version) {
- return (this.projectVersions[project_id] = { version })
- },
- setProjectVersionInfo(project_id, versionInfo) {
- return (this.projectVersions[project_id] = versionInfo)
- },
- addLabel(project_id, label) {
- if (label.id == null) {
- label.id = new ObjectId().toString()
- }
- if (this.labels[project_id] == null) {
- this.labels[project_id] = {}
- }
- return (this.labels[project_id][label.id] = label)
- },
- deleteLabel(project_id, label_id) {
- return delete this.labels[project_id][label_id]
- },
- getLabels(project_id) {
- if (this.labels[project_id] == null) {
- return null
- }
- return _.values(this.labels[project_id])
- },
- reset() {
- this.oldFiles = {}
- this.projectHistoryId = 1
- this.projectVersions = {}
- return (this.labels = {})
- },
- run() {
- this.reset()
- app.post('/project', (req, res, next) => {
- return res.json({ project: { id: this.projectHistoryId++ } })
- })
- app.get(
- '/project/:project_id/version/:version/:pathname',
- (req, res, next) => {
- const { project_id, version, pathname } = req.params
- const key = `${project_id}:${version}:${pathname}`
- if (this.oldFiles[key] != null) {
- return res.send(this.oldFiles[key])
- } else {
- return res.send(404)
- }
- }
- )
- app.get('/project/:project_id/version/:version', (req, res, next) => {
- const { project_id, version } = req.params
- const key = `${project_id}:${version}`
- if (this.projectSnapshots[key] != null) {
- return res.json(this.projectSnapshots[key])
- } else {
- return res.sendStatus(404)
- }
- })
- app.get('/project/:project_id/version', (req, res, next) => {
- const { project_id } = req.params
- if (this.projectVersions[project_id] != null) {
- return res.json(this.projectVersions[project_id])
- } else {
- return res.send(404)
- }
- })
- app.get('/project/:project_id/labels', (req, res, next) => {
- const { project_id } = req.params
- const labels = this.getLabels(project_id)
- if (labels != null) {
- return res.json(labels)
- } else {
- return res.send(404)
- }
- })
- app.post(
- '/project/:project_id/user/:user_id/labels',
- bodyParser.json(),
- (req, res, next) => {
- const { project_id } = req.params
- const { comment, version } = req.body
- const label_id = new ObjectId().toString()
- this.addLabel(project_id, { id: label_id, comment, version })
- return res.json({ label_id, comment, version })
- }
- )
- app.delete(
- '/project/:project_id/user/:user_id/labels/:label_id',
- (req, res, next) => {
- const { project_id, label_id } = req.params
- const label =
- this.labels[project_id] != null
- ? this.labels[project_id][label_id]
- : undefined
- if (label != null) {
- this.deleteLabel(project_id, label_id)
- return res.send(204)
- } else {
- return res.send(404)
- }
- }
- )
- app.post('/project/:project_id/flush', (req, res, next) => {
- return res.sendStatus(200)
- })
- return app
- .listen(3054, error => {
- if (error != null) {
- throw error
- }
- })
- .on('error', error => {
- console.error('error starting MockProjectHistoryApi:', error.message)
- return process.exit(1)
- })
- }
- }
- MockProjectHistoryApi.run()
|