@wisemen/vue-core-api-utils 0.0.1-beta.0

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # Tanstack query wrapper
@@ -0,0 +1,307 @@
1
+ import * as _tanstack_vue_query0 from "@tanstack/vue-query";
2
+ import { Result } from "neverthrow";
3
+ import * as vue0 from "vue";
4
+ import { ComputedRef, MaybeRef, Ref, UnwrapRef } from "vue";
5
+
6
+ //#region src/types/queryKeys.type.d.ts
7
+ interface QueryKeys {}
8
+ //#endregion
9
+ //#region src/composables/mutation/mutation.composable.d.ts
10
+ type RequestParams<TReqData, TParams> = TReqData extends void ? TParams extends void ? void : {
11
+ params: TParams;
12
+ } : TParams extends void ? {
13
+ body: TReqData;
14
+ } : {
15
+ body: TReqData;
16
+ params: TParams;
17
+ };
18
+ interface UseMutationOptions<TParams, TReqData, TResData> {
19
+ /**
20
+ * Whether to enable debug mode
21
+ */
22
+ isDebug?: boolean;
23
+ /**
24
+ * Function that will be called to perform the mutation
25
+ * @param options - Parameters and body for the mutation
26
+ * @returns Promise with the response data
27
+ */
28
+ queryFn: (options: RequestParams<TReqData, TParams>) => Promise<TResData>;
29
+ /**
30
+ * Array of query keys which should be invalidated after mutation is successful
31
+ */
32
+ queryKeysToInvalidate: { [TQueryKey in keyof QueryKeys]?: { [TQueryKeyParam in keyof QueryKeys[TQueryKey]]: (params: TParams, data: TResData) => UnwrapRef<QueryKeys[TQueryKey][TQueryKeyParam]> } };
33
+ }
34
+ interface UseMutationReturnType<TReqData, TResData, TParams = void> {
35
+ /**
36
+ * Whether mutation is loading
37
+ */
38
+ isLoading: ComputedRef<boolean>;
39
+ /**
40
+ * Response data from the mutation
41
+ */
42
+ data: ComputedRef<TResData extends unknown[] ? TResData[] : TResData | null>;
43
+ /**
44
+ * Function to execute the mutation
45
+ * @param data - Parameters and body for the mutation
46
+ * @returns Promise with the response data
47
+ */
48
+ execute: (data: RequestParams<TReqData, TParams>) => Promise<TResData>;
49
+ }
50
+ declare function useMutation<TReqData = void, TResData = void, TParams = void>(options: UseMutationOptions<TParams, TReqData, TResData>): UseMutationReturnType<TReqData, TResData, TParams>;
51
+ //#endregion
52
+ //#region src/types/apiError.type.d.ts
53
+ interface ApiErrorCodes {}
54
+ type ApiErrorCode = ApiErrorCodes[keyof ApiErrorCodes];
55
+ interface ApiKnownErrorObject {
56
+ code: ApiErrorCode;
57
+ detail: string;
58
+ source?: {
59
+ pointer: string;
60
+ };
61
+ status: string;
62
+ }
63
+ interface ApiUnknownErrorObject {
64
+ code: string;
65
+ detail: string;
66
+ source?: {
67
+ pointer: string;
68
+ };
69
+ status: string;
70
+ }
71
+ type ApiErrorObject = ApiKnownErrorObject | ApiUnknownErrorObject;
72
+ interface ApiExpectedError {
73
+ errors: ApiErrorObject[];
74
+ }
75
+ type ApiUnexpectedError = Error;
76
+ type ApiError = ApiExpectedError | ApiUnexpectedError;
77
+ type ApiResult<T> = Result<T, ApiError>;
78
+ //#endregion
79
+ //#region src/types/sort.type.d.ts
80
+ type SortDirection = 'asc' | 'desc';
81
+ interface Sort<TKey extends string = string> {
82
+ direction: SortDirection;
83
+ key: TKey;
84
+ }
85
+ //#endregion
86
+ //#region src/types/query.type.d.ts
87
+ interface QueryParams {
88
+ filters?: Record<string, any>;
89
+ search?: string;
90
+ sort?: Sort[];
91
+ }
92
+ interface WithSearchQuery {
93
+ search: string;
94
+ }
95
+ interface WithSortQuery<TKeys extends string> {
96
+ sort: Sort<TKeys>[];
97
+ }
98
+ interface WithFilterQuery<TFilters extends Record<string, any>> {
99
+ filters: TFilters;
100
+ }
101
+ interface InfiniteQueryOptions<TParams> {
102
+ params: { [K in keyof TParams]: Ref<TParams[K]> };
103
+ }
104
+ //#endregion
105
+ //#region src/types/pagination.type.d.ts
106
+ interface OffsetPaginationParams {
107
+ limit: number;
108
+ offset: number;
109
+ }
110
+ type OffsetPagination<T extends QueryParams = Record<string, never>> = {
111
+ pagination: OffsetPaginationParams;
112
+ } & T;
113
+ interface KeysetPaginationParams {
114
+ key?: any;
115
+ limit: number;
116
+ }
117
+ type KeysetPagination<T extends QueryParams> = {
118
+ pagination: KeysetPaginationParams;
119
+ } & T;
120
+ interface OffsetPaginationResponse<TData> {
121
+ data: TData[];
122
+ meta: {
123
+ limit: number;
124
+ offset: number;
125
+ total: number;
126
+ };
127
+ }
128
+ interface KeysetPaginationResponse<TData> {
129
+ data: TData[];
130
+ meta: {
131
+ next: unknown;
132
+ };
133
+ }
134
+ type OffsetPaginationResult<TData> = ApiResult<OffsetPaginationResponse<TData>>;
135
+ type KeysetPaginationResult<TData> = ApiResult<KeysetPaginationResponse<TData>>;
136
+ interface PaginatedDataDto<TSchema> {
137
+ items: TSchema[];
138
+ meta: {
139
+ limit: number;
140
+ offset: number;
141
+ total: number;
142
+ };
143
+ }
144
+ //#endregion
145
+ //#region src/composables/query/keysetInfiniteQuery.composable.d.ts
146
+ type NonOptionalKeys$2<T> = { [K in keyof T]-?: T[K] };
147
+ interface KeysetInfiniteQueryOptions<TData> {
148
+ /**
149
+ * The time in milliseconds after which the query will be considered stale
150
+ * After this time, the query will be refetched automatically in the background when it is rendered or accessed
151
+ * @default 0
152
+ */
153
+ staleTime?: number;
154
+ /**
155
+ * Whether the query is enabled
156
+ * If false, the query will not be executed
157
+ * @default true
158
+ */
159
+ isEnabled?: MaybeRef<boolean>;
160
+ /**
161
+ * Maximum number of items to fetch per page, default can be set in config
162
+ * @default 20
163
+ */
164
+ limit?: number;
165
+ /**
166
+ * Function that will be called when query is executed
167
+ * @returns Promise with response data
168
+ */
169
+ queryFn: (paginationParams: KeysetPaginationParams) => Promise<KeysetPaginationResult<TData>>;
170
+ /**
171
+ * Query key associated with the query
172
+ */
173
+ queryKey: { [TQueryKey in keyof QueryKeys]?: { [TQueryKeyParam in keyof NonOptionalKeys$2<QueryKeys[TQueryKey]>]: MaybeRef<QueryKeys[TQueryKey][TQueryKeyParam]> } };
174
+ }
175
+ declare function useKeysetInfiniteQuery<TData>(options: KeysetInfiniteQueryOptions<TData>): {
176
+ hasNextPage: vue0.ComputedRef<boolean>;
177
+ isError: vue0.ComputedRef<boolean>;
178
+ isFetching: vue0.ComputedRef<boolean>;
179
+ isFetchingNextPage: vue0.ComputedRef<boolean>;
180
+ isLoading: vue0.ComputedRef<boolean>;
181
+ isSuccess: vue0.ComputedRef<boolean>;
182
+ fetchNextPage: () => Promise<_tanstack_vue_query0.InfiniteQueryObserverResult<_tanstack_vue_query0.InfiniteData<KeysetPaginationResult<TData>, unknown>, Error>> | undefined;
183
+ refetch: () => Promise<void>;
184
+ result: vue0.ComputedRef<KeysetPaginationResult<TData>>;
185
+ };
186
+ //#endregion
187
+ //#region src/composables/query/offsetInfiniteQuery.composable.d.ts
188
+ type NonOptionalKeys$1<T> = { [K in keyof T]-?: T[K] };
189
+ interface OffsetInfiniteQueryOptions<TData> {
190
+ /**
191
+ * The time in milliseconds after which the query will be considered stale
192
+ * After this time, the query will be refetched automatically in the background when it is rendered or accessed
193
+ * @default 0
194
+ */
195
+ staleTime?: number;
196
+ /**
197
+ * Whether the query is enabled
198
+ * If false, the query will not be executed
199
+ * @default true
200
+ */
201
+ isEnabled?: MaybeRef<boolean>;
202
+ /**
203
+ * Maximum number of items to fetch per page, default can be set in config
204
+ * @default 20
205
+ */
206
+ limit?: number;
207
+ /**
208
+ * Function that will be called when query is executed
209
+ * @returns Promise with response data
210
+ */
211
+ queryFn: (paginationParams: OffsetPaginationParams) => Promise<OffsetPaginationResult<TData>>;
212
+ /**
213
+ * Query key associated with the query
214
+ */
215
+ queryKey: { [TQueryKey in keyof QueryKeys]?: { [TQueryKeyParam in keyof NonOptionalKeys$1<QueryKeys[TQueryKey]>]: MaybeRef<QueryKeys[TQueryKey][TQueryKeyParam]> } };
216
+ }
217
+ declare function useOffsetInfiniteQuery<TData>(options: OffsetInfiniteQueryOptions<TData>): {
218
+ hasNextPage: vue0.ComputedRef<boolean>;
219
+ isError: vue0.ComputedRef<boolean>;
220
+ isFetching: vue0.ComputedRef<boolean>;
221
+ isFetchingNextPage: vue0.ComputedRef<boolean>;
222
+ isLoading: vue0.ComputedRef<boolean>;
223
+ isSuccess: vue0.ComputedRef<boolean>;
224
+ fetchNextPage: () => Promise<_tanstack_vue_query0.InfiniteQueryObserverResult<_tanstack_vue_query0.InfiniteData<OffsetPaginationResult<TData>, unknown>, Error>> | undefined;
225
+ refetch: () => Promise<void>;
226
+ result: vue0.ComputedRef<OffsetPaginationResult<TData>>;
227
+ };
228
+ //#endregion
229
+ //#region src/composables/query/query.composable.d.ts
230
+ type NonOptionalKeys<T> = { [K in keyof T]-?: T[K] };
231
+ interface UseQueryOptions<TResData> {
232
+ /**
233
+ * The time in milliseconds after which the query will be considered stale
234
+ * After this time, the query will be refetched automatically in the background when it is rendered or accessed
235
+ * @default 0
236
+ */
237
+ staleTime?: number;
238
+ /**
239
+ * Whether to enable debug mode
240
+ * When enabled, the query key and parameters will be logged to the console
241
+ * @default false
242
+ */
243
+ isDebug?: boolean;
244
+ /**
245
+ * Whether the query is enabled
246
+ * If false, the query will not be executed
247
+ * @default true
248
+ */
249
+ isEnabled?: MaybeRef<boolean>;
250
+ /**
251
+ * Function that will be called when query is executed
252
+ * @returns Promise with response data
253
+ */
254
+ queryFn: () => Promise<ApiResult<TResData>>;
255
+ /**
256
+ * Query key associated with the query
257
+ */
258
+ queryKey: { [TQueryKey in keyof QueryKeys]?: { [TQueryKeyParam in keyof NonOptionalKeys<QueryKeys[TQueryKey]>]: MaybeRef<QueryKeys[TQueryKey][TQueryKeyParam]> } };
259
+ }
260
+ interface UseQueryReturnType<TResData> {
261
+ /**
262
+ * Response data
263
+ */
264
+ /**
265
+ * Whether query has errored at least once
266
+ * @deprecated - use `result.isErr()` instead
267
+ */
268
+ isError: ComputedRef<boolean>;
269
+ /**
270
+ * Whether query is currently loading
271
+ */
272
+ isFetching: ComputedRef<boolean>;
273
+ /**
274
+ * Whether query is initially loading
275
+ */
276
+ isLoading: ComputedRef<boolean>;
277
+ /**
278
+ * Whether query has been executed successfully
279
+ * @deprecated - use `result.isOk()` instead
280
+ */
281
+ isSuccess: ComputedRef<boolean>;
282
+ /**
283
+ * Refetch the query
284
+ */
285
+ refetch: () => Promise<void>;
286
+ /**
287
+ * Computed result of the query
288
+ * It will return an instance of Result<TResData, ApiError>
289
+ * where TResData is the response data and ApiError is the error
290
+ */
291
+ result: ComputedRef<ApiResult<TResData> | null>;
292
+ }
293
+ declare function useQuery<TResData>(options: UseQueryOptions<TResData>): UseQueryReturnType<TResData>;
294
+ //#endregion
295
+ //#region src/composables/query/prefetchQuery.composable.d.ts
296
+ declare function usePrefetchQuery<TResData>(query: UseQueryOptions<TResData>): {
297
+ execute: () => Promise<void>;
298
+ };
299
+ //#endregion
300
+ //#region src/config/config.d.ts
301
+ interface QueryConfig {
302
+ prefetchStaleTime: number;
303
+ limit: number;
304
+ }
305
+ declare function setQueryConfig(config: Partial<QueryConfig>): void;
306
+ //#endregion
307
+ export { ApiError, ApiErrorCode, ApiErrorCodes, ApiErrorObject, ApiExpectedError, ApiKnownErrorObject, ApiResult, ApiUnexpectedError, ApiUnknownErrorObject, InfiniteQueryOptions, KeysetInfiniteQueryOptions, KeysetPagination, KeysetPaginationParams, KeysetPaginationResponse, KeysetPaginationResult, OffsetInfiniteQueryOptions, OffsetPagination, OffsetPaginationParams, OffsetPaginationResponse, OffsetPaginationResult, PaginatedDataDto, type QueryConfig, QueryKeys, QueryParams, Sort, SortDirection, UseMutationReturnType, UseQueryOptions, UseQueryReturnType, WithFilterQuery, WithSearchQuery, WithSortQuery, setQueryConfig, useKeysetInfiniteQuery, useMutation, useOffsetInfiniteQuery, usePrefetchQuery, useQuery };