From 01574bcb41694d11b928f7d1f3723777001b6b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Arabac=C4=B1?= Date: Tue, 30 Aug 2022 22:14:34 +0300 Subject: [PATCH] feat: select support added for react-query components (#83) * select support added for react-query * typo fix * fix: missing TSelectData on UseQueryOptions added * infer fetcher response type and support for blob * fix: use TData instead of TSelectData and infer queryResult * merge error fixed --- .../generateReactQueryComponents.test.ts | 10 +++++----- .../generators/generateReactQueryComponents.ts | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/plugins/typescript/src/generators/generateReactQueryComponents.test.ts b/plugins/typescript/src/generators/generateReactQueryComponents.test.ts index dd5b9ebb..ebabd1da 100644 --- a/plugins/typescript/src/generators/generateReactQueryComponents.test.ts +++ b/plugins/typescript/src/generators/generateReactQueryComponents.test.ts @@ -109,7 +109,7 @@ describe("generateReactQueryComponents", () => { /** * Get all the pets */ - export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { + export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { ...options, ...queryOptions }); }; @@ -231,7 +231,7 @@ describe("generateReactQueryComponents", () => { /** * Get all the pets */ - export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { + export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { ...options, ...queryOptions }); }; @@ -357,7 +357,7 @@ describe("generateReactQueryComponents", () => { /** * Get all the pets */ - export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { + export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { ...options, ...queryOptions }); }; @@ -454,7 +454,7 @@ describe("generateReactQueryComponents", () => { /** * Get all the pets */ - export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { + export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { ...options, ...queryOptions }); }; @@ -1162,7 +1162,7 @@ describe("generateReactQueryComponents", () => { /** * Get all the pets */ - export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { + export const useListPets = (variables: ListPetsVariables, options?: Omit, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), { ...options, ...queryOptions }); }; diff --git a/plugins/typescript/src/generators/generateReactQueryComponents.ts b/plugins/typescript/src/generators/generateReactQueryComponents.ts index f73d0274..a59ebe0c 100644 --- a/plugins/typescript/src/generators/generateReactQueryComponents.ts +++ b/plugins/typescript/src/generators/generateReactQueryComponents.ts @@ -462,7 +462,7 @@ const createQueryHook = ({ undefined, f.createArrowFunction( undefined, - undefined, + [f.createTypeParameterDeclaration("TData", undefined, dataType)], [ f.createParameterDeclaration( undefined, @@ -527,7 +527,14 @@ const createQueryHook = ({ f.createIdentifier("reactQuery"), f.createIdentifier("useQuery") ), - [dataType, errorType, dataType], + [ + dataType, + errorType, + f.createTypeReferenceNode( + f.createIdentifier("TData"), + [] + ), + ], [ f.createCallExpression( f.createIdentifier("queryKeyFn"), @@ -608,7 +615,11 @@ const createUseQueryOptionsType = ( f.createIdentifier("reactQuery"), f.createIdentifier("UseQueryOptions") ), - [dataType, errorType, dataType] + [ + dataType, + errorType, + f.createTypeReferenceNode(f.createIdentifier("TData"), []), + ] ), f.createUnionTypeNode([ f.createLiteralTypeNode(f.createStringLiteral("queryKey")),