@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
@@ -0,0 +1,4 @@
1
+ import type { QueryClient } from '@tanstack/query-core';
2
+ import type { InvalidationPredicate, QueryInfo } from '@zenstackhq/client-helpers';
3
+ export declare function invalidateQueriesMatchingPredicate(queryClient: QueryClient, predicate: InvalidationPredicate): Promise<void>;
4
+ export declare function getAllQueries(queryClient: QueryClient): readonly QueryInfo[];
@@ -0,0 +1,38 @@
1
+ import { parseQueryKey } from './query-key';
2
+ export function invalidateQueriesMatchingPredicate(queryClient, predicate) {
3
+ return queryClient.invalidateQueries({
4
+ predicate: ({ queryKey }) => {
5
+ const parsed = parseQueryKey(queryKey);
6
+ if (!parsed) {
7
+ return false;
8
+ }
9
+ return predicate({ model: parsed.model, args: parsed.args });
10
+ },
11
+ });
12
+ }
13
+ export function getAllQueries(queryClient) {
14
+ return queryClient
15
+ .getQueryCache()
16
+ .getAll()
17
+ .map(({ queryKey, state }) => {
18
+ const parsed = parseQueryKey(queryKey);
19
+ if (!parsed) {
20
+ return undefined;
21
+ }
22
+ return {
23
+ model: parsed?.model,
24
+ operation: parsed?.operation,
25
+ args: parsed?.args,
26
+ data: state.data,
27
+ optimisticUpdate: !!parsed.flags.optimisticUpdate,
28
+ updateData: (data, cancelOnTheFlyQueries) => {
29
+ queryClient.setQueryData(queryKey, data);
30
+ if (cancelOnTheFlyQueries) {
31
+ queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
32
+ }
33
+ },
34
+ };
35
+ })
36
+ .filter((entry) => !!entry);
37
+ }
38
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/common/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,UAAU,kCAAkC,CAAC,WAAwB,EAAE,SAAgC;IACzG,OAAO,WAAW,CAAC,iBAAiB,CAAC;QACjC,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;YACxB,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;YACvC,IAAI,CAAC,MAAM,EAAE,CAAC;gBACV,OAAO,KAAK,CAAC;YACjB,CAAC;YACD,OAAO,SAAS,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAe,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3E,CAAC;KACJ,CAAC,CAAC;AACP,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,WAAwB;IAClD,OAAO,WAAW;SACb,aAAa,EAAE;SACf,MAAM,EAAE;SACR,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE;QACzB,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,OAAO;YACH,KAAK,EAAE,MAAM,EAAE,KAAK;YACpB,SAAS,EAAE,MAAM,EAAE,SAAS;YAC5B,IAAI,EAAE,MAAM,EAAE,IAAI;YAClB,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,gBAAgB,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB;YACjD,UAAU,EAAE,CAAC,IAAa,EAAE,qBAA8B,EAAE,EAAE;gBAC1D,WAAW,CAAC,YAAY,CAAU,QAAQ,EAAE,IAAI,CAAC,CAAC;gBAClD,IAAI,qBAAqB,EAAE,CAAC;oBACxB,WAAW,CAAC,aAAa,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC7E,CAAC;YACL,CAAC;SACJ,CAAC;IACN,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Prefix for react-query keys.
3
+ */
4
+ export declare const QUERY_KEY_PREFIX = "zenstack";
5
+ export type QueryKey = [
6
+ string,
7
+ string,
8
+ string,
9
+ unknown,
10
+ {
11
+ infinite: boolean;
12
+ optimisticUpdate: boolean;
13
+ }
14
+ ];
15
+ /**
16
+ * Computes query key for the given model, operation and query args.
17
+ * @param model Model name.
18
+ * @param operation Query operation (e.g, `findMany`) or request URL. If it's a URL, the last path segment will be used as the operation name.
19
+ * @param args Query arguments.
20
+ * @param options Query options, including `infinite` indicating if it's an infinite query (defaults to false), and `optimisticUpdate` indicating if optimistic updates are enabled (defaults to true).
21
+ * @returns Query key
22
+ */
23
+ export declare function getQueryKey(model: string, operation: string, args: unknown, options?: {
24
+ infinite: boolean;
25
+ optimisticUpdate: boolean;
26
+ }): QueryKey;
27
+ /**
28
+ * Parses the given query key into its components.
29
+ */
30
+ export declare function parseQueryKey(queryKey: readonly unknown[]): {
31
+ model: string;
32
+ operation: string;
33
+ args: unknown;
34
+ flags: {
35
+ infinite: boolean;
36
+ optimisticUpdate: boolean;
37
+ };
38
+ } | undefined;
39
+ export declare function isZenStackQueryKey(queryKey: readonly unknown[]): queryKey is QueryKey;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Prefix for react-query keys.
3
+ */
4
+ export const QUERY_KEY_PREFIX = 'zenstack';
5
+ /**
6
+ * Computes query key for the given model, operation and query args.
7
+ * @param model Model name.
8
+ * @param operation Query operation (e.g, `findMany`) or request URL. If it's a URL, the last path segment will be used as the operation name.
9
+ * @param args Query arguments.
10
+ * @param options Query options, including `infinite` indicating if it's an infinite query (defaults to false), and `optimisticUpdate` indicating if optimistic updates are enabled (defaults to true).
11
+ * @returns Query key
12
+ */
13
+ export function getQueryKey(model, operation, args, options = { infinite: false, optimisticUpdate: true }) {
14
+ const infinite = options.infinite;
15
+ // infinite query doesn't support optimistic updates
16
+ const optimisticUpdate = options.infinite ? false : options.optimisticUpdate;
17
+ return [QUERY_KEY_PREFIX, model, operation, args, { infinite, optimisticUpdate }];
18
+ }
19
+ /**
20
+ * Parses the given query key into its components.
21
+ */
22
+ export function parseQueryKey(queryKey) {
23
+ const [prefix, model, operation, args, flags] = queryKey;
24
+ if (prefix !== QUERY_KEY_PREFIX) {
25
+ return undefined;
26
+ }
27
+ return { model, operation, args, flags };
28
+ }
29
+ export function isZenStackQueryKey(queryKey) {
30
+ if (queryKey.length < 5) {
31
+ return false;
32
+ }
33
+ if (queryKey[0] !== QUERY_KEY_PREFIX) {
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+ //# sourceMappingURL=query-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-key.js","sourceRoot":"","sources":["../../src/common/query-key.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,UAAU,CAAC;AAa3C;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CACvB,KAAa,EACb,SAAiB,EACjB,IAAa,EACb,UAA4D,EAAE,QAAQ,EAAE,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE;IAEvG,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAClC,oDAAoD;IACpD,MAAM,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC;IAC7E,OAAO,CAAC,gBAAgB,EAAE,KAAK,EAAE,SAAU,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAC,CAAC;AACvF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,QAA4B;IACtD,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,QAAoB,CAAC;IACrE,IAAI,MAAM,KAAK,gBAAgB,EAAE,CAAC;QAC9B,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7C,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,QAA4B;IAC3D,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,IAAI,QAAQ,CAAC,CAAC,CAAC,KAAK,gBAAgB,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { Logger, OptimisticDataProvider } from '@zenstackhq/client-helpers';
2
+ import type { FetchFn } from '@zenstackhq/client-helpers/fetch';
3
+ import type { OperationsIneligibleForDelegateModels } from '@zenstackhq/orm';
4
+ import type { GetModels, IsDelegateModel, SchemaDef } from '@zenstackhq/schema';
5
+ /**
6
+ * Context type for configuring the hooks.
7
+ */
8
+ export type QueryContext = {
9
+ /**
10
+ * The endpoint to use for the queries.
11
+ */
12
+ endpoint?: string;
13
+ /**
14
+ * A custom fetch function for sending the HTTP requests.
15
+ */
16
+ fetch?: FetchFn;
17
+ /**
18
+ * If logging is enabled.
19
+ */
20
+ logging?: Logger;
21
+ };
22
+ /**
23
+ * Extra query options.
24
+ */
25
+ export type ExtraQueryOptions = {
26
+ /**
27
+ * Whether to opt-in to optimistic updates for this query. Defaults to `true`.
28
+ */
29
+ optimisticUpdate?: boolean;
30
+ } & QueryContext;
31
+ /**
32
+ * Extra mutation options.
33
+ */
34
+ export type ExtraMutationOptions = {
35
+ /**
36
+ * Whether to automatically invalidate queries potentially affected by the mutation. Defaults to `true`.
37
+ */
38
+ invalidateQueries?: boolean;
39
+ /**
40
+ * Whether to optimistically update queries potentially affected by the mutation. Defaults to `false`.
41
+ */
42
+ optimisticUpdate?: boolean;
43
+ /**
44
+ * A callback for computing optimistic update data for each query cache entry.
45
+ */
46
+ optimisticDataProvider?: OptimisticDataProvider;
47
+ } & QueryContext;
48
+ type HooksOperationsIneligibleForDelegateModels = OperationsIneligibleForDelegateModels extends any ? `use${Capitalize<OperationsIneligibleForDelegateModels>}` : never;
49
+ /**
50
+ * Trim operations that are ineligible for delegate models from the given model operations type.
51
+ */
52
+ export type TrimDelegateModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>, T extends Record<string, unknown>> = IsDelegateModel<Schema, Model> extends true ? Omit<T, HooksOperationsIneligibleForDelegateModels> : T;
53
+ type WithOptimisticFlag<T> = T extends object ? T & {
54
+ /**
55
+ * Indicates if the item is in an optimistic update state
56
+ */
57
+ $optimistic?: boolean;
58
+ } : T;
59
+ export type WithOptimistic<T> = T extends Array<infer U> ? Array<WithOptimisticFlag<U>> : WithOptimisticFlag<T>;
60
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/common/types.ts"],"names":[],"mappings":""}