@ic-reactor/react 3.12.5 → 4.0.0-beta.1

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 (139) hide show
  1. package/README.md +236 -423
  2. package/dist/index.d.ts +259 -12
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +470 -17
  5. package/dist/index.js.map +1 -1
  6. package/llms.txt +82 -54
  7. package/package.json +12 -28
  8. package/src/index.tsx +611 -0
  9. package/dist/auth/authentication-manager.d.ts +0 -123
  10. package/dist/auth/authentication-manager.d.ts.map +0 -1
  11. package/dist/auth/authentication-manager.js +0 -767
  12. package/dist/auth/authentication-manager.js.map +0 -1
  13. package/dist/auth/constants.d.ts +0 -24
  14. package/dist/auth/constants.d.ts.map +0 -1
  15. package/dist/auth/constants.js +0 -24
  16. package/dist/auth/constants.js.map +0 -1
  17. package/dist/auth/createIdentityAttributeHooks.d.ts +0 -14
  18. package/dist/auth/createIdentityAttributeHooks.d.ts.map +0 -1
  19. package/dist/auth/createIdentityAttributeHooks.js +0 -106
  20. package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
  21. package/dist/auth/identity-attributes-manager.d.ts +0 -26
  22. package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
  23. package/dist/auth/identity-attributes-manager.js +0 -107
  24. package/dist/auth/identity-attributes-manager.js.map +0 -1
  25. package/dist/auth/identity-attributes.d.ts +0 -19
  26. package/dist/auth/identity-attributes.d.ts.map +0 -1
  27. package/dist/auth/identity-attributes.js +0 -170
  28. package/dist/auth/identity-attributes.js.map +0 -1
  29. package/dist/auth/index.d.ts +0 -8
  30. package/dist/auth/index.d.ts.map +0 -1
  31. package/dist/auth/index.js +0 -8
  32. package/dist/auth/index.js.map +0 -1
  33. package/dist/auth/local-ii-probe.d.ts +0 -46
  34. package/dist/auth/local-ii-probe.d.ts.map +0 -1
  35. package/dist/auth/local-ii-probe.js +0 -102
  36. package/dist/auth/local-ii-probe.js.map +0 -1
  37. package/dist/auth/types.d.ts +0 -179
  38. package/dist/auth/types.d.ts.map +0 -1
  39. package/dist/auth/types.js +0 -2
  40. package/dist/auth/types.js.map +0 -1
  41. package/dist/createActorHooks.d.ts +0 -52
  42. package/dist/createActorHooks.d.ts.map +0 -1
  43. package/dist/createActorHooks.js +0 -17
  44. package/dist/createActorHooks.js.map +0 -1
  45. package/dist/createInfiniteQuery.d.ts +0 -144
  46. package/dist/createInfiniteQuery.d.ts.map +0 -1
  47. package/dist/createInfiniteQuery.js +0 -174
  48. package/dist/createInfiniteQuery.js.map +0 -1
  49. package/dist/createMutation.d.ts +0 -30
  50. package/dist/createMutation.d.ts.map +0 -1
  51. package/dist/createMutation.js +0 -192
  52. package/dist/createMutation.js.map +0 -1
  53. package/dist/createQuery.d.ts +0 -30
  54. package/dist/createQuery.d.ts.map +0 -1
  55. package/dist/createQuery.js +0 -117
  56. package/dist/createQuery.js.map +0 -1
  57. package/dist/createSuspenseInfiniteQuery.d.ts +0 -147
  58. package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
  59. package/dist/createSuspenseInfiniteQuery.js +0 -177
  60. package/dist/createSuspenseInfiniteQuery.js.map +0 -1
  61. package/dist/createSuspenseQuery.d.ts +0 -25
  62. package/dist/createSuspenseQuery.d.ts.map +0 -1
  63. package/dist/createSuspenseQuery.js +0 -111
  64. package/dist/createSuspenseQuery.js.map +0 -1
  65. package/dist/defineReactor.d.ts +0 -125
  66. package/dist/defineReactor.d.ts.map +0 -1
  67. package/dist/defineReactor.js +0 -180
  68. package/dist/defineReactor.js.map +0 -1
  69. package/dist/hooks/createAuthHooks.d.ts +0 -43
  70. package/dist/hooks/createAuthHooks.d.ts.map +0 -1
  71. package/dist/hooks/createAuthHooks.js +0 -131
  72. package/dist/hooks/createAuthHooks.js.map +0 -1
  73. package/dist/hooks/index.d.ts +0 -21
  74. package/dist/hooks/index.d.ts.map +0 -1
  75. package/dist/hooks/index.js +0 -24
  76. package/dist/hooks/index.js.map +0 -1
  77. package/dist/hooks/useActorInfiniteQuery.d.ts +0 -39
  78. package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
  79. package/dist/hooks/useActorInfiniteQuery.js +0 -56
  80. package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
  81. package/dist/hooks/useActorMethod.d.ts +0 -115
  82. package/dist/hooks/useActorMethod.d.ts.map +0 -1
  83. package/dist/hooks/useActorMethod.js +0 -250
  84. package/dist/hooks/useActorMethod.js.map +0 -1
  85. package/dist/hooks/useActorMutation.d.ts +0 -48
  86. package/dist/hooks/useActorMutation.d.ts.map +0 -1
  87. package/dist/hooks/useActorMutation.js +0 -69
  88. package/dist/hooks/useActorMutation.js.map +0 -1
  89. package/dist/hooks/useActorQuery.d.ts +0 -32
  90. package/dist/hooks/useActorQuery.d.ts.map +0 -1
  91. package/dist/hooks/useActorQuery.js +0 -46
  92. package/dist/hooks/useActorQuery.js.map +0 -1
  93. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -39
  94. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
  95. package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -56
  96. package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +0 -1
  97. package/dist/hooks/useActorSuspenseQuery.d.ts +0 -32
  98. package/dist/hooks/useActorSuspenseQuery.d.ts.map +0 -1
  99. package/dist/hooks/useActorSuspenseQuery.js +0 -47
  100. package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
  101. package/dist/types.d.ts +0 -270
  102. package/dist/types.d.ts.map +0 -1
  103. package/dist/types.js +0 -5
  104. package/dist/types.js.map +0 -1
  105. package/dist/utils.d.ts +0 -51
  106. package/dist/utils.d.ts.map +0 -1
  107. package/dist/utils.js +0 -105
  108. package/dist/utils.js.map +0 -1
  109. package/dist/validation.d.ts +0 -131
  110. package/dist/validation.d.ts.map +0 -1
  111. package/dist/validation.js +0 -125
  112. package/dist/validation.js.map +0 -1
  113. package/src/auth/authentication-manager.ts +0 -860
  114. package/src/auth/constants.ts +0 -32
  115. package/src/auth/createIdentityAttributeHooks.ts +0 -143
  116. package/src/auth/identity-attributes-manager.ts +0 -131
  117. package/src/auth/identity-attributes.ts +0 -270
  118. package/src/auth/index.ts +0 -7
  119. package/src/auth/local-ii-probe.ts +0 -147
  120. package/src/auth/types.ts +0 -200
  121. package/src/createActorHooks.ts +0 -220
  122. package/src/createInfiniteQuery.ts +0 -578
  123. package/src/createMutation.ts +0 -341
  124. package/src/createQuery.ts +0 -237
  125. package/src/createSuspenseInfiniteQuery.ts +0 -601
  126. package/src/createSuspenseQuery.ts +0 -234
  127. package/src/defineReactor.ts +0 -324
  128. package/src/hooks/createAuthHooks.ts +0 -189
  129. package/src/hooks/index.ts +0 -103
  130. package/src/hooks/useActorInfiniteQuery.ts +0 -205
  131. package/src/hooks/useActorMethod.ts +0 -507
  132. package/src/hooks/useActorMutation.ts +0 -205
  133. package/src/hooks/useActorQuery.ts +0 -124
  134. package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -205
  135. package/src/hooks/useActorSuspenseQuery.ts +0 -132
  136. package/src/index.ts +0 -23
  137. package/src/types.ts +0 -499
  138. package/src/utils.ts +0 -121
  139. package/src/validation.ts +0 -202
@@ -1,578 +0,0 @@
1
- /**
2
- * Infinite Query Factory - Generic wrapper for React Query paginated canister data
3
- *
4
- * Creates unified fetch/hook/invalidate functions for any paginated canister method.
5
- * Works with any Reactor instance.
6
- *
7
- * @example
8
- * const postsQuery = createInfiniteQuery(reactor, {
9
- * functionName: "get_posts",
10
- * initialPageParam: 0,
11
- * getArgs: (cursor) => [{ cursor, limit: 10 }],
12
- * getNextPageParam: (lastPage) => lastPage.nextCursor,
13
- * })
14
- *
15
- * // In component
16
- * const { data, fetchNextPage, hasNextPage } = postsQuery.useInfiniteQuery()
17
- *
18
- * // Flatten all pages
19
- * const allPosts = data?.pages.flatMap(page => page.posts)
20
- *
21
- * // Invalidate cache
22
- * postsQuery.invalidate()
23
- */
24
-
25
- import { useMemo } from "react"
26
- import type {
27
- Reactor,
28
- FunctionName,
29
- ReactorArgs,
30
- BaseActor,
31
- TransformKey,
32
- ReactorReturnOk,
33
- ReactorQueryData,
34
- ReactorReturnErr,
35
- } from "@ic-reactor/core"
36
- import {
37
- QueryKey,
38
- useInfiniteQuery,
39
- InfiniteData,
40
- UseInfiniteQueryResult,
41
- UseInfiniteQueryOptions,
42
- QueryFunctionContext,
43
- FetchInfiniteQueryOptions,
44
- InfiniteQueryObserverOptions,
45
- } from "@tanstack/react-query"
46
- import { CallConfig } from "@icp-sdk/core/agent"
47
- import { NoInfer } from "./types.js"
48
- import {
49
- buildChainedSelect,
50
- mergeFactoryQueryKey,
51
- normalizeQueryData,
52
- } from "./utils.js"
53
-
54
- type InfiniteQueryFactoryFn<
55
- Service,
56
- Method extends FunctionName<Service>,
57
- Transform extends TransformKey,
58
- TPageParam,
59
- Selected,
60
- > = {
61
- (
62
- getArgs: (pageParam: TPageParam) => ReactorArgs<Service, Method, Transform>
63
- ): InfiniteQueryResult<
64
- InfiniteQueryPageData<Service, Method, Transform>,
65
- TPageParam,
66
- Selected,
67
- InfiniteQueryError<Service, Method, Transform>
68
- >
69
- }
70
-
71
- // ============================================================================
72
- // Type Definitions
73
- // ============================================================================
74
-
75
- /** The raw page data type returned by the query function */
76
- export type InfiniteQueryPageData<
77
- Service = BaseActor,
78
- Method extends FunctionName<Service> = FunctionName<Service>,
79
- Transform extends TransformKey = "candid",
80
- > = ReactorQueryData<ReactorReturnOk<Service, Method, Transform>>
81
-
82
- /** The error type for infinite queries */
83
- export type InfiniteQueryError<
84
- Service = BaseActor,
85
- Method extends FunctionName<Service> = FunctionName<Service>,
86
- Transform extends TransformKey = "candid",
87
- > = ReactorReturnErr<Service, Method, Transform>
88
-
89
- // ============================================================================
90
- // Configuration Types
91
- // ============================================================================
92
-
93
- /**
94
- * Configuration for createActorInfiniteQuery.
95
- * Extends InfiniteQueryObserverOptions to accept standard TanStack Query
96
- * infinite-query options at the create level (e.g. refetchInterval,
97
- * refetchOnMount, refetchOnWindowFocus, retry, gcTime, networkMode).
98
- *
99
- * @template Service - The actor interface type
100
- * @template Method - The method name on the actor
101
- * @template Transform - The transformation key (identity, display, etc.)
102
- * @template TPageParam - The type of the page parameter
103
- * @template Selected - The type returned after select transformation
104
- */
105
- export interface InfiniteQueryConfig<
106
- Service = BaseActor,
107
- Method extends FunctionName<Service> = FunctionName<Service>,
108
- Transform extends TransformKey = "candid",
109
- TPageParam = unknown,
110
- Selected = InfiniteData<
111
- InfiniteQueryPageData<Service, Method, Transform>,
112
- TPageParam
113
- >,
114
- > extends Omit<
115
- InfiniteQueryObserverOptions<
116
- InfiniteQueryPageData<Service, Method, Transform>,
117
- InfiniteQueryError<Service, Method, Transform>,
118
- Selected,
119
- QueryKey,
120
- TPageParam
121
- >,
122
- "queryKey" | "queryFn"
123
- > {
124
- /** The method to call on the canister */
125
- functionName: Method
126
- /** Call configuration for the actor method */
127
- callConfig?: CallConfig
128
- /** Custom query key (optional, auto-generated if not provided) */
129
- queryKey?: QueryKey
130
- /** Initial page parameter */
131
- initialPageParam: TPageParam
132
- /** Function to get args from page parameter */
133
- getArgs: (pageParam: TPageParam) => ReactorArgs<Service, Method, Transform>
134
- /**
135
- * Narrows what the cache key derives from the call arguments.
136
- *
137
- * By default the key is scoped by `getArgs(initialPageParam)`, so two
138
- * infinite queries on the same method with different arguments stay in
139
- * separate cache entries. Supply this when those args embed the cursor and
140
- * only part of them identifies the query — return the stable, serializable
141
- * portion (typically everything except the pagination field).
142
- */
143
- getKeyArgs?: (args: ReactorArgs<Service, Method, Transform>) => unknown
144
- /** Function to determine next page parameter */
145
- getNextPageParam: (
146
- lastPage: InfiniteQueryPageData<Service, Method, Transform>,
147
- allPages: InfiniteQueryPageData<Service, Method, Transform>[],
148
- lastPageParam: TPageParam,
149
- allPageParams: TPageParam[]
150
- ) => TPageParam | undefined | null
151
- /** Function to determine previous page parameter (for bi-directional) */
152
- getPreviousPageParam?: (
153
- firstPage: InfiniteQueryPageData<Service, Method, Transform>,
154
- allPages: InfiniteQueryPageData<Service, Method, Transform>[],
155
- firstPageParam: TPageParam,
156
- allPageParams: TPageParam[]
157
- ) => TPageParam | undefined | null
158
- }
159
-
160
- /**
161
- * Configuration for createActorInfiniteQueryFactory (without initialPageParam, getArgs determined at call time).
162
- */
163
- export type InfiniteQueryFactoryConfig<
164
- Service = BaseActor,
165
- Method extends FunctionName<Service> = FunctionName<Service>,
166
- Transform extends TransformKey = "candid",
167
- TPageParam = unknown,
168
- Selected = InfiniteData<
169
- InfiniteQueryPageData<Service, Method, Transform>,
170
- TPageParam
171
- >,
172
- > = Omit<
173
- // `getArgs` is supplied per factory call. `getKeyArgs` is inherited from
174
- // InfiniteQueryConfig, which now applies it for every infinite query rather
175
- // than only for factory-created ones.
176
- InfiniteQueryConfig<Service, Method, Transform, TPageParam, Selected>,
177
- "getArgs"
178
- >
179
-
180
- // ============================================================================
181
- // Hook Interface
182
- // ============================================================================
183
-
184
- /**
185
- * useInfiniteQuery hook with chained select support.
186
- */
187
- export interface UseInfiniteQueryWithSelect<
188
- TPageData,
189
- TPageParam,
190
- Selected = InfiniteData<TPageData, TPageParam>,
191
- TError = Error,
192
- > {
193
- // Overload 1: Without select - returns Selected
194
- (
195
- options?: Omit<
196
- UseInfiniteQueryOptions<
197
- TPageData,
198
- TError,
199
- Selected,
200
- QueryKey,
201
- TPageParam
202
- >,
203
- | "select"
204
- | "queryKey"
205
- | "queryFn"
206
- | "initialPageParam"
207
- | "getNextPageParam"
208
- | "getPreviousPageParam"
209
- >
210
- ): UseInfiniteQueryResult<Selected, TError>
211
-
212
- // Overload 2: With select - chains on top and returns TFinal
213
- <TFinal = Selected>(
214
- options: Omit<
215
- UseInfiniteQueryOptions<TPageData, TError, TFinal, QueryKey, TPageParam>,
216
- | "queryKey"
217
- | "queryFn"
218
- | "select"
219
- | "initialPageParam"
220
- | "getNextPageParam"
221
- | "getPreviousPageParam"
222
- > & {
223
- select: (data: Selected) => TFinal
224
- }
225
- ): UseInfiniteQueryResult<TFinal, TError>
226
- }
227
-
228
- // ============================================================================
229
- // Result Interface
230
- // ============================================================================
231
-
232
- /**
233
- * Result from createActorInfiniteQuery
234
- *
235
- * @template TPageData - The raw page data type
236
- * @template TPageParam - The page parameter type
237
- * @template Selected - The type after select transformation
238
- * @template TError - The error type
239
- */
240
- export interface InfiniteQueryResult<
241
- TPageData,
242
- TPageParam,
243
- Selected = InfiniteData<TPageData, TPageParam>,
244
- TError = Error,
245
- > {
246
- /** Fetch first page in loader (uses ensureQueryData for cache-first) */
247
- fetch: () => Promise<Selected>
248
-
249
- /** React hook for components - supports pagination */
250
- useInfiniteQuery: UseInfiniteQueryWithSelect<
251
- TPageData,
252
- TPageParam,
253
- Selected,
254
- TError
255
- >
256
-
257
- /** Invalidate the cache (refetches if query is active) */
258
- invalidate: () => Promise<void>
259
-
260
- /** Get query key (for advanced React Query usage) */
261
- getQueryKey: () => QueryKey
262
-
263
- /**
264
- * Read data directly from cache without fetching.
265
- * Returns undefined if data is not in cache.
266
- */
267
- getCacheData: {
268
- (): Selected | undefined
269
- <TFinal>(select: (data: Selected) => TFinal): TFinal | undefined
270
- }
271
- }
272
-
273
- // ============================================================================
274
- // Internal Implementation
275
- // ============================================================================
276
-
277
- const createInfiniteQueryImpl = <
278
- Service,
279
- Method extends FunctionName<Service> = FunctionName<Service>,
280
- Transform extends TransformKey = "candid",
281
- TPageParam = unknown,
282
- Selected = InfiniteData<
283
- InfiniteQueryPageData<Service, Method, Transform>,
284
- TPageParam
285
- >,
286
- >(
287
- reactor: Reactor<Service, Transform>,
288
- config: InfiniteQueryConfig<Service, Method, Transform, TPageParam, Selected>
289
- ): InfiniteQueryResult<
290
- InfiniteQueryPageData<Service, Method, Transform>,
291
- TPageParam,
292
- Selected,
293
- InfiniteQueryError<Service, Method, Transform>
294
- > => {
295
- type TPageData = InfiniteQueryPageData<Service, Method, Transform>
296
- type TError = InfiniteQueryError<Service, Method, Transform>
297
- type TInfiniteData = InfiniteData<TPageData, TPageParam>
298
-
299
- const {
300
- functionName,
301
- callConfig,
302
- queryKey: customQueryKey,
303
- initialPageParam,
304
- getArgs,
305
- getKeyArgs,
306
- getNextPageParam,
307
- getPreviousPageParam,
308
- maxPages,
309
- staleTime = 5 * 60 * 1000,
310
- select,
311
- ...rest
312
- } = config
313
-
314
- // Fold the call arguments into the key. Without them every infinite query on
315
- // a method shares one cache entry, so two lists with different `getArgs` (a
316
- // different page size, a different filter) serve each other's pages — the
317
- // arguments live in a closure, not in the config, so nothing else
318
- // distinguishes them.
319
- //
320
- // The first page's args stand for the query's identity; `getKeyArgs` lets a
321
- // caller drop the cursor field when it is part of those args.
322
- const getQueryKey = (): QueryKey => {
323
- const initialArgs = getArgs(initialPageParam)
324
- const keyArgs = getKeyArgs?.(initialArgs) ?? initialArgs
325
-
326
- return reactor.generateQueryKey(
327
- {
328
- functionName,
329
- queryKey: mergeFactoryQueryKey(customQueryKey, undefined, keyArgs),
330
- },
331
- callConfig
332
- )
333
- }
334
-
335
- // Query function - accepts QueryFunctionContext
336
- const queryFn = async (
337
- context: QueryFunctionContext<QueryKey, TPageParam>
338
- ): Promise<TPageData> => {
339
- // pageParam is typed as unknown in QueryFunctionContext, but we know its type
340
- const pageParam = context.pageParam as TPageParam
341
- const args = getArgs(pageParam)
342
- const result = await reactor.callMethod({
343
- functionName,
344
- args,
345
- callConfig,
346
- })
347
- return normalizeQueryData<ReactorReturnOk<Service, Method, Transform>>(
348
- result as ReactorReturnOk<Service, Method, Transform>
349
- )
350
- }
351
-
352
- // Get infinite query options for fetchInfiniteQuery
353
- const getInfiniteQueryOptions = (): FetchInfiniteQueryOptions<
354
- TPageData,
355
- TError,
356
- TPageData,
357
- QueryKey,
358
- TPageParam
359
- > => ({
360
- queryKey: getQueryKey(),
361
- queryFn,
362
- initialPageParam,
363
- getNextPageParam,
364
- })
365
-
366
- // Fetch function for loaders (cache-first, fetches first page)
367
- const fetch = async (): Promise<Selected> => {
368
- // Check cache first
369
- const cachedData = reactor.queryClient.getQueryData(getQueryKey()) as
370
- TInfiniteData | undefined
371
-
372
- if (cachedData !== undefined) {
373
- return select ? select(cachedData) : (cachedData as Selected)
374
- }
375
-
376
- // Fetch if not in cache
377
- const result = await reactor.queryClient.fetchInfiniteQuery(
378
- getInfiniteQueryOptions()
379
- )
380
-
381
- // Result is already InfiniteData format
382
- return select ? select(result) : (result as unknown as Selected)
383
- }
384
-
385
- // Implementation
386
- const useInfiniteQueryHook: UseInfiniteQueryWithSelect<
387
- TPageData,
388
- TPageParam,
389
- Selected,
390
- TError
391
- > = (options: any): any => {
392
- // Memoized and identity-preserving; see buildChainedSelect for why the
393
- // function identity matters to the observer's select-result cache.
394
- const chainedSelect = useMemo(
395
- () => buildChainedSelect(select as never, options?.select),
396
- [options?.select]
397
- )
398
-
399
- return useInfiniteQuery(
400
- {
401
- queryKey: getQueryKey(),
402
- queryFn,
403
- initialPageParam,
404
- getNextPageParam,
405
- getPreviousPageParam,
406
- maxPages,
407
- staleTime,
408
- ...rest,
409
- ...options,
410
- select: chainedSelect,
411
- } as any,
412
- reactor.queryClient
413
- )
414
- }
415
-
416
- // Invalidate function
417
- const invalidate = async (): Promise<void> => {
418
- const queryKey = getQueryKey()
419
- await reactor.queryClient.invalidateQueries({ queryKey })
420
- }
421
-
422
- // Get data from cache without fetching
423
- const getCacheData: any = (selectFn?: (data: Selected) => any) => {
424
- const queryKey = getQueryKey()
425
- const cachedRawData = reactor.queryClient.getQueryData(
426
- queryKey
427
- ) as TInfiniteData
428
-
429
- if (cachedRawData === undefined) {
430
- return undefined
431
- }
432
-
433
- // Apply config.select to raw cache data
434
- const selectedData = (
435
- select ? select(cachedRawData) : cachedRawData
436
- ) as Selected
437
-
438
- // Apply optional select parameter
439
- if (selectFn) {
440
- return selectFn(selectedData)
441
- }
442
-
443
- return selectedData
444
- }
445
-
446
- return {
447
- fetch,
448
- useInfiniteQuery: useInfiniteQueryHook,
449
- invalidate,
450
- getQueryKey,
451
- getCacheData,
452
- }
453
- }
454
-
455
- // ============================================================================
456
- // Factory Function
457
- // ============================================================================
458
-
459
- export function createInfiniteQuery<
460
- Service,
461
- Transform extends TransformKey,
462
- Method extends FunctionName<Service> = FunctionName<Service>,
463
- TPageParam = unknown,
464
- Selected = InfiniteData<
465
- InfiniteQueryPageData<Service, Method, Transform>,
466
- TPageParam
467
- >,
468
- >(
469
- reactor: Reactor<Service, Transform>,
470
- config: InfiniteQueryConfig<
471
- NoInfer<Service>,
472
- Method,
473
- Transform,
474
- TPageParam,
475
- Selected
476
- >
477
- ): InfiniteQueryResult<
478
- InfiniteQueryPageData<Service, Method, Transform>,
479
- TPageParam,
480
- Selected,
481
- InfiniteQueryError<Service, Method, Transform>
482
- > {
483
- return createInfiniteQueryImpl(
484
- reactor,
485
- config as InfiniteQueryConfig<
486
- Service,
487
- Method,
488
- Transform,
489
- TPageParam,
490
- Selected
491
- >
492
- )
493
- }
494
-
495
- // ============================================================================
496
- // Factory with Dynamic Args
497
- // ============================================================================
498
-
499
- /**
500
- * Create an infinite query factory that accepts getArgs at call time.
501
- * Useful when pagination logic varies by context.
502
- *
503
- * @template Service - The actor interface type
504
- * @template Method - The method name on the actor
505
- * @template Transform - The transformation key (identity, display, etc.)
506
- * @template TPageParam - The page parameter type
507
- * @template Selected - The type returned after select transformation
508
- *
509
- * @param reactor - The Reactor instance
510
- * @param config - Infinite query configuration (without getArgs)
511
- * @returns A function that accepts getArgs and returns an ActorInfiniteQueryResult
512
- *
513
- * @example
514
- * const getPostsQuery = createActorInfiniteQueryFactory(reactor, {
515
- * functionName: "get_posts",
516
- * initialPageParam: 0,
517
- * getKeyArgs: (args) => {
518
- * const [{ userId }] = args
519
- * return [{ userId }]
520
- * },
521
- * getNextPageParam: (lastPage) => lastPage.nextCursor,
522
- * })
523
- *
524
- * // Create query with specific args builder
525
- * const userPostsQuery = getPostsQuery((cursor) => [{ userId, cursor, limit: 10 }])
526
- * const { data, fetchNextPage } = userPostsQuery.useInfiniteQuery()
527
- */
528
-
529
- export function createInfiniteQueryFactory<
530
- Service,
531
- Transform extends TransformKey,
532
- Method extends FunctionName<Service> = FunctionName<Service>,
533
- TPageParam = unknown,
534
- Selected = InfiniteData<
535
- InfiniteQueryPageData<Service, Method, Transform>,
536
- TPageParam
537
- >,
538
- >(
539
- reactor: Reactor<Service, Transform>,
540
- config: InfiniteQueryFactoryConfig<
541
- NoInfer<Service>,
542
- Method,
543
- Transform,
544
- TPageParam,
545
- Selected
546
- >
547
- ): InfiniteQueryFactoryFn<Service, Method, Transform, TPageParam, Selected> {
548
- const factory: InfiniteQueryFactoryFn<
549
- Service,
550
- Method,
551
- Transform,
552
- TPageParam,
553
- Selected
554
- > = (
555
- getArgs: (pageParam: TPageParam) => ReactorArgs<Service, Method, Transform>
556
- ) => {
557
- // `getKeyArgs` and the args-derived key segment are applied by the impl,
558
- // which now does it for every infinite query rather than only for factories.
559
- return createInfiniteQueryImpl<
560
- Service,
561
- Method,
562
- Transform,
563
- TPageParam,
564
- Selected
565
- >(reactor, {
566
- ...(config as InfiniteQueryFactoryConfig<
567
- Service,
568
- Method,
569
- Transform,
570
- TPageParam,
571
- Selected
572
- >),
573
- getArgs,
574
- })
575
- }
576
-
577
- return factory
578
- }