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