1
- import axios from ' axios' ;
2
- import * as fs from 'fs' ;
1
+ import axios from " axios" ;
2
+ import * as fs from "fs" ;
3
3
import * as _ from "lodash" ;
4
- import * as queryString from ' query-string' ;
4
+ import * as queryString from " query-string" ;
5
5
import * as Bluebird from "bluebird" ;
6
6
7
7
const c = require ( "ansi-colors" ) ;
8
8
const parseCsvSync = require ( "csv-parse/lib/sync" ) ;
9
- const jsondiffpatch = require ( "jsondiffpatch" ) ;
10
9
11
10
export type Change = {
12
11
quality : "good" | "bad" | "neutral" ;
@@ -25,22 +24,28 @@ const NEW_KEY = "new";
25
24
26
25
export const globalCommandLineOptions = {
27
26
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" ,
31
30
} ,
31
+ color : {
32
+ type : "boolean" ,
33
+ default : true ,
34
+ description : "turns on/off colorized output"
35
+ }
32
36
} ;
33
37
34
38
export const apiEnvCommandLineOptions : Record < string , any > = {
35
39
host : {
36
- type : ' string' ,
37
- description : ' Host/port' ,
40
+ type : " string" ,
41
+ description : " Host/port" ,
38
42
// required: true,
39
43
} ,
40
44
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" ,
44
49
} ,
45
50
// key: {
46
51
// type: 'string',
@@ -84,6 +89,11 @@ function parseArgv() {
84
89
description : "A file containing url encoded query params, requires --endpoint" ,
85
90
} ) ;
86
91
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
+
87
97
yargs . option ( "input_csv" , {
88
98
type : "array" ,
89
99
description : "A file containingquery params in a csv, first line is , requires --endpoint" ,
@@ -99,14 +109,15 @@ function parseArgv() {
99
109
100
110
yargs . option ( "ignored_fields" , {
101
111
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" ,
103
114
} ) ;
104
115
105
116
yargs . group ( [ "input_params" , "endpoint" , "input_queries" , "input_csv" ] , "Query options:" ) ;
106
117
yargs . group ( oldParams , 'Options for "old" server to compare:' ) ;
107
118
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" ) ;
110
121
111
122
yargs . usage ( `This tool takes in a set of queries to compare against two radar api servers.
112
123
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
126
137
return yargs . argv ;
127
138
}
128
139
129
-
130
140
export interface ApiEnv {
131
- protocol : string ,
132
- host : string ,
141
+ protocol : string ;
142
+ host : string ;
133
143
}
134
144
135
145
export function argvToApiEnv ( argv : any ) : ApiEnv {
136
146
const apiEnv : Partial < ApiEnv > = _ . clone ( argv ) ;
137
147
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
+
138
154
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" ;
141
157
} else {
142
- apiEnv . protocol = ' https' ;
158
+ apiEnv . protocol = " https" ;
143
159
}
144
160
}
145
161
@@ -192,56 +208,59 @@ const generateQueries = () => {
192
208
}
193
209
} ;
194
210
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
+ ) {
206
225
const log = ( ...args ) => {
207
226
if ( verbose ) {
208
227
console . error . apply ( this , args ) ;
209
228
}
210
229
} ;
211
230
212
231
// 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:" ) ) {
214
233
endpoint = `/${ endpoint } ` ;
215
234
}
216
235
217
236
// someone was lazy and didn't specify /v1
218
- if ( ! endpoint . startsWith ( ' /v1' ) ) {
237
+ if ( ! endpoint . startsWith ( " /v1" ) ) {
219
238
endpoint = `/v1${ endpoint } ` ;
220
239
}
221
240
222
- let url = '' ;
241
+ let url = "" ;
223
242
224
243
// /xxx/api.... so we need to add host
225
- if ( endpoint [ 0 ] === '/' ) {
244
+ if ( endpoint [ 0 ] === "/" ) {
226
245
url = apiEnv . host + endpoint ;
227
246
}
228
247
229
248
// don't yet have a protocol in the url
230
- if ( ! url . startsWith ( ' http:' ) ) {
249
+ if ( ! url . startsWith ( " http:" ) ) {
231
250
url = `${ apiEnv . protocol } ://${ url } ` ;
232
251
}
233
252
234
253
log ( `Fetching ${ url } ` ) ;
235
254
236
- const headers = {
237
- ' User-Agent' : ' radar-compare-tool/unknown'
255
+ const headers = {
256
+ " User-Agent" : " radar-compare-tool/unknown" ,
238
257
} ;
239
258
240
259
try {
241
260
const response = await axios ( url , {
242
261
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 ,
245
264
method : method . toLowerCase ( ) as any ,
246
265
} ) ;
247
266
return response ;
@@ -253,7 +272,6 @@ export async function runQuery(apiEnv: ApiEnv, {
253
272
}
254
273
}
255
274
256
-
257
275
async function compareQuery ( {
258
276
oldApiEnv,
259
277
newApiEnv,
@@ -264,7 +282,7 @@ async function compareQuery({
264
282
query : string ;
265
283
} ) {
266
284
const [ endpoint , paramsString ] = query . split ( "?" ) ;
267
- const params = queryString . parse ( paramsString ) ;
285
+ const params = queryString . parse ( paramsString + '&' + argv . extra_params ) ;
268
286
const oldResponse = await runQuery ( oldApiEnv , {
269
287
endpoint,
270
288
params,
@@ -350,6 +368,8 @@ async function compareQueries({
350
368
351
369
const argv = parseArgv ( ) ;
352
370
371
+ const jsondiffpatch = require ( "jsondiffpatch" ) ;
372
+
353
373
const oldApiEnv = argvToApiEnv ( argv [ OLD_KEY ] ) ;
354
374
const newApiEnv = argvToApiEnv ( argv [ NEW_KEY ] ) ;
355
375
0 commit comments