Skip to content

Commit 33c4fc7

Browse files
author
David Blackman
committed
add more usage examples
1 parent dfe7afb commit 33c4fc7

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ Note that this is an interactive evaluation form for figuring out which queries
2323

2424
## tl;dr
2525

26+
### Try it out yourself!
27+
28+
This is a contrived example because our old and new servers are the same, but they return random data, so it is a good way to quickly see diffs.
29+
30+
31+
From CSV input:
32+
```
33+
api-diff \
34+
--old.host http://names.drycodes.com/ \
35+
--new.host http://names.drycodes.com/ \
36+
--endpoint "/10" \
37+
--input_csv docs/examples/input-names-drycodes.csv
38+
```
39+
40+
From a file of http paths
41+
42+
```
43+
api-diff \
44+
--old.host http://names.drycodes.com/ \
45+
--new.host http://names.drycodes.com/ \
46+
--input_queries docs/examples/input-names-drycodes.txt
47+
```
48+
2649
### Compare two servers
2750

2851
```
@@ -141,6 +164,9 @@ An example config looks like this
141164
# only required if using authStyle="param"
142165
# authParam: "api_key",
143166
167+
# optional, if using authStyle="header"
168+
# authType: "Basic",
169+
144170
# first keyType listed here will be the default if a
145171
# keyType is not specified in the commandline options to # compare
146172
keyTypes: ['test', 'live'],
@@ -181,6 +207,8 @@ APICOM_STAGING_TEST_KEY=XXXX
181207
APICOM_STAGING_LIVE_KEY=XXXX
182208
```
183209

210+
If your server requires authorization in request params, set authStyle="param" and authParam to the request parameter name the server expects. If your server uses the HTTP Authoization header, then set authStyle="header" and (optionally) authType to what auth type prefix the server expects, such as "Basic" or "Bearer" - this can be omitted if your server takes bare keys in the Authoization header.
211+
184212
Our config defines two types of keys - test and live. It also defines two key environments. One for "prod" and one for "staging," the host configs for "local" and "user" are both configured to look in the "staging" key env"
185213

186214
### Specifying a server

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blackmad/api-diff",
3-
"version": "1.0.31",
3+
"version": "1.0.32",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/api-diff/formatters/console-formatter.ts

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import * as _ from 'lodash';
66

77
import * as config from '../../config';
88
import { Change } from '../change';
9-
import { CompareFormatter, FinishedStats, makeResponseTimesHistogram } from './compare-formatter';
9+
import {
10+
CompareFormatter,
11+
FinishedStats,
12+
} from './compare-formatter';
1013
import { ApiEnv } from '../../apiEnv';
1114

1215
export default class ConsoleFormatter extends CompareFormatter {
@@ -15,7 +18,9 @@ export default class ConsoleFormatter extends CompareFormatter {
1518
const commandParts: string[] = [];
1619

1720
if (config.CONFIG_FILE_ENV_VARIABLE && config.API_DIFF_CONFIG_FILE) {
18-
commandParts.push(`${config.CONFIG_FILE_ENV_VARIABLE}=${config.API_DIFF_CONFIG_FILE}`);
21+
commandParts.push(
22+
`${config.CONFIG_FILE_ENV_VARIABLE}=${config.API_DIFF_CONFIG_FILE}`,
23+
);
1924
}
2025

2126
if (_.some(process.argv, (arg) => arg.includes('ts-node'))) {
@@ -32,8 +37,12 @@ export default class ConsoleFormatter extends CompareFormatter {
3237
return commandParts.join(' ');
3338
};
3439
const outputLines = `${JSON.stringify(change.query.params)}
35-
${apiEnvToApiSh(this.oldApiEnv)} ${change.oldResponse.request?.res?.responseUrl}
36-
${apiEnvToApiSh(this.newApiEnv)} ${change.newResponse.request.res.responseUrl}`;
40+
${apiEnvToApiSh(this.oldApiEnv)} ${
41+
change.oldResponse.request?.res?.responseUrl
42+
}
43+
${apiEnvToApiSh(this.newApiEnv)} ${
44+
change.newResponse.request.res.responseUrl
45+
}`;
3746

3847
if (!change.delta) {
3948
this.writeln(chalk.cyan(`Unchanged: ${outputLines}`));
@@ -46,38 +55,47 @@ export default class ConsoleFormatter extends CompareFormatter {
4655
}
4756

4857
onFinished(finishedStats: FinishedStats): Promise<void> {
49-
this.writeln(`Elapsed: ${(Date.now() - this.startDate.getTime()) / 1000} seconds`);
58+
this.writeln(
59+
`Elapsed: ${(Date.now() - this.startDate.getTime()) / 1000} seconds`,
60+
);
5061

51-
this.writeln('');
62+
// this.writeln('');
5263

53-
// Response times table
54-
this.writeln('Response times');
55-
const oldResponseTimes = makeResponseTimesHistogram(finishedStats.old.responseTimes);
56-
const newResponseTimes = makeResponseTimesHistogram(finishedStats.new.responseTimes);
64+
// // Response times table
65+
// this.writeln('Response times');
66+
// const oldResponseTimes = makeResponseTimesHistogram(finishedStats.old.responseTimes);
67+
// const newResponseTimes = makeResponseTimesHistogram(finishedStats.new.responseTimes);
5768

58-
const responseTimesTable = [['', 'old', 'new']];
59-
_.keys(oldResponseTimes).forEach((key) => {
60-
responseTimesTable.push([
61-
key,
62-
oldResponseTimes[key].toString(),
63-
newResponseTimes[key].toString(),
64-
]);
65-
});
69+
// const responseTimesTable = [['', 'old', 'new']];
70+
// _.keys(oldResponseTimes).forEach((key) => {
71+
// responseTimesTable.push([
72+
// key,
73+
// oldResponseTimes[key].toString(),
74+
// newResponseTimes[key].toString(),
75+
// ]);
76+
// });
6677

67-
this.writeln(table(responseTimesTable));
78+
// this.writeln(table(responseTimesTable));
6879

6980
// Status codes table
70-
this.writeln('Status codes');
71-
const statusCodesTable = [['', 'old', 'new']];
72-
_.keys(finishedStats.old.statusCodes).forEach((key) => {
73-
statusCodesTable.push([
74-
key,
75-
finishedStats.old.statusCodes[key].toString(),
76-
finishedStats.old.statusCodes[key].toString(),
77-
]);
78-
});
79-
80-
this.writeln(table(statusCodesTable));
81+
// Only output this if there's a reason to, that we had some non-200 codes
82+
if (
83+
!_.isEqual(_.keys(finishedStats.new.statusCodes), ['200'])
84+
|| !_.isEqual(_.keys(finishedStats.old.statusCodes), ['200'])
85+
) {
86+
this.writeln('Status codes');
87+
88+
const statusCodesTable = [['', 'old', 'new']];
89+
_.keys(finishedStats.old.statusCodes).forEach((key) => {
90+
statusCodesTable.push([
91+
key,
92+
finishedStats.old.statusCodes[key].toString(),
93+
finishedStats.new.statusCodes[key].toString(),
94+
]);
95+
});
96+
97+
this.writeln(table(statusCodesTable));
98+
}
8199

82100
return Promise.resolve();
83101
}

src/api-diff/query-reader.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import * as queryString from 'querystring';
55
import * as fs from 'fs';
66
import * as _ from 'lodash';
7-
import chalk from 'chalk';
87
import parseCsvSync from 'csv-parse/lib/sync';
98
import { failedExit } from '../cli-utils';
109
import { ParsedArgs } from './argv';
@@ -30,15 +29,6 @@ type QueryReaderArgs = Pick<
3029
* @returns {Query[]} list of queries read
3130
*/
3231
function readQueriesHelper(argv: QueryReaderArgs): Query[] {
33-
const hasInputFile = argv.input_params || argv.input_csv;
34-
if ((argv.endpoint && !hasInputFile) || (!argv.endpoint && hasInputFile)) {
35-
console.error(
36-
chalk.red(
37-
'Must specify both --endpoint and (--input_params or --input_csv) , perhaps you wanted --input_queries?',
38-
),
39-
);
40-
}
41-
4232
/**
4333
* Convert a /path?params=X string to a Query
4434
*

0 commit comments

Comments
 (0)