katanakit-js 4.0.2 → 4.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.
- package/README.md +54 -55
- package/dist/adapters/angular/index.d.ts +1 -0
- package/dist/adapters/angular/index.d.ts.map +1 -1
- package/dist/adapters/angular/index.js +1 -0
- package/dist/adapters/angular/index.js.map +1 -1
- package/dist/adapters/angular/query.d.ts +56 -73
- package/dist/adapters/angular/query.d.ts.map +1 -1
- package/dist/adapters/angular/query.js +66 -129
- package/dist/adapters/angular/query.js.map +1 -1
- package/dist/adapters/react/index.d.ts +1 -0
- package/dist/adapters/react/index.d.ts.map +1 -1
- package/dist/adapters/react/index.js +1 -0
- package/dist/adapters/react/index.js.map +1 -1
- package/dist/adapters/react/query.d.ts +27 -72
- package/dist/adapters/react/query.d.ts.map +1 -1
- package/dist/adapters/react/query.js +51 -165
- package/dist/adapters/react/query.js.map +1 -1
- package/dist/adapters/solid/index.d.ts +1 -0
- package/dist/adapters/solid/index.d.ts.map +1 -1
- package/dist/adapters/solid/index.js +1 -0
- package/dist/adapters/solid/index.js.map +1 -1
- package/dist/adapters/solid/query.d.ts +32 -74
- package/dist/adapters/solid/query.d.ts.map +1 -1
- package/dist/adapters/solid/query.js +56 -131
- package/dist/adapters/solid/query.js.map +1 -1
- package/dist/adapters/svelte/index.d.ts +1 -0
- package/dist/adapters/svelte/index.d.ts.map +1 -1
- package/dist/adapters/svelte/index.js +1 -0
- package/dist/adapters/svelte/index.js.map +1 -1
- package/dist/adapters/svelte/query.d.ts +24 -72
- package/dist/adapters/svelte/query.d.ts.map +1 -1
- package/dist/adapters/svelte/query.js +41 -135
- package/dist/adapters/svelte/query.js.map +1 -1
- package/dist/adapters/vanilla/index.d.ts +3 -0
- package/dist/adapters/vanilla/index.d.ts.map +1 -0
- package/dist/adapters/vanilla/index.js +3 -0
- package/dist/adapters/vanilla/index.js.map +1 -0
- package/dist/adapters/vanilla/query.d.ts +69 -0
- package/dist/adapters/vanilla/query.d.ts.map +1 -0
- package/dist/adapters/vanilla/query.js +76 -0
- package/dist/adapters/vanilla/query.js.map +1 -0
- package/dist/adapters/vue/index.d.ts +1 -0
- package/dist/adapters/vue/index.d.ts.map +1 -1
- package/dist/adapters/vue/index.js +1 -0
- package/dist/adapters/vue/index.js.map +1 -1
- package/dist/adapters/vue/query.d.ts +35 -86
- package/dist/adapters/vue/query.d.ts.map +1 -1
- package/dist/adapters/vue/query.js +57 -168
- package/dist/adapters/vue/query.js.map +1 -1
- package/dist/core/services/query.service.d.ts +87 -134
- package/dist/core/services/query.service.d.ts.map +1 -1
- package/dist/core/services/query.service.js +99 -378
- package/dist/core/services/query.service.js.map +1 -1
- package/package.json +10 -3
|
@@ -1,119 +1,62 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
status: "idle",
|
|
5
|
-
data: null,
|
|
6
|
-
error: null,
|
|
7
|
-
isLoading: false,
|
|
8
|
-
isSuccess: false,
|
|
9
|
-
isError: false,
|
|
10
|
-
isStale: true,
|
|
11
|
-
dataUpdatedAt: 0,
|
|
12
|
-
errorUpdatedAt: 0,
|
|
13
|
-
fetchCount: 0,
|
|
14
|
-
};
|
|
1
|
+
import { MutationObserver, notifyManager, QueryObserver, } from "@tanstack/query-core";
|
|
2
|
+
import { useCallback, useEffect, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { useQueryClient } from "../../core/services/query.service.js";
|
|
15
4
|
/**
|
|
16
|
-
* React hook
|
|
17
|
-
* Integrates with the API manager — your `queryFn` typically calls `useGetApi`
|
|
18
|
-
* or `useFetch` and returns the Safe Result.
|
|
5
|
+
* React hook for a cached query, backed by TanStack Query Core.
|
|
19
6
|
*
|
|
20
|
-
*
|
|
21
|
-
* `
|
|
7
|
+
* Returns TanStack's native {@link QueryObserverResult}
|
|
8
|
+
* (`{ data, error, isPending, isFetching, isSuccess, isError, status, fetchStatus, refetch, … }`),
|
|
9
|
+
* with stale-while-revalidate, focus/reconnect refetch, retries and garbage
|
|
10
|
+
* collection handled by the engine.
|
|
22
11
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
12
|
+
* `client` is captured for the component's lifetime — pass a stable instance.
|
|
13
|
+
*
|
|
14
|
+
* @param options - TanStack query options (`queryKey`, `queryFn`, `staleTime`, …).
|
|
15
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
16
|
+
* @returns The TanStack {@link QueryObserverResult}.
|
|
26
17
|
*
|
|
27
18
|
* @example
|
|
28
19
|
* ```tsx
|
|
29
|
-
* import { useQuery } from "katanakit-js/adapters/react";
|
|
20
|
+
* import { useQuery, useSafeQueryFn } from "katanakit-js/adapters/react";
|
|
30
21
|
* import { useGetApi } from "katanakit-js";
|
|
31
22
|
*
|
|
32
23
|
* function Pokemon({ id }: { id: number }) {
|
|
33
|
-
* const { data,
|
|
24
|
+
* const { data, isPending, isError, error } = useQuery({
|
|
34
25
|
* queryKey: ["pokemon", id],
|
|
35
|
-
* queryFn: () => useGetApi("pokeapi", "pokemonById", { params: { id } }),
|
|
26
|
+
* queryFn: useSafeQueryFn(() => useGetApi("pokeapi", "pokemonById", { params: { id } })),
|
|
36
27
|
* });
|
|
37
|
-
* if (
|
|
38
|
-
* if (
|
|
28
|
+
* if (isPending) return <div>Loading…</div>;
|
|
29
|
+
* if (isError) return <div>{error.message}</div>;
|
|
39
30
|
* return <div>{data?.name}</div>;
|
|
40
31
|
* }
|
|
41
32
|
* ```
|
|
42
33
|
*/
|
|
43
|
-
export function useQuery(
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
const
|
|
34
|
+
export function useQuery(options, client) {
|
|
35
|
+
const queryClient = client ?? useQueryClient();
|
|
36
|
+
const defaultedOptions = queryClient.defaultQueryOptions(options);
|
|
37
|
+
const [observer] = useState(() => new QueryObserver(queryClient, defaultedOptions));
|
|
38
|
+
const subscribe = useCallback((onStoreChange) => {
|
|
39
|
+
const unsubscribe = observer.subscribe(notifyManager.batchCalls(onStoreChange));
|
|
40
|
+
// Sync the optimistic result with the store after subscribing.
|
|
41
|
+
observer.updateResult();
|
|
42
|
+
return unsubscribe;
|
|
43
|
+
}, [observer]);
|
|
44
|
+
const result = useSyncExternalStore(subscribe, () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
45
|
+
// Keep the observer's options in sync on every render (TanStack's pattern).
|
|
48
46
|
useEffect(() => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const unsubscribe = qc.subscribe(qKey, (next) => {
|
|
53
|
-
if (!disposed)
|
|
54
|
-
setState(next);
|
|
55
|
-
});
|
|
56
|
-
const existing = qc.getQueryState(qKey);
|
|
57
|
-
if (existing)
|
|
58
|
-
setState(existing);
|
|
59
|
-
if (config.enabled !== false) {
|
|
60
|
-
void qc
|
|
61
|
-
.fetchQuery({
|
|
62
|
-
queryKey: qKey,
|
|
63
|
-
queryFn: config.queryFn,
|
|
64
|
-
staleTime: config.staleTime,
|
|
65
|
-
cacheTime: config.cacheTime,
|
|
66
|
-
retry: config.retry,
|
|
67
|
-
retryDelay: config.retryDelay,
|
|
68
|
-
})
|
|
69
|
-
.catch(() => {
|
|
70
|
-
// Error is already reflected in the cache state.
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return () => {
|
|
74
|
-
disposed = true;
|
|
75
|
-
unsubscribe();
|
|
76
|
-
};
|
|
77
|
-
}, [keyHash, config.enabled]);
|
|
78
|
-
const refetch = useCallback(async () => {
|
|
79
|
-
await qc
|
|
80
|
-
.fetchQuery({
|
|
81
|
-
queryKey: config.queryKey,
|
|
82
|
-
queryFn: config.queryFn,
|
|
83
|
-
staleTime: 0,
|
|
84
|
-
cacheTime: config.cacheTime,
|
|
85
|
-
retry: config.retry,
|
|
86
|
-
retryDelay: config.retryDelay,
|
|
87
|
-
})
|
|
88
|
-
.catch(() => {
|
|
89
|
-
// Error is already reflected in the cache state.
|
|
90
|
-
});
|
|
91
|
-
}, [qc, keyHash]);
|
|
92
|
-
return {
|
|
93
|
-
data: state.data,
|
|
94
|
-
error: state.error,
|
|
95
|
-
isLoading: state.isLoading,
|
|
96
|
-
isSuccess: state.isSuccess,
|
|
97
|
-
isError: state.isError,
|
|
98
|
-
isStale: state.isStale,
|
|
99
|
-
status: state.status,
|
|
100
|
-
refetch,
|
|
101
|
-
};
|
|
47
|
+
observer.setOptions(queryClient.defaultQueryOptions(options));
|
|
48
|
+
});
|
|
49
|
+
return result;
|
|
102
50
|
}
|
|
103
|
-
const IDLE_MUTATION_STATE = {
|
|
104
|
-
data: null,
|
|
105
|
-
error: null,
|
|
106
|
-
isLoading: false,
|
|
107
|
-
isSuccess: false,
|
|
108
|
-
isError: false,
|
|
109
|
-
status: "idle",
|
|
110
|
-
};
|
|
111
51
|
/**
|
|
112
|
-
* React hook for
|
|
113
|
-
*
|
|
52
|
+
* React hook for a mutation, backed by TanStack Query Core.
|
|
53
|
+
*
|
|
54
|
+
* Returns TanStack's native {@link MutationObserverResult}
|
|
55
|
+
* (`{ data, error, variables, isPending, isError, isSuccess, status, mutate, reset }`).
|
|
114
56
|
*
|
|
115
|
-
* @param
|
|
116
|
-
* @
|
|
57
|
+
* @param options - TanStack mutation options (`mutationFn`, `onSuccess`, …).
|
|
58
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
59
|
+
* @returns The TanStack {@link MutationObserverResult}.
|
|
117
60
|
*
|
|
118
61
|
* @example
|
|
119
62
|
* ```tsx
|
|
@@ -122,79 +65,22 @@ const IDLE_MUTATION_STATE = {
|
|
|
122
65
|
*
|
|
123
66
|
* function CreateUser() {
|
|
124
67
|
* const qc = useQueryClient();
|
|
125
|
-
* const { mutate,
|
|
68
|
+
* const { mutate, isPending } = useMutation({
|
|
126
69
|
* mutationFn: (name: string) => usePost("api", "createUser", { name }),
|
|
127
70
|
* onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
128
71
|
* });
|
|
129
|
-
* return <button disabled={
|
|
72
|
+
* return <button disabled={isPending} onClick={() => mutate("Ada")}>Create</button>;
|
|
130
73
|
* }
|
|
131
74
|
* ```
|
|
132
75
|
*/
|
|
133
|
-
export function useMutation(
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}));
|
|
143
|
-
try {
|
|
144
|
-
const result = await config.mutationFn(variables);
|
|
145
|
-
if (result.ok) {
|
|
146
|
-
setState({
|
|
147
|
-
data: result.data,
|
|
148
|
-
error: null,
|
|
149
|
-
isLoading: false,
|
|
150
|
-
isSuccess: true,
|
|
151
|
-
isError: false,
|
|
152
|
-
status: "success",
|
|
153
|
-
});
|
|
154
|
-
config.onSuccess?.(result.data, variables);
|
|
155
|
-
config.onSettled?.(result.data, null, variables);
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
setState({
|
|
159
|
-
data: null,
|
|
160
|
-
error: result.error,
|
|
161
|
-
isLoading: false,
|
|
162
|
-
isSuccess: false,
|
|
163
|
-
isError: true,
|
|
164
|
-
status: "error",
|
|
165
|
-
});
|
|
166
|
-
config.onError?.(result.error, variables);
|
|
167
|
-
config.onSettled?.(null, result.error, variables);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
catch (error) {
|
|
171
|
-
const apiError = error instanceof Error
|
|
172
|
-
? { message: error.message, status: 0 }
|
|
173
|
-
: { message: String(error), status: 0 };
|
|
174
|
-
setState({
|
|
175
|
-
data: null,
|
|
176
|
-
error: apiError,
|
|
177
|
-
isLoading: false,
|
|
178
|
-
isSuccess: false,
|
|
179
|
-
isError: true,
|
|
180
|
-
status: "error",
|
|
181
|
-
});
|
|
182
|
-
config.onError?.(apiError, variables);
|
|
183
|
-
config.onSettled?.(null, apiError, variables);
|
|
184
|
-
}
|
|
185
|
-
}, []);
|
|
186
|
-
const reset = useCallback(() => {
|
|
187
|
-
setState(IDLE_MUTATION_STATE);
|
|
188
|
-
}, []);
|
|
189
|
-
return {
|
|
190
|
-
data: state.data,
|
|
191
|
-
error: state.error,
|
|
192
|
-
isLoading: state.isLoading,
|
|
193
|
-
isSuccess: state.isSuccess,
|
|
194
|
-
isError: state.isError,
|
|
195
|
-
status: state.status,
|
|
196
|
-
mutate,
|
|
197
|
-
reset,
|
|
198
|
-
};
|
|
76
|
+
export function useMutation(options, client) {
|
|
77
|
+
const queryClient = client ?? useQueryClient();
|
|
78
|
+
const [observer] = useState(() => new MutationObserver(queryClient, options));
|
|
79
|
+
const subscribe = useCallback((onStoreChange) => observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer]);
|
|
80
|
+
const result = useSyncExternalStore(subscribe, () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
observer.setOptions(options);
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
199
85
|
}
|
|
200
86
|
//# sourceMappingURL=query.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/adapters/react/query.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/adapters/react/query.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAGhB,aAAa,EAEb,aAAa,GAGb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAE/E,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,QAAQ,CACvB,OAA0D,EAC1D,MAAoB;IAEpB,MAAM,WAAW,GAAG,MAAM,IAAI,cAAc,EAAE,CAAC;IAC/C,MAAM,gBAAgB,GAAG,WAAW,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IAElE,MAAM,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,aAAa,CAAC,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAEpF,MAAM,SAAS,GAAG,WAAW,CAC5B,CAAC,aAAyB,EAAE,EAAE;QAC7B,MAAM,WAAW,GAAG,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC;QAChF,+DAA+D;QAC/D,QAAQ,CAAC,YAAY,EAAE,CAAC;QACxB,OAAO,WAAW,CAAC;IACpB,CAAC,EACD,CAAC,QAAQ,CAAC,CACV,CAAC;IAEF,MAAM,MAAM,GAAG,oBAAoB,CAClC,SAAS,EACT,GAAG,EAAE,CAAC,QAAQ,CAAC,gBAAgB,EAAE,EACjC,GAAG,EAAE,CAAC,QAAQ,CAAC,gBAAgB,EAAE,CACjC,CAAC;IAEF,4EAA4E;IAC5E,SAAS,CAAC,GAAG,EAAE;QACd,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,WAAW,CAC1B,OAAqE,EACrE,MAAoB;IAEpB,MAAM,WAAW,GAAG,MAAM,IAAI,cAAc,EAAE,CAAC;IAE/C,MAAM,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,gBAAgB,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;IAE9E,MAAM,SAAS,GAAG,WAAW,CAC5B,CAAC,aAAyB,EAAE,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,EAC1F,CAAC,QAAQ,CAAC,CACV,CAAC;IAEF,MAAM,MAAM,GAAG,oBAAoB,CAClC,SAAS,EACT,GAAG,EAAE,CAAC,QAAQ,CAAC,gBAAgB,EAAE,EACjC,GAAG,EAAE,CAAC,QAAQ,CAAC,gBAAgB,EAAE,CACjC,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QACd,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,kBAAkB,EAClB,cAAc,EACd,cAAc,GACd,MAAM,sCAAsC,CAAC;AAC9C,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/solid/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/solid/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,kBAAkB,EAClB,cAAc,EACd,cAAc,GACd,MAAM,sCAAsC,CAAC;AAC9C,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -1,87 +1,45 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/** Configuration for `useQuery` (Solid primitive). */
|
|
5
|
-
export interface UseQueryConfig<T = unknown> {
|
|
6
|
-
/** Cache key (stable array). */
|
|
7
|
-
queryKey: QueryKey;
|
|
8
|
-
/** Fetcher function returning a Safe Result. */
|
|
9
|
-
queryFn: () => Promise<FetchResult<T>>;
|
|
10
|
-
/** Time in ms before cached data is stale (default: 0). */
|
|
11
|
-
staleTime?: number;
|
|
12
|
-
/** Time in ms to keep unused data (default: 5 min). */
|
|
13
|
-
cacheTime?: number;
|
|
14
|
-
/** Retry count on failure (default: 3). */
|
|
15
|
-
retry?: number;
|
|
16
|
-
/** Base delay for exponential backoff (default: 1000). */
|
|
17
|
-
retryDelay?: number;
|
|
18
|
-
/** Whether the query is enabled (default: true). */
|
|
19
|
-
enabled?: boolean;
|
|
20
|
-
}
|
|
21
|
-
/** Reactive return value of `useQuery` (Solid signals are accessors). */
|
|
22
|
-
export interface UseQueryReturn<T = unknown> {
|
|
23
|
-
data: Signal<T | null>[0];
|
|
24
|
-
error: Signal<ApiError | null>[0];
|
|
25
|
-
isLoading: Signal<boolean>[0];
|
|
26
|
-
isSuccess: Signal<boolean>[0];
|
|
27
|
-
isError: Signal<boolean>[0];
|
|
28
|
-
isStale: Signal<boolean>[0];
|
|
29
|
-
status: Signal<QueryState<T>["status"]>[0];
|
|
30
|
-
/** Manually refetch the query. */
|
|
31
|
-
refetch: () => Promise<void>;
|
|
32
|
-
}
|
|
33
|
-
/** Configuration for `useMutation`. */
|
|
34
|
-
export interface UseMutationConfig<TData = unknown, TVariables = unknown> {
|
|
35
|
-
mutationFn: (variables: TVariables) => Promise<FetchResult<TData>>;
|
|
36
|
-
onSuccess?: (data: TData, variables: TVariables) => void;
|
|
37
|
-
onError?: (error: ApiError, variables: TVariables) => void;
|
|
38
|
-
onSettled?: (data: TData | null, error: ApiError | null, variables: TVariables) => void;
|
|
39
|
-
}
|
|
40
|
-
/** Reactive return value of `useMutation`. */
|
|
41
|
-
export interface UseMutationReturn<TData = unknown, TVariables = unknown> {
|
|
42
|
-
data: Signal<TData | null>[0];
|
|
43
|
-
error: Signal<ApiError | null>[0];
|
|
44
|
-
isLoading: Signal<boolean>[0];
|
|
45
|
-
isSuccess: Signal<boolean>[0];
|
|
46
|
-
isError: Signal<boolean>[0];
|
|
47
|
-
status: Signal<"idle" | "loading" | "success" | "error">[0];
|
|
48
|
-
/** Execute the mutation. */
|
|
49
|
-
mutate: (variables: TVariables) => Promise<void>;
|
|
50
|
-
/** Reset the mutation state. */
|
|
51
|
-
reset: () => void;
|
|
52
|
-
}
|
|
1
|
+
import { type MutationObserverOptions, type MutationObserverResult, type QueryClient, type QueryObserverOptions, type QueryObserverResult } from "@tanstack/query-core";
|
|
2
|
+
/** Options for the Solid binding: a plain object or a reactive getter. */
|
|
3
|
+
export type SolidQueryOptions<TQueryFnData, TError, TData> = QueryObserverOptions<TQueryFnData, TError, TData> | (() => QueryObserverOptions<TQueryFnData, TError, TData>);
|
|
53
4
|
/**
|
|
54
|
-
* Solid primitive
|
|
55
|
-
* Integrates with the API manager — your `queryFn` typically calls `useGetApi`
|
|
56
|
-
* or `useFetch` and returns the Safe Result.
|
|
5
|
+
* Solid primitive for a cached query, backed by TanStack Query Core.
|
|
57
6
|
*
|
|
58
|
-
* @
|
|
59
|
-
*
|
|
60
|
-
*
|
|
7
|
+
* Returns a reactive {@link QueryObserverResult} store — read `query.data`,
|
|
8
|
+
* `query.isPending`, `query.isError`, `query.status`, etc. directly. Pass a
|
|
9
|
+
* getter (`() => ({ queryKey, queryFn })`) to react to key changes.
|
|
10
|
+
*
|
|
11
|
+
* Call it inside a component or `createRoot` so `onCleanup` disposes the observer.
|
|
12
|
+
*
|
|
13
|
+
* @param options - TanStack query options, or a getter returning them.
|
|
14
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
15
|
+
* @returns A reactive {@link QueryObserverResult} store.
|
|
61
16
|
*
|
|
62
17
|
* @example
|
|
63
18
|
* ```tsx
|
|
64
|
-
* import {
|
|
19
|
+
* import { Show } from "solid-js";
|
|
20
|
+
* import { useQuery, useSafeQueryFn } from "katanakit-js/adapters/solid";
|
|
65
21
|
* import { useGetApi } from "katanakit-js";
|
|
66
22
|
*
|
|
67
23
|
* function Pokemon() {
|
|
68
|
-
* const
|
|
24
|
+
* const query = useQuery(() => ({
|
|
69
25
|
* queryKey: ["pokemon", 25],
|
|
70
|
-
* queryFn: () => useGetApi("pokeapi", "pokemonById", { params: { id: 25 } }),
|
|
71
|
-
* });
|
|
72
|
-
* return
|
|
73
|
-
* <
|
|
74
|
-
*
|
|
26
|
+
* queryFn: useSafeQueryFn(() => useGetApi("pokeapi", "pokemonById", { params: { id: 25 } })),
|
|
27
|
+
* }));
|
|
28
|
+
* return (
|
|
29
|
+
* <Show when={!query.isPending} fallback={<div>Loading…</div>}>
|
|
30
|
+
* <div>{query.data?.name}</div>
|
|
31
|
+
* </Show>
|
|
32
|
+
* );
|
|
75
33
|
* }
|
|
76
34
|
* ```
|
|
77
35
|
*/
|
|
78
|
-
export declare function useQuery<
|
|
36
|
+
export declare function useQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData>(options: SolidQueryOptions<TQueryFnData, TError, TData>, client?: QueryClient): QueryObserverResult<TData, TError>;
|
|
79
37
|
/**
|
|
80
|
-
* Solid primitive for
|
|
81
|
-
* Does not cache — returns a `mutate` function that executes the mutation.
|
|
38
|
+
* Solid primitive for a mutation, backed by TanStack Query Core.
|
|
82
39
|
*
|
|
83
|
-
* @param
|
|
84
|
-
* @
|
|
40
|
+
* @param options - TanStack mutation options, or a getter returning them.
|
|
41
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
42
|
+
* @returns A reactive {@link MutationObserverResult} store.
|
|
85
43
|
*
|
|
86
44
|
* @example
|
|
87
45
|
* ```tsx
|
|
@@ -90,13 +48,13 @@ export declare function useQuery<T>(config: UseQueryConfig<T>, client?: QueryCli
|
|
|
90
48
|
*
|
|
91
49
|
* function CreateUser() {
|
|
92
50
|
* const qc = useQueryClient();
|
|
93
|
-
* const
|
|
51
|
+
* const mutation = useMutation(() => ({
|
|
94
52
|
* mutationFn: (name: string) => usePost("api", "createUser", { name }),
|
|
95
53
|
* onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
96
|
-
* });
|
|
97
|
-
* return <button disabled={
|
|
54
|
+
* }));
|
|
55
|
+
* return <button disabled={mutation.isPending} onClick={() => mutation.mutate("Ada")}>Create</button>;
|
|
98
56
|
* }
|
|
99
57
|
* ```
|
|
100
58
|
*/
|
|
101
|
-
export declare function useMutation<TData = unknown, TVariables = unknown>(
|
|
59
|
+
export declare function useMutation<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(options: MutationObserverOptions<TData, TError, TVariables, TContext> | (() => MutationObserverOptions<TData, TError, TVariables, TContext>), client?: QueryClient): MutationObserverResult<TData, TError, TVariables, TContext>;
|
|
102
60
|
//# sourceMappingURL=query.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid/query.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAC3B,KAAK,WAAW,EAEhB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,MAAM,sBAAsB,CAAC;AAM9B,0EAA0E;AAC1E,MAAM,MAAM,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,IACtD,oBAAoB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,GACjD,CAAC,MAAM,oBAAoB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,QAAQ,CAAC,YAAY,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,KAAK,GAAG,YAAY,EACpF,OAAO,EAAE,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,EACvD,MAAM,CAAC,EAAE,WAAW,GAClB,mBAAmB,CAAC,KAAK,EAAE,MAAM,CAAC,CAsBpC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,UAAU,GAAG,IAAI,EAAE,QAAQ,GAAG,OAAO,EACjG,OAAO,EACJ,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,GAC5D,CAAC,MAAM,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,EACvE,MAAM,CAAC,EAAE,WAAW,GAClB,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAqB7D"}
|