Skip to content

Commit

Permalink
feat: extend endpoint overrides for openapi codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
sebws committed Mar 26, 2024
1 parent 1afcdd4 commit d0d0db1
Show file tree
Hide file tree
Showing 4 changed files with 1,484 additions and 20 deletions.
21 changes: 19 additions & 2 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import ts from 'typescript';
import type { ObjectPropertyDefinitions } from './codegen';
import { generateCreateApiCall, generateEndpointDefinition, generateImportNode, generateTagTypes } from './codegen';
import { generateReactHooks } from './generators/react-hooks';
import type { EndpointMatcher, EndpointOverrides, GenerationOptions, OperationDefinition, TextMatcher } from './types';
import type {
EndpointMatcher,
EndpointOverrides,
GenerationOptions,
OperationDefinition,
ParameterDefinition,
ParameterMatcher,
TextMatcher,
} from './types';
import { capitalize, getOperationDefinitions, getV3Doc, removeUndefined, isQuery as testIsQuery } from './utils';
import { factory } from './utils/factory';

Expand Down Expand Up @@ -55,6 +63,15 @@ function operationMatches(pattern?: EndpointMatcher) {
};
}

function argumentMatches(pattern?: ParameterMatcher) {
const checkMatch = typeof pattern === 'function' ? pattern : patternMatches(pattern);
return function matcher(argumentDefinition: ParameterDefinition) {
if (!pattern || argumentDefinition.in === 'path') return true;
const argumentName = argumentDefinition.name;
return checkMatch(argumentName, argumentDefinition);
};
}

function withQueryComment<T extends ts.Node>(node: T, def: QueryArgDefinition, hasTrailingNewLine: boolean): T {
const comment = def.origin === 'param' ? def.param.description : def.body.description;
if (comment) {
Expand Down Expand Up @@ -260,7 +277,7 @@ export async function generateApi(
const parameters = supportDeepObjects([
...apiGen.resolveArray(pathItem.parameters),
...apiGen.resolveArray(operation.parameters),
]);
]).filter(argumentMatches(overrides?.parameterFilter));

const allNames = parameters.map((p) => p.name);
const queryArg: QueryArgDefinitions = {};
Expand Down
15 changes: 13 additions & 2 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export type OperationDefinition = {
operation: OpenAPIV3.OperationObject;
};

export type ParameterDefinition = OpenAPIV3.ParameterObject;

type Require<T, K extends keyof T> = { [k in K]-?: NonNullable<T[k]> } & Omit<T, K>;
type Optional<T, K extends keyof T> = { [k in K]?: NonNullable<T[k]> } & Omit<T, K>;
type Id<T> = { [K in keyof T]: T[K] } & {};
type AtLeastOneKey<T> = {
[K in keyof T]-?: Pick<T, K> & Partial<T>;
}[keyof T];

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

Expand Down Expand Up @@ -80,6 +85,10 @@ export type EndpointMatcherFunction = (operationName: string, operationDefinitio

export type EndpointMatcher = TextMatcher | EndpointMatcherFunction;

export type ParameterMatcherFunction = (argumentName: string, argumentDefinition: ParameterDefinition) => boolean;

export type ParameterMatcher = TextMatcher | ParameterMatcherFunction;

export interface OutputFileOptions extends Partial<CommonOptions> {
outputFile: string;
filterEndpoints?: EndpointMatcher;
Expand All @@ -91,10 +100,12 @@ export interface OutputFileOptions extends Partial<CommonOptions> {
useEnumType?: boolean;
}

export interface EndpointOverrides {
export type EndpointOverrides = {
pattern: EndpointMatcher;
} & AtLeastOneKey<{
type: 'mutation' | 'query';
}
parameterFilter: ParameterMatcher;
}>;

export type ConfigFile =
| Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>>
Expand Down

0 comments on commit d0d0db1

Please sign in to comment.