Skip to content

Commit 61e93a7

Browse files
committed
force color, --extra_params
1 parent e50332d commit 61e93a7

File tree

1 file changed

+63
-43
lines changed

1 file changed

+63
-43
lines changed

compare.ts

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import axios from 'axios';
2-
import * as fs from 'fs';
1+
import axios from "axios";
2+
import * as fs from "fs";
33
import * as _ from "lodash";
4-
import * as queryString from 'query-string';
4+
import * as queryString from "query-string";
55
import * as Bluebird from "bluebird";
66

77
const c = require("ansi-colors");
88
const parseCsvSync = require("csv-parse/lib/sync");
9-
const jsondiffpatch = require("jsondiffpatch");
109

1110
export type Change = {
1211
quality: "good" | "bad" | "neutral";
@@ -25,22 +24,28 @@ const NEW_KEY = "new";
2524

2625
export const globalCommandLineOptions = {
2726
method: {
28-
choices: ['GET', 'POST', 'PUT'],
29-
default: 'GET',
30-
description: 'what http method to use',
27+
choices: ["GET", "POST", "PUT"],
28+
default: "GET",
29+
description: "what http method to use",
3130
},
31+
color: {
32+
type: "boolean",
33+
default: true,
34+
description: "turns on/off colorized output"
35+
}
3236
};
3337

3438
export const apiEnvCommandLineOptions: Record<string, any> = {
3539
host: {
36-
type: 'string',
37-
description: 'Host/port',
40+
type: "string",
41+
description: "Host/port",
3842
// required: true,
3943
},
4044
protocol: {
41-
choices: ['http', 'https'],
42-
default: 'http',
43-
description: 'What protocol to use (if not specified in url), defaults to http for local, https otherwise',
45+
choices: ["http", "https"],
46+
default: "http",
47+
description:
48+
"What protocol to use (if not specified in url), defaults to http for local, https otherwise",
4449
},
4550
// key: {
4651
// type: 'string',
@@ -84,6 +89,11 @@ function parseArgv() {
8489
description: "A file containing url encoded query params, requires --endpoint",
8590
});
8691

92+
yargs.option("extra_params", {
93+
type: "string",
94+
description: "Extra static parameters that will be added to each query, maybe something like limit=2 to make diffs less noisy",
95+
});
96+
8797
yargs.option("input_csv", {
8898
type: "array",
8999
description: "A file containingquery params in a csv, first line is , requires --endpoint",
@@ -99,14 +109,15 @@ function parseArgv() {
99109

100110
yargs.option("ignored_fields", {
101111
type: "array",
102-
description: "field names to ignore when diffing responses. geometry latitude longitude are common for geocode compare runs",
112+
description:
113+
"field names to ignore when diffing responses. geometry latitude longitude are common for geocode compare runs",
103114
});
104115

105116
yargs.group(["input_params", "endpoint", "input_queries", "input_csv"], "Query options:");
106117
yargs.group(oldParams, 'Options for "old" server to compare:');
107118
yargs.group(newParams, 'Options for "new" server to compare:');
108-
yargs.implies('input_csv', 'endpoint');
109-
yargs.implies('input_params', 'endpoint');
119+
yargs.implies("input_csv", "endpoint");
120+
yargs.implies("input_params", "endpoint");
110121

111122
yargs.usage(`This tool takes in a set of queries to compare against two radar api servers.
112123
It has a bunch of options, here are some examples:
@@ -126,20 +137,25 @@ There are other ways to configure old and new, look in the help for more. These
126137
return yargs.argv;
127138
}
128139

129-
130140
export interface ApiEnv {
131-
protocol: string,
132-
host: string,
141+
protocol: string;
142+
host: string;
133143
}
134144

135145
export function argvToApiEnv(argv: any): ApiEnv {
136146
const apiEnv: Partial<ApiEnv> = _.clone(argv);
137147

148+
if (argv.host.startsWith("http")) {
149+
const url = new URL(argv.host);
150+
argv.host = url.host;
151+
argv.protocol = url.protocol;
152+
}
153+
138154
if (!apiEnv.protocol) {
139-
if (apiEnv.host.includes('localhost') || apiEnv.host.includes('127.0.0.1')) {
140-
apiEnv.protocol = 'http';
155+
if (apiEnv.host.includes("localhost") || apiEnv.host.includes("127.0.0.1")) {
156+
apiEnv.protocol = "http";
141157
} else {
142-
apiEnv.protocol = 'https';
158+
apiEnv.protocol = "https";
143159
}
144160
}
145161

@@ -192,56 +208,59 @@ const generateQueries = () => {
192208
}
193209
};
194210

195-
export async function runQuery(apiEnv: ApiEnv, {
196-
params,
197-
endpoint,
198-
method,
199-
verbose,
200-
}: {
201-
params: Record<string, any>
202-
method: string,
203-
endpoint: string
204-
verbose?: boolean,
205-
}) {
211+
export async function runQuery(
212+
apiEnv: ApiEnv,
213+
{
214+
params,
215+
endpoint,
216+
method,
217+
verbose,
218+
}: {
219+
params: Record<string, any>;
220+
method: string;
221+
endpoint: string;
222+
verbose?: boolean;
223+
}
224+
) {
206225
const log = (...args) => {
207226
if (verbose) {
208227
console.error.apply(this, args);
209228
}
210229
};
211230

212231
// v1/xxxx ... maybe someone was lazy and didn't start with an opening slash
213-
if (endpoint[0] !== '/' && !endpoint.startsWith('http:')) {
232+
if (endpoint[0] !== "/" && !endpoint.startsWith("http:")) {
214233
endpoint = `/${endpoint}`;
215234
}
216235

217236
// someone was lazy and didn't specify /v1
218-
if (!endpoint.startsWith('/v1')) {
237+
if (!endpoint.startsWith("/v1")) {
219238
endpoint = `/v1${endpoint}`;
220239
}
221240

222-
let url = '';
241+
let url = "";
223242

224243
// /xxx/api.... so we need to add host
225-
if (endpoint[0] === '/') {
244+
if (endpoint[0] === "/") {
226245
url = apiEnv.host + endpoint;
227246
}
228247

229248
// don't yet have a protocol in the url
230-
if (!url.startsWith('http:')) {
249+
if (!url.startsWith("http:")) {
231250
url = `${apiEnv.protocol}://${url}`;
232251
}
233252

234253
log(`Fetching ${url}`);
235254

236-
const headers = {
237-
'User-Agent': 'radar-compare-tool/unknown'
255+
const headers = {
256+
"User-Agent": "radar-compare-tool/unknown",
238257
};
239258

240259
try {
241260
const response = await axios(url, {
242261
headers,
243-
params: method === 'GET' ? params : undefined,
244-
data: method === 'POST' ? params : undefined,
262+
params: method === "GET" ? params : undefined,
263+
data: method === "POST" ? params : undefined,
245264
method: method.toLowerCase() as any,
246265
});
247266
return response;
@@ -253,7 +272,6 @@ export async function runQuery(apiEnv: ApiEnv, {
253272
}
254273
}
255274

256-
257275
async function compareQuery({
258276
oldApiEnv,
259277
newApiEnv,
@@ -264,7 +282,7 @@ async function compareQuery({
264282
query: string;
265283
}) {
266284
const [endpoint, paramsString] = query.split("?");
267-
const params = queryString.parse(paramsString);
285+
const params = queryString.parse(paramsString + '&' + argv.extra_params);
268286
const oldResponse = await runQuery(oldApiEnv, {
269287
endpoint,
270288
params,
@@ -350,6 +368,8 @@ async function compareQueries({
350368

351369
const argv = parseArgv();
352370

371+
const jsondiffpatch = require("jsondiffpatch");
372+
353373
const oldApiEnv = argvToApiEnv(argv[OLD_KEY]);
354374
const newApiEnv = argvToApiEnv(argv[NEW_KEY]);
355375

0 commit comments

Comments
 (0)