mock-blocklist.ts 666 B

1234567891011121314151617181920212223242526272829303132333435
  1. import * as express from 'express'
  2. import { Server } from 'http'
  3. type BlocklistResponse = {
  4. data: {
  5. value: string
  6. action?: 'add' | 'remove'
  7. updatedAt?: string
  8. }[]
  9. }
  10. export class MockBlocklist {
  11. private body: BlocklistResponse
  12. private server: Server
  13. initialize () {
  14. return new Promise(res => {
  15. const app = express()
  16. app.get('/blocklist', (req: express.Request, res: express.Response) => {
  17. return res.json(this.body)
  18. })
  19. this.server = app.listen(42100, () => res())
  20. })
  21. }
  22. replace (body: BlocklistResponse) {
  23. this.body = body
  24. }
  25. terminate () {
  26. if (this.server) this.server.close()
  27. }
  28. }