diff --git a/plugins/typescript/src/core/createOperationFetcherFnNodes.ts b/plugins/typescript/src/core/createOperationFetcherFnNodes.ts index 1f6ec75..72b6c7c 100644 --- a/plugins/typescript/src/core/createOperationFetcherFnNodes.ts +++ b/plugins/typescript/src/core/createOperationFetcherFnNodes.ts @@ -141,4 +141,4 @@ export const createOperationFetcherFnNodes = ({ * `pet/{pet_id}` -> `pet/{petId}` */ const camelizedPathParams = (url: string) => - url.replace(/\{[\w\d\-_]*\}/g, (match) => `{${camel(match)}}`); + url.replace(/\{[\w\d\-_.]*\}/g, (match) => `{${camel(match)}}`); diff --git a/plugins/typescript/src/generators/generateReactQueryComponents.test.ts b/plugins/typescript/src/generators/generateReactQueryComponents.test.ts index 91151d5..5b8b9d1 100644 --- a/plugins/typescript/src/generators/generateReactQueryComponents.test.ts +++ b/plugins/typescript/src/generators/generateReactQueryComponents.test.ts @@ -989,7 +989,7 @@ describe("generateReactQueryComponents", () => { `); }); - it("should deal with pathParams (kebab case)", async () => { + it("should deal with pathParams (dash case)", async () => { const writeFile = jest.fn(); const openAPIDocument: OpenAPIObject = { openapi: "3.0.0", @@ -1101,6 +1101,118 @@ describe("generateReactQueryComponents", () => { `); }); + it("should deal with pathParams (dot case)", async () => { + const writeFile = jest.fn(); + const openAPIDocument: OpenAPIObject = { + openapi: "3.0.0", + info: { + title: "petshop", + version: "1.0.0", + }, + components: { + requestBodies: { + UpdatePetRequestBody: { + content: { + "application/json": { + schema: { + type: "object", + properties: { + name: { + type: "string", + }, + }, + }, + }, + }, + }, + }, + }, + paths: { + "/pet/{pet.id}": { + parameters: [ + { + in: "path", + name: "pet.id", + schema: { + type: "string", + }, + required: true, + }, + ], + put: { + operationId: "updatePet", + requestBody: { + $ref: "#/components/requestBodies/UpdatePetRequestBody", + }, + responses: { + 200: { + content: { + "application/json": { + description: "Successful response", + schema: { + type: "string", + }, + }, + }, + }, + }, + }, + }, + }, + }; + await generateReactQueryComponents( + { + openAPIDocument, + writeFile, + existsFile: () => true, + readFile: async () => "", + }, + config + ); + + expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts"); + expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(` + "/** + * Generated by @openapi-codegen + * + * @version 1.0.0 + */ + import * as reactQuery from \\"@tanstack/react-query\\"; + import { usePetstoreContext, PetstoreContext } from \\"./petstoreContext\\"; + import type * as Fetcher from \\"./petstoreFetcher\\"; + import { petstoreFetch } from \\"./petstoreFetcher\\"; + import type * as RequestBodies from \\"./petstoreRequestBodies\\"; + + export type UpdatePetPathParams = { + petId: string; + }; + + export type UpdatePetError = Fetcher.ErrorWrapper; + + export type UpdatePetVariables = { + body?: RequestBodies.UpdatePetRequestBody; + pathParams: UpdatePetPathParams; + } & PetstoreContext[\\"fetcherOptions\\"]; + + export const fetchUpdatePet = (variables: UpdatePetVariables, signal?: AbortSignal) => petstoreFetch({ url: \\"/pet/{petId}\\", method: \\"put\\", ...variables, signal }); + + export const useUpdatePet = (options?: Omit, \\"mutationFn\\">) => { + const { fetcherOptions } = usePetstoreContext(); + return reactQuery.useMutation({ + mutationFn: (variables: UpdatePetVariables) => fetchUpdatePet({ ...fetcherOptions, ...variables }), + ...options + }); + }; + + export type QueryOperation = { + path: string; + operationId: never; + variables: unknown; + }; + " + `); + }); + it("should build components without prefix", async () => { const writeFile = jest.fn(); const openAPIDocument: OpenAPIObject = {