From 84767b4635d2c9f0aba768245d933189f6872a71 Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Sat, 16 Mar 2024 00:35:33 +0100 Subject: [PATCH] chore: fix key --- packages/react/src/components/Editor.tsx | 5 +- packages/react/src/components/Export.tsx | 5 +- packages/react/src/components/File.tsx | 7 +- packages/react/src/components/Import.tsx | 5 +- packages/react/src/jsx-runtime.ts | 2 + packages/react/src/types.ts | 2 + .../mocks/petStore.yaml | 2 +- .../src/OperationGenerator.test.tsx | 6 +- .../OperationGenerator.test.tsx.snap | 169 ++++++++++++++---- 9 files changed, 161 insertions(+), 42 deletions(-) diff --git a/packages/react/src/components/Editor.tsx b/packages/react/src/components/Editor.tsx index bc32de4a6..578d8a8a5 100644 --- a/packages/react/src/components/Editor.tsx +++ b/packages/react/src/components/Editor.tsx @@ -12,6 +12,7 @@ export type EditorContextProps = { const EditorContext = createContext({ language: 'text' }) type Props = { + key?: React.Key /** * Name of the language used. * @default 'text' @@ -30,9 +31,9 @@ export function TypeScript({ children }: Omit): KubbNode { return children } -export function Editor({ language = 'text', children }: Props): KubbNode { +export function Editor({ key, language = 'text', children }: Props): KubbNode { return ( - + {children} diff --git a/packages/react/src/components/Export.tsx b/packages/react/src/components/Export.tsx index 8ee81e8ea..aaac1e40e 100644 --- a/packages/react/src/components/Export.tsx +++ b/packages/react/src/components/Export.tsx @@ -2,6 +2,7 @@ import type { KubbFile } from '@kubb/core' import type { KubbNode } from '../types.ts' type Props = KubbFile.Export & { + key?: React.Key /** * When true, it will return the generated import. * When false, it will add the import to a KubbFile instance(see fileManager) @@ -9,6 +10,6 @@ type Props = KubbFile.Export & { print?: boolean } -export function Export({ name, path, isTypeOnly, asAlias, print }: Props): KubbNode { - return +export function Export({ key, name, path, isTypeOnly, asAlias, print }: Props): KubbNode { + return } diff --git a/packages/react/src/components/File.tsx b/packages/react/src/components/File.tsx index b41fa22f3..bb9ae971a 100644 --- a/packages/react/src/components/File.tsx +++ b/packages/react/src/components/File.tsx @@ -5,6 +5,7 @@ import type { KubbFile } from '@kubb/core' import type { KubbNode } from '../types.ts' type BasePropsWithBaseName = { + key?: React.Key /** * Name to be used to dynamicly create the baseName(based on input.path). * Based on UNIX basename @@ -18,6 +19,7 @@ type BasePropsWithBaseName = { } type BasePropsWithoutBaseName = { + key?: React.Key baseName?: never /** * Path will be full qualified path to a specified file. @@ -72,6 +74,7 @@ type FileSourceUnionProps = { } type FileSourceProps = FileSourceUnionProps & { + key?: React.Key /** * When true, it will return the generated import. * When false, it will add the import to a KubbFile instance(see fileManager). @@ -79,9 +82,9 @@ type FileSourceProps = FileSourceUnionProps & { print?: boolean } -function FileSource({ path, print, children }: FileSourceProps): KubbNode { +function FileSource({ key, path, print, children }: FileSourceProps): KubbNode { return ( - + {children} ) diff --git a/packages/react/src/components/Import.tsx b/packages/react/src/components/Import.tsx index c3990c812..ac8a15c74 100644 --- a/packages/react/src/components/Import.tsx +++ b/packages/react/src/components/Import.tsx @@ -2,6 +2,7 @@ import type { KubbFile } from '@kubb/core' import type { KubbNode } from '../types.ts' type Props = KubbFile.Import & { + key?: React.Key /** * When true, it will return the generated import. * When false, it will add the import to a KubbFile instance(see fileManager). @@ -9,6 +10,6 @@ type Props = KubbFile.Import & { print?: boolean } -export function Import({ name, root, path, isTypeOnly, isNameSpace, print }: Props): KubbNode { - return +export function Import({ key, name, root, path, isTypeOnly, isNameSpace, print }: Props): KubbNode { + return } diff --git a/packages/react/src/jsx-runtime.ts b/packages/react/src/jsx-runtime.ts index c18aee3b0..689cfc8c2 100644 --- a/packages/react/src/jsx-runtime.ts +++ b/packages/react/src/jsx-runtime.ts @@ -40,10 +40,12 @@ declare global { } 'kubb-import': KubbFile.Import & { + key?: Key print?: boolean } 'kubb-export': KubbFile.Export & { + key?: Key print?: boolean } diff --git a/packages/react/src/types.ts b/packages/react/src/types.ts index 8ba52a154..1a7b0fb95 100644 --- a/packages/react/src/types.ts +++ b/packages/react/src/types.ts @@ -35,10 +35,12 @@ declare global { } 'kubb-import': KubbFile.Import & { + key?: Key print?: boolean } 'kubb-export': KubbFile.Export & { + key?: Key print?: boolean } diff --git a/packages/swagger-tanstack-query/mocks/petStore.yaml b/packages/swagger-tanstack-query/mocks/petStore.yaml index 0c2b67859..9c4bce638 100644 --- a/packages/swagger-tanstack-query/mocks/petStore.yaml +++ b/packages/swagger-tanstack-query/mocks/petStore.yaml @@ -8,7 +8,7 @@ info: That way, with time, we can improve the API in general, and expose some of the new features in OAS3. Some useful links: - - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore) + - [The Pet Store repository](httppetss://github.com/swagger-api/swagger-petstore) - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml) termsOfService: http://swagger.io/terms/ diff --git a/packages/swagger-tanstack-query/src/OperationGenerator.test.tsx b/packages/swagger-tanstack-query/src/OperationGenerator.test.tsx index f0c4f65f6..60f057974 100644 --- a/packages/swagger-tanstack-query/src/OperationGenerator.test.tsx +++ b/packages/swagger-tanstack-query/src/OperationGenerator.test.tsx @@ -52,7 +52,7 @@ describe('OperationGenerator', async () => { override: undefined, }, ) - const operation = oas.operation('/pets/{uuid}', 'get') + const operation = oas.operation('/pet/{petId}', 'get') const files = await og.get(operation, options) as KubbFile.File[] files.forEach(file => { @@ -92,7 +92,7 @@ describe('OperationGenerator', async () => { override: undefined, }, ) - const operation = oas.operation('/pets/{pet_id}', 'get') + const operation = oas.operation('/pet/{petId}', 'get') const files = await og.get(operation, options) as KubbFile.File[] files.forEach(file => { @@ -135,7 +135,7 @@ describe('OperationGenerator', async () => { override: undefined, }, ) - const operation = oas.operation('/pets', 'post') + const operation = oas.operation('/pet', 'post') const files = await og.post(operation, options) as KubbFile.File[] files.forEach(file => { diff --git a/packages/swagger-tanstack-query/src/__snapshots__/OperationGenerator.test.tsx.snap b/packages/swagger-tanstack-query/src/__snapshots__/OperationGenerator.test.tsx.snap index 5f793e018..c47335269 100644 --- a/packages/swagger-tanstack-query/src/__snapshots__/OperationGenerator.test.tsx.snap +++ b/packages/swagger-tanstack-query/src/__snapshots__/OperationGenerator.test.tsx.snap @@ -57,33 +57,33 @@ export function deletePet(options: { exports[`OperationGenerator > [GET] should generate with pathParamsType \`inline\` 1`] = ` "import client from "@kubb/swagger-client/client"; import { useQuery } from "@tanstack/react-query"; -import type { GetPetsUuidQueryResponse } from "./"; +import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from "./"; import type { UseBaseQueryOptions, UseQueryResult, QueryKey, WithRequired } from "@tanstack/react-query"; - type GetPetsUuidClient = typeof client; -type GetPetsUuid = { - data: GetPetsUuidQueryResponse; - error: never; + type GetPetByIdClient = typeof client; +type GetPetById = { + data: GetPetByIdQueryResponse; + error: GetPetById400 | GetPetById404; request: never; - pathParams: never; + pathParams: GetPetByIdPathParams; queryParams: never; headerParams: never; - response: GetPetsUuidQueryResponse; + response: GetPetByIdQueryResponse; client: { - parameters: Partial[0]>; - return: Awaited>; + parameters: Partial[0]>; + return: Awaited>; }; }; -export const GetPetsUuidQueryKey = () => [{ url: "/pets/:uuid", params: { uuid: uuid } }] as const; -export type GetPetsUuidQueryKey = ReturnType; -export function GetPetsUuidQueryOptions(options: GetPetsUuid["client"]["parameters"] = {}): WithRequired, "queryKey"> { - const queryKey = GetPetsUuidQueryKey(); +export const GetPetByIdQueryKey = (petId: GetPetByIdPathParams["petId"]) => [{ url: "/pet/:petId", params: { petId: petId } }] as const; +export type GetPetByIdQueryKey = ReturnType; +export function GetPetByIdQueryOptions(petId: GetPetByIdPathParams["petId"], options: GetPetById["client"]["parameters"] = {}): WithRequired, "queryKey"> { + const queryKey = GetPetByIdQueryKey(petId); return { queryKey, queryFn: async () => { - const res = await client({ + const res = await client({ method: "get", - url: \`/pets/\${uuid}\`, + url: \`/pet/\${petId}\`, ...options }); return res.data; @@ -91,25 +91,83 @@ export function GetPetsUuidQueryOptions (options); -{ - query ? : Partial>, - client ? : GetPetsUuid["client"]["parameters"]; + * @description Returns a single pet + * @summary Find pet by ID + * @link /pet/:petId */ +export function getPetById(petId: GetPetByIdPathParams["petId"], options: { + query?: Partial>; + client?: GetPetById["client"]["parameters"]; +} = {}): UseQueryResult & { + queryKey: TQueryKey; +} { + const { query: queryOptions, client: clientOptions = {} } = options ?? {}; + const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey(petId); + const query = useQuery({ + ...GetPetByIdQueryOptions(petId, clientOptions), + queryKey, + ...queryOptions + }) as UseQueryResult & { + queryKey: TQueryKey; + }; + query.queryKey = queryKey as TQueryKey; + return query; } -{ } -UseQueryResult & { queryKey: TQueryKey }; -{ +" +`; + +exports[`OperationGenerator > [GET] should generate with pathParamsType \`object\` 1`] = ` +"import client from "@kubb/swagger-client/client"; +import { useQuery } from "@tanstack/react-query"; +import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from "./"; +import type { UseBaseQueryOptions, UseQueryResult, QueryKey, WithRequired } from "@tanstack/react-query"; + + type GetPetByIdClient = typeof client; +type GetPetById = { + data: GetPetByIdQueryResponse; + error: GetPetById400 | GetPetById404; + request: never; + pathParams: GetPetByIdPathParams; + queryParams: never; + headerParams: never; + response: GetPetByIdQueryResponse; + client: { + parameters: Partial[0]>; + return: Awaited>; + }; +}; +export const GetPetByIdQueryKey = ({ petId }: GetPetByIdPathParams) => [{ url: "/pet/:petId", params: { petId: petId } }] as const; +export type GetPetByIdQueryKey = ReturnType; +export function GetPetByIdQueryOptions({ petId }: GetPetByIdPathParams, options: GetPetById["client"]["parameters"] = {}): WithRequired, "queryKey"> { + const queryKey = GetPetByIdQueryKey(petId); + return { + queryKey, + queryFn: async () => { + const res = await client({ + method: "get", + url: \`/pet/\${petId}\`, + ...options + }); + return res.data; + }, + }; +} +/** + * @description Returns a single pet + * @summary Find pet by ID + * @link /pet/:petId */ +export function getPetById({ petId }: GetPetByIdPathParams, options: { + query?: Partial>; + client?: GetPetById["client"]["parameters"]; +} = {}): UseQueryResult & { + queryKey: TQueryKey; +} { const { query: queryOptions, client: clientOptions = {} } = options ?? {}; - const queryKey = queryOptions?.queryKey ?? GetPetsUuidQueryKey(); - const query = useQuery({ - ...GetPetsUuidQueryOptions(clientOptions), + const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey(petId); + const query = useQuery({ + ...GetPetByIdQueryOptions({ petId }, GetPetByIdPathParams, clientOptions), queryKey, ...queryOptions - }) as UseQueryResult & { + }) as UseQueryResult & { queryKey: TQueryKey; }; query.queryKey = queryKey as TQueryKey; @@ -117,3 +175,54 @@ UseQueryResult & { queryKey: TQueryKey }; } " `; + +exports[`OperationGenerator > [POST] should generate with variablesType \`mutate\` 1`] = ` +"import client from "@kubb/swagger-client/client"; +import { useMutation } from "@tanstack/react-query"; +import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from "./"; +import type { UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; + + type AddPetClient = typeof client; +type AddPet = { + data: AddPetMutationResponse; + error: AddPet405; + request: AddPetMutationRequest; + pathParams: never; + queryParams: never; + headerParams: never; + response: AddPetMutationResponse; + client: { + parameters: Partial[0]>; + return: Awaited>; + }; +}; +/** + * @description Add a new pet to the store + * @summary Add a new pet to the store + * @link /pet */ +export function addPet(options: { + mutation?: UseMutationOptions; + client?: AddPet["client"]["parameters"]; +} = {}): UseMutationResult { + const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}; + return useMutation({ + mutationFn: async ({ data }) => { + const res = await client({ + method: "post", + url: \`/pet\`, + data, + ...clientOptions + }); + return res.data; + }, + ...mutationOptions + }); +} +" +`;