MockGitBridgeApi.mjs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import AbstractMockApi from './AbstractMockApi.mjs'
  2. class MockGitBridgeApi extends AbstractMockApi {
  3. reset() {
  4. this.projects = {}
  5. this.postbacks = {}
  6. }
  7. applyRoutes() {
  8. this.app.delete('/api/projects/:projectId', (req, res) => {
  9. this.deleteProject(req, res)
  10. })
  11. this.app.post('/postback/:id', (req, res) => {
  12. this.postback(req, res)
  13. })
  14. }
  15. deleteProject(req, res) {
  16. const projectId = req.params.projectId
  17. delete this.projects[projectId]
  18. res.sendStatus(204)
  19. }
  20. // Git bridge accepts a postback to indicate when a operation is complete.
  21. // Each postback is identified by a unique ID.
  22. // Allow registering a handler which resolves when a postback is received.
  23. registerPostback(id) {
  24. return new Promise((resolve, reject) => {
  25. this.postbacks[id] = { resolve, reject }
  26. })
  27. }
  28. postback(req, res) {
  29. const { id } = req.params
  30. const postbackData = req.body
  31. if (this.postbacks[id]) {
  32. this.postbacks[id].resolve(postbackData)
  33. }
  34. res.sendStatus(204)
  35. }
  36. }
  37. export default MockGitBridgeApi