From 11b3c9eab8b2a79ae5ccb097cae9beb14710468e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20Gene=C5=9F?= Date: Mon, 28 Oct 2024 21:49:27 +0300 Subject: [PATCH 1/4] import Options type from hey-api --- src/createImports.mts | 64 +++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/createImports.mts b/src/createImports.mts index aa1b433..b203e32 100644 --- a/src/createImports.mts +++ b/src/createImports.mts @@ -30,7 +30,7 @@ export const createImports = ({ : []; const serviceExports = Array.from( - serviceFile.getExportedDeclarations().keys(), + serviceFile.getExportedDeclarations().keys() ); const serviceNames = serviceExports; @@ -45,47 +45,63 @@ export const createImports = ({ ts.factory.createImportSpecifier( true, undefined, - ts.factory.createIdentifier("QueryClient"), + ts.factory.createIdentifier("Options") + ), + ]) + ), + ts.factory.createStringLiteral("@hey-api/client-fetch"), + undefined + ), + ts.factory.createImportDeclaration( + undefined, + ts.factory.createImportClause( + false, + undefined, + ts.factory.createNamedImports([ + ts.factory.createImportSpecifier( + true, + undefined, + ts.factory.createIdentifier("QueryClient") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useQuery"), + ts.factory.createIdentifier("useQuery") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useSuspenseQuery"), + ts.factory.createIdentifier("useSuspenseQuery") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useMutation"), + ts.factory.createIdentifier("useMutation") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseQueryResult"), + ts.factory.createIdentifier("UseQueryResult") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseQueryOptions"), + ts.factory.createIdentifier("UseQueryOptions") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseMutationOptions"), + ts.factory.createIdentifier("UseMutationOptions") ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseMutationResult"), + ts.factory.createIdentifier("UseMutationResult") ), - ]), + ]) ), ts.factory.createStringLiteral("@tanstack/react-query"), - undefined, + undefined ), ts.factory.createImportDeclaration( undefined, @@ -98,13 +114,13 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier(serviceName), - ), + ts.factory.createIdentifier(serviceName) + ) ), - ]), + ]) ), ts.factory.createStringLiteral(join("../requests", serviceFileName)), - undefined, + undefined ), ]; if (modelsFile) { @@ -120,14 +136,14 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier(modelName), - ), + ts.factory.createIdentifier(modelName) + ) ), - ]), + ]) ), ts.factory.createStringLiteral(join("../requests/", modelsFileName)), - undefined, - ), + undefined + ) ); } @@ -142,12 +158,12 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("AxiosError"), + ts.factory.createIdentifier("AxiosError") ), - ]), + ]) ), - ts.factory.createStringLiteral("axios"), - ), + ts.factory.createStringLiteral("axios") + ) ); } return imports; From 78ad4a90701149cc29964cad3578e44c366f6500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20Gene=C5=9F?= Date: Mon, 28 Oct 2024 22:51:46 +0300 Subject: [PATCH 2/4] revert formatting --- src/createImports.mts | 48 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/createImports.mts b/src/createImports.mts index b203e32..7c56950 100644 --- a/src/createImports.mts +++ b/src/createImports.mts @@ -30,7 +30,7 @@ export const createImports = ({ : []; const serviceExports = Array.from( - serviceFile.getExportedDeclarations().keys() + serviceFile.getExportedDeclarations().keys(), ); const serviceNames = serviceExports; @@ -66,42 +66,42 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useQuery") + ts.factory.createIdentifier("useQuery"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useSuspenseQuery") + ts.factory.createIdentifier("useSuspenseQuery"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("useMutation") + ts.factory.createIdentifier("useMutation"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseQueryResult") + ts.factory.createIdentifier("UseQueryResult"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseQueryOptions") + ts.factory.createIdentifier("UseQueryOptions"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseMutationOptions") + ts.factory.createIdentifier("UseMutationOptions"), ), ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("UseMutationResult") + ts.factory.createIdentifier("UseMutationResult"), ), - ]) + ]), ), ts.factory.createStringLiteral("@tanstack/react-query"), - undefined + undefined, ), ts.factory.createImportDeclaration( undefined, @@ -114,13 +114,13 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier(serviceName) - ) + ts.factory.createIdentifier(serviceName), + ), ), - ]) + ]), ), ts.factory.createStringLiteral(join("../requests", serviceFileName)), - undefined + undefined, ), ]; if (modelsFile) { @@ -136,14 +136,14 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier(modelName) - ) + ts.factory.createIdentifier(modelName), + ), ), - ]) + ]), ), ts.factory.createStringLiteral(join("../requests/", modelsFileName)), - undefined - ) + undefined, + ), ); } @@ -158,13 +158,13 @@ export const createImports = ({ ts.factory.createImportSpecifier( false, undefined, - ts.factory.createIdentifier("AxiosError") + ts.factory.createIdentifier("AxiosError"), ), - ]) + ]), ), - ts.factory.createStringLiteral("axios") - ) + ts.factory.createStringLiteral("axios"), + ), ); } return imports; -}; +}; \ No newline at end of file From 2297531a98054620e0331af3fa577e81bb8afcf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20Gene=C5=9F?= Date: Mon, 28 Oct 2024 22:54:58 +0300 Subject: [PATCH 3/4] add trailing space back for not creating formatting conflict --- src/createImports.mts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createImports.mts b/src/createImports.mts index 7c56950..cb4c100 100644 --- a/src/createImports.mts +++ b/src/createImports.mts @@ -167,4 +167,4 @@ export const createImports = ({ ); } return imports; -}; \ No newline at end of file +}; From a4976ac9fec11117c40a3bc26e1b9c998c822c1f Mon Sep 17 00:00:00 2001 From: Onur Genes Date: Mon, 4 Nov 2024 07:55:53 +0300 Subject: [PATCH 4/4] updated tests. added client check based on client type --- src/createImports.mts | 14 +++++++++----- tests/__snapshots__/createSource.test.ts.snap | 8 ++++++++ tests/createImports.test.ts | 2 ++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/createImports.mts b/src/createImports.mts index cb4c100..ac887ec 100644 --- a/src/createImports.mts +++ b/src/createImports.mts @@ -45,12 +45,16 @@ export const createImports = ({ ts.factory.createImportSpecifier( true, undefined, - ts.factory.createIdentifier("Options") + ts.factory.createIdentifier("Options"), ), - ]) + ]), + ), + ts.factory.createStringLiteral( + client === "@hey-api/client-axios" + ? "@hey-api/client-axios" + : "@hey-api/client-fetch", ), - ts.factory.createStringLiteral("@hey-api/client-fetch"), - undefined + undefined, ), ts.factory.createImportDeclaration( undefined, @@ -61,7 +65,7 @@ export const createImports = ({ ts.factory.createImportSpecifier( true, undefined, - ts.factory.createIdentifier("QueryClient") + ts.factory.createIdentifier("QueryClient"), ), ts.factory.createImportSpecifier( false, diff --git a/tests/__snapshots__/createSource.test.ts.snap b/tests/__snapshots__/createSource.test.ts.snap index 88a3e5e..861fc50 100644 --- a/tests/__snapshots__/createSource.test.ts.snap +++ b/tests/__snapshots__/createSource.test.ts.snap @@ -11,6 +11,7 @@ export * from "./queries"; exports[`createSource > createSource - @hey-api/client-axios 2`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 +import { type Options } from "@hey-api/client-axios"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -47,6 +48,7 @@ exports[`createSource > createSource - @hey-api/client-axios 3`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-axios"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -65,6 +67,7 @@ exports[`createSource > createSource - @hey-api/client-axios 4`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-axios"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -80,6 +83,7 @@ exports[`createSource > createSource - @hey-api/client-axios 5`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-axios"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -102,6 +106,7 @@ export * from "./queries"; exports[`createSource > createSource - @hey-api/client-fetch 2`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 +import { type Options } from "@hey-api/client-fetch"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -137,6 +142,7 @@ exports[`createSource > createSource - @hey-api/client-fetch 3`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-fetch"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -154,6 +160,7 @@ exports[`createSource > createSource - @hey-api/client-fetch 4`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-fetch"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; @@ -168,6 +175,7 @@ exports[`createSource > createSource - @hey-api/client-fetch 5`] = ` "// generated with @7nohe/openapi-react-query-codegen@1.0.0 import * as Common from "./common"; +import { type Options } from "@hey-api/client-fetch"; import { type QueryClient, useQuery, useSuspenseQuery, useMutation, UseQueryResult, UseQueryOptions, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; import { client, findPets, addPet, getNotDefined, postNotDefined, findPetById, deletePet, findPaginatedPets } from "../requests/services.gen"; import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, FindPetsError, AddPetData, AddPetResponse, AddPetError, GetNotDefinedResponse, GetNotDefinedError, PostNotDefinedResponse, PostNotDefinedError, FindPetByIdData, FindPetByIdResponse, FindPetByIdError, DeletePetData, DeletePetResponse, DeletePetError, FindPaginatedPetsData, FindPaginatedPetsResponse, FindPaginatedPetsError } from "../requests/types.gen"; diff --git a/tests/createImports.test.ts b/tests/createImports.test.ts index 80b39ef..7c6ee1f 100644 --- a/tests/createImports.test.ts +++ b/tests/createImports.test.ts @@ -20,6 +20,7 @@ describe(fileName, () => { // @ts-ignore const moduleNames = imports.map((i) => i.moduleSpecifier.text); expect(moduleNames).toStrictEqual([ + "@hey-api/client-fetch", "@tanstack/react-query", "../requests/services.gen", "../requests/types.gen", @@ -41,6 +42,7 @@ describe(fileName, () => { // @ts-ignore const moduleNames = imports.map((i) => i.moduleSpecifier.text); expect(moduleNames).toStrictEqual([ + "@hey-api/client-fetch", "@tanstack/react-query", "../requests/services.gen", "../requests/types.gen",