Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Camel case query function operation IDs #245

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plugins/typescript/src/core/createOperationQueryFnNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const createOperationQueryFnNodes = ({
variablesType,
fetcherFn,
operation,
operationId,
url,
verb,
name,
Expand All @@ -32,6 +33,7 @@ export const createOperationQueryFnNodes = ({
queryParamsType: ts.TypeNode;
variablesType: ts.TypeNode;
operation: OperationObject;
operationId: string;
fetcherFn: string;
url: string;
verb: string;
Expand Down Expand Up @@ -161,9 +163,7 @@ export const createOperationQueryFnNodes = ({
),
f.createPropertyAssignment(
f.createIdentifier("operationId"),
f.createStringLiteral(
operation.operationId as string
)
f.createStringLiteral(operationId)
),
f.createShorthandPropertyAssignment(
f.createIdentifier("variables"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,4 +1235,99 @@ describe("generateReactQueryFunctions", () => {
"
`);
});

it("should camel case operation IDs and remove special characters", async () => {
const writeFile = jest.fn();
const openAPIDocument: OpenAPIObject = {
openapi: "3.0.0",
info: {
title: "petshop",
version: "1.0.0",
},
paths: {
"/pets": {
get: {
operationId: "list_pets",
description: "Get all the pets",
responses: {
"200": {
description: "pet response",
content: {
"application/json": {
schema: {
type: "array",
items: {
$ref: "#/components/schemas/Pet",
},
},
},
},
},
},
},
},
},
};

await generateReactQueryFunctions(
{
openAPIDocument,
writeFile,
existsFile: () => true,
readFile: async () => "",
},
config,
);

expect(writeFile.mock.calls[0][0]).toBe("petstoreFunctions.ts");
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import * as reactQuery from "@tanstack/react-query";
import { PetstoreContext, queryKeyFn } from "./petstoreContext";
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";

export type ListPetsError = Fetcher.ErrorWrapper<undefined>;

export type ListPetsResponse = Schemas.Pet[];

export type ListPetsVariables = PetstoreContext["fetcherOptions"];

/**
* Get all the pets
*/
export const fetchListPets = (variables: ListPetsVariables, signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", ...variables, signal });

/**
* Get all the pets
*/
export const listPetsQuery = (variables: ListPetsVariables): [
reactQuery.QueryKey,
({ signal }: {
signal?: AbortSignal;
}) => Promise<ListPetsResponse>
] => [
queryKeyFn({
path: "/pets",
operationId: "listPets",
variables
}),
async ({ signal }: {
signal?: AbortSignal;
}) => fetchListPets({ ...variables }, signal)
];

export type QueryOperation = {
path: "/pets";
operationId: "listPets";
variables: ListPetsVariables;
};
"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ export const generateReactQueryFunctions = async (
),
});



const operationFetcherFnName = `fetch${c.pascal(operationId)}`;
const operationQueryFnName = `${c.pascal(operationId)}Query`;
const component: "useQuery" | "useMutate" =
Expand All @@ -157,7 +155,6 @@ export const generateReactQueryFunctions = async (
}

if (component === "useQuery") {

nodes.push(...declarationNodes);

keyManagerItems.push(
Expand Down Expand Up @@ -210,6 +207,7 @@ export const generateReactQueryFunctions = async (
queryParamsType,
headersType,
operation,
operationId,
fetcherFn,
url: route,
verb,
Expand Down