diff --git a/.changeset/salty-falcons-march.md b/.changeset/salty-falcons-march.md new file mode 100644 index 0000000..a8160b5 --- /dev/null +++ b/.changeset/salty-falcons-march.md @@ -0,0 +1,5 @@ +--- +'@softnetics/hono-react-query': minor +--- + +Introduce useQuery with DefinedUseQueryResult type and queryOptions with DefinedInitialDataOptions diff --git a/src/index.ts b/src/index.ts index d8df8f0..d41f3eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,7 +88,12 @@ function useQueryFactory>( ): UseHonoQuery { return ((path, method, honoPayload, hookOptions) => { return useQuery( - queryOptionsFactory(client)(path.toString(), method, honoPayload as any, hookOptions as any) + queryOptionsFactory(client)( + path.toString(), + method, + honoPayload as any, + hookOptions as any + ) as any ) }) as UseHonoQuery } @@ -112,7 +117,7 @@ function queryOptionsFactory>( return responseParser(response) }, ...hookOptions, - }) + }) as any } } diff --git a/src/types.ts b/src/types.ts index f7de0ce..b18867f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,8 @@ import type { + DefinedInitialDataOptions, + DefinedUseQueryResult, InvalidateQueryFilters, + UndefinedInitialDataOptions, UseMutationOptions, UseMutationResult, UseQueryOptions, @@ -57,21 +60,33 @@ export type HonoPayload = undefined extends TInput export type UseHonoQuery> = < TPath extends keyof TApp, TMethod extends keyof TApp[TPath], + TQueryOptions extends Omit< + UseQueryOptions< + SuccessResponse>, + ErrorResponse> | Error + >, + 'queryKey' | 'queryFn' + > = Omit< + UseQueryOptions< + SuccessResponse>, + ErrorResponse> | Error + >, + 'queryKey' | 'queryFn' + >, >( path: TPath, method: TMethod, honoPayload: HonoPayload>, - queryOptions?: Omit< - UseQueryOptions< + queryOptions?: TQueryOptions +) => 'initialData' extends keyof TQueryOptions + ? DefinedUseQueryResult< SuccessResponse>, ErrorResponse> | Error - >, - 'queryKey' | 'queryFn' - > -) => UseQueryResult< - SuccessResponse>, - ErrorResponse> | Error -> + > + : UseQueryResult< + SuccessResponse>, + ErrorResponse> | Error + > export type UseHonoMutation> = < TPath extends keyof TApp, @@ -100,21 +115,33 @@ export type UseHonoMutation> = < export type HonoQueryOptions> = < TPath extends keyof TApp, TMethod extends keyof TApp[TPath], + TQueryOptions extends Omit< + UndefinedInitialDataOptions< + SuccessResponse>, + ErrorResponse> | Error + >, + 'queryKey' | 'queryFn' + > = Omit< + UndefinedInitialDataOptions< + SuccessResponse>, + ErrorResponse> | Error + >, + 'queryKey' | 'queryFn' + >, >( path: TPath, method: TMethod, honoPayload: HonoPayload>, - queryOptions?: Omit< - UseQueryOptions< + queryOptions?: TQueryOptions +) => 'initialData' extends keyof TQueryOptions + ? DefinedInitialDataOptions< SuccessResponse>, ErrorResponse> | Error - >, - 'queryKey' | 'queryFn' - > -) => UseQueryOptions< - SuccessResponse>, - ErrorResponse> | Error -> + > + : UndefinedInitialDataOptions< + SuccessResponse>, + ErrorResponse> | Error + > export type HonoMutationOptions> = < TPath extends keyof TApp,