pending-interceptors-formatter.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. const { Transform } = require('stream')
  3. const { Console } = require('console')
  4. /**
  5. * Gets the output of `console.table(…)` as a string.
  6. */
  7. module.exports = class PendingInterceptorsFormatter {
  8. constructor ({ disableColors } = {}) {
  9. this.transform = new Transform({
  10. transform (chunk, _enc, cb) {
  11. cb(null, chunk)
  12. }
  13. })
  14. this.logger = new Console({
  15. stdout: this.transform,
  16. inspectOptions: {
  17. colors: !disableColors && !process.env.CI
  18. }
  19. })
  20. }
  21. format (pendingInterceptors) {
  22. const withPrettyHeaders = pendingInterceptors.map(
  23. ({ method, path, data: { statusCode }, persist, times, timesInvoked, origin }) => ({
  24. Method: method,
  25. Origin: origin,
  26. Path: path,
  27. 'Status code': statusCode,
  28. Persistent: persist ? '✅' : '❌',
  29. Invocations: timesInvoked,
  30. Remaining: persist ? Infinity : times - timesInvoked
  31. }))
  32. this.logger.table(withPrettyHeaders)
  33. return this.transform.read().toString()
  34. }
  35. }