pending-interceptors-formatter.js 1.2 KB

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