@openapi-qraft/react 2.15.0-beta.4 → 2.15.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cjs/callbacks/useInfiniteQuery.cjs +4 -2
  3. package/dist/cjs/callbacks/useInfiniteQuery.cjs.map +1 -1
  4. package/dist/cjs/callbacks/useMutation.cjs +34 -16
  5. package/dist/cjs/callbacks/useMutation.cjs.map +1 -1
  6. package/dist/cjs/callbacks/useQuery.cjs +4 -2
  7. package/dist/cjs/callbacks/useQuery.cjs.map +1 -1
  8. package/dist/cjs/callbacks/useSuspenseInfiniteQuery.cjs +4 -2
  9. package/dist/cjs/callbacks/useSuspenseInfiniteQuery.cjs.map +1 -1
  10. package/dist/cjs/callbacks/useSuspenseQuery.cjs +4 -2
  11. package/dist/cjs/callbacks/useSuspenseQuery.cjs.map +1 -1
  12. package/dist/cjs/index.cjs +2 -0
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs/lib/useComposeUseQueryOptions.cjs +21 -11
  15. package/dist/cjs/lib/useComposeUseQueryOptions.cjs.map +1 -1
  16. package/dist/cjs/qraftAPIClient.cjs.map +1 -1
  17. package/dist/cjs/qraftReactAPIClient.cjs +36 -0
  18. package/dist/cjs/qraftReactAPIClient.cjs.map +1 -0
  19. package/dist/esm/callbacks/useInfiniteQuery.js +4 -2
  20. package/dist/esm/callbacks/useInfiniteQuery.js.map +1 -1
  21. package/dist/esm/callbacks/useMutation.js +34 -16
  22. package/dist/esm/callbacks/useMutation.js.map +1 -1
  23. package/dist/esm/callbacks/useQuery.js +4 -2
  24. package/dist/esm/callbacks/useQuery.js.map +1 -1
  25. package/dist/esm/callbacks/useSuspenseInfiniteQuery.js +4 -2
  26. package/dist/esm/callbacks/useSuspenseInfiniteQuery.js.map +1 -1
  27. package/dist/esm/callbacks/useSuspenseQuery.js +4 -2
  28. package/dist/esm/callbacks/useSuspenseQuery.js.map +1 -1
  29. package/dist/esm/index.js +1 -0
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/lib/useComposeUseQueryOptions.js +21 -11
  32. package/dist/esm/lib/useComposeUseQueryOptions.js.map +1 -1
  33. package/dist/esm/qraftAPIClient.js.map +1 -1
  34. package/dist/esm/qraftReactAPIClient.js +34 -0
  35. package/dist/esm/qraftReactAPIClient.js.map +1 -0
  36. package/dist/types/callbacks/useInfiniteQuery.d.ts.map +1 -1
  37. package/dist/types/callbacks/useMutation.d.ts +5 -2
  38. package/dist/types/callbacks/useMutation.d.ts.map +1 -1
  39. package/dist/types/callbacks/useQuery.d.ts.map +1 -1
  40. package/dist/types/callbacks/useSuspenseInfiniteQuery.d.ts.map +1 -1
  41. package/dist/types/callbacks/useSuspenseQuery.d.ts.map +1 -1
  42. package/dist/types/index.d.ts +1 -0
  43. package/dist/types/index.d.ts.map +1 -1
  44. package/dist/types/lib/useComposeUseQueryOptions.d.ts.map +1 -1
  45. package/dist/types/qraftAPIClient.d.ts +4 -22
  46. package/dist/types/qraftAPIClient.d.ts.map +1 -1
  47. package/dist/types/qraftReactAPIClient.d.ts +132 -0
  48. package/dist/types/qraftReactAPIClient.d.ts.map +1 -0
  49. package/package.json +6 -6
  50. package/src/callbacks/useInfiniteQuery.ts +9 -3
  51. package/src/callbacks/useMutation.ts +69 -45
  52. package/src/callbacks/useQuery.ts +8 -3
  53. package/src/callbacks/useSuspenseInfiniteQuery.ts +8 -3
  54. package/src/callbacks/useSuspenseQuery.ts +8 -3
  55. package/src/index.ts +4 -0
  56. package/src/lib/useComposeUseQueryOptions.ts +54 -27
  57. package/src/qraftAPIClient.ts +4 -30
  58. package/src/qraftReactAPIClient.ts +273 -0
@@ -36,32 +36,6 @@ export type CreateAPIClientOptions =
36
36
  | CreateAPIBasicQueryClientOptions
37
37
  | CreateAPIQueryClientOptions;
38
38
 
39
- /**
40
- * Creates a QueryClient compatible API Client which contains all operations
41
- * such as `useQuery`, `useMutation`.
42
- *
43
- * @example Fetching data with QueryClient
44
- * ```ts
45
- * const api = qraftAPIClient(services, callbacks, {
46
- * requestFn: requestFn,
47
- * baseUrl: 'https://api.example.com',
48
- * queryClient: new QueryClient(),
49
- * });
50
- *
51
- * api.service.operation({
52
- * parameters: { path: { id: 1 } },
53
- * });
54
- * ```
55
- */
56
- export function qraftAPIClient<
57
- Services extends UnionServiceOperationsDeclaration<Services>,
58
- Callbacks extends ServiceMethods,
59
- >(
60
- services: Services,
61
- callbacks: Callbacks,
62
- options: CreateAPIQueryClientOptions
63
- ): APIDefaultQueryClientServices<Services>;
64
-
65
39
  /**
66
40
  * Creates a QueryClient compatible API Client which contains all operations
67
41
  * such as `useQuery`, `useMutation`.
@@ -389,7 +363,7 @@ export type UnionServiceOperationsDeclaration<Services> =
389
363
  | OperationsDeclaration<Services>
390
364
  | OperationDeclaration;
391
365
 
392
- type QueryOperationHookCallbacks = Extract<
366
+ export type QueryOperationHookCallbacks = Extract<
393
367
  keyof ServiceMethods,
394
368
  | 'useInfiniteQuery'
395
369
  | 'useQueries'
@@ -436,7 +410,7 @@ type QueryOperationStateCallbacks =
436
410
  >
437
411
  | QueryOperationStateHookCallbacks;
438
412
 
439
- type MutationOperationHookCallbacks = Extract<
413
+ export type MutationOperationHookCallbacks = Extract<
440
414
  keyof ServiceMethods,
441
415
  'useMutation'
442
416
  >;
@@ -455,7 +429,7 @@ type InvokeOperationCallback = Extract<
455
429
  'operationInvokeFn'
456
430
  >;
457
431
 
458
- type UtilityOperationCallbacks = Extract<
432
+ export type UtilityOperationCallbacks = Extract<
459
433
  keyof ServiceMethods,
460
434
  | 'getQueryKey'
461
435
  | 'getInfiniteQueryKey'
@@ -533,7 +507,7 @@ export type APIUtilityClientServices<
533
507
  Extract<keyof Callbacks, UtilityOperationCallbacks>
534
508
  >;
535
509
 
536
- type ServicesFilteredByCallbacks<
510
+ export type ServicesFilteredByCallbacks<
537
511
  Services extends UnionServiceOperationsDeclaration<Services>,
538
512
  CallbackList extends OperationCallbackList,
539
513
  > = Services extends OperationDeclaration
@@ -0,0 +1,273 @@
1
+ 'use client';
2
+
3
+ import type { Context } from 'react';
4
+ import type * as callbacks from './callbacks/index.js';
5
+ import type { OperationSchema } from './lib/requestFn.js';
6
+ import type {
7
+ APIBasicClientServices,
8
+ APIBasicQueryClientServices,
9
+ APIDefaultQueryClientServices,
10
+ APIQueryClientServices,
11
+ APIUtilityClientServices,
12
+ CreateAPIBasicClientOptions,
13
+ CreateAPIBasicQueryClientOptions,
14
+ CreateAPIClientOptions,
15
+ CreateAPIQueryClientOptions,
16
+ MutationOperationHookCallbacks,
17
+ QueryOperationHookCallbacks,
18
+ ServicesFilteredByCallbacks,
19
+ UnionServiceOperationsDeclaration,
20
+ UtilityOperationCallbacks,
21
+ } from './qraftAPIClient.js';
22
+ import { useContext } from 'react';
23
+ import { qraftAPIClient } from './qraftAPIClient.js';
24
+
25
+ type ServiceMethods = typeof callbacks;
26
+
27
+ type PartialServiceMethods = Partial<ServiceMethods>;
28
+
29
+ /**
30
+ * Creates a QueryClient compatible API Client which contains all operations
31
+ * such as `useQuery`, `useMutation` with React Context support.
32
+ *
33
+ * @example Fetching data with QueryClient
34
+ * ```ts
35
+ * const api = qraftReactAPIClient(services, callbacks, {
36
+ * requestFn: requestFn,
37
+ * baseUrl: 'https://api.example.com',
38
+ * queryClient: new QueryClient(),
39
+ * });
40
+ *
41
+ * api.service.operation({
42
+ * parameters: { path: { id: 1 } },
43
+ * });
44
+ * ```
45
+ */
46
+ export function qraftReactAPIClient<
47
+ Services extends UnionServiceOperationsDeclaration<Services>,
48
+ Callbacks extends ServiceMethods,
49
+ >(
50
+ services: Services,
51
+ callbacks: Callbacks,
52
+ options:
53
+ | CreateAPIQueryClientOptions
54
+ | Context<CreateAPIQueryClientOptions | undefined>
55
+ ): APIDefaultQueryClientServices<Services>;
56
+
57
+ /**
58
+ * It creates a QueryClient-compatible API client that contains all the hooks
59
+ * (useQuery, useMutation) and utilities for non-context operations,
60
+ * such as `getQueryKey`.
61
+ *
62
+ * @example Fetching data with QueryClient from React Context
63
+ * ```ts
64
+ * const api = qraftReactAPIClient(services, callbacks, context);
65
+ *
66
+ * api.service.operation.useQuery({
67
+ * parameters: { path: { id: 1 } },
68
+ * });
69
+ * ```
70
+ */
71
+ export function qraftReactAPIClient<
72
+ Services extends UnionServiceOperationsDeclaration<Services>,
73
+ Callbacks extends PartialServiceMethods,
74
+ >(
75
+ services: Services,
76
+ callbacks: Callbacks,
77
+ options: Context<CreateAPIQueryClientOptions | undefined>
78
+ ): APIContextQueryClientServices<Services, Callbacks>;
79
+
80
+ /**
81
+ * Creates a QueryClient compatible API Client which contains all operations
82
+ * such as `useQuery`, `useMutation`.
83
+ *
84
+ * @example Fetching data with QueryClient
85
+ * ```ts
86
+ * const api = qraftReactAPIClient(services, callbacks, {
87
+ * requestFn: requestFn,
88
+ * baseUrl: 'https://api.example.com',
89
+ * queryClient: new QueryClient(),
90
+ * });
91
+ *
92
+ * api.service.operation({
93
+ * parameters: { path: { id: 1 } },
94
+ * });
95
+ * ```
96
+ */
97
+ export function qraftReactAPIClient<
98
+ Services extends UnionServiceOperationsDeclaration<Services>,
99
+ Callbacks extends PartialServiceMethods,
100
+ >(
101
+ services: Services,
102
+ callbacks: Callbacks,
103
+ options: CreateAPIQueryClientOptions
104
+ ): APIQueryClientServices<Services, Callbacks>;
105
+
106
+ /**
107
+ * Creates a QueryClient compatible API Client which contains
108
+ * only state management manipulations such as `useIsMutating`,
109
+ * `setQueryData`, `getQueryData` and `invalidateQueries`.
110
+ *
111
+ * @example Invalidating queries with QueryClient
112
+ * ```ts
113
+ * const api = qraftReactAPIClient(services, callbacks, {
114
+ * // an instance of QueryClient shared between all clients
115
+ * queryClient: sharedQueryClient
116
+ * });
117
+ *
118
+ * api.service.operation.invalidateQueries({
119
+ * parameters: { path: { id: 1 } },
120
+ * });
121
+ */
122
+ export function qraftReactAPIClient<
123
+ Services extends UnionServiceOperationsDeclaration<Services>,
124
+ Callbacks extends PartialServiceMethods,
125
+ >(
126
+ services: Services,
127
+ callbacks: Callbacks,
128
+ options: CreateAPIBasicQueryClientOptions
129
+ ): APIBasicQueryClientServices<Services, Callbacks>;
130
+
131
+ /**
132
+ * Creates a basic API Client which contains all hooks and methods
133
+ * that don't require an explicitly provided QueryClient.
134
+ * Hooks like `useQuery` and `useMutation` will automatically retrieve
135
+ * the QueryClient from the `<QueryClientProvider />` context.
136
+ *
137
+ * @example Fetching data with QueryClient from context
138
+ * ```ts
139
+ * const api = qraftReactAPIClient(services, callbacks, {
140
+ * requestFn: requestFn,
141
+ * baseUrl: 'https://api.example.com',
142
+ * });
143
+ *
144
+ * // QueryClient will be retrieved from React context
145
+ * api.service.operation.useQuery({
146
+ * parameters: { path: { id: 1 } },
147
+ * });
148
+ * ```
149
+ *
150
+ * @example Fetching data without QueryClient
151
+ * ```ts
152
+ * const api = qraftReactAPIClient(services, callbacks, {
153
+ * requestFn: requestFn,
154
+ * baseUrl: 'https://api.example.com',
155
+ * });
156
+ *
157
+ * api.service.operation({
158
+ * parameters: { path: { id: 1 } },
159
+ * });
160
+ * ```
161
+ */
162
+ export function qraftReactAPIClient<
163
+ Services extends UnionServiceOperationsDeclaration<Services>,
164
+ Callbacks extends PartialServiceMethods,
165
+ >(
166
+ services: Services,
167
+ callbacks: Callbacks,
168
+ options: CreateAPIBasicClientOptions
169
+ ): APIBasicClientServices<Services, Callbacks>;
170
+
171
+ /**
172
+ * Creates a utility API Client which contains utility operations
173
+ * such as `getQueryKey`, `getInfiniteQueryKey`, `getMutationKey` and state hooks
174
+ * like `useIsFetching` and `useMutationData`.
175
+ *
176
+ * @example Using state hooks
177
+ * ```ts
178
+ * const api = qraftReactAPIClient(services, callbacks);
179
+ *
180
+ * // Check if any query is currently fetching
181
+ * const isFetching = api.service.operation.useIsFetching();
182
+ * ```
183
+ *
184
+ * @example Getting query keys with utility client
185
+ * ```ts
186
+ * const api = qraftReactAPIClient(services, callbacks);
187
+ *
188
+ * api.service.operation.getQueryKey({
189
+ * parameters: { path: { id: 1 } },
190
+ * });
191
+ * ```
192
+ */
193
+ export function qraftReactAPIClient<
194
+ Services extends UnionServiceOperationsDeclaration<Services>,
195
+ Callbacks extends Pick<PartialServiceMethods, UtilityOperationCallbacks>,
196
+ >(
197
+ services: Services,
198
+ callbacks: Callbacks
199
+ ): APIUtilityClientServices<Services, Callbacks>;
200
+
201
+ export function qraftReactAPIClient<
202
+ Services extends UnionServiceOperationsDeclaration<Services>,
203
+ Callbacks extends PartialServiceMethods,
204
+ >(
205
+ services: Services,
206
+ callbacks: Callbacks,
207
+ options?:
208
+ | CreateAPIClientOptions
209
+ | Context<CreateAPIQueryClientOptions | undefined>
210
+ ):
211
+ | APIQueryClientServices<Services, Callbacks>
212
+ | APIDefaultQueryClientServices<Services>
213
+ | APIBasicQueryClientServices<Services, Callbacks>
214
+ | APIUtilityClientServices<Services, Callbacks>
215
+ | APIContextQueryClientServices<Services, Callbacks> {
216
+ if (options && 'Provider' in options && 'Consumer' in options) {
217
+ const wrappedCallbacks = wrapHookCallbacks(callbacks, options);
218
+ return qraftAPIClient(services, wrappedCallbacks) as never;
219
+ }
220
+
221
+ return qraftAPIClient(services, callbacks, options as never) as never;
222
+ }
223
+
224
+ function wrapHookCallbacks<Callbacks extends PartialServiceMethods>(
225
+ callbacks: Callbacks,
226
+ context: Context<CreateAPIQueryClientOptions | undefined>
227
+ ): Callbacks {
228
+ return Object.fromEntries(
229
+ Object.entries(callbacks).map(([callbackName, callback]) => {
230
+ if (!callbackName.startsWith('use')) return [callbackName, callback];
231
+
232
+ return [
233
+ callbackName,
234
+ wrapHookCallbackWithUseContext(context, callback as never),
235
+ ];
236
+ })
237
+ ) as Callbacks;
238
+ }
239
+
240
+ function wrapHookCallbackWithUseContext(
241
+ context: Context<CreateAPIQueryClientOptions | undefined>,
242
+ callback: (
243
+ options: CreateAPIClientOptions,
244
+ schema: OperationSchema,
245
+ args: unknown[]
246
+ ) => any
247
+ ) {
248
+ function useCallbackContext(
249
+ _options: CreateAPIQueryClientOptions | undefined,
250
+ schema: OperationSchema,
251
+ args: unknown[]
252
+ ) {
253
+ const options = useContext(context);
254
+ if (!options) throw new Error('No API Client context found');
255
+
256
+ return callback(options, schema, args);
257
+ }
258
+
259
+ return useCallbackContext;
260
+ }
261
+
262
+ export type APIContextQueryClientServices<
263
+ Services extends UnionServiceOperationsDeclaration<Services>,
264
+ Callbacks extends PartialServiceMethods,
265
+ > = ServicesFilteredByCallbacks<
266
+ Services,
267
+ Extract<keyof Callbacks, HookOperationCallbackList>
268
+ >;
269
+
270
+ type HookOperationCallbackList =
271
+ | UtilityOperationCallbacks
272
+ | QueryOperationHookCallbacks
273
+ | MutationOperationHookCallbacks;