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,93 +1,65 @@
|
|
|
1
|
-
import { DestroyRef, inject, signal } from "@angular/core";
|
|
2
|
-
import {
|
|
1
|
+
import { computed, DestroyRef, inject, signal } from "@angular/core";
|
|
2
|
+
import { MutationObserver, QueryObserver, } from "@tanstack/query-core";
|
|
3
|
+
import { useQueryClient } from "../../core/services/query.service.js";
|
|
3
4
|
/**
|
|
4
|
-
* Angular signal factory
|
|
5
|
-
* Integrates with the API manager — your `queryFn` typically calls `useGetApi`
|
|
6
|
-
* or `useFetch` and returns the Safe Result.
|
|
5
|
+
* Angular signal factory for a cached query, backed by TanStack Query Core.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* `DestroyRef`. Requires Angular 16+ signals.
|
|
7
|
+
* Returns one signal per field — `query.data()`, `query.isPending()`,
|
|
8
|
+
* `query.isError()`, `query.status()`, etc.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @
|
|
10
|
+
* Must be called within an injection context (so it auto-cleans via `DestroyRef`).
|
|
11
|
+
*
|
|
12
|
+
* @param options - TanStack query options (`queryKey`, `queryFn`, `staleTime`, …).
|
|
13
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
14
|
+
* @returns An {@link AngularQueryResult} of signals.
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
|
-
* import { Component
|
|
19
|
-
* import { useQuery } from "katanakit-js/adapters/angular";
|
|
18
|
+
* import { Component } from "@angular/core";
|
|
19
|
+
* import { useQuery, useSafeQueryFn } from "katanakit-js/adapters/angular";
|
|
20
20
|
* import { useGetApi } from "katanakit-js";
|
|
21
21
|
*
|
|
22
|
-
* @Component({
|
|
22
|
+
* @Component({
|
|
23
|
+
* selector: "app-pokemon",
|
|
24
|
+
* template: `@if (query.isPending()) { Loading… } @else { {{ query.data()?.name }} }`,
|
|
25
|
+
* })
|
|
23
26
|
* export class PokemonComponent {
|
|
24
|
-
* readonly
|
|
27
|
+
* readonly query = useQuery({
|
|
28
|
+
* queryKey: ["pokemon", 25],
|
|
29
|
+
* queryFn: useSafeQueryFn(() => useGetApi("pokeapi", "pokemonById", { params: { id: 25 } })),
|
|
30
|
+
* });
|
|
25
31
|
* }
|
|
26
32
|
* ```
|
|
27
33
|
*/
|
|
28
|
-
export function useQuery(
|
|
29
|
-
const
|
|
34
|
+
export function useQuery(options, client) {
|
|
35
|
+
const queryClient = client ?? useQueryClient();
|
|
30
36
|
const destroyRef = inject(DestroyRef);
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const isSuccess = signal(false);
|
|
35
|
-
const isError = signal(false);
|
|
36
|
-
const isStale = signal(true);
|
|
37
|
-
const status = signal("idle");
|
|
38
|
-
const sync = (state) => {
|
|
39
|
-
data.set(state.data);
|
|
40
|
-
error.set(state.error);
|
|
41
|
-
isLoading.set(state.isLoading);
|
|
42
|
-
isSuccess.set(state.isSuccess);
|
|
43
|
-
isError.set(state.isError);
|
|
44
|
-
isStale.set(state.isStale);
|
|
45
|
-
status.set(state.status);
|
|
46
|
-
};
|
|
47
|
-
const qKey = config.queryKey;
|
|
48
|
-
const unsubscribe = qc.subscribe(qKey, (next) => sync(next));
|
|
49
|
-
const existing = qc.getQueryState(qKey);
|
|
50
|
-
if (existing)
|
|
51
|
-
sync(existing);
|
|
52
|
-
if (config.enabled !== false) {
|
|
53
|
-
void qc
|
|
54
|
-
.fetchQuery({
|
|
55
|
-
queryKey: qKey,
|
|
56
|
-
queryFn: config.queryFn,
|
|
57
|
-
staleTime: config.staleTime,
|
|
58
|
-
cacheTime: config.cacheTime,
|
|
59
|
-
retry: config.retry,
|
|
60
|
-
retryDelay: config.retryDelay,
|
|
61
|
-
})
|
|
62
|
-
.catch(() => {
|
|
63
|
-
// Error is already reflected in the cache state.
|
|
64
|
-
});
|
|
65
|
-
}
|
|
37
|
+
const observer = new QueryObserver(queryClient, queryClient.defaultQueryOptions(options));
|
|
38
|
+
const result = signal(observer.getCurrentResult());
|
|
39
|
+
const unsubscribe = observer.subscribe((next) => result.set(next));
|
|
66
40
|
destroyRef.onDestroy(unsubscribe);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
});
|
|
41
|
+
return {
|
|
42
|
+
data: computed(() => result().data),
|
|
43
|
+
error: computed(() => result().error),
|
|
44
|
+
isPending: computed(() => result().isPending),
|
|
45
|
+
isFetching: computed(() => result().isFetching),
|
|
46
|
+
isLoading: computed(() => result().isLoading),
|
|
47
|
+
isSuccess: computed(() => result().isSuccess),
|
|
48
|
+
isError: computed(() => result().isError),
|
|
49
|
+
isStale: computed(() => result().isStale),
|
|
50
|
+
status: computed(() => result().status),
|
|
51
|
+
fetchStatus: computed(() => result().fetchStatus),
|
|
52
|
+
refetch: (refetchOptions) => observer.refetch(refetchOptions),
|
|
80
53
|
};
|
|
81
|
-
return { data, error, isLoading, isSuccess, isError, isStale, status, refetch };
|
|
82
54
|
}
|
|
83
55
|
/**
|
|
84
|
-
* Angular signal factory for
|
|
85
|
-
* Does not cache — returns a `mutate` function that executes the mutation.
|
|
56
|
+
* Angular signal factory for a mutation, backed by TanStack Query Core.
|
|
86
57
|
*
|
|
87
|
-
* Must be called within an injection context
|
|
58
|
+
* Must be called within an injection context (so it auto-cleans via `DestroyRef`).
|
|
88
59
|
*
|
|
89
|
-
* @param
|
|
90
|
-
* @
|
|
60
|
+
* @param options - TanStack mutation options (`mutationFn`, `onSuccess`, …).
|
|
61
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
62
|
+
* @returns An {@link AngularMutationResult} of signals.
|
|
91
63
|
*
|
|
92
64
|
* @example
|
|
93
65
|
* ```ts
|
|
@@ -95,72 +67,37 @@ export function useQuery(config, client) {
|
|
|
95
67
|
* import { useMutation, useQueryClient } from "katanakit-js/adapters/angular";
|
|
96
68
|
* import { usePost } from "katanakit-js";
|
|
97
69
|
*
|
|
98
|
-
* @Component({
|
|
70
|
+
* @Component({
|
|
71
|
+
* selector: "app-create",
|
|
72
|
+
* template: `<button [disabled]="mutation.isPending()" (click)="mutation.mutate('Ada')">Create</button>`,
|
|
73
|
+
* })
|
|
99
74
|
* export class CreateUserComponent {
|
|
100
75
|
* private readonly qc = useQueryClient();
|
|
101
|
-
* readonly
|
|
76
|
+
* readonly mutation = useMutation({
|
|
102
77
|
* mutationFn: (name: string) => usePost("api", "createUser", { name }),
|
|
103
78
|
* onSuccess: () => this.qc.invalidateQueries({ queryKey: ["users"] }),
|
|
104
79
|
* });
|
|
105
|
-
* readonly isLoading = this.result.isLoading;
|
|
106
|
-
* readonly mutate = this.result.mutate;
|
|
107
80
|
* }
|
|
108
81
|
* ```
|
|
109
82
|
*/
|
|
110
|
-
export function useMutation(
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
status.set("success");
|
|
129
|
-
config.onSuccess?.(result.data, variables);
|
|
130
|
-
config.onSettled?.(result.data, null, variables);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
data.set(null);
|
|
134
|
-
error.set(result.error);
|
|
135
|
-
isError.set(true);
|
|
136
|
-
status.set("error");
|
|
137
|
-
config.onError?.(result.error, variables);
|
|
138
|
-
config.onSettled?.(null, result.error, variables);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
catch (caught) {
|
|
142
|
-
const apiError = caught instanceof Error
|
|
143
|
-
? { message: caught.message, status: 0 }
|
|
144
|
-
: { message: String(caught), status: 0 };
|
|
145
|
-
data.set(null);
|
|
146
|
-
error.set(apiError);
|
|
147
|
-
isError.set(true);
|
|
148
|
-
status.set("error");
|
|
149
|
-
config.onError?.(apiError, variables);
|
|
150
|
-
config.onSettled?.(null, apiError, variables);
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
153
|
-
isLoading.set(false);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
const reset = () => {
|
|
157
|
-
data.set(null);
|
|
158
|
-
error.set(null);
|
|
159
|
-
isLoading.set(false);
|
|
160
|
-
isSuccess.set(false);
|
|
161
|
-
isError.set(false);
|
|
162
|
-
status.set("idle");
|
|
83
|
+
export function useMutation(options, client) {
|
|
84
|
+
const queryClient = client ?? useQueryClient();
|
|
85
|
+
const destroyRef = inject(DestroyRef);
|
|
86
|
+
const observer = new MutationObserver(queryClient, options);
|
|
87
|
+
const result = signal(observer.getCurrentResult());
|
|
88
|
+
const unsubscribe = observer.subscribe((next) => result.set(next));
|
|
89
|
+
destroyRef.onDestroy(unsubscribe);
|
|
90
|
+
return {
|
|
91
|
+
data: computed(() => result().data),
|
|
92
|
+
error: computed(() => result().error),
|
|
93
|
+
variables: computed(() => result().variables),
|
|
94
|
+
isPending: computed(() => result().isPending),
|
|
95
|
+
isSuccess: computed(() => result().isSuccess),
|
|
96
|
+
isError: computed(() => result().isError),
|
|
97
|
+
isIdle: computed(() => result().isIdle),
|
|
98
|
+
status: computed(() => result().status),
|
|
99
|
+
mutate: (variables) => observer.mutate(variables),
|
|
100
|
+
reset: () => observer.reset(),
|
|
163
101
|
};
|
|
164
|
-
return { data, error, isLoading, isSuccess, isError, status, mutate, reset };
|
|
165
102
|
}
|
|
166
103
|
//# sourceMappingURL=query.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/adapters/angular/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAe,MAAM,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/adapters/angular/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAe,MAAM,EAAE,MAAM,eAAe,CAAC;AAClF,OAAO,EAEN,gBAAgB,EAIhB,aAAa,GAKb,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AA+BtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,QAAQ,CACvB,OAA0D,EAC1D,MAAoB;IAEpB,MAAM,WAAW,GAAG,MAAM,IAAI,cAAc,EAAE,CAAC;IAC/C,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;IAEtC,MAAM,QAAQ,GAAG,IAAI,aAAa,CAAC,WAAW,EAAE,WAAW,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1F,MAAM,MAAM,GAAG,MAAM,CAAqC,QAAQ,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAEvF,MAAM,WAAW,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC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AC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/react/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/react/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/react/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,89 +1,44 @@
|
|
|
1
|
-
import { type QueryClient, type
|
|
2
|
-
import type { ApiError, FetchResult } from "../../types/index.js";
|
|
3
|
-
/** Configuration for `useQuery` (React hook). */
|
|
4
|
-
export interface UseQueryConfig<T = unknown> {
|
|
5
|
-
/** Cache key — the query refetches when its serialized value changes. */
|
|
6
|
-
queryKey: QueryKey;
|
|
7
|
-
/** Fetcher function returning a Safe Result. */
|
|
8
|
-
queryFn: () => Promise<FetchResult<T>>;
|
|
9
|
-
/** Time in ms before cached data is stale (default: 0). */
|
|
10
|
-
staleTime?: number;
|
|
11
|
-
/** Time in ms to keep unused data (default: 5 min). */
|
|
12
|
-
cacheTime?: number;
|
|
13
|
-
/** Retry count on failure (default: 3). */
|
|
14
|
-
retry?: number;
|
|
15
|
-
/** Base delay for exponential backoff (default: 1000). */
|
|
16
|
-
retryDelay?: number;
|
|
17
|
-
/** Whether the query is enabled (default: true). */
|
|
18
|
-
enabled?: boolean;
|
|
19
|
-
}
|
|
20
|
-
/** Reactive return value of `useQuery`. */
|
|
21
|
-
export interface UseQueryReturn<T = unknown> {
|
|
22
|
-
data: T | null;
|
|
23
|
-
error: ApiError | null;
|
|
24
|
-
isLoading: boolean;
|
|
25
|
-
isSuccess: boolean;
|
|
26
|
-
isError: boolean;
|
|
27
|
-
isStale: boolean;
|
|
28
|
-
status: QueryState<T>["status"];
|
|
29
|
-
/** Manually refetch the query. */
|
|
30
|
-
refetch: () => Promise<void>;
|
|
31
|
-
}
|
|
32
|
-
/** Configuration for `useMutation`. */
|
|
33
|
-
export interface UseMutationConfig<TData = unknown, TVariables = unknown> {
|
|
34
|
-
mutationFn: (variables: TVariables) => Promise<FetchResult<TData>>;
|
|
35
|
-
onSuccess?: (data: TData, variables: TVariables) => void;
|
|
36
|
-
onError?: (error: ApiError, variables: TVariables) => void;
|
|
37
|
-
onSettled?: (data: TData | null, error: ApiError | null, variables: TVariables) => void;
|
|
38
|
-
}
|
|
39
|
-
/** Reactive return value of `useMutation`. */
|
|
40
|
-
export interface UseMutationReturn<TData = unknown, TVariables = unknown> {
|
|
41
|
-
data: TData | null;
|
|
42
|
-
error: ApiError | null;
|
|
43
|
-
isLoading: boolean;
|
|
44
|
-
isSuccess: boolean;
|
|
45
|
-
isError: boolean;
|
|
46
|
-
status: "idle" | "loading" | "success" | "error";
|
|
47
|
-
/** Execute the mutation. */
|
|
48
|
-
mutate: (variables: TVariables) => Promise<void>;
|
|
49
|
-
/** Reset the mutation state. */
|
|
50
|
-
reset: () => void;
|
|
51
|
-
}
|
|
1
|
+
import { type MutationObserverOptions, type MutationObserverResult, type QueryClient, type QueryObserverOptions, type QueryObserverResult } from "@tanstack/query-core";
|
|
52
2
|
/**
|
|
53
|
-
* React hook
|
|
54
|
-
* Integrates with the API manager — your `queryFn` typically calls `useGetApi`
|
|
55
|
-
* or `useFetch` and returns the Safe Result.
|
|
3
|
+
* React hook for a cached query, backed by TanStack Query Core.
|
|
56
4
|
*
|
|
57
|
-
*
|
|
58
|
-
* `
|
|
5
|
+
* Returns TanStack's native {@link QueryObserverResult}
|
|
6
|
+
* (`{ data, error, isPending, isFetching, isSuccess, isError, status, fetchStatus, refetch, … }`),
|
|
7
|
+
* with stale-while-revalidate, focus/reconnect refetch, retries and garbage
|
|
8
|
+
* collection handled by the engine.
|
|
59
9
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
10
|
+
* `client` is captured for the component's lifetime — pass a stable instance.
|
|
11
|
+
*
|
|
12
|
+
* @param options - TanStack query options (`queryKey`, `queryFn`, `staleTime`, …).
|
|
13
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
14
|
+
* @returns The TanStack {@link QueryObserverResult}.
|
|
63
15
|
*
|
|
64
16
|
* @example
|
|
65
17
|
* ```tsx
|
|
66
|
-
* import { useQuery } from "katanakit-js/adapters/react";
|
|
18
|
+
* import { useQuery, useSafeQueryFn } from "katanakit-js/adapters/react";
|
|
67
19
|
* import { useGetApi } from "katanakit-js";
|
|
68
20
|
*
|
|
69
21
|
* function Pokemon({ id }: { id: number }) {
|
|
70
|
-
* const { data,
|
|
22
|
+
* const { data, isPending, isError, error } = useQuery({
|
|
71
23
|
* queryKey: ["pokemon", id],
|
|
72
|
-
* queryFn: () => useGetApi("pokeapi", "pokemonById", { params: { id } }),
|
|
24
|
+
* queryFn: useSafeQueryFn(() => useGetApi("pokeapi", "pokemonById", { params: { id } })),
|
|
73
25
|
* });
|
|
74
|
-
* if (
|
|
75
|
-
* if (
|
|
26
|
+
* if (isPending) return <div>Loading…</div>;
|
|
27
|
+
* if (isError) return <div>{error.message}</div>;
|
|
76
28
|
* return <div>{data?.name}</div>;
|
|
77
29
|
* }
|
|
78
30
|
* ```
|
|
79
31
|
*/
|
|
80
|
-
export declare function useQuery<
|
|
32
|
+
export declare function useQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData>(options: QueryObserverOptions<TQueryFnData, TError, TData>, client?: QueryClient): QueryObserverResult<TData, TError>;
|
|
81
33
|
/**
|
|
82
|
-
* React hook for
|
|
83
|
-
*
|
|
34
|
+
* React hook for a mutation, backed by TanStack Query Core.
|
|
35
|
+
*
|
|
36
|
+
* Returns TanStack's native {@link MutationObserverResult}
|
|
37
|
+
* (`{ data, error, variables, isPending, isError, isSuccess, status, mutate, reset }`).
|
|
84
38
|
*
|
|
85
|
-
* @param
|
|
86
|
-
* @
|
|
39
|
+
* @param options - TanStack mutation options (`mutationFn`, `onSuccess`, …).
|
|
40
|
+
* @param client - Optional {@link QueryClient} (defaults to the shared singleton).
|
|
41
|
+
* @returns The TanStack {@link MutationObserverResult}.
|
|
87
42
|
*
|
|
88
43
|
* @example
|
|
89
44
|
* ```tsx
|
|
@@ -92,13 +47,13 @@ export declare function useQuery<T>(config: UseQueryConfig<T>, client?: QueryCli
|
|
|
92
47
|
*
|
|
93
48
|
* function CreateUser() {
|
|
94
49
|
* const qc = useQueryClient();
|
|
95
|
-
* const { mutate,
|
|
50
|
+
* const { mutate, isPending } = useMutation({
|
|
96
51
|
* mutationFn: (name: string) => usePost("api", "createUser", { name }),
|
|
97
52
|
* onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
98
53
|
* });
|
|
99
|
-
* return <button disabled={
|
|
54
|
+
* return <button disabled={isPending} onClick={() => mutate("Ada")}>Create</button>;
|
|
100
55
|
* }
|
|
101
56
|
* ```
|
|
102
57
|
*/
|
|
103
|
-
export declare function useMutation<TData = unknown, TVariables = unknown>(
|
|
58
|
+
export declare function useMutation<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(options: MutationObserverOptions<TData, TError, TVariables, TContext>, client?: QueryClient): MutationObserverResult<TData, TError, TVariables, TContext>;
|
|
104
59
|
//# sourceMappingURL=query.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../src/adapters/react/query.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../src/adapters/react/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAE3B,KAAK,WAAW,EAEhB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,MAAM,sBAAsB,CAAC;AAK9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,QAAQ,CAAC,YAAY,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,KAAK,GAAG,YAAY,EACpF,OAAO,EAAE,oBAAoB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,EAC1D,MAAM,CAAC,EAAE,WAAW,GAClB,mBAAmB,CAAC,KAAK,EAAE,MAAM,CAAC,CA4BpC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,UAAU,GAAG,IAAI,EAAE,QAAQ,GAAG,OAAO,EACjG,OAAO,EAAE,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,EACrE,MAAM,CAAC,EAAE,WAAW,GAClB,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAqB7D"}
|