@zenstackhq/tanstack-query 1.0.0-beta.7 → 1.0.0-beta.9

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/res/shared.ts DELETED
@@ -1,50 +0,0 @@
1
- /**
2
- * The default query endpoint.
3
- */
4
- export const DEFAULT_QUERY_ENDPOINT = '/api/model';
5
-
6
- /**
7
- * Prefix for react-query keys.
8
- */
9
- export const QUERY_KEY_PREFIX = 'zenstack:';
10
-
11
- /**
12
- * Function signature for `fetch`.
13
- */
14
- export type FetchFn = (url: string, options?: RequestInit) => Promise<Response>;
15
-
16
- /**
17
- * Context type for configuring the hooks.
18
- */
19
- export type RequestHandlerContext = {
20
- /**
21
- * The endpoint to use for the queries.
22
- */
23
- endpoint: string;
24
-
25
- /**
26
- * A custom fetch function for sending the HTTP requests.
27
- */
28
- fetch?: FetchFn;
29
- };
30
-
31
- async function fetcher<R>(url: string, options?: RequestInit, fetch?: FetchFn) {
32
- const _fetch = fetch ?? window.fetch;
33
- const res = await _fetch(url, options);
34
- if (!res.ok) {
35
- const error: Error & { info?: unknown; status?: number } = new Error(
36
- 'An error occurred while fetching the data.'
37
- );
38
- error.info = unmarshal(await res.text());
39
- error.status = res.status;
40
- throw error;
41
- }
42
-
43
- const textResult = await res.text();
44
- try {
45
- return unmarshal(textResult) as R;
46
- } catch (err) {
47
- console.error(`Unable to deserialize data:`, textResult);
48
- throw err;
49
- }
50
- }
@@ -1,155 +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>, fetch?: FetchFn) {
29
- const reqUrl = makeUrl(url, args);
30
- return createQuery<R>({
31
- queryKey: [QUERY_KEY_PREFIX + model, url, args],
32
- queryFn: () => fetcher<R>(reqUrl, undefined, fetch),
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
- fetch?: FetchFn,
51
- invalidateQueries = true
52
- ) {
53
- const queryClient = useQueryClient();
54
- const mutationFn = (data: any) =>
55
- fetcher<R>(
56
- url,
57
- {
58
- method: 'POST',
59
- headers: {
60
- 'content-type': 'application/json',
61
- },
62
- body: marshal(data),
63
- },
64
- fetch
65
- );
66
-
67
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
68
- const mutation = createMutation<R, unknown, T>(finalOptions);
69
- return mutation;
70
- }
71
-
72
- /**
73
- * Creates a PUT mutation with svelte-query.
74
- *
75
- * @param model The name of the model under mutation.
76
- * @param url The request URL.
77
- * @param options The svelte-query options.
78
- * @param invalidateQueries Whether to invalidate queries after mutation.
79
- * @returns useMutation hooks
80
- */
81
- export function putMutation<T, R = any>(
82
- model: string,
83
- url: string,
84
- options?: Omit<MutationOptions<R, unknown, T>, 'mutationFn'>,
85
- fetch?: FetchFn,
86
- invalidateQueries = true
87
- ) {
88
- const queryClient = useQueryClient();
89
- const mutationFn = (data: any) =>
90
- fetcher<R>(
91
- url,
92
- {
93
- method: 'PUT',
94
- headers: {
95
- 'content-type': 'application/json',
96
- },
97
- body: marshal(data),
98
- },
99
- fetch
100
- );
101
-
102
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
103
- const mutation = createMutation<R, unknown, T>(finalOptions);
104
- return mutation;
105
- }
106
-
107
- /**
108
- * Creates a DELETE mutation with svelte-query.
109
- *
110
- * @param model The name of the model under mutation.
111
- * @param url The request URL.
112
- * @param options The svelte-query options.
113
- * @param invalidateQueries Whether to invalidate queries after mutation.
114
- * @returns useMutation hooks
115
- */
116
- export function deleteMutation<T, R = any>(
117
- model: string,
118
- url: string,
119
- options?: Omit<MutationOptions<R, unknown, T>, 'mutationFn'>,
120
- fetch?: FetchFn,
121
- invalidateQueries = true
122
- ) {
123
- const queryClient = useQueryClient();
124
- const mutationFn = (data: any) =>
125
- fetcher<R>(
126
- makeUrl(url, data),
127
- {
128
- method: 'DELETE',
129
- },
130
- fetch
131
- );
132
-
133
- const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient);
134
- const mutation = createMutation<R, unknown, T>(finalOptions);
135
- return mutation;
136
- }
137
-
138
- function mergeOptions<T, R = any>(
139
- model: string,
140
- options: Omit<MutationOptions<R, unknown, T, unknown>, 'mutationFn'> | undefined,
141
- invalidateQueries: boolean,
142
- mutationFn: MutateFunction<R, unknown, T>,
143
- queryClient: QueryClient
144
- ): MutationOptions<R, unknown, T, unknown> {
145
- const result = { ...options, mutationFn };
146
- if (options?.onSuccess || invalidateQueries) {
147
- result.onSuccess = (...args) => {
148
- if (invalidateQueries) {
149
- queryClient.invalidateQueries([QUERY_KEY_PREFIX + model]);
150
- }
151
- return options?.onSuccess?.(...args);
152
- };
153
- }
154
- return result;
155
- }