Skip to content

Commit c10196e

Browse files
author
David Blackman
committed
dynamically load a query processor
1 parent 0893509 commit c10196e

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/api-diff/api-diff.ts

+34-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type CompareArgs = Pick<
2727
| 'timeout'
2828
| 'retries'
2929
| 'response_filter'
30+
| 'response_filter_function'
3031
>;
3132

3233
/**
@@ -53,36 +54,52 @@ async function compareQuery({
5354
// otherwise run it against the old server
5455
const oldResponse = query.baselineResponse
5556
? ({ data: query.baselineResponse } as AxiosResponse<unknown>)
56-
: await runQuery(oldApiEnv, {
57-
...query,
58-
params: { ...query.params, ...oldApiEnv.extraParams },
59-
}, {
60-
timeout: argv.timeout,
61-
retries: argv.retries,
62-
}).catch((e) => {
57+
: await runQuery(
58+
oldApiEnv,
59+
{
60+
...query,
61+
params: { ...query.params, ...oldApiEnv.extraParams },
62+
},
63+
{
64+
timeout: argv.timeout,
65+
retries: argv.retries,
66+
},
67+
).catch((e) => {
6368
console.error(e);
6469
throw e;
6570
});
6671

6772
const newResponse = newApiEnv
68-
? await runQuery(newApiEnv, {
69-
...query,
70-
params: { ...query.params, ...newApiEnv.extraParams },
71-
}, {
72-
timeout: argv.timeout,
73-
retries: argv.retries,
74-
}).catch((e) => {
73+
? await runQuery(
74+
newApiEnv,
75+
{
76+
...query,
77+
params: { ...query.params, ...newApiEnv.extraParams },
78+
},
79+
{
80+
timeout: argv.timeout,
81+
retries: argv.retries,
82+
},
83+
).catch((e) => {
7584
console.error(e);
7685
throw e;
7786
})
7887
: undefined;
7988

80-
if (argv.response_filter) {
89+
if (argv.response_filter || argv.response_filter_function) {
8190
const hadData = !_.isEmpty(oldResponse.data) || !_.isEmpty(newResponse.data);
8291

8392
try {
84-
oldResponse.data = jp.query(oldResponse.data, argv.response_filter);
85-
newResponse.data = jp.query(newResponse.data, argv.response_filter);
93+
if (argv.response_filter_function) {
94+
/* eslint-disable import/no-dynamic-require, global-require,
95+
@typescript-eslint/no-var-requires */
96+
const filter = require(argv.response_filter_function);
97+
oldResponse.data = filter(oldResponse.data);
98+
newResponse.data = filter(newResponse.data);
99+
} else {
100+
oldResponse.data = jp.query(oldResponse.data, argv.response_filter);
101+
newResponse.data = jp.query(newResponse.data, argv.response_filter);
102+
}
86103
} catch (e) {
87104
console.error(e);
88105
}

src/api-diff/argv.ts

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type ParsedArgs = {
1515
method: string;
1616
ignored_fields: string[];
1717
response_filter: string;
18+
response_filter_function: string;
1819
concurrency: number;
1920
unchanged: boolean;
2021
key_map: string[];
@@ -102,6 +103,12 @@ function createYargs(yargs: Argv, envs: string[]) {
102103
'run the responses through this filter (specified in jsonpath format - https://www.npmjs.com/package/jsonpath) before diffing',
103104
});
104105

106+
yargs.option('response_filter_function', {
107+
type: 'string',
108+
description:
109+
'path to a .ts or .js file to load (using default export) to filter the api response',
110+
});
111+
105112
yargs.option('concurrency', {
106113
type: 'number',
107114
default: 10,

0 commit comments

Comments
 (0)