@api-client/core 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/build/browser.d.ts +2 -0
  2. package/build/browser.js +8 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +10 -1
  5. package/build/index.js +19 -1
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/fs/Fs.d.ts +52 -0
  8. package/build/src/lib/fs/Fs.js +245 -0
  9. package/build/src/lib/fs/Fs.js.map +1 -0
  10. package/build/src/lib/timers/Timers.d.ts +5 -0
  11. package/build/src/lib/timers/Timers.js +10 -0
  12. package/build/src/lib/timers/Timers.js.map +1 -0
  13. package/build/src/mocking/ProjectMock.d.ts +13 -0
  14. package/build/src/mocking/ProjectMock.js +16 -0
  15. package/build/src/mocking/ProjectMock.js.map +1 -0
  16. package/build/src/mocking/lib/Request.d.ts +32 -0
  17. package/build/src/mocking/lib/Request.js +63 -0
  18. package/build/src/mocking/lib/Request.js.map +1 -0
  19. package/build/src/mocking/lib/Response.d.ts +33 -0
  20. package/build/src/mocking/lib/Response.js +79 -0
  21. package/build/src/mocking/lib/Response.js.map +1 -0
  22. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  23. package/build/src/runtime/node/BaseRunner.js +27 -0
  24. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  25. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  26. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  27. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  28. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  29. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  30. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  31. package/build/src/runtime/node/ProjectRunner.d.ts +164 -62
  32. package/build/src/runtime/node/ProjectRunner.js +191 -146
  33. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  34. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  35. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  36. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  37. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  38. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  39. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  40. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  41. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  42. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  43. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  44. package/build/src/runtime/reporters/Reporter.js +98 -0
  45. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  46. package/build/src/testing/TestCliHelper.d.ts +23 -0
  47. package/build/src/testing/TestCliHelper.js +71 -0
  48. package/build/src/testing/TestCliHelper.js.map +1 -0
  49. package/build/src/testing/getPort.d.ts +52 -0
  50. package/build/src/testing/getPort.js +169 -0
  51. package/build/src/testing/getPort.js.map +1 -0
  52. package/package.json +2 -1
  53. package/src/lib/fs/Fs.ts +258 -0
  54. package/src/lib/timers/Timers.ts +9 -0
  55. package/src/mocking/LegacyInterfaces.ts +1 -1
  56. package/src/mocking/ProjectMock.ts +20 -0
  57. package/src/mocking/lib/Request.ts +85 -0
  58. package/src/mocking/lib/Response.ts +101 -0
  59. package/src/runtime/node/BaseRunner.ts +29 -0
  60. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  61. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  62. package/src/runtime/node/ProjectRunner.ts +279 -186
  63. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  64. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  65. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  66. package/src/runtime/reporters/Reporter.ts +142 -0
  67. package/src/testing/TestCliHelper.ts +76 -0
  68. package/src/testing/getPort.ts +212 -0
@@ -0,0 +1,62 @@
1
+ import { IRequestLog } from '../../models/RequestLog.js';
2
+ export interface IProjectExecutionIteration {
3
+ /**
4
+ * The index of the iteration.
5
+ */
6
+ index: number;
7
+ /**
8
+ * The list of requests executed in the iteration.
9
+ */
10
+ executed: IRequestLog[];
11
+ /**
12
+ * Optional general error message.
13
+ */
14
+ error?: string;
15
+ }
16
+ export interface IProjectExecutionLog {
17
+ /**
18
+ * The timestamp when the execution started
19
+ */
20
+ started: number;
21
+ /**
22
+ * The timestamp when the execution ended
23
+ */
24
+ ended: number;
25
+ /**
26
+ * The execution logs for each iteration.
27
+ */
28
+ iterations: IProjectExecutionIteration[];
29
+ }
30
+ /**
31
+ * Base class for project execution reporters.
32
+ */
33
+ export declare abstract class Reporter {
34
+ info: IProjectExecutionLog;
35
+ constructor(info: IProjectExecutionLog);
36
+ /**
37
+ * Generates the report for the current execution log.
38
+ */
39
+ abstract generate(): Promise<void>;
40
+ /**
41
+ * Checks whether the execution log should be considered a failure.
42
+ * @param log The execution log.
43
+ * @returns `true` when the request was a failure.
44
+ */
45
+ protected isFailedLog(log: IRequestLog): boolean;
46
+ /**
47
+ * Computes the number of requests that failed.
48
+ */
49
+ computeFailed(): number;
50
+ /**
51
+ * Computes the number of requests that ended with the status code 399 at the most.
52
+ */
53
+ computeSucceeded(): number;
54
+ /**
55
+ * Computes the total time of sending each request.
56
+ */
57
+ computeTotalTime(): number;
58
+ /**
59
+ * Computes the total size of received data.
60
+ */
61
+ computeTotalSize(): number;
62
+ }
@@ -0,0 +1,98 @@
1
+ import { ErrorResponse } from '../../models/ErrorResponse.js';
2
+ /**
3
+ * Base class for project execution reporters.
4
+ */
5
+ export class Reporter {
6
+ info;
7
+ constructor(info) {
8
+ this.info = info;
9
+ }
10
+ /**
11
+ * Checks whether the execution log should be considered a failure.
12
+ * @param log The execution log.
13
+ * @returns `true` when the request was a failure.
14
+ */
15
+ isFailedLog(log) {
16
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
17
+ return true;
18
+ }
19
+ const response = log.response;
20
+ if (response.status >= 400) {
21
+ return true;
22
+ }
23
+ return false;
24
+ }
25
+ /**
26
+ * Computes the number of requests that failed.
27
+ */
28
+ computeFailed() {
29
+ let result = 0;
30
+ const { info } = this;
31
+ const { iterations } = info;
32
+ iterations.forEach((iteration) => {
33
+ iteration.executed.forEach((log) => {
34
+ if (this.isFailedLog(log)) {
35
+ result++;
36
+ }
37
+ });
38
+ });
39
+ return result;
40
+ }
41
+ /**
42
+ * Computes the number of requests that ended with the status code 399 at the most.
43
+ */
44
+ computeSucceeded() {
45
+ let result = 0;
46
+ const { info } = this;
47
+ const { iterations } = info;
48
+ iterations.forEach((iteration) => {
49
+ iteration.executed.forEach((log) => {
50
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
51
+ return;
52
+ }
53
+ const response = log.response;
54
+ if (response.status < 400) {
55
+ result++;
56
+ }
57
+ });
58
+ });
59
+ return result;
60
+ }
61
+ /**
62
+ * Computes the total time of sending each request.
63
+ */
64
+ computeTotalTime() {
65
+ let result = 0;
66
+ const { info } = this;
67
+ const { iterations } = info;
68
+ iterations.forEach((iteration) => {
69
+ iteration.executed.forEach((log) => {
70
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
71
+ return;
72
+ }
73
+ const response = log.response;
74
+ if (response.loadingTime && response.loadingTime > 0) {
75
+ result += response.loadingTime;
76
+ }
77
+ });
78
+ });
79
+ return result;
80
+ }
81
+ /**
82
+ * Computes the total size of received data.
83
+ */
84
+ computeTotalSize() {
85
+ let result = 0;
86
+ const { info } = this;
87
+ const { iterations } = info;
88
+ iterations.forEach((iteration) => {
89
+ iteration.executed.forEach((log) => {
90
+ if (log.size && log.size.response) {
91
+ result += log.size.response;
92
+ }
93
+ });
94
+ });
95
+ return result;
96
+ }
97
+ }
98
+ //# sourceMappingURL=Reporter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Reporter.js","sourceRoot":"","sources":["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C,SAAS,EAAE,EAAE;YAC/B,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBACjC,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE;oBACjC,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC;iBAC7B;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
@@ -0,0 +1,23 @@
1
+ export interface ITestRunCommandOptions {
2
+ includeError?: boolean;
3
+ noCleaning?: boolean;
4
+ }
5
+ export declare class TestCliHelper {
6
+ static cleanTerminalOutput(s: string): string;
7
+ static splitLines(table: string): string[];
8
+ /**
9
+ * Executes a passed asynchronous function and captures stdout.
10
+ * When the function fails, it cleans up output listeners and throws the error.
11
+ *
12
+ * ```javascript
13
+ * const out = grabOutput(async () => {
14
+ * // ...
15
+ * });
16
+ * console.log(out); // combined stdout and stderr.
17
+ * ```
18
+ *
19
+ * @param fn The function to execute.
20
+ * @returns The terminal output.
21
+ */
22
+ static grabOutput(fn: () => Promise<void>): Promise<string>;
23
+ }
@@ -0,0 +1,71 @@
1
+ export class TestCliHelper {
2
+ static cleanTerminalOutput(s) {
3
+ let result = s.trim();
4
+ result = result.replace(/[^\x20-\x7E\n]/gm, '');
5
+ // result = result.replace(/\[\d+m/gm, '');
6
+ result = result.replace(/\[\d+[a-zA-Z]/gm, '');
7
+ result = result.split('\n').filter(i => !!i.trim()).join('\n');
8
+ return result;
9
+ }
10
+ static splitLines(table) {
11
+ const result = [];
12
+ table.split('\n').forEach((line) => {
13
+ const value = line.trim();
14
+ if (!value) {
15
+ return;
16
+ }
17
+ result.push(value);
18
+ });
19
+ return result;
20
+ }
21
+ /**
22
+ * Executes a passed asynchronous function and captures stdout.
23
+ * When the function fails, it cleans up output listeners and throws the error.
24
+ *
25
+ * ```javascript
26
+ * const out = grabOutput(async () => {
27
+ * // ...
28
+ * });
29
+ * console.log(out); // combined stdout and stderr.
30
+ * ```
31
+ *
32
+ * @param fn The function to execute.
33
+ * @returns The terminal output.
34
+ */
35
+ static async grabOutput(fn) {
36
+ const messages = [];
37
+ function noop() {
38
+ //
39
+ }
40
+ const origOut = process.stdout.write;
41
+ const origErr = process.stderr.write;
42
+ const origClear = console.clear;
43
+ function messageHandler(buffer) {
44
+ if (typeof buffer === 'string') {
45
+ messages.push(buffer);
46
+ }
47
+ else {
48
+ messages.push(buffer.toString('utf8'));
49
+ }
50
+ return true;
51
+ }
52
+ function stop() {
53
+ process.stdout.write = origOut;
54
+ process.stderr.write = origErr;
55
+ console.clear = origClear;
56
+ }
57
+ process.stdout.write = messageHandler;
58
+ process.stderr.write = messageHandler;
59
+ console.clear = noop;
60
+ try {
61
+ await fn();
62
+ stop();
63
+ }
64
+ catch (e) {
65
+ stop();
66
+ throw e;
67
+ }
68
+ return messages.join('');
69
+ }
70
+ }
71
+ //# sourceMappingURL=TestCliHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TestCliHelper.js","sourceRoot":"","sources":["../../../src/testing/TestCliHelper.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,aAAa;IACxB,MAAM,CAAC,mBAAmB,CAAC,CAAS;QAClC,IAAI,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QACtB,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;QAChD,2CAA2C;QAC3C,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;QAC/C,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MA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tC,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;QAErB,IAAI;YACF,MAAM,EAAE,EAAE,CAAC;YACX,IAAI,EAAE,CAAC;SACR;QAAC,OAAO,CAAC,EAAE;YACV,IAAI,EAAE,CAAC;YACP,MAAM,CAAC,CAAC;SACT;QACD,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,52 @@
1
+ /// <reference types="node" />
2
+ import net from 'net';
3
+ export interface IGetPortOptions extends Omit<net.ListenOptions, 'port'> {
4
+ /**
5
+ A preferred port or an iterable of preferred ports to use.
6
+ */
7
+ readonly port?: number | Iterable<number>;
8
+ /**
9
+ The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
10
+
11
+ By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host.
12
+ */
13
+ readonly host?: string;
14
+ }
15
+ /**
16
+ Get an available TCP port number.
17
+
18
+ @returns Port number.
19
+
20
+ @example
21
+ ```
22
+ import getPort from 'get-port';
23
+
24
+ console.log(await getPort());
25
+ //=> 51402
26
+
27
+ // Pass in a preferred port
28
+ console.log(await getPort({port: 3000}));
29
+ // Will use 3000 if available, otherwise fall back to a random port
30
+
31
+ // Pass in an array of preferred ports
32
+ console.log(await getPort({port: [3000, 3001, 3002]}));
33
+ // Will use any element in the preferred ports array if available, otherwise fall back to a random port
34
+ ```
35
+ */
36
+ export declare function getPort(options?: IGetPortOptions): Promise<number>;
37
+ /**
38
+ Generate port numbers in the given range `from`...`to`.
39
+
40
+ @param from - The first port of the range. Must be in the range `1024`...`65535`.
41
+ @param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
42
+ @returns The port numbers in the range.
43
+
44
+ @example
45
+ ```
46
+ import getPort, {portNumbers} from 'get-port';
47
+
48
+ console.log(await getPort({port: portNumbers(3000, 3100)}));
49
+ // Will use any port from 3000 to 3100, otherwise fall back to a random port
50
+ ```
51
+ */
52
+ export declare function portNumbers(from: number, to: number): Iterable<number>;
@@ -0,0 +1,169 @@
1
+ import net from 'net';
2
+ import os from 'os';
3
+ class Locked extends Error {
4
+ constructor(port = 0) {
5
+ super(`${port} is locked`);
6
+ }
7
+ }
8
+ const lockedPorts = {
9
+ old: new Set(),
10
+ young: new Set(),
11
+ };
12
+ // On this interval, the old locked ports are discarded,
13
+ // the young locked ports are moved to old locked ports,
14
+ // and a new young set for locked ports are created.
15
+ const releaseOldLockedPortsIntervalMs = 1000 * 15;
16
+ // Lazily create interval on first use
17
+ let interval;
18
+ const getLocalHosts = () => {
19
+ const interfaces = os.networkInterfaces();
20
+ // Add undefined value for createServer function to use default host,
21
+ // and default IPv4 host in case createServer defaults to IPv6.
22
+ const results = new Set([undefined, '0.0.0.0']);
23
+ for (const _interface of Object.values(interfaces)) {
24
+ if (_interface) {
25
+ for (const config of _interface) {
26
+ results.add(config.address);
27
+ }
28
+ }
29
+ }
30
+ return results;
31
+ };
32
+ const checkAvailablePort = (options) => new Promise((resolve, reject) => {
33
+ const server = net.createServer();
34
+ server.unref();
35
+ server.on('error', reject);
36
+ server.listen(options, () => {
37
+ const { port } = server.address();
38
+ server.close(() => {
39
+ resolve(port);
40
+ });
41
+ });
42
+ });
43
+ const getAvailablePort = async (options, hosts) => {
44
+ if (options.host || options.port === 0) {
45
+ return checkAvailablePort(options);
46
+ }
47
+ for (const host of hosts) {
48
+ try {
49
+ await checkAvailablePort({ port: options.port, host }); // eslint-disable-line no-await-in-loop
50
+ }
51
+ catch (error) {
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ const typed = error;
54
+ if (!['EADDRNOTAVAIL', 'EINVAL'].includes(typed.code)) {
55
+ throw typed;
56
+ }
57
+ }
58
+ }
59
+ return options.port;
60
+ };
61
+ const portCheckSequence = function* (ports) {
62
+ if (ports) {
63
+ yield* ports;
64
+ }
65
+ yield 0; // Fall back to 0 if anything else failed
66
+ };
67
+ /**
68
+ Get an available TCP port number.
69
+
70
+ @returns Port number.
71
+
72
+ @example
73
+ ```
74
+ import getPort from 'get-port';
75
+
76
+ console.log(await getPort());
77
+ //=> 51402
78
+
79
+ // Pass in a preferred port
80
+ console.log(await getPort({port: 3000}));
81
+ // Will use 3000 if available, otherwise fall back to a random port
82
+
83
+ // Pass in an array of preferred ports
84
+ console.log(await getPort({port: [3000, 3001, 3002]}));
85
+ // Will use any element in the preferred ports array if available, otherwise fall back to a random port
86
+ ```
87
+ */
88
+ export async function getPort(options) {
89
+ let ports = [];
90
+ if (options) {
91
+ if (typeof options.port === 'number') {
92
+ ports = [options.port];
93
+ }
94
+ else if (options.port) {
95
+ ports = options.port;
96
+ }
97
+ }
98
+ if (interval === undefined) {
99
+ interval = setInterval(() => {
100
+ lockedPorts.old = lockedPorts.young;
101
+ lockedPorts.young = new Set();
102
+ }, releaseOldLockedPortsIntervalMs);
103
+ // Does not exist in some environments (Electron, Jest jsdom env, browser, etc).
104
+ if (interval.unref) {
105
+ interval.unref();
106
+ }
107
+ }
108
+ const hosts = getLocalHosts();
109
+ for (const port of portCheckSequence(ports)) {
110
+ try {
111
+ let availablePort = await getAvailablePort({ ...options, port }, hosts); // eslint-disable-line no-await-in-loop
112
+ while (lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort)) {
113
+ if (port !== 0) {
114
+ throw new Locked(port);
115
+ }
116
+ availablePort = await getAvailablePort({ ...options, port }, hosts); // eslint-disable-line no-await-in-loop
117
+ }
118
+ lockedPorts.young.add(availablePort);
119
+ if (!availablePort) {
120
+ throw new Error('No available ports found');
121
+ }
122
+ return availablePort;
123
+ }
124
+ catch (error) {
125
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
+ const typed = error;
127
+ if (!['EADDRINUSE', 'EACCES'].includes(typed.code) && !(typed instanceof Locked)) {
128
+ throw error;
129
+ }
130
+ }
131
+ }
132
+ throw new Error('No available ports found');
133
+ }
134
+ /**
135
+ Generate port numbers in the given range `from`...`to`.
136
+
137
+ @param from - The first port of the range. Must be in the range `1024`...`65535`.
138
+ @param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
139
+ @returns The port numbers in the range.
140
+
141
+ @example
142
+ ```
143
+ import getPort, {portNumbers} from 'get-port';
144
+
145
+ console.log(await getPort({port: portNumbers(3000, 3100)}));
146
+ // Will use any port from 3000 to 3100, otherwise fall back to a random port
147
+ ```
148
+ */
149
+ export function portNumbers(from, to) {
150
+ if (!Number.isInteger(from) || !Number.isInteger(to)) {
151
+ throw new TypeError('`from` and `to` must be integer numbers');
152
+ }
153
+ if (from < 1024 || from > 65_535) {
154
+ throw new RangeError('`from` must be between 1024 and 65535');
155
+ }
156
+ if (to < 1024 || to > 65_536) {
157
+ throw new RangeError('`to` must be between 1024 and 65536');
158
+ }
159
+ if (to < from) {
160
+ throw new RangeError('`to` must be greater than or equal to `from`');
161
+ }
162
+ const generator = function* (from, to) {
163
+ for (let port = from; port <= to; port++) {
164
+ yield port;
165
+ }
166
+ };
167
+ return generator(from, to);
168
+ }
169
+ //# sourceMappingURL=getPort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPort.js","sourceRoot":"","sources":["../../../src/testing/getPort.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,MAAM,IAAI,CAAC;AAkBpB,MAAM,MAAO,SAAQ,KAAK;IACzB,YAAY,IAAI,GAAG,CAAC;QACnB,KAAK,CAAC,GAAG,IAAI,YAAY,CAAC,CAAC;IAC5B,CAAC;CACD;AAED,MAAM,WAAW,GAAG;IACnB,GAAG,EAAE,IAAI,GAAG,EAAE;IACd,KAAK,EAAE,IAAI,GAAG,EAAE;CAChB,CAAC;AAEF,wDAAwD;AACxD,wDAAwD;AACxD,oDAAoD;AACpD,MAAM,+BAA+B,GAAG,IAAI,GAAG,EAAE,CAAC;AAElD,sCAAsC;AACtC,IAAI,QAAqB,CAAC;AAE1B,MAAM,aAAa,GAAG,GAA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EAAE,CAAC,EAAE;QACrD,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;KAC/D;IAED,IAAI,IAAI,GAAG,IAAI,IAAI,IAAI,GAAG,MAAM,EAAE;QACjC,MAAM,IAAI,UAAU,CAAC,uCAAuC,CAAC,CAAC;KAC9D;IAED,IAAI,EAAE,GAAG,IAAI,IAAI,EAAE,GAAG,MAAM,EAAE;QAC7B,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;KAC5D;IAED,IAAI,EAAE,GAAG,IAAI,EAAE;QACd,MAAM,IAAI,UAAU,CAAC,8CAA8C,CAAC,CAAC;KACrE;IAED,MAAM,SAAS,GAAG,QAAS,CAAC,EAAE,IAAY,EAAE,EAAU;QACrD,KAAK,IAAI,IAAI,GAAG,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE;YACzC,MAAM,IAAI,CAAC;SACX;IACF,CAAC,CAAC;IAEF,OAAO,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAC5B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@api-client/core",
3
3
  "description": "The ARC core client library. Works in NodeJS and in a ES enabled browser.",
4
- "version": "0.3.5",
4
+ "version": "0.3.6",
5
5
  "license": "Apache-2.0",
6
6
  "main": "build/index.js",
7
7
  "module": "build/index.js",
@@ -89,6 +89,7 @@
89
89
  "@metrichor/jmespath": "^0.3.1",
90
90
  "@pawel-up/jexl": "^3.0.0",
91
91
  "@xmldom/xmldom": "^0.8.1",
92
+ "console-table-printer": "^2.10.0",
92
93
  "form-data": "^4.0.0",
93
94
  "json8-patch": "^1.0.6",
94
95
  "ws": "^8.5.0",