Skip to content

Commit 2915a72

Browse files
sebwsmarkerikson
authored andcommitted
feat: extend endpoint overrides for openapi codegen
1 parent cd42073 commit 2915a72

File tree

4 files changed

+1484
-20
lines changed

4 files changed

+1484
-20
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ import ts from 'typescript';
1515
import type { ObjectPropertyDefinitions } from './codegen';
1616
import { generateCreateApiCall, generateEndpointDefinition, generateImportNode, generateTagTypes } from './codegen';
1717
import { generateReactHooks } from './generators/react-hooks';
18-
import type { EndpointMatcher, EndpointOverrides, GenerationOptions, OperationDefinition, TextMatcher } from './types';
18+
import type {
19+
EndpointMatcher,
20+
EndpointOverrides,
21+
GenerationOptions,
22+
OperationDefinition,
23+
ParameterDefinition,
24+
ParameterMatcher,
25+
TextMatcher,
26+
} from './types';
1927
import { capitalize, getOperationDefinitions, getV3Doc, removeUndefined, isQuery as testIsQuery } from './utils';
2028
import { factory } from './utils/factory';
2129

@@ -57,6 +65,15 @@ function operationMatches(pattern?: EndpointMatcher) {
5765
};
5866
}
5967

68+
function argumentMatches(pattern?: ParameterMatcher) {
69+
const checkMatch = typeof pattern === 'function' ? pattern : patternMatches(pattern);
70+
return function matcher(argumentDefinition: ParameterDefinition) {
71+
if (!pattern || argumentDefinition.in === 'path') return true;
72+
const argumentName = argumentDefinition.name;
73+
return checkMatch(argumentName, argumentDefinition);
74+
};
75+
}
76+
6077
function withQueryComment<T extends ts.Node>(node: T, def: QueryArgDefinition, hasTrailingNewLine: boolean): T {
6178
const comment = def.origin === 'param' ? def.param.description : def.body.description;
6279
if (comment) {
@@ -264,7 +281,7 @@ export async function generateApi(
264281
const parameters = supportDeepObjects([
265282
...apiGen.resolveArray(pathItem.parameters),
266283
...apiGen.resolveArray(operation.parameters),
267-
]);
284+
]).filter(argumentMatches(overrides?.parameterFilter));
268285

269286
const allNames = parameters.map((p) => p.name);
270287
const queryArg: QueryArgDefinitions = {};

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ export type OperationDefinition = {
88
operation: OpenAPIV3.OperationObject;
99
};
1010

11+
export type ParameterDefinition = OpenAPIV3.ParameterObject;
12+
1113
type Require<T, K extends keyof T> = { [k in K]-?: NonNullable<T[k]> } & Omit<T, K>;
1214
type Optional<T, K extends keyof T> = { [k in K]?: NonNullable<T[k]> } & Omit<T, K>;
1315
type Id<T> = { [K in keyof T]: T[K] } & {};
16+
type AtLeastOneKey<T> = {
17+
[K in keyof T]-?: Pick<T, K> & Partial<T>;
18+
}[keyof T];
1419

1520
export const operationKeys = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'] as const;
1621

@@ -98,6 +103,10 @@ export type EndpointMatcherFunction = (operationName: string, operationDefinitio
98103

99104
export type EndpointMatcher = TextMatcher | EndpointMatcherFunction;
100105

106+
export type ParameterMatcherFunction = (parameterName: string, parameterDefinition: ParameterDefinition) => boolean;
107+
108+
export type ParameterMatcher = TextMatcher | ParameterMatcherFunction;
109+
101110
export interface OutputFileOptions extends Partial<CommonOptions> {
102111
outputFile: string;
103112
filterEndpoints?: EndpointMatcher;
@@ -109,10 +118,12 @@ export interface OutputFileOptions extends Partial<CommonOptions> {
109118
useEnumType?: boolean;
110119
}
111120

112-
export interface EndpointOverrides {
121+
export type EndpointOverrides = {
113122
pattern: EndpointMatcher;
123+
} & AtLeastOneKey<{
114124
type: 'mutation' | 'query';
115-
}
125+
parameterFilter: ParameterMatcher;
126+
}>;
116127

117128
export type ConfigFile =
118129
| Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>>

0 commit comments

Comments
 (0)