@wisemen/vue-core-api-utils 2.0.2 → 2.0.3

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.
@@ -0,0 +1,847 @@
1
+ import { Result } from "neverthrow";
2
+ import { QueryClient as QueryClient$1, QueryClient as TanstackQueryClient, QueryClientConfig } from "@tanstack/vue-query";
3
+ import { App, ComputedRef, MaybeRef, MaybeRefOrGetter, Ref } from "vue";
4
+ import z from "zod";
5
+
6
+ //#region src/async-result/asyncResult.d.ts
7
+ /**
8
+ * Base class for AsyncResult - internal use only.
9
+ * Use AsyncResult<T, E> as the public type.
10
+ */
11
+ declare abstract class AsyncResultBase<T, E$1> {
12
+ protected readonly _error: E$1 | undefined;
13
+ protected readonly _status: 'err' | 'loading' | 'ok';
14
+ protected readonly _value: T | undefined;
15
+ protected constructor(status: 'err' | 'loading' | 'ok', value?: T, error?: E$1);
16
+ /**
17
+ * Check if the result is an error (type predicate for narrowing)
18
+ */
19
+ isErr(): this is AsyncResultErr<T, E$1>;
20
+ /**
21
+ * Check if the result is in loading state (type predicate for narrowing)
22
+ */
23
+ isLoading(): this is AsyncResultLoading<T, E$1>;
24
+ /**
25
+ * Check if the result is a success (type predicate for narrowing)
26
+ */
27
+ isOk(): this is AsyncResultOk<T, E$1>;
28
+ /**
29
+ * Map the success value to a new value
30
+ */
31
+ map<U>(fn: (value: T) => U): AsyncResult<U, E$1>;
32
+ /**
33
+ * Map the error to a new error
34
+ */
35
+ mapErr<F>(fn: (error: E$1) => F): AsyncResult<T, F>;
36
+ /**
37
+ * Pattern match on all three states
38
+ */
39
+ match<U>(handlers: {
40
+ err: (error: E$1) => U;
41
+ loading: () => U;
42
+ ok: (value: T) => U;
43
+ }): U;
44
+ /**
45
+ * Get the success value, or return null if loading or error.
46
+ * Returns T | null when null is passed as the default value.
47
+ */
48
+ unwrapOr(defaultValue: null): T | null;
49
+ /**
50
+ * Get the success value, or return the default value of type T if loading or error.
51
+ * Returns T when a value of type T is passed as the default value.
52
+ */
53
+ unwrapOr(defaultValue: T): T;
54
+ }
55
+ /**
56
+ * AsyncResult representing an error state
57
+ */
58
+ declare class AsyncResultErr<T, E$1> extends AsyncResultBase<T, E$1> {
59
+ private constructor();
60
+ /** @internal */
61
+ static _create<T, E$1>(error: E$1): AsyncResultErr<T, E$1>;
62
+ /** Get the error value - only available after isErr() check */
63
+ getError(): E$1;
64
+ getResult(): Result<T, E$1>;
65
+ }
66
+ /**
67
+ * AsyncResult representing a loading state
68
+ */
69
+ declare class AsyncResultLoading<T, E$1> extends AsyncResultBase<T, E$1> {
70
+ private constructor();
71
+ /** @internal */
72
+ static _create<T, E$1>(): AsyncResultLoading<T, E$1>;
73
+ getResult(): null;
74
+ }
75
+ /**
76
+ * AsyncResult representing a success state
77
+ */
78
+ declare class AsyncResultOk<T, E$1> extends AsyncResultBase<T, E$1> {
79
+ private constructor();
80
+ /** @internal */
81
+ static _create<T, E$1>(value: T): AsyncResultOk<T, E$1>;
82
+ getResult(): Result<T, E$1>;
83
+ /** Get the success value - only available after isOk() check */
84
+ getValue(): T;
85
+ }
86
+ /**
87
+ * Union type of all AsyncResult states.
88
+ * Use isOk(), isErr(), or isLoading() to narrow to specific state.
89
+ */
90
+ type AsyncResult<T, E$1> = AsyncResultErr<T, E$1> | AsyncResultLoading<T, E$1> | AsyncResultOk<T, E$1>;
91
+ /**
92
+ * Static factory methods for creating AsyncResult instances.
93
+ * This pattern (same name for type and value) is intentional for ergonomic API.
94
+ */
95
+ declare const AsyncResult: {
96
+ /**
97
+ * Create a failed AsyncResult with error
98
+ */
99
+ readonly err: <T, E$1>(error: E$1) => AsyncResultErr<T, E$1>;
100
+ /**
101
+ * Create an AsyncResult from an existing neverthrow Result
102
+ */
103
+ readonly fromResult: <T, E$1>(result: Result<T, E$1>) => AsyncResult<T, E$1>;
104
+ /**
105
+ * Create a loading AsyncResult
106
+ */
107
+ readonly loading: <T, E$1>() => AsyncResultLoading<T, E$1>;
108
+ /**
109
+ * Create a successful AsyncResult with data
110
+ */
111
+ readonly ok: <T, E$1>(value: T) => AsyncResultOk<T, E$1>;
112
+ };
113
+ //#endregion
114
+ //#region src/register.d.ts
115
+ interface Register {}
116
+ type RegisteredQueryKeys = Register extends {
117
+ queryKeys: infer T;
118
+ } ? T extends object ? T : object : object;
119
+ type RegisteredErrorCodes = Register extends {
120
+ errorCodes: infer T;
121
+ } ? T extends string ? T : string : string;
122
+ type RegisteredApiUseMutationOptions<TReqData, TResData, TParams = void> = UseMutationOptions<TReqData, TResData, TParams, RegisteredErrorCodes>;
123
+ type RegisteredQueryKeyInput = { [K in keyof RegisteredQueryKeys]?: RegisteredQueryKeys[K] extends {
124
+ params: infer P;
125
+ } ? P : unknown };
126
+ type RegisteredQueryKeyConfig<TKey extends PropertyKey> = RegisteredQueryKeys extends Record<TKey, infer V> ? V : unknown;
127
+ type RegisteredQueryKeyEntity<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
128
+ entity: infer E;
129
+ } ? E : unknown;
130
+ type RegisteredQueryKeyParams<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
131
+ params: infer P;
132
+ } ? P : undefined;
133
+ //#endregion
134
+ //#region src/types/apiError.type.d.ts
135
+ interface ApiKnownErrorObject<TCode extends string = string> {
136
+ code: TCode;
137
+ detail: string;
138
+ source?: {
139
+ pointer: string;
140
+ };
141
+ status: string;
142
+ }
143
+ interface ApiUnknownErrorObject {
144
+ code: string;
145
+ detail: string;
146
+ source?: {
147
+ pointer: string;
148
+ };
149
+ status: string;
150
+ }
151
+ type ApiErrorObject<TCode extends string = string> = ApiKnownErrorObject<TCode> | ApiUnknownErrorObject;
152
+ interface ApiExpectedError<TCode extends string = string> {
153
+ errors: ApiErrorObject<TCode>[];
154
+ }
155
+ type ApiUnexpectedError = Error;
156
+ type ApiError<TCode extends string = string> = ApiExpectedError<TCode> | ApiUnexpectedError;
157
+ type ApiResult<T, TCode extends string = string> = Result<T, ApiError<TCode>>;
158
+ type AsyncApiResult<T, TCode extends string = string> = AsyncResult<T, ApiError<TCode>>;
159
+ //#endregion
160
+ //#region src/composables/mutation/mutation.composable.d.ts
161
+ type RequestParams<TReqData, TParams> = TReqData extends void ? TParams extends void ? void : {
162
+ params: TParams;
163
+ } : TParams extends void ? {
164
+ body: TReqData;
165
+ } : {
166
+ body: TReqData;
167
+ params: TParams;
168
+ };
169
+ interface UseMutationOptions<TReqData, TResData, TParams = void, TErrorCode extends string = RegisteredErrorCodes> {
170
+ /**
171
+ * Whether to enable debug mode
172
+ */
173
+ isDebug?: boolean;
174
+ /**
175
+ * Function that will be called to perform the mutation
176
+ * @param options - Parameters and body for the mutation
177
+ * @returns Promise with ApiResult containing either the response data or an error
178
+ */
179
+ queryFn: (options: RequestParams<TReqData, TParams>) => Promise<ApiResult<TResData, TErrorCode>>;
180
+ /**
181
+ * Object where each key is a query key to invalidate after mutation succeeds.
182
+ * Each query key can optionally have nested parameter extractors.
183
+ * @example
184
+ * ```typescript
185
+ * queryKeysToInvalidate: {
186
+ * contactDetail: {
187
+ * contactUuid: (params, result) => params.contactUuid,
188
+ * },
189
+ * contactIndex: {},
190
+ * }
191
+ * ```
192
+ */
193
+ queryKeysToInvalidate?: { [K in keyof RegisteredQueryKeyInput]?: Record<string, (params: TParams, data: TResData) => any> };
194
+ }
195
+ interface UseMutationReturnType<TReqData, TResData, TParams = void, TErrorCode extends string = RegisteredErrorCodes> {
196
+ /**
197
+ * Whether mutation is loading
198
+ * @deprecated - use `result.value.isLoading()` instead
199
+ */
200
+ isLoading: ComputedRef<boolean>;
201
+ /**
202
+ * Response data from the mutation
203
+ * @deprecated - use `result.value.getValue()` instead
204
+ */
205
+ data: ComputedRef<TResData | null>;
206
+ /**
207
+ * Function to execute the mutation
208
+ * @param data - Parameters and body for the mutation
209
+ * @returns Promise with ApiResult containing either the response data or an error
210
+ */
211
+ execute: (data: RequestParams<TReqData, TParams>) => Promise<ApiResult<TResData, TErrorCode>>;
212
+ /**
213
+ * Computed result of the mutation
214
+ * Returns an AsyncResult with three states:
215
+ * - loading: use `result.value.isLoading()`
216
+ * - ok: use `result.value.isOk()` and `result.value.getValue()`
217
+ * - err: use `result.value.isErr()` and `result.value.getError()`
218
+ */
219
+ result: ComputedRef<AsyncResult<TResData, ApiError<TErrorCode>>>;
220
+ }
221
+ declare function useMutation<TReqData = void, TResData = void, TParams = void, TErrorCode extends string = RegisteredErrorCodes>(options: UseMutationOptions<TReqData, TResData, TParams, TErrorCode>): UseMutationReturnType<TReqData, TResData, TParams, TErrorCode>;
222
+ //#endregion
223
+ //#region src/types/sort.type.d.ts
224
+ type SortDirection = 'asc' | 'desc';
225
+ interface Sort<TKey extends string = string> {
226
+ direction: SortDirection;
227
+ key: TKey;
228
+ }
229
+ //#endregion
230
+ //#region src/types/queryOptions.d.ts
231
+ interface QueryParams {
232
+ filters?: Record<string, any>;
233
+ search?: string;
234
+ sort?: Sort[];
235
+ }
236
+ interface WithSearchQuery {
237
+ search?: string | undefined;
238
+ }
239
+ interface WithSortQuery<TKeys extends string> {
240
+ sort: Sort<TKeys>[];
241
+ }
242
+ interface WithFilterQuery<TFilters extends Record<string, any>> {
243
+ filters?: TFilters;
244
+ }
245
+ interface WithStaticFilterQuery<TFilters extends Record<string, any>> {
246
+ staticFilters: TFilters;
247
+ }
248
+ interface InfiniteQueryOptions<TParams> {
249
+ params: { [K in keyof TParams]: Ref<TParams[K]> };
250
+ }
251
+ //#endregion
252
+ //#region src/types/pagination.type.d.ts
253
+ interface OffsetPaginationParams {
254
+ limit: number;
255
+ offset: number;
256
+ }
257
+ type OffsetPagination<T extends QueryParams = Record<string, never>> = {
258
+ pagination: OffsetPaginationParams;
259
+ } & T;
260
+ interface KeysetPaginationParams {
261
+ key?: any;
262
+ limit: number;
263
+ }
264
+ type KeysetPagination<T extends QueryParams> = {
265
+ pagination: KeysetPaginationParams;
266
+ } & T;
267
+ interface OffsetPaginationResponse<TData> {
268
+ data: TData[];
269
+ meta: {
270
+ limit: number;
271
+ offset: number;
272
+ total: number;
273
+ };
274
+ }
275
+ interface KeysetPaginationResponse<TData> {
276
+ data: TData[];
277
+ meta: {
278
+ next: unknown;
279
+ };
280
+ }
281
+ type OffsetPaginationResult<TData, TErrorCode extends string = string> = ApiResult<OffsetPaginationResponse<TData>, TErrorCode>;
282
+ type KeysetPaginationResult<TData, TErrorCode extends string = string> = ApiResult<KeysetPaginationResponse<TData>, TErrorCode>;
283
+ interface PaginatedDataDto<TSchema> {
284
+ items: TSchema[];
285
+ meta: {
286
+ limit: number;
287
+ offset: number;
288
+ total: number;
289
+ };
290
+ }
291
+ //#endregion
292
+ //#region src/composables/query/keysetInfiniteQuery.composable.d.ts
293
+ type NestedMaybeRefOrGetter$2<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
294
+ type ArrayItemOf$1<E$1> = E$1 extends (infer I)[] ? I : E$1;
295
+ type EntityItemOf$1<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
296
+ entity: infer E;
297
+ } ? ArrayItemOf$1<E> : unknown;
298
+ type WithParams$5<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
299
+ params?: undefined;
300
+ } : {
301
+ params: NestedMaybeRefOrGetter$2<RegisteredQueryKeyParams<TKey>>;
302
+ };
303
+ type KeysetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
304
+ /**
305
+ * The time in milliseconds after which the query will be considered stale
306
+ * @default 0
307
+ */
308
+ staleTime?: number;
309
+ /**
310
+ * Whether the query is enabled
311
+ * @default true
312
+ */
313
+ isEnabled?: MaybeRef<boolean>;
314
+ /**
315
+ * Maximum number of items to fetch per page
316
+ * @default 20
317
+ */
318
+ limit?: number;
319
+ /**
320
+ * Function that will be called when query is executed
321
+ */
322
+ queryFn: (paginationParams: KeysetPaginationParams) => Promise<KeysetPaginationResult<TData, TErrorCode>>;
323
+ } & WithParams$5<TKey>;
324
+ interface UseKeysetInfiniteQueryReturnType<TData, TErrorCode extends string = RegisteredErrorCodes> {
325
+ /**
326
+ * Whether there is a next page available to fetch
327
+ */
328
+ hasNextPage: ComputedRef<boolean>;
329
+ /**
330
+ * Whether query has errored at least once
331
+ * @deprecated - use `result.value.isErr()` instead
332
+ */
333
+ isError: ComputedRef<boolean>;
334
+ /**
335
+ * Whether query is currently fetching data, regardless of cache status
336
+ */
337
+ isFetching: ComputedRef<boolean>;
338
+ /**
339
+ * Whether query is currently fetching the next page
340
+ */
341
+ isFetchingNextPage: ComputedRef<boolean>;
342
+ /**
343
+ * Whether query is initially loading
344
+ * @deprecated - use `result.value.isLoading()` instead
345
+ */
346
+ isLoading: ComputedRef<boolean>;
347
+ /**
348
+ * Whether query has been executed successfully
349
+ * @deprecated - use `result.value.isOk()` instead
350
+ */
351
+ isSuccess: ComputedRef<boolean>;
352
+ /**
353
+ * Fetch the next page of results using the keyset cursor
354
+ */
355
+ fetchNextPage: () => Promise<void>;
356
+ /**
357
+ * Refetch the query
358
+ */
359
+ refetch: () => Promise<void>;
360
+ /**
361
+ * Computed result of the query containing all accumulated pages
362
+ * Returns an AsyncResult with three states:
363
+ * - loading: use `result.value.isLoading()`
364
+ * - ok: use `result.value.isOk()` and `result.value.getValue()`
365
+ * - err: use `result.value.isErr()` and `result.value.getError()`
366
+ *
367
+ * Use `result.value.match({ loading, ok, err })` for exhaustive handling
368
+ */
369
+ result: ComputedRef<AsyncResult<KeysetPaginationResponse<TData>, ApiError<TErrorCode>>>;
370
+ }
371
+ declare function useKeysetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = EntityItemOf$1<TKey>, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: KeysetInfiniteQueryOptions<TKey, TData, TErrorCode>): UseKeysetInfiniteQueryReturnType<TData, TErrorCode>;
372
+ //#endregion
373
+ //#region src/composables/query/offsetInfiniteQuery.composable.d.ts
374
+ type NestedMaybeRefOrGetter$1<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
375
+ type ArrayItemOf<E$1> = E$1 extends (infer I)[] ? I : E$1;
376
+ type EntityItemOf<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
377
+ entity: infer E;
378
+ } ? ArrayItemOf<E> : unknown;
379
+ type WithParams$4<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
380
+ params?: undefined;
381
+ } : {
382
+ params: NestedMaybeRefOrGetter$1<RegisteredQueryKeyParams<TKey>>;
383
+ };
384
+ type OffsetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
385
+ /**
386
+ * The time in milliseconds after which the query will be considered stale
387
+ * @default 0
388
+ */
389
+ staleTime?: number;
390
+ /**
391
+ * Whether the query is enabled
392
+ * @default true
393
+ */
394
+ isEnabled?: MaybeRef<boolean>;
395
+ /**
396
+ * Maximum number of items to fetch per page
397
+ * @default 20
398
+ */
399
+ limit?: number;
400
+ /**
401
+ * Function that will be called when query is executed
402
+ */
403
+ queryFn: (paginationParams: OffsetPaginationParams) => Promise<OffsetPaginationResult<TData, TErrorCode>>;
404
+ } & WithParams$4<TKey>;
405
+ interface UseOffsetInfiniteQueryReturnType<TData, TErrorCode extends string = RegisteredErrorCodes> {
406
+ /**
407
+ * Whether there is a next page available to fetch
408
+ */
409
+ hasNextPage: ComputedRef<boolean>;
410
+ /**
411
+ * Whether query has errored at least once
412
+ * @deprecated - use `result.value.isErr()` instead
413
+ */
414
+ isError: ComputedRef<boolean>;
415
+ /**
416
+ * Whether query is currently fetching data, regardless of cache status
417
+ */
418
+ isFetching: ComputedRef<boolean>;
419
+ /**
420
+ * Whether query is currently fetching the next page
421
+ */
422
+ isFetchingNextPage: ComputedRef<boolean>;
423
+ /**
424
+ * Whether query is initially loading
425
+ * @deprecated - use `result.value.isLoading()` instead
426
+ */
427
+ isLoading: ComputedRef<boolean>;
428
+ /**
429
+ * Whether query has been executed successfully
430
+ * @deprecated - use `result.value.isOk()` instead
431
+ */
432
+ isSuccess: ComputedRef<boolean>;
433
+ /**
434
+ * Fetch the next page of results using offset-based pagination
435
+ */
436
+ fetchNextPage: () => Promise<void>;
437
+ /**
438
+ * Refetch the query
439
+ */
440
+ refetch: () => Promise<void>;
441
+ /**
442
+ * Computed result of the query containing all accumulated pages
443
+ * Returns an AsyncResult with three states:
444
+ * - loading: use `result.value.isLoading()`
445
+ * - ok: use `result.value.isOk()` and `result.value.getValue()`
446
+ * - err: use `result.value.isErr()` and `result.value.getError()`
447
+ *
448
+ * Use `result.value.match({ loading, ok, err })` for exhaustive handling
449
+ */
450
+ result: ComputedRef<AsyncResult<OffsetPaginationResponse<TData>, ApiError<TErrorCode>>>;
451
+ }
452
+ declare function useOffsetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = EntityItemOf<TKey>, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: OffsetInfiniteQueryOptions<TKey, TData, TErrorCode>): UseOffsetInfiniteQueryReturnType<TData, TErrorCode>;
453
+ //#endregion
454
+ //#region src/composables/query/prefetchKeysetInfiniteQuery.composable.d.ts
455
+ type WithParams$3<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
456
+ params?: undefined;
457
+ } : {
458
+ params: RegisteredQueryKeyParams<TKey>;
459
+ };
460
+ type PrefetchKeysetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
461
+ /**
462
+ * The time in milliseconds after which the prefetched query will be considered stale
463
+ * @default config.prefetchStaleTime
464
+ */
465
+ staleTime?: number;
466
+ /**
467
+ * Maximum number of items to fetch per page
468
+ * @default 20
469
+ */
470
+ limit?: number;
471
+ /**
472
+ * Function that will be called when query is executed
473
+ */
474
+ queryFn: (paginationParams: KeysetPaginationParams) => Promise<KeysetPaginationResult<TData, TErrorCode>>;
475
+ } & WithParams$3<TKey>;
476
+ declare function usePrefetchKeysetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: PrefetchKeysetInfiniteQueryOptions<TKey, TData, TErrorCode>): {
477
+ execute: () => Promise<void>;
478
+ };
479
+ //#endregion
480
+ //#region src/composables/query/prefetchOffsetInfiniteQuery.composable.d.ts
481
+ type WithParams$2<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
482
+ params?: undefined;
483
+ } : {
484
+ params: RegisteredQueryKeyParams<TKey>;
485
+ };
486
+ type PrefetchOffsetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
487
+ /**
488
+ * The time in milliseconds after which the prefetched query will be considered stale
489
+ * @default config.prefetchStaleTime
490
+ */
491
+ staleTime?: number;
492
+ /**
493
+ * Maximum number of items to fetch per page
494
+ * @default 20
495
+ */
496
+ limit?: number;
497
+ /**
498
+ * Function that will be called when query is executed
499
+ */
500
+ queryFn: (paginationParams: OffsetPaginationParams) => Promise<OffsetPaginationResult<TData, TErrorCode>>;
501
+ } & WithParams$2<TKey>;
502
+ declare function usePrefetchOffsetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: PrefetchOffsetInfiniteQueryOptions<TKey, TData, TErrorCode>): {
503
+ execute: () => Promise<void>;
504
+ };
505
+ //#endregion
506
+ //#region src/composables/query/prefetchQuery.composable.d.ts
507
+ type WithParams$1<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
508
+ params?: undefined;
509
+ } : {
510
+ params: RegisteredQueryKeyParams<TKey>;
511
+ };
512
+ type UsePrefetchQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
513
+ /**
514
+ * The time in milliseconds after which the prefetched query will be considered stale
515
+ * @default config.prefetchStaleTime
516
+ */
517
+ staleTime?: number;
518
+ /**
519
+ * Function that will be called when query is executed
520
+ */
521
+ queryFn: () => Promise<ApiResult<TData, TErrorCode>>;
522
+ } & WithParams$1<TKey>;
523
+ declare function usePrefetchQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: UsePrefetchQueryOptions<TKey, TData, TErrorCode>): {
524
+ execute: () => Promise<void>;
525
+ };
526
+ //#endregion
527
+ //#region src/composables/query/query.composable.d.ts
528
+ type NestedMaybeRefOrGetter<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
529
+ type WithParams<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
530
+ params?: undefined;
531
+ } : {
532
+ params: NestedMaybeRefOrGetter<RegisteredQueryKeyParams<TKey>>;
533
+ };
534
+ type UseQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
535
+ /**
536
+ * The time in milliseconds after which the query will be considered stale
537
+ * @default 0
538
+ */
539
+ staleTime?: number;
540
+ /**
541
+ * Whether to enable debug mode
542
+ * @default false
543
+ */
544
+ isDebug?: boolean;
545
+ /**
546
+ * Whether the query is enabled
547
+ * @default true
548
+ */
549
+ isEnabled?: MaybeRef<boolean>;
550
+ /**
551
+ * Function that will be called when query is executed
552
+ */
553
+ queryFn: () => Promise<ApiResult<TData, TErrorCode>>;
554
+ } & WithParams<TKey>;
555
+ interface UseQueryReturnType<TResData, TErrorCode extends string = RegisteredErrorCodes> {
556
+ /**
557
+ * Whether query has errored at least once
558
+ * @deprecated - use `result.value.isErr()` instead
559
+ */
560
+ isError: ComputedRef<boolean>;
561
+ /**
562
+ * Whether query is currently fetching data, regardless of cache status
563
+ */
564
+ isFetching: ComputedRef<boolean>;
565
+ /**
566
+ * Whether query is initially loading
567
+ * @deprecated - use `result.value.isLoading()` instead
568
+ */
569
+ isLoading: ComputedRef<boolean>;
570
+ /**
571
+ * Whether query has been executed successfully
572
+ * @deprecated - use `result.value.isOk()` instead
573
+ */
574
+ isSuccess: ComputedRef<boolean>;
575
+ /**
576
+ * Refetch the query
577
+ */
578
+ refetch: () => Promise<void>;
579
+ /**
580
+ * Computed result of the query
581
+ * Returns an AsyncResult with three states:
582
+ * - loading: use `result.value.isLoading()`
583
+ * - ok: use `result.value.isOk()` and `result.value.getValue()`
584
+ * - err: use `result.value.isErr()` and `result.value.getError()`
585
+ *
586
+ * Use `result.value.match({ loading, ok, err })` for exhaustive handling
587
+ */
588
+ result: ComputedRef<AsyncResult<TResData, ApiError<TErrorCode>>>;
589
+ }
590
+ declare function useQuery<TKey extends keyof RegisteredQueryKeys, TData = RegisteredQueryKeyEntity<TKey>, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: UseQueryOptions<TKey, TData, TErrorCode>): UseQueryReturnType<TData, TErrorCode>;
591
+ //#endregion
592
+ //#region src/config/config.d.ts
593
+ /**
594
+ * Initialize the API utilities with a QueryClient.
595
+ * Call this once during app setup (e.g. in a plugin or main.ts).
596
+ *
597
+ * After calling this, `createApiUtils()` can be called without options.
598
+ *
599
+ * @example
600
+ * ```typescript
601
+ * import { initializeApiUtils } from '@wisemen/vue-core-api-utils'
602
+ *
603
+ * const queryClient = new QueryClient()
604
+ * initializeApiUtils(queryClient)
605
+ *
606
+ * // Then in your api lib:
607
+ * export const { useQuery, useMutation, ... } = createApiUtils<MyQueryKeys>()
608
+ * ```
609
+ */
610
+ declare function initializeApiUtils(queryClient: QueryClient$1): void;
611
+ /**
612
+ * @internal
613
+ */
614
+ declare function getQueryClient(): QueryClient$1;
615
+ interface QueryConfig {
616
+ prefetchStaleTime: number;
617
+ limit: number;
618
+ }
619
+ declare function setQueryConfig(config: Partial<QueryConfig>): void;
620
+ //#endregion
621
+ //#region src/plugin/apiUtilsPlugin.d.ts
622
+ /**
623
+ * Create a Vue plugin that sets up TanStack Query and initializes API utilities.
624
+ *
625
+ * This plugin handles:
626
+ * - Creating a QueryClient with the provided config
627
+ * - Installing VueQueryPlugin on the app
628
+ * - Initializing the global QueryClient for api-utils
629
+ *
630
+ * @example
631
+ * ```typescript
632
+ * import { apiUtilsPlugin } from '@wisemen/vue-core-api-utils'
633
+ * import { vueQueryClientConfig } from '@wisemen/vue-core-configs'
634
+ *
635
+ * app.use(apiUtilsPlugin(vueQueryClientConfig()))
636
+ * ```
637
+ *
638
+ * @param config - QueryClient configuration
639
+ * @returns A Vue plugin that can be used with app.use()
640
+ */
641
+ declare function apiUtilsPlugin(config: QueryClientConfig): {
642
+ install: (app: App<any>) => void;
643
+ };
644
+ //#endregion
645
+ //#region src/utils/api/api.util.d.ts
646
+ declare class ApiUtil {
647
+ static fromPromise<T>(promise: PromiseLike<T>, message?: string): Promise<ApiResult<T, RegisteredErrorCodes>>;
648
+ static getKeysetPaginationNextOffset(keysetPaginationMeta: KeysetPaginationResponse<any>['meta']): number | null;
649
+ static getResultError(result: AsyncResult<unknown, ApiError> | Result<unknown, ApiError> | null): ApiError<RegisteredErrorCodes> | null;
650
+ private static isAsyncResult;
651
+ static void<T, TApiResult extends ApiResult<void>>(result: ApiResult<T>): TApiResult;
652
+ }
653
+ //#endregion
654
+ //#region src/utils/api-error/apiError.util.d.ts
655
+ declare class ApiErrorUtil {
656
+ static getApiErrorCode(error: ApiExpectedError): string | null;
657
+ static getApiErrorMessage(error: ApiExpectedError): string | null;
658
+ static getMessage(error: ApiExpectedError): string | null;
659
+ static handleApiError({
660
+ error,
661
+ message
662
+ }: {
663
+ error: unknown;
664
+ message?: string;
665
+ }): ApiError<RegisteredErrorCodes>;
666
+ static isExpectedApiError(error: unknown): error is ApiExpectedError<RegisteredErrorCodes>;
667
+ static isZodError(error: unknown): error is z.ZodError;
668
+ }
669
+ //#endregion
670
+ //#region src/types/queryKeys.type.d.ts
671
+ /**
672
+ * Generic helper types for libraries/factories that want to infer query key typing
673
+ * from a user-provided config object (instead of relying on module augmentation).
674
+ */
675
+ /**
676
+ * Extract the entity type from a query-keys config for a specific key.
677
+ */
678
+ type QueryKeyEntityFromConfig<TQueryKeys extends object, TKey extends PropertyKey> = TKey extends keyof TQueryKeys ? TQueryKeys[TKey] extends {
679
+ entity: infer E;
680
+ } ? E : never : never;
681
+ /**
682
+ * Extract the raw params type from a query-keys config for a specific key (unwrapped from Computed).
683
+ * Used for optimistic updates which accept plain values.
684
+ */
685
+ type QueryKeyRawParamsFromConfig<TQueryKeys extends object, TKey extends PropertyKey> = TKey extends keyof TQueryKeys ? TQueryKeys[TKey] extends {
686
+ params: infer P;
687
+ } ? P : void : never;
688
+ /**
689
+ * Get all keys that have an associated entity in a query-keys config.
690
+ */
691
+ type QueryKeysWithEntityFromConfig<TQueryKeys extends object> = ({ [K in keyof TQueryKeys]: TQueryKeys[K] extends {
692
+ entity: any;
693
+ } ? K : never }[keyof TQueryKeys]) & string;
694
+ //#endregion
695
+ //#region src/utils/query-client/queryClient.d.ts
696
+ /**
697
+ * Helper type to extract the item type from an entity (array item or entity itself)
698
+ */
699
+ type EntityItem<TEntity> = TEntity extends any[] ? TEntity[number] : TEntity;
700
+ /**
701
+ * Options for type-safe query client update
702
+ */
703
+ interface QueryClientUpdateOptions<TEntity> {
704
+ /**
705
+ * Predicate function that receives the current item and returns true if it should be updated
706
+ */
707
+ by: (item: EntityItem<TEntity>) => boolean;
708
+ /**
709
+ * Function that receives the current item and returns the updated item
710
+ */
711
+ value: (item: EntityItem<TEntity>) => EntityItem<TEntity>;
712
+ }
713
+ /**
714
+ * Result of an update operation, providing a rollback function
715
+ */
716
+ interface QueryClientUpdateResult {
717
+ /**
718
+ * Reverts the cache entries affected by this update to their state before the update was applied.
719
+ * Safe to call multiple times (subsequent calls are no-ops).
720
+ */
721
+ rollback: () => void;
722
+ }
723
+ /**
724
+ * QueryClient utility class for type-safe query operations
725
+ */
726
+ declare class QueryClient<TQueryKeys extends object> {
727
+ private readonly queryClient;
728
+ constructor(queryClient: QueryClient$1);
729
+ /**
730
+ * Extract the raw entity from AsyncResult data
731
+ */
732
+ private extractEntityFromAsyncResult;
733
+ private hasDataArray;
734
+ private isInfiniteDataLike;
735
+ /**
736
+ * Determine if an item should be updated
737
+ */
738
+ private shouldUpdateItem;
739
+ /**
740
+ * Internal method to update entity based on the "by" option
741
+ */
742
+ private updateEntity;
743
+ private updateInfinitePageValue;
744
+ /**
745
+ * Wrap a raw entity in an AsyncResult (preserving ok state)
746
+ */
747
+ private wrapEntityInAsyncResult;
748
+ /**
749
+ * Get raw entity data from the query cache
750
+ * Automatically extracts the entity from AsyncResult wrapper
751
+ *
752
+ * When using just a key string:
753
+ * - By default (isExact=false), returns ALL queries with that key as first element
754
+ * - With isExact=true, returns only the query stored as [key]
755
+ *
756
+ * @example
757
+ * ```typescript
758
+ * // Get all userDetail queries (returns array)
759
+ * const allUsers = queryClient.get('userDetail')
760
+ *
761
+ * // Get exact query stored as ['userDetail']
762
+ * const exactUser = queryClient.get('userDetail', { isExact: true })
763
+ *
764
+ * // Get specific userDetail query with params
765
+ * const user = queryClient.get(['userDetail', { userUuid: '123' }] as const)
766
+ * ```
767
+ */
768
+ get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, options?: {
769
+ isExact?: false;
770
+ }): QueryKeyEntityFromConfig<TQueryKeys, TKey>[];
771
+ get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, options: {
772
+ isExact: true;
773
+ }): QueryKeyEntityFromConfig<TQueryKeys, TKey> | null;
774
+ get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>]): QueryKeyEntityFromConfig<TQueryKeys, TKey> | null;
775
+ /**
776
+ * Invalidate queries to trigger a refetch
777
+ *
778
+ * When using just the key, invalidates ALL queries with that key
779
+ * When using key + params tuple, invalidates SPECIFIC query
780
+ *
781
+ * @example
782
+ * ```typescript
783
+ * // Invalidate all userDetail queries
784
+ * await queryClient.invalidate('userDetail')
785
+ *
786
+ * // Invalidate specific query
787
+ * await queryClient.invalidate(['userDetail', { userUuid: '123' }] as const)
788
+ * ```
789
+ */
790
+ invalidate<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(key: TKey): Promise<void>;
791
+ invalidate<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(keyTuple: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>]): Promise<void>;
792
+ /**
793
+ * Set raw entity data in the query cache for a specific query
794
+ * Automatically wraps the entity in AsyncResult
795
+ *
796
+ * Both formats set a single query - just with different key representations:
797
+ * - 'userDetail' sets the query with key ['userDetail']
798
+ * - ['userDetail', { userUuid: '123' }] sets the query with that exact key
799
+ *
800
+ * @example
801
+ * ```typescript
802
+ * // Set query with just the key
803
+ * queryClient.set('userDetail', userData)
804
+ *
805
+ * // Set query with key + params
806
+ * queryClient.set(['userDetail', { userUuid: '123' }] as const, userData)
807
+ * ```
808
+ */
809
+ set<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, entity: QueryKeyEntityFromConfig<TQueryKeys, TKey>): void;
810
+ set<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>], entity: QueryKeyEntityFromConfig<TQueryKeys, TKey>): void;
811
+ /**
812
+ * Update entity data in the query cache
813
+ *
814
+ * When using just the key, updates ALL queries with that key
815
+ * When using key + params tuple, updates SPECIFIC query
816
+ *
817
+ * @example
818
+ * ```typescript
819
+ * // Update a specific user by id
820
+ * const { rollback } = queryClient.update('userDetail', {
821
+ * by: (user) => user.id === '123',
822
+ * value: (user) => ({ ...user, name: 'John Doe' })
823
+ * })
824
+ *
825
+ * // Revert if the mutation fails
826
+ * rollback()
827
+ *
828
+ * // Update all electronics products to out of stock
829
+ * queryClient.update('productList', {
830
+ * by: (product) => product.category === 'electronics',
831
+ * value: (product) => ({ ...product, inStock: false })
832
+ * })
833
+ * ```
834
+ */
835
+ update<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>, TEntity extends QueryKeyEntityFromConfig<TQueryKeys, TKey> = QueryKeyEntityFromConfig<TQueryKeys, TKey>>(key: TKey, options: QueryClientUpdateOptions<TEntity>): QueryClientUpdateResult;
836
+ update<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>, TEntity extends QueryKeyEntityFromConfig<TQueryKeys, TKey> = QueryKeyEntityFromConfig<TQueryKeys, TKey>>(keyTuple: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>], options: QueryClientUpdateOptions<TEntity>): QueryClientUpdateResult;
837
+ }
838
+ //#endregion
839
+ //#region src/utils/sort/sort.utils.d.ts
840
+ declare class SortUtil {
841
+ static toDto<SortKey extends string, QueryKey>(sort: Sort<SortKey>[], sortKeyMap: Record<SortKey, QueryKey>): {
842
+ key: QueryKey;
843
+ order: SortDirection;
844
+ }[];
845
+ }
846
+ //#endregion
847
+ export { type ApiError, type ApiErrorObject, ApiErrorUtil, type ApiExpectedError, type ApiKnownErrorObject, type ApiResult, type ApiUnexpectedError, type ApiUnknownErrorObject, ApiUtil, type AsyncApiResult, AsyncResult, AsyncResultErr, AsyncResultLoading, AsyncResultOk, type InfiniteQueryOptions, type KeysetInfiniteQueryOptions, type KeysetPagination, type KeysetPaginationParams, type KeysetPaginationResponse, type KeysetPaginationResult, type OffsetInfiniteQueryOptions, type OffsetPagination, type OffsetPaginationParams, type OffsetPaginationResponse, type OffsetPaginationResult, type PaginatedDataDto, type PrefetchKeysetInfiniteQueryOptions, type PrefetchOffsetInfiniteQueryOptions, QueryClient, type QueryClientUpdateOptions, type QueryClientUpdateResult, type QueryConfig, type QueryParams, type Register, type RegisteredApiUseMutationOptions, type RegisteredErrorCodes, type RegisteredQueryKeyInput, type RegisteredQueryKeys, type Sort, type SortDirection, SortUtil, type TanstackQueryClient, type UseKeysetInfiniteQueryReturnType, type UseMutationOptions, type UseMutationReturnType, type UseOffsetInfiniteQueryReturnType, type UseQueryOptions, type UseQueryReturnType, type WithFilterQuery, type WithSearchQuery, type WithSortQuery, type WithStaticFilterQuery, apiUtilsPlugin, getQueryClient as getTanstackQueryClient, initializeApiUtils, setQueryConfig, useKeysetInfiniteQuery, useMutation, useOffsetInfiniteQuery, usePrefetchKeysetInfiniteQuery, usePrefetchOffsetInfiniteQuery, usePrefetchQuery, useQuery };