Skip to content

Commit f6e2977

Browse files
authored
Camel case query function operation IDs (#245)
Operation IDs in query functions currently don't line up with keys in the `QueryOperation` object.
1 parent d693f63 commit f6e2977

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

plugins/typescript/src/core/createOperationQueryFnNodes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const createOperationQueryFnNodes = ({
1919
variablesType,
2020
fetcherFn,
2121
operation,
22+
operationId,
2223
url,
2324
verb,
2425
name,
@@ -32,6 +33,7 @@ export const createOperationQueryFnNodes = ({
3233
queryParamsType: ts.TypeNode;
3334
variablesType: ts.TypeNode;
3435
operation: OperationObject;
36+
operationId: string;
3537
fetcherFn: string;
3638
url: string;
3739
verb: string;
@@ -161,9 +163,7 @@ export const createOperationQueryFnNodes = ({
161163
),
162164
f.createPropertyAssignment(
163165
f.createIdentifier("operationId"),
164-
f.createStringLiteral(
165-
operation.operationId as string
166-
)
166+
f.createStringLiteral(operationId)
167167
),
168168
f.createShorthandPropertyAssignment(
169169
f.createIdentifier("variables"),

plugins/typescript/src/generators/generateReactQueryFunctions.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,4 +1235,99 @@ describe("generateReactQueryFunctions", () => {
12351235
"
12361236
`);
12371237
});
1238+
1239+
it("should camel case operation IDs and remove special characters", async () => {
1240+
const writeFile = jest.fn();
1241+
const openAPIDocument: OpenAPIObject = {
1242+
openapi: "3.0.0",
1243+
info: {
1244+
title: "petshop",
1245+
version: "1.0.0",
1246+
},
1247+
paths: {
1248+
"/pets": {
1249+
get: {
1250+
operationId: "list_pets",
1251+
description: "Get all the pets",
1252+
responses: {
1253+
"200": {
1254+
description: "pet response",
1255+
content: {
1256+
"application/json": {
1257+
schema: {
1258+
type: "array",
1259+
items: {
1260+
$ref: "#/components/schemas/Pet",
1261+
},
1262+
},
1263+
},
1264+
},
1265+
},
1266+
},
1267+
},
1268+
},
1269+
},
1270+
};
1271+
1272+
await generateReactQueryFunctions(
1273+
{
1274+
openAPIDocument,
1275+
writeFile,
1276+
existsFile: () => true,
1277+
readFile: async () => "",
1278+
},
1279+
config,
1280+
);
1281+
1282+
expect(writeFile.mock.calls[0][0]).toBe("petstoreFunctions.ts");
1283+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
1284+
"/**
1285+
* Generated by @openapi-codegen
1286+
*
1287+
* @version 1.0.0
1288+
*/
1289+
import * as reactQuery from "@tanstack/react-query";
1290+
import { PetstoreContext, queryKeyFn } from "./petstoreContext";
1291+
import type * as Fetcher from "./petstoreFetcher";
1292+
import { petstoreFetch } from "./petstoreFetcher";
1293+
import type * as Schemas from "./petstoreSchemas";
1294+
1295+
export type ListPetsError = Fetcher.ErrorWrapper<undefined>;
1296+
1297+
export type ListPetsResponse = Schemas.Pet[];
1298+
1299+
export type ListPetsVariables = PetstoreContext["fetcherOptions"];
1300+
1301+
/**
1302+
* Get all the pets
1303+
*/
1304+
export const fetchListPets = (variables: ListPetsVariables, signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", ...variables, signal });
1305+
1306+
/**
1307+
* Get all the pets
1308+
*/
1309+
export const listPetsQuery = (variables: ListPetsVariables): [
1310+
reactQuery.QueryKey,
1311+
({ signal }: {
1312+
signal?: AbortSignal;
1313+
}) => Promise<ListPetsResponse>
1314+
] => [
1315+
queryKeyFn({
1316+
path: "/pets",
1317+
operationId: "listPets",
1318+
variables
1319+
}),
1320+
async ({ signal }: {
1321+
signal?: AbortSignal;
1322+
}) => fetchListPets({ ...variables }, signal)
1323+
];
1324+
1325+
export type QueryOperation = {
1326+
path: "/pets";
1327+
operationId: "listPets";
1328+
variables: ListPetsVariables;
1329+
};
1330+
"
1331+
`);
1332+
});
12381333
});

plugins/typescript/src/generators/generateReactQueryFunctions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ export const generateReactQueryFunctions = async (
143143
),
144144
});
145145

146-
147-
148146
const operationFetcherFnName = `fetch${c.pascal(operationId)}`;
149147
const operationQueryFnName = `${c.pascal(operationId)}Query`;
150148
const component: "useQuery" | "useMutate" =
@@ -157,7 +155,6 @@ export const generateReactQueryFunctions = async (
157155
}
158156

159157
if (component === "useQuery") {
160-
161158
nodes.push(...declarationNodes);
162159

163160
keyManagerItems.push(
@@ -210,6 +207,7 @@ export const generateReactQueryFunctions = async (
210207
queryParamsType,
211208
headersType,
212209
operation,
210+
operationId,
213211
fetcherFn,
214212
url: route,
215213
verb,

0 commit comments

Comments
 (0)