@zenstackhq/tanstack-query 3.0.0 → 3.1.1

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.
Files changed (56) hide show
  1. package/dist/common/client.d.ts +4 -0
  2. package/dist/common/client.js +38 -0
  3. package/dist/common/client.js.map +1 -0
  4. package/dist/common/query-key.d.ts +39 -0
  5. package/dist/common/query-key.js +38 -0
  6. package/dist/common/query-key.js.map +1 -0
  7. package/dist/common/types.d.ts +60 -0
  8. package/dist/common/types.js +2 -0
  9. package/dist/common/types.js.map +1 -0
  10. package/dist/react.d.ts +99 -150
  11. package/dist/react.js +248 -1178
  12. package/dist/react.js.map +1 -1
  13. package/dist/svelte/index.svelte.d.ts +79 -0
  14. package/dist/svelte/index.svelte.js +245 -0
  15. package/dist/vue.d.ts +313 -342
  16. package/dist/vue.js +224 -1138
  17. package/dist/vue.js.map +1 -1
  18. package/package.json +36 -48
  19. package/.turbo/turbo-build.log +0 -47
  20. package/dist/react.cjs +0 -1240
  21. package/dist/react.cjs.map +0 -1
  22. package/dist/react.d.cts +0 -616
  23. package/dist/svelte.cjs +0 -1224
  24. package/dist/svelte.cjs.map +0 -1
  25. package/dist/svelte.d.cts +0 -381
  26. package/dist/svelte.d.ts +0 -381
  27. package/dist/svelte.js +0 -1183
  28. package/dist/svelte.js.map +0 -1
  29. package/dist/types-C8iIZD-7.d.cts +0 -99
  30. package/dist/types-C8iIZD-7.d.ts +0 -99
  31. package/dist/vue.cjs +0 -1192
  32. package/dist/vue.cjs.map +0 -1
  33. package/dist/vue.d.cts +0 -382
  34. package/eslint.config.js +0 -4
  35. package/src/react.ts +0 -562
  36. package/src/svelte.ts +0 -502
  37. package/src/utils/common.ts +0 -448
  38. package/src/utils/mutator.ts +0 -441
  39. package/src/utils/nested-read-visitor.ts +0 -61
  40. package/src/utils/nested-write-visitor.ts +0 -359
  41. package/src/utils/query-analysis.ts +0 -116
  42. package/src/utils/serialization.ts +0 -39
  43. package/src/utils/types.ts +0 -43
  44. package/src/vue.ts +0 -448
  45. package/test/react-query.test.tsx +0 -1787
  46. package/test/react-typing-test.ts +0 -113
  47. package/test/schemas/basic/input.ts +0 -110
  48. package/test/schemas/basic/models.ts +0 -14
  49. package/test/schemas/basic/schema-lite.ts +0 -172
  50. package/test/schemas/basic/schema.zmodel +0 -35
  51. package/test/svelte-typing-test.ts +0 -111
  52. package/test/vue-typing-test.ts +0 -111
  53. package/tsconfig.json +0 -7
  54. package/tsconfig.test.json +0 -8
  55. package/tsup.config.ts +0 -15
  56. package/vitest.config.ts +0 -11
package/src/vue.ts DELETED
@@ -1,448 +0,0 @@
1
- import {
2
- useInfiniteQuery,
3
- useMutation,
4
- useQuery,
5
- useQueryClient,
6
- type DefaultError,
7
- type InfiniteData,
8
- type QueryKey,
9
- type UseInfiniteQueryOptions,
10
- type UseInfiniteQueryReturnType,
11
- type UseMutationOptions,
12
- type UseMutationReturnType,
13
- type UseQueryOptions,
14
- type UseQueryReturnType,
15
- } from '@tanstack/vue-query';
16
- import { lowerCaseFirst } from '@zenstackhq/common-helpers';
17
- import type {
18
- AggregateArgs,
19
- AggregateResult,
20
- BatchResult,
21
- CountArgs,
22
- CountResult,
23
- CreateArgs,
24
- CreateManyAndReturnArgs,
25
- CreateManyArgs,
26
- DeleteArgs,
27
- DeleteManyArgs,
28
- FindFirstArgs,
29
- FindManyArgs,
30
- FindUniqueArgs,
31
- GroupByArgs,
32
- GroupByResult,
33
- QueryOptions,
34
- SelectSubset,
35
- SimplifiedPlainResult,
36
- SimplifiedResult,
37
- Subset,
38
- UpdateArgs,
39
- UpdateManyAndReturnArgs,
40
- UpdateManyArgs,
41
- UpsertArgs,
42
- } from '@zenstackhq/orm';
43
- import type { GetModels, SchemaDef } from '@zenstackhq/schema';
44
- import { inject, provide, toValue, type MaybeRefOrGetter, type UnwrapRef } from 'vue';
45
- import {
46
- DEFAULT_QUERY_ENDPOINT,
47
- fetcher,
48
- getQueryKey,
49
- makeUrl,
50
- marshal,
51
- setupInvalidation,
52
- setupOptimisticUpdate,
53
- type APIContext,
54
- type ExtraMutationOptions,
55
- type ExtraQueryOptions,
56
- } from './utils/common';
57
- import type { TrimDelegateModelOperations, WithOptimistic } from './utils/types';
58
-
59
- export type { FetchFn } from './utils/common';
60
- export const VueQueryContextKey = 'zenstack-vue-query-context';
61
-
62
- /**
63
- * Provide context for query settings.
64
- *
65
- * @deprecated Use {@link provideQuerySettingsContext} instead.
66
- */
67
- export function provideHooksContext(context: APIContext) {
68
- provide<APIContext>(VueQueryContextKey, context);
69
- }
70
-
71
- /**
72
- * Provide context for query settings.
73
- */
74
- export function provideQuerySettingsContext(context: APIContext) {
75
- provide<APIContext>(VueQueryContextKey, context);
76
- }
77
-
78
- function getQuerySettings() {
79
- const { endpoint, ...rest } = inject<APIContext>(VueQueryContextKey, {
80
- endpoint: DEFAULT_QUERY_ENDPOINT,
81
- fetch: undefined,
82
- logging: false,
83
- });
84
- return { endpoint: endpoint ?? DEFAULT_QUERY_ENDPOINT, ...rest };
85
- }
86
-
87
- export type ModelQueryOptions<T> = MaybeRefOrGetter<
88
- Omit<UnwrapRef<UseQueryOptions<T, DefaultError>>, 'queryKey'> & ExtraQueryOptions
89
- >;
90
-
91
- export type ModelQueryResult<T> = UseQueryReturnType<WithOptimistic<T>, DefaultError> & { queryKey: QueryKey };
92
-
93
- export type ModelInfiniteQueryOptions<T> = MaybeRefOrGetter<
94
- Omit<UnwrapRef<UseInfiniteQueryOptions<T, DefaultError, InfiniteData<T>>>, 'queryKey' | 'initialPageParam'>
95
- >;
96
-
97
- export type ModelInfiniteQueryResult<T> = UseInfiniteQueryReturnType<T, DefaultError> & { queryKey: QueryKey };
98
-
99
- export type ModelMutationOptions<T, TArgs> = MaybeRefOrGetter<
100
- Omit<UnwrapRef<UseMutationOptions<T, DefaultError, TArgs>>, 'mutationFn'> & ExtraMutationOptions
101
- >;
102
-
103
- export type ModelMutationResult<T, TArgs> = UseMutationReturnType<T, DefaultError, TArgs, unknown>;
104
-
105
- export type ModelMutationModelResult<
106
- Schema extends SchemaDef,
107
- Model extends GetModels<Schema>,
108
- TArgs,
109
- Array extends boolean = false,
110
- Options extends QueryOptions<Schema> = QueryOptions<Schema>,
111
- > = Omit<
112
- ModelMutationResult<SimplifiedResult<Schema, Model, TArgs, QueryOptions<Schema>, false, Array>, TArgs>,
113
- 'mutateAsync'
114
- > & {
115
- mutateAsync<T extends TArgs>(
116
- args: T,
117
- options?: ModelMutationOptions<SimplifiedResult<Schema, Model, T, Options, false, Array>, T>,
118
- ): Promise<SimplifiedResult<Schema, Model, T, Options, false, Array>>;
119
- };
120
-
121
- export type ClientHooks<Schema extends SchemaDef, Options extends QueryOptions<Schema> = QueryOptions<Schema>> = {
122
- [Model in GetModels<Schema> as `${Uncapitalize<Model>}`]: ModelQueryHooks<Schema, Model, Options>;
123
- };
124
-
125
- // Note that we can potentially use TypeScript's mapped type to directly map from ORM contract, but that seems
126
- // to significantly slow down tsc performance ...
127
- export type ModelQueryHooks<
128
- Schema extends SchemaDef,
129
- Model extends GetModels<Schema>,
130
- Options extends QueryOptions<Schema> = QueryOptions<Schema>,
131
- > = TrimDelegateModelOperations<
132
- Schema,
133
- Model,
134
- {
135
- useFindUnique<T extends FindUniqueArgs<Schema, Model>>(
136
- args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
137
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
138
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
139
-
140
- useFindFirst<T extends FindFirstArgs<Schema, Model>>(
141
- args?: SelectSubset<T, FindFirstArgs<Schema, Model>>,
142
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
143
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
144
-
145
- useFindMany<T extends FindManyArgs<Schema, Model>>(
146
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
147
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
148
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options>[]>;
149
-
150
- useInfiniteFindMany<T extends FindManyArgs<Schema, Model>>(
151
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
152
- options?: ModelInfiniteQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
153
- ): ModelInfiniteQueryResult<InfiniteData<SimplifiedPlainResult<Schema, Model, T, Options>[]>>;
154
-
155
- useCreate<T extends CreateArgs<Schema, Model>>(
156
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
157
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
158
-
159
- useCreateMany<T extends CreateManyArgs<Schema, Model>>(
160
- options?: ModelMutationOptions<BatchResult, T>,
161
- ): ModelMutationResult<BatchResult, T>;
162
-
163
- useCreateManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(
164
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>[], T>,
165
- ): ModelMutationModelResult<Schema, Model, T, true, Options>;
166
-
167
- useUpdate<T extends UpdateArgs<Schema, Model>>(
168
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
169
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
170
-
171
- useUpdateMany<T extends UpdateManyArgs<Schema, Model>>(
172
- options?: ModelMutationOptions<BatchResult, T>,
173
- ): ModelMutationResult<BatchResult, T>;
174
-
175
- useUpdateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(
176
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>[], T>,
177
- ): ModelMutationModelResult<Schema, Model, T, true, Options>;
178
-
179
- useUpsert<T extends UpsertArgs<Schema, Model>>(
180
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
181
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
182
-
183
- useDelete<T extends DeleteArgs<Schema, Model>>(
184
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
185
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
186
-
187
- useDeleteMany<T extends DeleteManyArgs<Schema, Model>>(
188
- options?: ModelMutationOptions<BatchResult, T>,
189
- ): ModelMutationResult<BatchResult, T>;
190
-
191
- useCount<T extends CountArgs<Schema, Model>>(
192
- args?: Subset<T, CountArgs<Schema, Model>>,
193
- options?: ModelQueryOptions<CountResult<Schema, Model, T>>,
194
- ): ModelQueryResult<CountResult<Schema, Model, T>>;
195
-
196
- useAggregate<T extends AggregateArgs<Schema, Model>>(
197
- args: Subset<T, AggregateArgs<Schema, Model>>,
198
- options?: ModelQueryOptions<AggregateResult<Schema, Model, T>>,
199
- ): ModelQueryResult<AggregateResult<Schema, Model, T>>;
200
-
201
- useGroupBy<T extends GroupByArgs<Schema, Model>>(
202
- args: Subset<T, GroupByArgs<Schema, Model>>,
203
- options?: ModelQueryOptions<GroupByResult<Schema, Model, T>>,
204
- ): ModelQueryResult<GroupByResult<Schema, Model, T>>;
205
- }
206
- >;
207
-
208
- /**
209
- * Gets data query hooks for all models in the schema.
210
- */
211
- export function useClientQueries<Schema extends SchemaDef, Options extends QueryOptions<Schema> = QueryOptions<Schema>>(
212
- schema: Schema,
213
- ): ClientHooks<Schema, Options> {
214
- return Object.keys(schema.models).reduce(
215
- (acc, model) => {
216
- (acc as any)[lowerCaseFirst(model)] = useModelQueries<Schema, GetModels<Schema>, Options>(
217
- schema,
218
- model as GetModels<Schema>,
219
- );
220
- return acc;
221
- },
222
- {} as ClientHooks<Schema, Options>,
223
- );
224
- }
225
-
226
- /**
227
- * Gets data query hooks for a specific model in the schema.
228
- */
229
- export function useModelQueries<
230
- Schema extends SchemaDef,
231
- Model extends GetModels<Schema>,
232
- Options extends QueryOptions<Schema>,
233
- >(schema: Schema, model: Model): ModelQueryHooks<Schema, Model, Options> {
234
- const modelDef = Object.values(schema.models).find((m) => m.name.toLowerCase() === model.toLowerCase());
235
- if (!modelDef) {
236
- throw new Error(`Model "${model}" not found in schema`);
237
- }
238
-
239
- const modelName = modelDef.name;
240
-
241
- return {
242
- useFindUnique: (args: any, options?: any) => {
243
- return useInternalQuery(schema, modelName, 'findUnique', args, options);
244
- },
245
-
246
- useFindFirst: (args: any, options?: any) => {
247
- return useInternalQuery(schema, modelName, 'findFirst', args, options);
248
- },
249
-
250
- useFindMany: (args: any, options?: any) => {
251
- return useInternalQuery(schema, modelName, 'findMany', args, options);
252
- },
253
-
254
- useInfiniteFindMany: (args: any, options?: any) => {
255
- return useInternalInfiniteQuery(schema, modelName, 'findMany', args, options);
256
- },
257
-
258
- useCreate: (options?: any) => {
259
- return useInternalMutation(schema, modelName, 'POST', 'create', options);
260
- },
261
-
262
- useCreateMany: (options?: any) => {
263
- return useInternalMutation(schema, modelName, 'POST', 'createMany', options);
264
- },
265
-
266
- useCreateManyAndReturn: (options?: any) => {
267
- return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options);
268
- },
269
-
270
- useUpdate: (options?: any) => {
271
- return useInternalMutation(schema, modelName, 'PUT', 'update', options);
272
- },
273
-
274
- useUpdateMany: (options?: any) => {
275
- return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options);
276
- },
277
-
278
- useUpdateManyAndReturn: (options?: any) => {
279
- return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options);
280
- },
281
-
282
- useUpsert: (options?: any) => {
283
- return useInternalMutation(schema, modelName, 'POST', 'upsert', options);
284
- },
285
-
286
- useDelete: (options?: any) => {
287
- return useInternalMutation(schema, modelName, 'DELETE', 'delete', options);
288
- },
289
-
290
- useDeleteMany: (options?: any) => {
291
- return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
292
- },
293
-
294
- useCount: (args: any, options?: any) => {
295
- return useInternalQuery(schema, modelName, 'count', args, options);
296
- },
297
-
298
- useAggregate: (args: any, options?: any) => {
299
- return useInternalQuery(schema, modelName, 'aggregate', args, options);
300
- },
301
-
302
- useGroupBy: (args: any, options?: any) => {
303
- return useInternalQuery(schema, modelName, 'groupBy', args, options);
304
- },
305
- } as ModelQueryHooks<Schema, Model, Options>;
306
- }
307
-
308
- export function useInternalQuery<TQueryFnData, TData>(
309
- _schema: SchemaDef,
310
- model: string,
311
- operation: string,
312
- args?: MaybeRefOrGetter<unknown>,
313
- options?: MaybeRefOrGetter<
314
- Omit<UnwrapRef<UseQueryOptions<TQueryFnData, DefaultError, TData>>, 'queryKey'> & ExtraQueryOptions
315
- >,
316
- ) {
317
- const argsValue = toValue(args);
318
- const { optimisticUpdate, ...restOptions } = toValue(options) ?? {};
319
- const queryKey = getQueryKey(model, operation, argsValue, {
320
- infinite: false,
321
- optimisticUpdate: optimisticUpdate !== false,
322
- });
323
- const { endpoint, fetch } = getQuerySettings();
324
-
325
- const finalOptions: any = {
326
- queryKey,
327
- queryFn: ({ queryKey, signal }: any) => {
328
- const [_prefix, _model, _op, args] = queryKey;
329
- const reqUrl = makeUrl(endpoint, model, operation, args);
330
- return fetcher<TQueryFnData>(reqUrl, { signal }, fetch);
331
- },
332
- ...restOptions,
333
- };
334
- return { queryKey, ...useQuery<TQueryFnData, DefaultError, TData>(finalOptions) };
335
- }
336
-
337
- export function useInternalInfiniteQuery<TQueryFnData, TData>(
338
- _schema: SchemaDef,
339
- model: string,
340
- operation: string,
341
- args: MaybeRefOrGetter<unknown>,
342
- options:
343
- | MaybeRefOrGetter<
344
- Omit<
345
- UnwrapRef<UseInfiniteQueryOptions<TQueryFnData, DefaultError, InfiniteData<TData>>>,
346
- 'queryKey' | 'initialPageParam'
347
- >
348
- >
349
- | undefined,
350
- ) {
351
- options = options ?? { getNextPageParam: () => undefined };
352
- const { endpoint, fetch } = getQuerySettings();
353
- const argsValue = toValue(args);
354
- const optionsValue = toValue(options);
355
- const queryKey = getQueryKey(model, operation, argsValue, { infinite: true, optimisticUpdate: false });
356
-
357
- const finalOptions: any = {
358
- queryKey,
359
- queryFn: ({ queryKey, signal }: any) => {
360
- const [_prefix, _model, _op, args] = queryKey;
361
- const reqUrl = makeUrl(endpoint, model, operation, args);
362
- return fetcher<TQueryFnData>(reqUrl, { signal }, fetch);
363
- },
364
- initialPageParam: argsValue,
365
- ...optionsValue,
366
- };
367
- return {
368
- queryKey,
369
- ...useInfiniteQuery(finalOptions),
370
- };
371
- }
372
-
373
- /**
374
- * Creates a vue-query mutation
375
- *
376
- * @private
377
- *
378
- * @param model The name of the model under mutation.
379
- * @param method The HTTP method.
380
- * @param operation The mutation operation (e.g. `create`).
381
- * @param options The vue-query options.
382
- * @param checkReadBack Whether to check for read back errors and return undefined if found.
383
- */
384
- export function useInternalMutation<TArgs, R = any>(
385
- schema: SchemaDef,
386
- model: string,
387
- method: 'POST' | 'PUT' | 'DELETE',
388
- operation: string,
389
- options?: MaybeRefOrGetter<
390
- Omit<UnwrapRef<UseMutationOptions<R, DefaultError, TArgs>>, 'mutationFn'> & ExtraMutationOptions
391
- >,
392
- ) {
393
- const { endpoint, fetch, logging } = getQuerySettings();
394
- const queryClient = useQueryClient();
395
- const mutationFn = (data: any) => {
396
- const reqUrl =
397
- method === 'DELETE' ? makeUrl(endpoint, model, operation, data) : makeUrl(endpoint, model, operation);
398
- const fetchInit: RequestInit = {
399
- method,
400
- ...(method !== 'DELETE' && {
401
- headers: {
402
- 'content-type': 'application/json',
403
- },
404
- body: marshal(data),
405
- }),
406
- };
407
- return fetcher<R>(reqUrl, fetchInit, fetch) as Promise<R>;
408
- };
409
-
410
- const optionsValue = toValue(options);
411
- const finalOptions: any = { ...optionsValue, mutationFn };
412
- const invalidateQueries = optionsValue?.invalidateQueries !== false;
413
- const optimisticUpdate = !!optionsValue?.optimisticUpdate;
414
-
415
- if (operation) {
416
- if (invalidateQueries) {
417
- setupInvalidation(
418
- model,
419
- operation,
420
- schema,
421
- finalOptions,
422
- (predicate) => queryClient.invalidateQueries({ predicate }),
423
- logging,
424
- );
425
- }
426
-
427
- if (optimisticUpdate) {
428
- setupOptimisticUpdate(
429
- model,
430
- operation,
431
- schema,
432
- finalOptions,
433
- queryClient.getQueryCache().getAll(),
434
- (queryKey, data) => {
435
- // update query cache
436
- queryClient.setQueryData<unknown>(queryKey, data);
437
- // cancel on-flight queries to avoid redundant cache updates,
438
- // the settlement of the current mutation will trigger a new revalidation
439
- queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
440
- },
441
- invalidateQueries ? (predicate) => queryClient.invalidateQueries({ predicate }) : undefined,
442
- logging,
443
- );
444
- }
445
- }
446
-
447
- return useMutation(finalOptions);
448
- }