Skip to content

Commit eeaa35b

Browse files
fix: Generated hooks also includes operationNameSuffix when provided (#5089)
1 parent 15b379f commit eeaa35b

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export async function generateApi(
204204
operationDefinitions,
205205
endpointOverrides,
206206
config: hooks,
207+
operationNameSuffix,
207208
}),
208209
]
209210
: []),

packages/rtk-query-codegen-openapi/src/generators/react-hooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,40 @@ type GetReactHookNameParams = {
1111
operationDefinition: OperationDefinition;
1212
endpointOverrides: EndpointOverrides[] | undefined;
1313
config: HooksConfigOptions;
14+
operationNameSuffix?: string;
1415
};
1516

1617
type CreateBindingParams = {
1718
operationDefinition: OperationDefinition;
1819
overrides?: EndpointOverrides;
1920
isLazy?: boolean;
21+
operationNameSuffix?: string;
2022
};
2123

2224
const createBinding = ({
2325
operationDefinition: { verb, path, operation },
2426
overrides,
2527
isLazy = false,
28+
operationNameSuffix,
2629
}: CreateBindingParams) =>
2730
factory.createBindingElement(
2831
undefined,
2932
undefined,
3033
factory.createIdentifier(
31-
`use${isLazy ? 'Lazy' : ''}${capitalize(getOperationName(verb, path, operation.operationId))}${
34+
`use${isLazy ? 'Lazy' : ''}${capitalize(getOperationName(verb, path, operation.operationId))}${operationNameSuffix ?? ''}${
3235
isQuery(verb, overrides) ? 'Query' : 'Mutation'
3336
}`
3437
),
3538
undefined
3639
);
3740

38-
const getReactHookName = ({ operationDefinition, endpointOverrides, config }: GetReactHookNameParams) => {
41+
const getReactHookName = ({ operationDefinition, endpointOverrides, config, operationNameSuffix }: GetReactHookNameParams) => {
3942
const overrides = getOverrides(operationDefinition, endpointOverrides);
4043

4144
const baseParams = {
4245
operationDefinition,
4346
overrides,
47+
operationNameSuffix,
4448
};
4549

4650
const _isQuery = isQuery(operationDefinition.verb, overrides);
@@ -66,12 +70,14 @@ type GenerateReactHooksParams = {
6670
operationDefinitions: OperationDefinition[];
6771
endpointOverrides: EndpointOverrides[] | undefined;
6872
config: HooksConfigOptions;
73+
operationNameSuffix?: string;
6974
};
7075
export const generateReactHooks = ({
7176
exportName,
7277
operationDefinitions,
7378
endpointOverrides,
7479
config,
80+
operationNameSuffix,
7581
}: GenerateReactHooksParams) =>
7682
factory.createVariableStatement(
7783
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
@@ -80,7 +86,7 @@ export const generateReactHooks = ({
8086
factory.createVariableDeclaration(
8187
factory.createObjectBindingPattern(
8288
operationDefinitions
83-
.map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config }))
89+
.map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config, operationNameSuffix }))
8490
.flat()
8591
),
8692
undefined,

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,64 @@ export const { useLoginUserMutation } = injectedRtkApi;
24482448
"
24492449
`;
24502450

2451+
exports[`hooks generation with operationNameSuffix > should generate an \`useGetPetByIdMySuffixQuery\` query hook and an \`useAddPetMySuffixMutation\` mutation hook 1`] = `
2452+
"import { api } from "./fixtures/emptyApi";
2453+
const injectedRtkApi = api.injectEndpoints({
2454+
endpoints: (build) => ({
2455+
AddPetMySuffix: build.mutation<
2456+
AddPetMySuffixApiResponse,
2457+
AddPetMySuffixApiArg
2458+
>({
2459+
query: (queryArg) => ({
2460+
url: \`/pet\`,
2461+
method: "POST",
2462+
body: queryArg.pet,
2463+
}),
2464+
}),
2465+
GetPetByIdMySuffix: build.query<
2466+
GetPetByIdMySuffixApiResponse,
2467+
GetPetByIdMySuffixApiArg
2468+
>({
2469+
query: (queryArg) => ({ url: \`/pet/\${queryArg.petId}\` }),
2470+
}),
2471+
}),
2472+
overrideExisting: false,
2473+
});
2474+
export { injectedRtkApi as enhancedApi };
2475+
export type AddPetMySuffixApiResponse =
2476+
/** status 200 Successful operation */ Pet;
2477+
export type AddPetMySuffixApiArg = {
2478+
/** Create a new pet in the store */
2479+
pet: Pet;
2480+
};
2481+
export type GetPetByIdMySuffixApiResponse =
2482+
/** status 200 successful operation */ Pet;
2483+
export type GetPetByIdMySuffixApiArg = {
2484+
/** ID of pet to return */
2485+
petId: number;
2486+
};
2487+
export type Category = {
2488+
id?: number | undefined;
2489+
name?: string | undefined;
2490+
};
2491+
export type Tag = {
2492+
id?: number | undefined;
2493+
name?: string | undefined;
2494+
};
2495+
export type Pet = {
2496+
id?: number | undefined;
2497+
name: string;
2498+
category?: Category | undefined;
2499+
photoUrls: string[];
2500+
tags?: Tag[] | undefined;
2501+
/** pet status in the store */
2502+
status?: ("available" | "pending" | "sold") | undefined;
2503+
};
2504+
export const { useAddPetMySuffixMutation, useGetPetByIdMySuffixQuery } =
2505+
injectedRtkApi;
2506+
"
2507+
`;
2508+
24512509
exports[`openapi spec > readOnly / writeOnly are merged 1`] = `
24522510
"import { api } from "./fixtures/emptyApi";
24532511
const injectedRtkApi = api.injectEndpoints({

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,22 @@ test('hooks generation', async () => {
348348
);
349349
});
350350

351+
test('hooks generation with operationNameSuffix', async () => {
352+
const api = await generateEndpoints({
353+
unionUndefined: true,
354+
apiFile: './fixtures/emptyApi.ts',
355+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
356+
filterEndpoints: ['getPetById', 'addPet'],
357+
hooks: true,
358+
operationNameSuffix: 'MySuffix',
359+
});
360+
expect(api).toContain('useGetPetByIdMySuffixQuery');
361+
expect(api).toContain('useAddPetMySuffixMutation');
362+
expect(api).toMatchSnapshot(
363+
'should generate an `useGetPetByIdMySuffixQuery` query hook and an `useAddPetMySuffixMutation` mutation hook'
364+
);
365+
});
366+
351367
it('supports granular hooks generation that includes all query types', async () => {
352368
const api = await generateEndpoints({
353369
apiFile: './fixtures/emptyApi.ts',

0 commit comments

Comments
 (0)