Debian Codegraph @

lib/mock/pending-interceptors-formatter.js

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