11import type { QraftServiceOperationsToken } from '@openapi-qraft/tanstack-query-react-types' ;
22import type { QueryClient } from '@tanstack/react-query' ;
3- import type { Context } from 'react' ;
43import type * as callbacks from './callbacks/index.js' ;
54import type * as operationInvokeModule from './callbacks/operationInvokeFn.js' ;
65import type {
76 OperationSchema ,
87 RequestFnInfo ,
98 RequestFnResponse ,
109} from './lib/requestFn.js' ;
11- import { useContext } from 'react' ;
1210import { createRecursiveProxy } from './lib/createRecursiveProxy.js' ;
1311
1412export interface CreateAPIBasicClientOptions {
@@ -38,57 +36,6 @@ export type CreateAPIClientOptions =
3836 | CreateAPIBasicQueryClientOptions
3937 | CreateAPIQueryClientOptions ;
4038
41- /**
42- * Creates a QueryClient compatible API Client which contains all operations
43- * such as `useQuery`, `useMutation`.
44- *
45- * @example Fetching data with QueryClient
46- * ```ts
47- * const api = qraftAPIClient(services, callbacks, {
48- * requestFn: requestFn,
49- * baseUrl: 'https://api.example.com',
50- * queryClient: new QueryClient(),
51- * });
52- *
53- * api.service.operation({
54- * parameters: { path: { id: 1 } },
55- * });
56- * ```
57- */
58- export function qraftAPIClient <
59- Services extends UnionServiceOperationsDeclaration < Services > ,
60- Callbacks extends ServiceMethods ,
61- > (
62- services : Services ,
63- callbacks : Callbacks ,
64- options :
65- | CreateAPIQueryClientOptions
66- | Context < CreateAPIQueryClientOptions | undefined >
67- ) : APIDefaultQueryClientServices < Services > ;
68-
69- /**
70- * It creates a QueryClient-compatible API client that contains all the hooks
71- * (useQuery, useMutation) and utilities for non-context operations,
72- * such as, and `getQueryKey`.
73- *
74- * @example Fetching data with QueryClient
75- * ```ts
76- * const api = qraftAPIClient(services, callbacks, context);
77- *
78- * api.service.operation.useQuery({
79- * parameters: { path: { id: 1 } },
80- * });
81- * ```
82- */
83- export function qraftAPIClient <
84- Services extends UnionServiceOperationsDeclaration < Services > ,
85- Callbacks extends PartialServiceMethods ,
86- > (
87- services : Services ,
88- callbacks : Callbacks ,
89- options : Context < CreateAPIQueryClientOptions | undefined >
90- ) : APIContextQueryClientServices < Services , Callbacks > ;
91-
9239/**
9340 * Creates a QueryClient compatible API Client which contains all operations
9441 * such as `useQuery`, `useMutation`.
@@ -216,15 +163,12 @@ export function qraftAPIClient<
216163> (
217164 services : Services ,
218165 callbacks : Callbacks ,
219- options ?:
220- | CreateAPIClientOptions
221- | Context < CreateAPIQueryClientOptions | undefined >
166+ options ?: CreateAPIClientOptions
222167) :
223168 | APIQueryClientServices < Services , Callbacks >
224169 | APIDefaultQueryClientServices < Services >
225170 | APIBasicQueryClientServices < Services , Callbacks >
226- | APIUtilityClientServices < Services , Callbacks >
227- | APIContextQueryClientServices < Services , Callbacks > {
171+ | APIUtilityClientServices < Services , Callbacks > {
228172 const stringTag = 'QraftAPIClient' ;
229173
230174 const toString = ( path : ( symbol | string ) [ ] ) : string => {
@@ -290,42 +234,27 @@ export function qraftAPIClient<
290234 if ( ! isServiceOperation ( serviceOperation ) )
291235 throw new Error ( `Service operation not found: ${ path . join ( '.' ) } ` ) ;
292236
293- const hookList = [
294- 'useInfiniteQuery' ,
295- 'useQueries' ,
296- 'useQuery' ,
297- 'useSuspenseInfiniteQuery' ,
298- 'useSuspenseQueries' ,
299- 'useSuspenseQuery' ,
300- 'useIsFetching' ,
301- 'useMutation' ,
302- 'useIsMutating' ,
303- 'useMutationState' ,
304- ] as const ;
305-
306237 if (
307238 callbackName !== 'operationInvokeFn' &&
308239 callbackName !== 'getQueryKey' &&
309240 callbackName !== 'getMutationKey' &&
310241 callbackName !== 'getInfiniteQueryKey' &&
311- ! hookList . includes ( callbackName as ( typeof hookList ) [ number ] )
242+ callbackName !== 'useInfiniteQuery' &&
243+ callbackName !== 'useQueries' &&
244+ callbackName !== 'useQuery' &&
245+ callbackName !== 'useSuspenseInfiniteQuery' &&
246+ callbackName !== 'useSuspenseQueries' &&
247+ callbackName !== 'useSuspenseQuery' &&
248+ callbackName !== 'useIsFetching' &&
249+ callbackName !== 'useMutation' &&
250+ callbackName !== 'useIsMutating' &&
251+ callbackName !== 'useMutationState'
312252 )
313253 if ( ! options || ! ( 'queryClient' in options && options . queryClient ) )
314254 throw new Error (
315255 `'qraft.<service>.<operation>.${ String ( callbackName ) } ()' requires 'queryClient' in 'createAPIClient(...)' options.`
316256 ) ;
317257
318- if (
319- options &&
320- 'Provider' in options &&
321- 'Consumer' in options &&
322- hookList . includes ( callbackName as ( typeof hookList ) [ number ] )
323- ) {
324- options = useContext ( options ) ;
325-
326- if ( ! options ) throw new Error ( 'No API Client context found' ) ;
327- }
328-
329258 // @ts -expect-error - Too complex union type
330259 return callbacks [ callbackName ] ( options , serviceOperation . schema , args ) ;
331260 } ,
@@ -434,7 +363,7 @@ export type UnionServiceOperationsDeclaration<Services> =
434363 | OperationsDeclaration < Services >
435364 | OperationDeclaration ;
436365
437- type QueryOperationHookCallbacks = Extract <
366+ export type QueryOperationHookCallbacks = Extract <
438367 keyof ServiceMethods ,
439368 | 'useInfiniteQuery'
440369 | 'useQueries'
@@ -481,7 +410,7 @@ type QueryOperationStateCallbacks =
481410 >
482411 | QueryOperationStateHookCallbacks ;
483412
484- type MutationOperationHookCallbacks = Extract <
413+ export type MutationOperationHookCallbacks = Extract <
485414 keyof ServiceMethods ,
486415 'useMutation'
487416> ;
@@ -500,7 +429,7 @@ type InvokeOperationCallback = Extract<
500429 'operationInvokeFn'
501430> ;
502431
503- type UtilityOperationCallbacks = Extract <
432+ export type UtilityOperationCallbacks = Extract <
504433 keyof ServiceMethods ,
505434 | 'getQueryKey'
506435 | 'getInfiniteQueryKey'
@@ -517,11 +446,6 @@ type OperationCallbackList =
517446 | InvokeOperationCallback
518447 | UtilityOperationCallbacks ;
519448
520- type HookOperationCallbackList =
521- | UtilityOperationCallbacks
522- | QueryOperationHookCallbacks
523- | MutationOperationHookCallbacks ;
524-
525449export type APIQueryClientServices <
526450 Services extends UnionServiceOperationsDeclaration < Services > ,
527451 Callbacks extends PartialServiceMethods ,
@@ -530,14 +454,6 @@ export type APIQueryClientServices<
530454 Extract < keyof Callbacks , OperationCallbackList >
531455> ;
532456
533- export type APIContextQueryClientServices <
534- Services extends UnionServiceOperationsDeclaration < Services > ,
535- Callbacks extends PartialServiceMethods ,
536- > = ServicesFilteredByCallbacks <
537- Services ,
538- Extract < keyof Callbacks , HookOperationCallbackList >
539- > ;
540-
541457export type APIDefaultQueryClientServices <
542458 Services extends UnionServiceOperationsDeclaration < Services > ,
543459> = Services extends OperationDeclaration
@@ -591,7 +507,7 @@ export type APIUtilityClientServices<
591507 Extract < keyof Callbacks , UtilityOperationCallbacks >
592508> ;
593509
594- type ServicesFilteredByCallbacks <
510+ export type ServicesFilteredByCallbacks <
595511 Services extends UnionServiceOperationsDeclaration < Services > ,
596512 CallbackList extends OperationCallbackList ,
597513> = Services extends OperationDeclaration
0 commit comments