@zenstackhq/tanstack-query 1.0.0-beta.1 → 1.0.0-beta.11

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.
@@ -1,140 +0,0 @@
1
- /* eslint-disable */
2
-
3
- import {
4
- createMutation,
5
- createQuery,
6
- useQueryClient,
7
- type MutateFunction,
8
- type MutationOptions,
9
- type QueryClient,
10
- type QueryOptions,
11
- } from '@tanstack/svelte-query';
12
-
13
- /**
14
- * Key for setting and getting the global query context.
15
- */
16
- export const SvelteQueryContextKey = 'zenstack-svelte-query-context';
17
-
18
- /**
19
- * Creates a svelte-query query.
20
- *
21
- * @param model The name of the model under query.
22
- * @param url The request URL.
23
- * @param args The request args object, URL-encoded and appended as "?q=" parameter
24
- * @param options The svelte-query options object
25
- * @returns useQuery hook
26
- */
27
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- export function query<R>(model: string, url: string, args?: unknown, options?: QueryOptions<R>) {
29
- const reqUrl = makeUrl(url, args);
30
- return createQuery<R>({
31
- queryKey: [QUERY_KEY_PREFIX + model, url, args],
32
- queryFn: () => fetcher<R>(reqUrl),
33
- ...options,
34
- });
35
- }
36
-
37
- /**
38
- * Creates a POST mutation with svelte-query.
39
- *
40
- * @param model The name of the model under mutation.
41
- * @param url The request URL.
42
- * @param options The svelte-query options.
43
- * @param invalidateQueries Whether to invalidate queries after mutation.
44
- * @returns useMutation hooks
45
- */
46
- export function postMutation<T, R = any>(
47
- model: string,
48
- url: string,
49
- options?: Omit<MutationOptions<R, unknown, T>, 'mutationFn'>,
50
- invalidateQueries = true
51
- ) {
52
- const queryClient = useQueryClient();
53
- const mutationFn = (data: any) =>
54
- fetcher<R>(url, {
55
- method: 'POST',
56
- headers: {
57
- 'content-type': 'application/json',
58
- },
59
- body: marshal(data),
60
- });
61
-
62
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
63
- const mutation = createMutation<R, unknown, T>(finalOptions);
64
- return mutation;
65
- }
66
-
67
- /**
68
- * Creates a PUT mutation with svelte-query.
69
- *
70
- * @param model The name of the model under mutation.
71
- * @param url The request URL.
72
- * @param options The svelte-query options.
73
- * @param invalidateQueries Whether to invalidate queries after mutation.
74
- * @returns useMutation hooks
75
- */
76
- export function putMutation<T, R = any>(
77
- model: string,
78
- url: string,
79
- options?: Omit<MutationOptions<R, unknown, T>, 'mutationFn'>,
80
- invalidateQueries = true
81
- ) {
82
- const queryClient = useQueryClient();
83
- const mutationFn = (data: any) =>
84
- fetcher<R>(url, {
85
- method: 'PUT',
86
- headers: {
87
- 'content-type': 'application/json',
88
- },
89
- body: marshal(data),
90
- });
91
-
92
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
93
- const mutation = createMutation<R, unknown, T>(finalOptions);
94
- return mutation;
95
- }
96
-
97
- /**
98
- * Creates a DELETE mutation with svelte-query.
99
- *
100
- * @param model The name of the model under mutation.
101
- * @param url The request URL.
102
- * @param options The svelte-query options.
103
- * @param invalidateQueries Whether to invalidate queries after mutation.
104
- * @returns useMutation hooks
105
- */
106
- export function deleteMutation<T, R = any>(
107
- model: string,
108
- url: string,
109
- options?: Omit<MutationOptions<R, unknown, T>, 'mutationFn'>,
110
- invalidateQueries = true
111
- ) {
112
- const queryClient = useQueryClient();
113
- const mutationFn = (data: any) =>
114
- fetcher<R>(makeUrl(url, data), {
115
- method: 'DELETE',
116
- });
117
-
118
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
119
- const mutation = createMutation<R, unknown, T>(finalOptions);
120
- return mutation;
121
- }
122
-
123
- function mergeOptions<T, R = any>(
124
- model: string,
125
- options: Omit<MutationOptions<R, unknown, T, unknown>, 'mutationFn'> | undefined,
126
- invalidateQueries: boolean,
127
- mutationFn: MutateFunction<R, unknown, T>,
128
- queryClient: QueryClient
129
- ): MutationOptions<R, unknown, T, unknown> {
130
- const result = { ...options, mutationFn };
131
- if (options?.onSuccess || invalidateQueries) {
132
- result.onSuccess = (...args) => {
133
- if (invalidateQueries) {
134
- queryClient.invalidateQueries([QUERY_KEY_PREFIX + model]);
135
- }
136
- return options?.onSuccess?.(...args);
137
- };
138
- }
139
- return result;
140
- }