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

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.
package/dist/index.d.mts DELETED
@@ -1,850 +0,0 @@
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
- declare enum SortDirection {
225
- ASC = "asc",
226
- DESC = "desc",
227
- }
228
- interface Sort<TKey extends string = string> {
229
- direction: SortDirection;
230
- key: TKey;
231
- }
232
- //#endregion
233
- //#region src/types/queryOptions.d.ts
234
- interface QueryParams {
235
- filters?: Record<string, any>;
236
- search?: string;
237
- sort?: Sort[];
238
- }
239
- interface WithSearchQuery {
240
- search?: string | undefined;
241
- }
242
- interface WithSortQuery<TKeys extends string> {
243
- sort: Sort<TKeys>[];
244
- }
245
- interface WithFilterQuery<TFilters extends Record<string, any>> {
246
- filters?: TFilters;
247
- }
248
- interface WithStaticFilterQuery<TFilters extends Record<string, any>> {
249
- staticFilters: TFilters;
250
- }
251
- interface InfiniteQueryOptions<TParams> {
252
- params: { [K in keyof TParams]: Ref<TParams[K]> };
253
- }
254
- //#endregion
255
- //#region src/types/pagination.type.d.ts
256
- interface OffsetPaginationParams {
257
- limit: number;
258
- offset: number;
259
- }
260
- type OffsetPagination<T extends QueryParams = Record<string, never>> = {
261
- pagination: OffsetPaginationParams;
262
- } & T;
263
- interface KeysetPaginationParams {
264
- key?: any;
265
- limit: number;
266
- }
267
- type KeysetPagination<T extends QueryParams> = {
268
- pagination: KeysetPaginationParams;
269
- } & T;
270
- interface OffsetPaginationResponse<TData> {
271
- data: TData[];
272
- meta: {
273
- limit: number;
274
- offset: number;
275
- total: number;
276
- };
277
- }
278
- interface KeysetPaginationResponse<TData> {
279
- data: TData[];
280
- meta: {
281
- next: unknown;
282
- };
283
- }
284
- type OffsetPaginationResult<TData, TErrorCode extends string = string> = ApiResult<OffsetPaginationResponse<TData>, TErrorCode>;
285
- type KeysetPaginationResult<TData, TErrorCode extends string = string> = ApiResult<KeysetPaginationResponse<TData>, TErrorCode>;
286
- interface PaginatedDataDto<TSchema> {
287
- items: TSchema[];
288
- meta: {
289
- limit: number;
290
- offset: number;
291
- total: number;
292
- };
293
- }
294
- //#endregion
295
- //#region src/composables/query/keysetInfiniteQuery.composable.d.ts
296
- type NestedMaybeRefOrGetter$2<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
297
- type ArrayItemOf$1<E$1> = E$1 extends (infer I)[] ? I : E$1;
298
- type EntityItemOf$1<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
299
- entity: infer E;
300
- } ? ArrayItemOf$1<E> : unknown;
301
- type WithParams$5<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
302
- params?: undefined;
303
- } : {
304
- params: NestedMaybeRefOrGetter$2<RegisteredQueryKeyParams<TKey>>;
305
- };
306
- type KeysetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
307
- /**
308
- * The time in milliseconds after which the query will be considered stale
309
- * @default 0
310
- */
311
- staleTime?: number;
312
- /**
313
- * Whether the query is enabled
314
- * @default true
315
- */
316
- isEnabled?: MaybeRef<boolean>;
317
- /**
318
- * Maximum number of items to fetch per page
319
- * @default 20
320
- */
321
- limit?: number;
322
- /**
323
- * Function that will be called when query is executed
324
- */
325
- queryFn: (paginationParams: KeysetPaginationParams) => Promise<KeysetPaginationResult<TData, TErrorCode>>;
326
- } & WithParams$5<TKey>;
327
- interface UseKeysetInfiniteQueryReturnType<TData, TErrorCode extends string = RegisteredErrorCodes> {
328
- /**
329
- * Whether there is a next page available to fetch
330
- */
331
- hasNextPage: ComputedRef<boolean>;
332
- /**
333
- * Whether query has errored at least once
334
- * @deprecated - use `result.value.isErr()` instead
335
- */
336
- isError: ComputedRef<boolean>;
337
- /**
338
- * Whether query is currently fetching data, regardless of cache status
339
- */
340
- isFetching: ComputedRef<boolean>;
341
- /**
342
- * Whether query is currently fetching the next page
343
- */
344
- isFetchingNextPage: ComputedRef<boolean>;
345
- /**
346
- * Whether query is initially loading
347
- * @deprecated - use `result.value.isLoading()` instead
348
- */
349
- isLoading: ComputedRef<boolean>;
350
- /**
351
- * Whether query has been executed successfully
352
- * @deprecated - use `result.value.isOk()` instead
353
- */
354
- isSuccess: ComputedRef<boolean>;
355
- /**
356
- * Fetch the next page of results using the keyset cursor
357
- */
358
- fetchNextPage: () => Promise<void>;
359
- /**
360
- * Refetch the query
361
- */
362
- refetch: () => Promise<void>;
363
- /**
364
- * Computed result of the query containing all accumulated pages
365
- * Returns an AsyncResult with three states:
366
- * - loading: use `result.value.isLoading()`
367
- * - ok: use `result.value.isOk()` and `result.value.getValue()`
368
- * - err: use `result.value.isErr()` and `result.value.getError()`
369
- *
370
- * Use `result.value.match({ loading, ok, err })` for exhaustive handling
371
- */
372
- result: ComputedRef<AsyncResult<KeysetPaginationResponse<TData>, ApiError<TErrorCode>>>;
373
- }
374
- 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>;
375
- //#endregion
376
- //#region src/composables/query/offsetInfiniteQuery.composable.d.ts
377
- type NestedMaybeRefOrGetter$1<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
378
- type ArrayItemOf<E$1> = E$1 extends (infer I)[] ? I : E$1;
379
- type EntityItemOf<TKey extends PropertyKey> = RegisteredQueryKeyConfig<TKey> extends {
380
- entity: infer E;
381
- } ? ArrayItemOf<E> : unknown;
382
- type WithParams$4<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
383
- params?: undefined;
384
- } : {
385
- params: NestedMaybeRefOrGetter$1<RegisteredQueryKeyParams<TKey>>;
386
- };
387
- type OffsetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
388
- /**
389
- * The time in milliseconds after which the query will be considered stale
390
- * @default 0
391
- */
392
- staleTime?: number;
393
- /**
394
- * Whether the query is enabled
395
- * @default true
396
- */
397
- isEnabled?: MaybeRef<boolean>;
398
- /**
399
- * Maximum number of items to fetch per page
400
- * @default 20
401
- */
402
- limit?: number;
403
- /**
404
- * Function that will be called when query is executed
405
- */
406
- queryFn: (paginationParams: OffsetPaginationParams) => Promise<OffsetPaginationResult<TData, TErrorCode>>;
407
- } & WithParams$4<TKey>;
408
- interface UseOffsetInfiniteQueryReturnType<TData, TErrorCode extends string = RegisteredErrorCodes> {
409
- /**
410
- * Whether there is a next page available to fetch
411
- */
412
- hasNextPage: ComputedRef<boolean>;
413
- /**
414
- * Whether query has errored at least once
415
- * @deprecated - use `result.value.isErr()` instead
416
- */
417
- isError: ComputedRef<boolean>;
418
- /**
419
- * Whether query is currently fetching data, regardless of cache status
420
- */
421
- isFetching: ComputedRef<boolean>;
422
- /**
423
- * Whether query is currently fetching the next page
424
- */
425
- isFetchingNextPage: ComputedRef<boolean>;
426
- /**
427
- * Whether query is initially loading
428
- * @deprecated - use `result.value.isLoading()` instead
429
- */
430
- isLoading: ComputedRef<boolean>;
431
- /**
432
- * Whether query has been executed successfully
433
- * @deprecated - use `result.value.isOk()` instead
434
- */
435
- isSuccess: ComputedRef<boolean>;
436
- /**
437
- * Fetch the next page of results using offset-based pagination
438
- */
439
- fetchNextPage: () => Promise<void>;
440
- /**
441
- * Refetch the query
442
- */
443
- refetch: () => Promise<void>;
444
- /**
445
- * Computed result of the query containing all accumulated pages
446
- * Returns an AsyncResult with three states:
447
- * - loading: use `result.value.isLoading()`
448
- * - ok: use `result.value.isOk()` and `result.value.getValue()`
449
- * - err: use `result.value.isErr()` and `result.value.getError()`
450
- *
451
- * Use `result.value.match({ loading, ok, err })` for exhaustive handling
452
- */
453
- result: ComputedRef<AsyncResult<OffsetPaginationResponse<TData>, ApiError<TErrorCode>>>;
454
- }
455
- declare function useOffsetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = EntityItemOf<TKey>, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: OffsetInfiniteQueryOptions<TKey, TData, TErrorCode>): UseOffsetInfiniteQueryReturnType<TData, TErrorCode>;
456
- //#endregion
457
- //#region src/composables/query/prefetchKeysetInfiniteQuery.composable.d.ts
458
- type WithParams$3<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
459
- params?: undefined;
460
- } : {
461
- params: RegisteredQueryKeyParams<TKey>;
462
- };
463
- type PrefetchKeysetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
464
- /**
465
- * The time in milliseconds after which the prefetched query will be considered stale
466
- * @default config.prefetchStaleTime
467
- */
468
- staleTime?: number;
469
- /**
470
- * Maximum number of items to fetch per page
471
- * @default 20
472
- */
473
- limit?: number;
474
- /**
475
- * Function that will be called when query is executed
476
- */
477
- queryFn: (paginationParams: KeysetPaginationParams) => Promise<KeysetPaginationResult<TData, TErrorCode>>;
478
- } & WithParams$3<TKey>;
479
- declare function usePrefetchKeysetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: PrefetchKeysetInfiniteQueryOptions<TKey, TData, TErrorCode>): {
480
- execute: () => Promise<void>;
481
- };
482
- //#endregion
483
- //#region src/composables/query/prefetchOffsetInfiniteQuery.composable.d.ts
484
- type WithParams$2<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
485
- params?: undefined;
486
- } : {
487
- params: RegisteredQueryKeyParams<TKey>;
488
- };
489
- type PrefetchOffsetInfiniteQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
490
- /**
491
- * The time in milliseconds after which the prefetched query will be considered stale
492
- * @default config.prefetchStaleTime
493
- */
494
- staleTime?: number;
495
- /**
496
- * Maximum number of items to fetch per page
497
- * @default 20
498
- */
499
- limit?: number;
500
- /**
501
- * Function that will be called when query is executed
502
- */
503
- queryFn: (paginationParams: OffsetPaginationParams) => Promise<OffsetPaginationResult<TData, TErrorCode>>;
504
- } & WithParams$2<TKey>;
505
- declare function usePrefetchOffsetInfiniteQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: PrefetchOffsetInfiniteQueryOptions<TKey, TData, TErrorCode>): {
506
- execute: () => Promise<void>;
507
- };
508
- //#endregion
509
- //#region src/composables/query/prefetchQuery.composable.d.ts
510
- type WithParams$1<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
511
- params?: undefined;
512
- } : {
513
- params: RegisteredQueryKeyParams<TKey>;
514
- };
515
- type UsePrefetchQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
516
- /**
517
- * The time in milliseconds after which the prefetched query will be considered stale
518
- * @default config.prefetchStaleTime
519
- */
520
- staleTime?: number;
521
- /**
522
- * Function that will be called when query is executed
523
- */
524
- queryFn: () => Promise<ApiResult<TData, TErrorCode>>;
525
- } & WithParams$1<TKey>;
526
- declare function usePrefetchQuery<TKey extends keyof RegisteredQueryKeys, TData = unknown, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: UsePrefetchQueryOptions<TKey, TData, TErrorCode>): {
527
- execute: () => Promise<void>;
528
- };
529
- //#endregion
530
- //#region src/composables/query/query.composable.d.ts
531
- type NestedMaybeRefOrGetter<T> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
532
- type WithParams<TKey extends PropertyKey> = RegisteredQueryKeyParams<TKey> extends undefined ? {
533
- params?: undefined;
534
- } : {
535
- params: NestedMaybeRefOrGetter<RegisteredQueryKeyParams<TKey>>;
536
- };
537
- type UseQueryOptions<TKey extends keyof RegisteredQueryKeys, TData, TErrorCode extends string = RegisteredErrorCodes> = {
538
- /**
539
- * The time in milliseconds after which the query will be considered stale
540
- * @default 0
541
- */
542
- staleTime?: number;
543
- /**
544
- * Whether to enable debug mode
545
- * @default false
546
- */
547
- isDebug?: boolean;
548
- /**
549
- * Whether the query is enabled
550
- * @default true
551
- */
552
- isEnabled?: MaybeRef<boolean>;
553
- /**
554
- * Function that will be called when query is executed
555
- */
556
- queryFn: () => Promise<ApiResult<TData, TErrorCode>>;
557
- } & WithParams<TKey>;
558
- interface UseQueryReturnType<TResData, TErrorCode extends string = RegisteredErrorCodes> {
559
- /**
560
- * Whether query has errored at least once
561
- * @deprecated - use `result.value.isErr()` instead
562
- */
563
- isError: ComputedRef<boolean>;
564
- /**
565
- * Whether query is currently fetching data, regardless of cache status
566
- */
567
- isFetching: ComputedRef<boolean>;
568
- /**
569
- * Whether query is initially loading
570
- * @deprecated - use `result.value.isLoading()` instead
571
- */
572
- isLoading: ComputedRef<boolean>;
573
- /**
574
- * Whether query has been executed successfully
575
- * @deprecated - use `result.value.isOk()` instead
576
- */
577
- isSuccess: ComputedRef<boolean>;
578
- /**
579
- * Refetch the query
580
- */
581
- refetch: () => Promise<void>;
582
- /**
583
- * Computed result of the query
584
- * Returns an AsyncResult with three states:
585
- * - loading: use `result.value.isLoading()`
586
- * - ok: use `result.value.isOk()` and `result.value.getValue()`
587
- * - err: use `result.value.isErr()` and `result.value.getError()`
588
- *
589
- * Use `result.value.match({ loading, ok, err })` for exhaustive handling
590
- */
591
- result: ComputedRef<AsyncResult<TResData, ApiError<TErrorCode>>>;
592
- }
593
- declare function useQuery<TKey extends keyof RegisteredQueryKeys, TData = RegisteredQueryKeyEntity<TKey>, TErrorCode extends string = RegisteredErrorCodes>(key: TKey, options: UseQueryOptions<TKey, TData, TErrorCode>): UseQueryReturnType<TData, TErrorCode>;
594
- //#endregion
595
- //#region src/config/config.d.ts
596
- /**
597
- * Initialize the API utilities with a QueryClient.
598
- * Call this once during app setup (e.g. in a plugin or main.ts).
599
- *
600
- * After calling this, `createApiUtils()` can be called without options.
601
- *
602
- * @example
603
- * ```typescript
604
- * import { initializeApiUtils } from '@wisemen/vue-core-api-utils'
605
- *
606
- * const queryClient = new QueryClient()
607
- * initializeApiUtils(queryClient)
608
- *
609
- * // Then in your api lib:
610
- * export const { useQuery, useMutation, ... } = createApiUtils<MyQueryKeys>()
611
- * ```
612
- */
613
- declare function initializeApiUtils(queryClient: QueryClient$1): void;
614
- /**
615
- * @internal
616
- */
617
- declare function getQueryClient(): QueryClient$1;
618
- interface QueryConfig {
619
- prefetchStaleTime: number;
620
- limit: number;
621
- }
622
- declare function setQueryConfig(config: Partial<QueryConfig>): void;
623
- //#endregion
624
- //#region src/plugin/apiUtilsPlugin.d.ts
625
- /**
626
- * Create a Vue plugin that sets up TanStack Query and initializes API utilities.
627
- *
628
- * This plugin handles:
629
- * - Creating a QueryClient with the provided config
630
- * - Installing VueQueryPlugin on the app
631
- * - Initializing the global QueryClient for api-utils
632
- *
633
- * @example
634
- * ```typescript
635
- * import { apiUtilsPlugin } from '@wisemen/vue-core-api-utils'
636
- * import { vueQueryClientConfig } from '@wisemen/vue-core-configs'
637
- *
638
- * app.use(apiUtilsPlugin(vueQueryClientConfig()))
639
- * ```
640
- *
641
- * @param config - QueryClient configuration
642
- * @returns A Vue plugin that can be used with app.use()
643
- */
644
- declare function apiUtilsPlugin(config: QueryClientConfig): {
645
- install: (app: App<any>) => void;
646
- };
647
- //#endregion
648
- //#region src/utils/api/api.util.d.ts
649
- declare class ApiUtil {
650
- static fromPromise<T>(promise: PromiseLike<T>, message?: string): Promise<ApiResult<T, RegisteredErrorCodes>>;
651
- static getKeysetPaginationNextOffset(keysetPaginationMeta: KeysetPaginationResponse<any>['meta']): number | null;
652
- static getResultError(result: AsyncResult<unknown, ApiError> | Result<unknown, ApiError> | null): ApiError<RegisteredErrorCodes> | null;
653
- private static isAsyncResult;
654
- static void<T, TApiResult extends ApiResult<void>>(result: ApiResult<T>): TApiResult;
655
- }
656
- //#endregion
657
- //#region src/utils/api-error/apiError.util.d.ts
658
- declare class ApiErrorUtil {
659
- static getApiErrorCode(error: ApiExpectedError): string | null;
660
- static getApiErrorMessage(error: ApiExpectedError): string | null;
661
- static getMessage(error: ApiExpectedError): string | null;
662
- static handleApiError({
663
- error,
664
- message
665
- }: {
666
- error: unknown;
667
- message?: string;
668
- }): ApiError<RegisteredErrorCodes>;
669
- static isExpectedApiError(error: unknown): error is ApiExpectedError<RegisteredErrorCodes>;
670
- static isZodError(error: unknown): error is z.ZodError;
671
- }
672
- //#endregion
673
- //#region src/types/queryKeys.type.d.ts
674
- /**
675
- * Generic helper types for libraries/factories that want to infer query key typing
676
- * from a user-provided config object (instead of relying on module augmentation).
677
- */
678
- /**
679
- * Extract the entity type from a query-keys config for a specific key.
680
- */
681
- type QueryKeyEntityFromConfig<TQueryKeys extends object, TKey extends PropertyKey> = TKey extends keyof TQueryKeys ? TQueryKeys[TKey] extends {
682
- entity: infer E;
683
- } ? E : never : never;
684
- /**
685
- * Extract the raw params type from a query-keys config for a specific key (unwrapped from Computed).
686
- * Used for optimistic updates which accept plain values.
687
- */
688
- type QueryKeyRawParamsFromConfig<TQueryKeys extends object, TKey extends PropertyKey> = TKey extends keyof TQueryKeys ? TQueryKeys[TKey] extends {
689
- params: infer P;
690
- } ? P : void : never;
691
- /**
692
- * Get all keys that have an associated entity in a query-keys config.
693
- */
694
- type QueryKeysWithEntityFromConfig<TQueryKeys extends object> = ({ [K in keyof TQueryKeys]: TQueryKeys[K] extends {
695
- entity: any;
696
- } ? K : never }[keyof TQueryKeys]) & string;
697
- //#endregion
698
- //#region src/utils/query-client/queryClient.d.ts
699
- /**
700
- * Helper type to extract the item type from an entity (array item or entity itself)
701
- */
702
- type EntityItem<TEntity> = TEntity extends any[] ? TEntity[number] : TEntity;
703
- /**
704
- * Options for type-safe query client update
705
- */
706
- interface QueryClientUpdateOptions<TEntity> {
707
- /**
708
- * Predicate function that receives the current item and returns true if it should be updated
709
- */
710
- by: (item: EntityItem<TEntity>) => boolean;
711
- /**
712
- * Function that receives the current item and returns the updated item
713
- */
714
- value: (item: EntityItem<TEntity>) => EntityItem<TEntity>;
715
- }
716
- /**
717
- * Result of an update operation, providing a rollback function
718
- */
719
- interface QueryClientUpdateResult {
720
- /**
721
- * Reverts the cache entries affected by this update to their state before the update was applied.
722
- * Safe to call multiple times (subsequent calls are no-ops).
723
- */
724
- rollback: () => void;
725
- }
726
- /**
727
- * QueryClient utility class for type-safe query operations
728
- */
729
- declare class QueryClient<TQueryKeys extends object> {
730
- private readonly queryClient;
731
- constructor(queryClient: QueryClient$1);
732
- /**
733
- * Extract the raw entity from AsyncResult data
734
- */
735
- private extractEntityFromAsyncResult;
736
- private hasDataArray;
737
- private isInfiniteDataLike;
738
- /**
739
- * Determine if an item should be updated
740
- */
741
- private shouldUpdateItem;
742
- /**
743
- * Internal method to update entity based on the "by" option
744
- */
745
- private updateEntity;
746
- private updateInfinitePageValue;
747
- /**
748
- * Wrap a raw entity in an AsyncResult (preserving ok state)
749
- */
750
- private wrapEntityInAsyncResult;
751
- /**
752
- * Get raw entity data from the query cache
753
- * Automatically extracts the entity from AsyncResult wrapper
754
- *
755
- * When using just a key string:
756
- * - By default (isExact=false), returns ALL queries with that key as first element
757
- * - With isExact=true, returns only the query stored as [key]
758
- *
759
- * @example
760
- * ```typescript
761
- * // Get all userDetail queries (returns array)
762
- * const allUsers = queryClient.get('userDetail')
763
- *
764
- * // Get exact query stored as ['userDetail']
765
- * const exactUser = queryClient.get('userDetail', { isExact: true })
766
- *
767
- * // Get specific userDetail query with params
768
- * const user = queryClient.get(['userDetail', { userUuid: '123' }] as const)
769
- * ```
770
- */
771
- get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, options?: {
772
- isExact?: false;
773
- }): QueryKeyEntityFromConfig<TQueryKeys, TKey>[];
774
- get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, options: {
775
- isExact: true;
776
- }): QueryKeyEntityFromConfig<TQueryKeys, TKey> | null;
777
- get<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>]): QueryKeyEntityFromConfig<TQueryKeys, TKey> | null;
778
- /**
779
- * Invalidate queries to trigger a refetch
780
- *
781
- * When using just the key, invalidates ALL queries with that key
782
- * When using key + params tuple, invalidates SPECIFIC query
783
- *
784
- * @example
785
- * ```typescript
786
- * // Invalidate all userDetail queries
787
- * await queryClient.invalidate('userDetail')
788
- *
789
- * // Invalidate specific query
790
- * await queryClient.invalidate(['userDetail', { userUuid: '123' }] as const)
791
- * ```
792
- */
793
- invalidate<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(key: TKey): Promise<void>;
794
- invalidate<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(keyTuple: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>]): Promise<void>;
795
- /**
796
- * Set raw entity data in the query cache for a specific query
797
- * Automatically wraps the entity in AsyncResult
798
- *
799
- * Both formats set a single query - just with different key representations:
800
- * - 'userDetail' sets the query with key ['userDetail']
801
- * - ['userDetail', { userUuid: '123' }] sets the query with that exact key
802
- *
803
- * @example
804
- * ```typescript
805
- * // Set query with just the key
806
- * queryClient.set('userDetail', userData)
807
- *
808
- * // Set query with key + params
809
- * queryClient.set(['userDetail', { userUuid: '123' }] as const, userData)
810
- * ```
811
- */
812
- set<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: TKey, entity: QueryKeyEntityFromConfig<TQueryKeys, TKey>): void;
813
- set<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>>(queryKey: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>], entity: QueryKeyEntityFromConfig<TQueryKeys, TKey>): void;
814
- /**
815
- * Update entity data in the query cache
816
- *
817
- * When using just the key, updates ALL queries with that key
818
- * When using key + params tuple, updates SPECIFIC query
819
- *
820
- * @example
821
- * ```typescript
822
- * // Update a specific user by id
823
- * const { rollback } = queryClient.update('userDetail', {
824
- * by: (user) => user.id === '123',
825
- * value: (user) => ({ ...user, name: 'John Doe' })
826
- * })
827
- *
828
- * // Revert if the mutation fails
829
- * rollback()
830
- *
831
- * // Update all electronics products to out of stock
832
- * queryClient.update('productList', {
833
- * by: (product) => product.category === 'electronics',
834
- * value: (product) => ({ ...product, inStock: false })
835
- * })
836
- * ```
837
- */
838
- update<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>, TEntity extends QueryKeyEntityFromConfig<TQueryKeys, TKey> = QueryKeyEntityFromConfig<TQueryKeys, TKey>>(key: TKey, options: QueryClientUpdateOptions<TEntity>): QueryClientUpdateResult;
839
- update<TKey extends QueryKeysWithEntityFromConfig<TQueryKeys>, TEntity extends QueryKeyEntityFromConfig<TQueryKeys, TKey> = QueryKeyEntityFromConfig<TQueryKeys, TKey>>(keyTuple: readonly [TKey, Partial<QueryKeyRawParamsFromConfig<TQueryKeys, TKey>>], options: QueryClientUpdateOptions<TEntity>): QueryClientUpdateResult;
840
- }
841
- //#endregion
842
- //#region src/utils/sort/sort.utils.d.ts
843
- declare class SortUtil {
844
- static toDto<SortKey extends string, QueryKey>(sort: Sort<SortKey>[], sortKeyMap: Record<SortKey, QueryKey>): {
845
- key: QueryKey;
846
- order: SortDirection;
847
- }[];
848
- }
849
- //#endregion
850
- 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, 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 };