Skip to content

Commit f0e0127

Browse files
match endpoints with a function, exclude endpoints (#1713)
Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>
1 parent 4b8f6b4 commit f0e0127

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from 'oazapfts/lib/codegen/tscodegen';
1616
import type { OpenAPIV3 } from 'openapi-types';
1717
import { generateReactHooks } from './generators/react-hooks';
18-
import type { EndpointOverrides, GenerationOptions, OperationDefinition } from './types';
18+
import type { EndpointMatcher, EndpointOverrides, GenerationOptions, OperationDefinition, TextMatcher } from './types';
1919
import { capitalize, getOperationDefinitions, getV3Doc, isQuery as testIsQuery, removeUndefined } from './utils';
2020
import type { ObjectPropertyDefinitions } from './codegen';
2121
import { generateCreateApiCall, generateEndpointDefinition, generateImportNode } from './codegen';
@@ -33,22 +33,30 @@ function getOperationName({ verb, path, operation }: Pick<OperationDefinition, '
3333
return _getOperationName(verb, path, operation.operationId);
3434
}
3535

36-
function patternMatches(pattern?: string | RegExp | (string | RegExp)[]) {
36+
function patternMatches(pattern?: TextMatcher) {
3737
const filters = Array.isArray(pattern) ? pattern : [pattern];
38-
return function matcher(operationDefinition: OperationDefinition) {
38+
return function matcher(operationName: string) {
3939
if (!pattern) return true;
40-
const operationName = getOperationName(operationDefinition);
4140
return filters.some((filter) =>
4241
typeof filter === 'string' ? filter === operationName : filter?.test(operationName)
4342
);
4443
};
4544
}
4645

46+
function operationMatches(pattern?: EndpointMatcher) {
47+
const checkMatch = typeof pattern === 'function' ? pattern : patternMatches(pattern);
48+
return function matcher(operationDefinition: OperationDefinition) {
49+
if (!pattern) return true;
50+
const operationName = getOperationName(operationDefinition);
51+
return checkMatch(operationName, operationDefinition);
52+
};
53+
}
54+
4755
export function getOverrides(
4856
operation: OperationDefinition,
4957
endpointOverrides?: EndpointOverrides[]
5058
): EndpointOverrides | undefined {
51-
return endpointOverrides?.find((override) => patternMatches(override.pattern)(operation));
59+
return endpointOverrides?.find((override) => operationMatches(override.pattern)(operation));
5260
}
5361

5462
export async function generateApi(
@@ -70,7 +78,7 @@ export async function generateApi(
7078

7179
const apiGen = new ApiGenerator(v3Doc, {});
7280

73-
const operationDefinitions = getOperationDefinitions(v3Doc).filter(patternMatches(filterEndpoints));
81+
const operationDefinitions = getOperationDefinitions(v3Doc).filter(operationMatches(filterEndpoints));
7482

7583
const resultFile = ts.createSourceFile(
7684
'someFileName.ts',

packages/rtk-query-codegen-openapi/src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ export interface CommonOptions {
5353
hooks?: boolean;
5454
}
5555

56+
export type TextMatcher = string | RegExp | (string | RegExp)[];
57+
58+
export type EndpointMatcherFunction = (operationName: string, operationDefinition: OperationDefinition) => boolean;
59+
60+
export type EndpointMatcher = TextMatcher | EndpointMatcherFunction;
61+
5662
export interface OutputFileOptions extends Partial<CommonOptions> {
5763
outputFile: string;
58-
filterEndpoints?: string | RegExp | (string | RegExp)[];
64+
filterEndpoints?: EndpointMatcher;
5965
endpointOverrides?: EndpointOverrides[];
6066
}
6167

6268
export interface EndpointOverrides {
63-
pattern: string | RegExp | (string | RegExp)[];
69+
pattern: EndpointMatcher;
6470
type: 'mutation' | 'query';
6571
}
6672

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ test('endpoint filtering', async () => {
3131
expect(api).toMatchSnapshot('should only have endpoints loginUser, placeOrder, getOrderById, deleteOrder');
3232
});
3333

34+
test('endpoint filtering by function', async () => {
35+
const api = await generateEndpoints({
36+
apiFile: './fixtures/emptyApi.ts',
37+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
38+
filterEndpoints: (name, endpoint) => name.match(/order/i) !== null && endpoint.verb === 'get',
39+
});
40+
expect(api).toMatch(/getOrderById:/);
41+
expect(api).not.toMatch(/placeOrder:/);
42+
expect(api).not.toMatch(/loginUser:/);
43+
});
44+
45+
test('negated endpoint filtering', async () => {
46+
const api = await generateEndpoints({
47+
apiFile: './fixtures/emptyApi.ts',
48+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
49+
filterEndpoints: (name) => !/user/i.test(name),
50+
});
51+
expect(api).not.toMatch(/loginUser:/);
52+
});
53+
3454
test('endpoint overrides', async () => {
3555
const api = await generateEndpoints({
3656
apiFile: './fixtures/emptyApi.ts',

0 commit comments

Comments
 (0)