tanstack-query-callbacks 0.2.1 → 0.3.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) zhong666 <hi@zhong666.me> and bouzu contributors
3
+ Copyright (c) 2023-PRESENT zhong666 <hi@zhong666.me>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -5,6 +5,10 @@
5
5
  [![bundle][bundle-src]][bundle-href]
6
6
  [![coverage][coverage-src]][coverage-href]
7
7
 
8
+ [![npm peer dependency version - @tanstack/vue-query][peer-deps-tanstack-vue-query-src]][peer-deps-tanstack-vue-query-href]
9
+ [![npm peer dependency version - @tanstack/react-query][peer-deps-tanstack-react-query-src]][peer-deps-tanstack-react-query-href]
10
+ [![npm peer dependency version - @tanstack/svelte-query][peer-deps-tanstack-svelte-query-src]][peer-deps-tanstack-svelte-query-href]
11
+
8
12
  Use callbacks of query in the usual way, as before.
9
13
 
10
14
  The tanstack/query has removed `onSuccess`, `onError` and `onSettled` from useQuery in v5. You can find more information in the [RFC](https://github.com/TanStack/query/discussions/5279).
@@ -12,7 +16,7 @@ The tanstack/query has removed `onSuccess`, `onError` and `onSettled` from useQu
12
16
  # Features
13
17
 
14
18
  - Support Tanstack/Query v4, v5
15
- - Support Vue, React
19
+ - Support Vue, React, Svelte
16
20
 
17
21
  ## Instanll
18
22
 
@@ -30,6 +34,7 @@ pnpm add tanstack-query-callbacks
30
34
  <script setup lang="ts">
31
35
  import { useQuery } from '@tanstack/vue-query'
32
36
  import { useQueryCallbacks } from 'tanstack-query-callbacks/vue'
37
+
33
38
  const queryKey = ['foo']
34
39
  const query = useQuery(queryKey, () => Promise.resolve('bar'))
35
40
  useQueryCallbacks({
@@ -70,6 +75,34 @@ useQueryCallbacks({
70
75
  })
71
76
  ```
72
77
 
78
+ ## Usage (Svelte)
79
+
80
+ ```svelte
81
+ <script>
82
+ import { createQuery } from '@tanstack/svelte-query'
83
+ import { useQueryCallbacks } from 'tanstack-query-callbacks/svelte'
84
+
85
+ const queryKey = ['foo']
86
+ const query = createQuery({
87
+ queryKey,
88
+ queryFn: () => Promise.resolve('bar')
89
+ })
90
+
91
+ useQueryCallbacks({
92
+ queryKey,
93
+ onSuccess: (data) => {
94
+ console.log('success', data)
95
+ },
96
+ onError: (err) => {
97
+ console.error('error', err)
98
+ },
99
+ onSettled: (data, err) => {
100
+ console.log('settled', { data, err })
101
+ }
102
+ })
103
+ </script>
104
+ ```
105
+
73
106
  <!-- Link Resources -->
74
107
 
75
108
  [npm-version-src]: https://img.shields.io/npm/v/tanstack-query-callbacks?style=flat&colorA=18181B&colorB=F0DB4F
@@ -78,5 +111,11 @@ useQueryCallbacks({
78
111
  [npm-downloads-href]: https://npmjs.com/package/tanstack-query-callbacks
79
112
  [bundle-src]: https://img.shields.io/bundlephobia/minzip/tanstack-query-callbacks?style=flat&colorA=18181B&colorB=F0DB4F
80
113
  [bundle-href]: https://bundlephobia.com/result?p=tanstack-query-callbacks
81
- [coverage-src]: https://img.shields.io/codecov/c/gh/aa900031/tanstack-query-callbacks?token=AG56Z6EP8U&logo=codecov&color=F0DB4F
114
+ [coverage-src]: https://img.shields.io/codecov/c/gh/aa900031/tanstack-query-callbacks?logo=codecov&style=flat&colorA=18181B&colorB=F0DB4F
82
115
  [coverage-href]: https://codecov.io/gh/aa900031/tanstack-query-callbacks
116
+ [peer-deps-tanstack-vue-query-src]: https://img.shields.io/npm/dependency-version/tanstack-query-callbacks/peer/@tanstack/vue-query?style=flat&colorA=18181B&colorB=F0DB4F
117
+ [peer-deps-tanstack-vue-query-href]: https://www.npmjs.com/package/@tanstack/vue-query
118
+ [peer-deps-tanstack-react-query-src]: https://img.shields.io/npm/dependency-version/tanstack-query-callbacks/peer/@tanstack/react-query?style=flat&colorA=18181B&colorB=F0DB4F
119
+ [peer-deps-tanstack-react-query-href]: https://www.npmjs.com/package/@tanstack/react-query
120
+ [peer-deps-tanstack-svelte-query-src]: https://img.shields.io/npm/dependency-version/tanstack-query-callbacks/peer/@tanstack/svelte-query?style=flat&colorA=18181B&colorB=F0DB4F
121
+ [peer-deps-tanstack-svelte-query-href]: https://www.npmjs.com/package/@tanstack/svelte-query
@@ -1,42 +1,35 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _tanstack_query_core = require("@tanstack/query-core");
2
3
 
3
- const queryCore = require('@tanstack/query-core');
4
-
5
- function subscribeQueryCallbacks({
6
- queryClient,
7
- queryKey,
8
- onSuccess,
9
- onError,
10
- onSettled
11
- }) {
12
- if (!onSuccess && !onError && !onSettled)
13
- return noop;
14
- const cache = queryClient.getQueryCache();
15
- const unsubscribe = cache.subscribe((event) => {
16
- if (event.type !== "updated")
17
- return;
18
- if (!queryCore.matchQuery({ queryKey, exact: true }, event.query))
19
- return;
20
- switch (event.action.type) {
21
- case "success": {
22
- if (!event.action.manual) {
23
- onSuccess?.(event.action.data);
24
- onSettled?.(event.action.data, null);
25
- }
26
- break;
27
- }
28
- case "error": {
29
- if (!queryCore.isCancelledError(event.action.error)) {
30
- onError?.(event.action.error);
31
- onSettled?.(void 0, event.action.error);
32
- }
33
- break;
34
- }
35
- }
36
- });
37
- return unsubscribe;
38
- }
39
- function noop() {
4
+ //#region src/core/index.ts
5
+ function subscribeQueryCallbacks({ queryClient, queryKey, onSuccess, onError, onSettled }) {
6
+ if (!onSuccess && !onError && !onSettled) return noop;
7
+ return queryClient.getQueryCache().subscribe((event) => {
8
+ if (event.type !== "updated") return;
9
+ if (!(0, _tanstack_query_core.matchQuery)({
10
+ queryKey,
11
+ exact: true
12
+ }, event.query)) return;
13
+ switch (event.action.type) {
14
+ case "success":
15
+ /* istanbul ignore else */
16
+ if (!event.action.manual) {
17
+ onSuccess?.(event.action.data);
18
+ onSettled?.(event.action.data, null);
19
+ }
20
+ break;
21
+ case "failed":
22
+ case "error":
23
+ /* istanbul ignore else */
24
+ if (!(0, _tanstack_query_core.isCancelledError)(event.action.error)) {
25
+ onError?.(event.action.error);
26
+ onSettled?.(void 0, event.action.error);
27
+ }
28
+ break;
29
+ }
30
+ });
40
31
  }
32
+ function noop() {}
41
33
 
42
- exports.subscribeQueryCallbacks = subscribeQueryCallbacks;
34
+ //#endregion
35
+ exports.subscribeQueryCallbacks = subscribeQueryCallbacks;
@@ -1,18 +1,24 @@
1
- import { QueryClient, QueryKey } from '@tanstack/query-core';
1
+ import { QueryClient, QueryKey } from "@tanstack/query-core";
2
2
 
3
+ //#region src/core/index.d.ts
3
4
  type QuerySuccessHandler<TQueryFnData> = (data: TQueryFnData) => void;
4
5
  type QueryErrorHandler<TError> = (err: TError) => void;
5
6
  type QuerySettledHandler<TQueryFnData, TError> = (data: TQueryFnData | undefined, error: TError | null) => void;
6
7
  interface QueryCallbacks<TQueryFnData, TError> {
7
- onSuccess?: QuerySuccessHandler<TQueryFnData>;
8
- onError?: QueryErrorHandler<TError>;
9
- onSettled?: QuerySettledHandler<TQueryFnData, TError>;
8
+ onSuccess?: QuerySuccessHandler<TQueryFnData>;
9
+ onError?: QueryErrorHandler<TError>;
10
+ onSettled?: QuerySettledHandler<TQueryFnData, TError>;
10
11
  }
11
12
  interface SubscribeQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
12
- queryClient: QueryClient;
13
- queryKey: QueryKey;
13
+ queryClient: QueryClient;
14
+ queryKey: QueryKey;
14
15
  }
15
- declare function subscribeQueryCallbacks<TQueryFnData = unknown, TError = unknown>({ queryClient, queryKey, onSuccess, onError, onSettled, }: SubscribeQueryCallbacksProps<TQueryFnData, TError>): () => void;
16
-
17
- export { subscribeQueryCallbacks };
18
- export type { QueryCallbacks, QueryErrorHandler, QuerySettledHandler, QuerySuccessHandler, SubscribeQueryCallbacksProps };
16
+ declare function subscribeQueryCallbacks<TQueryFnData = unknown, TError = unknown>({
17
+ queryClient,
18
+ queryKey,
19
+ onSuccess,
20
+ onError,
21
+ onSettled
22
+ }: SubscribeQueryCallbacksProps<TQueryFnData, TError>): () => void;
23
+ //#endregion
24
+ export { QueryCallbacks, QueryErrorHandler, QuerySettledHandler, QuerySuccessHandler, SubscribeQueryCallbacksProps, subscribeQueryCallbacks };
@@ -1,18 +1,24 @@
1
- import { QueryClient, QueryKey } from '@tanstack/query-core';
1
+ import { QueryClient, QueryKey } from "@tanstack/query-core";
2
2
 
3
+ //#region src/core/index.d.ts
3
4
  type QuerySuccessHandler<TQueryFnData> = (data: TQueryFnData) => void;
4
5
  type QueryErrorHandler<TError> = (err: TError) => void;
5
6
  type QuerySettledHandler<TQueryFnData, TError> = (data: TQueryFnData | undefined, error: TError | null) => void;
6
7
  interface QueryCallbacks<TQueryFnData, TError> {
7
- onSuccess?: QuerySuccessHandler<TQueryFnData>;
8
- onError?: QueryErrorHandler<TError>;
9
- onSettled?: QuerySettledHandler<TQueryFnData, TError>;
8
+ onSuccess?: QuerySuccessHandler<TQueryFnData>;
9
+ onError?: QueryErrorHandler<TError>;
10
+ onSettled?: QuerySettledHandler<TQueryFnData, TError>;
10
11
  }
11
12
  interface SubscribeQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
12
- queryClient: QueryClient;
13
- queryKey: QueryKey;
13
+ queryClient: QueryClient;
14
+ queryKey: QueryKey;
14
15
  }
15
- declare function subscribeQueryCallbacks<TQueryFnData = unknown, TError = unknown>({ queryClient, queryKey, onSuccess, onError, onSettled, }: SubscribeQueryCallbacksProps<TQueryFnData, TError>): () => void;
16
-
17
- export { subscribeQueryCallbacks };
18
- export type { QueryCallbacks, QueryErrorHandler, QuerySettledHandler, QuerySuccessHandler, SubscribeQueryCallbacksProps };
16
+ declare function subscribeQueryCallbacks<TQueryFnData = unknown, TError = unknown>({
17
+ queryClient,
18
+ queryKey,
19
+ onSuccess,
20
+ onError,
21
+ onSettled
22
+ }: SubscribeQueryCallbacksProps<TQueryFnData, TError>): () => void;
23
+ //#endregion
24
+ export { QueryCallbacks, QueryErrorHandler, QuerySettledHandler, QuerySuccessHandler, SubscribeQueryCallbacksProps, subscribeQueryCallbacks };
@@ -0,0 +1,34 @@
1
+ import { isCancelledError, matchQuery } from "@tanstack/query-core";
2
+
3
+ //#region src/core/index.ts
4
+ function subscribeQueryCallbacks({ queryClient, queryKey, onSuccess, onError, onSettled }) {
5
+ if (!onSuccess && !onError && !onSettled) return noop;
6
+ return queryClient.getQueryCache().subscribe((event) => {
7
+ if (event.type !== "updated") return;
8
+ if (!matchQuery({
9
+ queryKey,
10
+ exact: true
11
+ }, event.query)) return;
12
+ switch (event.action.type) {
13
+ case "success":
14
+ /* istanbul ignore else */
15
+ if (!event.action.manual) {
16
+ onSuccess?.(event.action.data);
17
+ onSettled?.(event.action.data, null);
18
+ }
19
+ break;
20
+ case "failed":
21
+ case "error":
22
+ /* istanbul ignore else */
23
+ if (!isCancelledError(event.action.error)) {
24
+ onError?.(event.action.error);
25
+ onSettled?.(void 0, event.action.error);
26
+ }
27
+ break;
28
+ }
29
+ });
30
+ }
31
+ function noop() {}
32
+
33
+ //#endregion
34
+ export { subscribeQueryCallbacks };
@@ -1,29 +1,26 @@
1
- 'use strict';
2
-
3
- const reactQuery = require('@tanstack/react-query');
4
- const react = require('react');
5
- const core_index = require('../core/index.cjs');
6
- require('@tanstack/query-core');
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_core_index = require('../core/index.cjs');
3
+ let _tanstack_react_query = require("@tanstack/react-query");
4
+ let react = require("react");
7
5
 
6
+ //#region src/react/index.ts
8
7
  function useQueryCallbacks(props) {
9
- const queryClient = reactQuery.useQueryClient(
10
- props.context ? { context: props.context } : props.queryClient
11
- // for v5
12
- );
13
- react.useEffect(() => {
14
- return core_index.subscribeQueryCallbacks({
15
- queryClient,
16
- queryKey: props.queryKey,
17
- onSuccess: props.onSuccess,
18
- onError: props.onError,
19
- onSettled: props.onSettled
20
- });
21
- }, [
22
- props.queryKey,
23
- props.onSuccess,
24
- props.onError,
25
- props.onSettled
26
- ]);
8
+ const queryClient = (0, _tanstack_react_query.useQueryClient)(props.context ? { context: props.context } : props.queryClient);
9
+ (0, react.useEffect)(() => {
10
+ return require_core_index.subscribeQueryCallbacks({
11
+ queryClient,
12
+ queryKey: props.queryKey,
13
+ onSuccess: props.onSuccess,
14
+ onError: props.onError,
15
+ onSettled: props.onSettled
16
+ });
17
+ }, [
18
+ props.queryKey,
19
+ props.onSuccess,
20
+ props.onError,
21
+ props.onSettled
22
+ ]);
27
23
  }
28
24
 
29
- exports.useQueryCallbacks = useQueryCallbacks;
25
+ //#endregion
26
+ exports.useQueryCallbacks = useQueryCallbacks;
@@ -1,20 +1,19 @@
1
- import { QueryKey, QueryClient } from '@tanstack/react-query';
2
- import { Context } from 'react';
3
- import { QueryCallbacks } from '../core/index.cjs';
4
- import '@tanstack/query-core';
1
+ import { QueryCallbacks } from "../core/index.cjs";
2
+ import { QueryClient, QueryKey } from "@tanstack/react-query";
3
+ import { Context } from "react";
5
4
 
5
+ //#region src/react/index.d.ts
6
6
  interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient context for v4
14
- */
15
- context?: Context<QueryClient | undefined>;
7
+ queryKey: QueryKey;
8
+ /**
9
+ * QueryClient instance
10
+ */
11
+ queryClient?: QueryClient;
12
+ /**
13
+ * QueryClient context for v4
14
+ */
15
+ context?: Context<QueryClient | undefined>;
16
16
  }
17
17
  declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
18
-
19
- export { useQueryCallbacks };
20
- export type { UseQueryCallbacksProps };
18
+ //#endregion
19
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -1,20 +1,19 @@
1
- import { QueryKey, QueryClient } from '@tanstack/react-query';
2
- import { Context } from 'react';
3
- import { QueryCallbacks } from '../core/index.js';
4
- import '@tanstack/query-core';
1
+ import { QueryCallbacks } from "../core/index.js";
2
+ import { QueryClient, QueryKey } from "@tanstack/react-query";
3
+ import { Context } from "react";
5
4
 
5
+ //#region src/react/index.d.ts
6
6
  interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient context for v4
14
- */
15
- context?: Context<QueryClient | undefined>;
7
+ queryKey: QueryKey;
8
+ /**
9
+ * QueryClient instance
10
+ */
11
+ queryClient?: QueryClient;
12
+ /**
13
+ * QueryClient context for v4
14
+ */
15
+ context?: Context<QueryClient | undefined>;
16
16
  }
17
17
  declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
18
-
19
- export { useQueryCallbacks };
20
- export type { UseQueryCallbacksProps };
18
+ //#endregion
19
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,25 @@
1
+ import { subscribeQueryCallbacks } from "../core/index.js";
2
+ import { useQueryClient } from "@tanstack/react-query";
3
+ import { useEffect } from "react";
4
+
5
+ //#region src/react/index.ts
6
+ function useQueryCallbacks(props) {
7
+ const queryClient = useQueryClient(props.context ? { context: props.context } : props.queryClient);
8
+ useEffect(() => {
9
+ return subscribeQueryCallbacks({
10
+ queryClient,
11
+ queryKey: props.queryKey,
12
+ onSuccess: props.onSuccess,
13
+ onError: props.onError,
14
+ onSettled: props.onSettled
15
+ });
16
+ }, [
17
+ props.queryKey,
18
+ props.onSuccess,
19
+ props.onError,
20
+ props.onSettled
21
+ ]);
22
+ }
23
+
24
+ //#endregion
25
+ export { useQueryCallbacks };
@@ -0,0 +1,4 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_index_svelte = require('./index.svelte.cjs');
3
+
4
+ exports.useQueryCallbacks = require_index_svelte.useQueryCallbacks;
@@ -0,0 +1,2 @@
1
+ import { UseQueryCallbacksProps, useQueryCallbacks } from "./index.svelte.cjs";
2
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,2 @@
1
+ import { UseQueryCallbacksProps, useQueryCallbacks } from "./index.svelte.js";
2
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,3 @@
1
+ import { useQueryCallbacks } from "./index.svelte.js";
2
+
3
+ export { useQueryCallbacks };
@@ -0,0 +1,23 @@
1
+ const require_core_index = require('../core/index.cjs');
2
+ let _tanstack_svelte_query = require("@tanstack/svelte-query");
3
+ let svelte = require("svelte");
4
+
5
+ //#region src/svelte/index.svelte.ts
6
+ function useQueryCallbacks(props) {
7
+ const _props = $derived(props());
8
+ const queryClient = $derived((0, _tanstack_svelte_query.useQueryClient)(_props.queryClient));
9
+ $effect(() => {
10
+ const _queryKey = _props.queryKey;
11
+ const _queryClient = queryClient;
12
+ return (0, svelte.untrack)(() => require_core_index.subscribeQueryCallbacks({
13
+ queryClient: _queryClient,
14
+ queryKey: _queryKey,
15
+ onSuccess: _props.onSuccess,
16
+ onError: _props.onError,
17
+ onSettled: _props.onSettled
18
+ }));
19
+ });
20
+ }
21
+
22
+ //#endregion
23
+ exports.useQueryCallbacks = useQueryCallbacks;
@@ -0,0 +1,14 @@
1
+ import { QueryCallbacks } from "../core/index.cjs";
2
+ import { QueryClient, QueryKey } from "@tanstack/svelte-query";
3
+
4
+ //#region src/svelte/index.svelte.d.ts
5
+ interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
6
+ queryKey: QueryKey;
7
+ /**
8
+ * QueryClient instance
9
+ */
10
+ queryClient?: QueryClient;
11
+ }
12
+ declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: () => UseQueryCallbacksProps<TQueryFnData, TError>): void;
13
+ //#endregion
14
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,14 @@
1
+ import { QueryCallbacks } from "../core/index.js";
2
+ import { QueryClient, QueryKey } from "@tanstack/svelte-query";
3
+
4
+ //#region src/svelte/index.svelte.d.ts
5
+ interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
6
+ queryKey: QueryKey;
7
+ /**
8
+ * QueryClient instance
9
+ */
10
+ queryClient?: QueryClient;
11
+ }
12
+ declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: () => UseQueryCallbacksProps<TQueryFnData, TError>): void;
13
+ //#endregion
14
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,23 @@
1
+ import { subscribeQueryCallbacks } from "../core/index.js";
2
+ import { useQueryClient } from "@tanstack/svelte-query";
3
+ import { untrack } from "svelte";
4
+
5
+ //#region src/svelte/index.svelte.ts
6
+ function useQueryCallbacks(props) {
7
+ const _props = $derived(props());
8
+ const queryClient = $derived(useQueryClient(_props.queryClient));
9
+ $effect(() => {
10
+ const _queryKey = _props.queryKey;
11
+ const _queryClient = queryClient;
12
+ return untrack(() => subscribeQueryCallbacks({
13
+ queryClient: _queryClient,
14
+ queryKey: _queryKey,
15
+ onSuccess: _props.onSuccess,
16
+ onError: _props.onError,
17
+ onSettled: _props.onSettled
18
+ }));
19
+ });
20
+ }
21
+
22
+ //#endregion
23
+ export { useQueryCallbacks };
@@ -1,28 +1,27 @@
1
- 'use strict';
2
-
3
- const vueQuery = require('@tanstack/vue-query');
4
- const vueDemi = require('vue-demi');
5
- const core_index = require('../core/index.cjs');
6
- require('@tanstack/query-core');
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_core_index = require('../core/index.cjs');
3
+ let _tanstack_vue_query = require("@tanstack/vue-query");
4
+ let vue_demi = require("vue-demi");
7
5
 
6
+ //#region src/vue/index.ts
8
7
  function useQueryCallbacks(props) {
9
- const queryClient = props.queryClient ?? (props.queryClientKey ? vueDemi.inject(props.queryClientKey, void 0) : void 0) ?? vueQuery.useQueryClient(props.queryClientId);
10
- vueDemi.watch(
11
- () => vueDemi.unref(props.queryKey),
12
- (queryKey, _oldVal, onCleanup) => {
13
- const unsubscribe = core_index.subscribeQueryCallbacks({
14
- queryClient,
15
- queryKey,
16
- onSuccess: props.onSuccess,
17
- onError: props.onError,
18
- onSettled: props.onSettled
19
- });
20
- onCleanup(() => {
21
- unsubscribe();
22
- });
23
- },
24
- { immediate: true }
25
- );
8
+ const queryClient = props.queryClient ?? (props.queryClientKey ? (0, vue_demi.inject)(props.queryClientKey, void 0) : void 0) ?? (0, _tanstack_vue_query.useQueryClient)(props.queryClientId);
9
+ (0, vue_demi.watch)(() => (0, vue_demi.unref)(props.queryKey), (queryKey, _oldVal, onCleanup) => {
10
+ const unsubscribe = require_core_index.subscribeQueryCallbacks({
11
+ queryClient,
12
+ queryKey,
13
+ onSuccess: props.onSuccess,
14
+ onError: props.onError,
15
+ onSettled: props.onSettled
16
+ });
17
+ onCleanup(() => {
18
+ unsubscribe();
19
+ });
20
+ }, {
21
+ immediate: true,
22
+ flush: "sync"
23
+ });
26
24
  }
27
25
 
28
- exports.useQueryCallbacks = useQueryCallbacks;
26
+ //#endregion
27
+ exports.useQueryCallbacks = useQueryCallbacks;
@@ -1,24 +1,23 @@
1
- import { QueryKey, QueryClient } from '@tanstack/vue-query';
2
- import { Ref } from 'vue-demi';
3
- import { QueryCallbacks } from '../core/index.cjs';
4
- import '@tanstack/query-core';
1
+ import { QueryCallbacks } from "../core/index.cjs";
2
+ import { QueryClient, QueryKey } from "@tanstack/vue-query";
3
+ import { Ref } from "vue-demi";
5
4
 
5
+ //#region src/vue/index.d.ts
6
6
  interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey | Ref<QueryKey>;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient Context Key for inject
14
- */
15
- queryClientKey?: string;
16
- /**
17
- * QueryClient ID for v5 useQueryClient()
18
- */
19
- queryClientId?: string;
7
+ queryKey: QueryKey | Ref<QueryKey>;
8
+ /**
9
+ * QueryClient instance
10
+ */
11
+ queryClient?: QueryClient;
12
+ /**
13
+ * QueryClient Context Key for inject
14
+ */
15
+ queryClientKey?: string;
16
+ /**
17
+ * QueryClient ID for v5 useQueryClient()
18
+ */
19
+ queryClientId?: string;
20
20
  }
21
21
  declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
22
-
23
- export { useQueryCallbacks };
24
- export type { UseQueryCallbacksProps };
22
+ //#endregion
23
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -1,24 +1,23 @@
1
- import { QueryKey, QueryClient } from '@tanstack/vue-query';
2
- import { Ref } from 'vue-demi';
3
- import { QueryCallbacks } from '../core/index.js';
4
- import '@tanstack/query-core';
1
+ import { QueryCallbacks } from "../core/index.js";
2
+ import { QueryClient, QueryKey } from "@tanstack/vue-query";
3
+ import { Ref } from "vue-demi";
5
4
 
5
+ //#region src/vue/index.d.ts
6
6
  interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey | Ref<QueryKey>;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient Context Key for inject
14
- */
15
- queryClientKey?: string;
16
- /**
17
- * QueryClient ID for v5 useQueryClient()
18
- */
19
- queryClientId?: string;
7
+ queryKey: QueryKey | Ref<QueryKey>;
8
+ /**
9
+ * QueryClient instance
10
+ */
11
+ queryClient?: QueryClient;
12
+ /**
13
+ * QueryClient Context Key for inject
14
+ */
15
+ queryClientKey?: string;
16
+ /**
17
+ * QueryClient ID for v5 useQueryClient()
18
+ */
19
+ queryClientId?: string;
20
20
  }
21
21
  declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
22
-
23
- export { useQueryCallbacks };
24
- export type { UseQueryCallbacksProps };
22
+ //#endregion
23
+ export { UseQueryCallbacksProps, useQueryCallbacks };
@@ -0,0 +1,26 @@
1
+ import { subscribeQueryCallbacks } from "../core/index.js";
2
+ import { useQueryClient } from "@tanstack/vue-query";
3
+ import { inject, unref, watch } from "vue-demi";
4
+
5
+ //#region src/vue/index.ts
6
+ function useQueryCallbacks(props) {
7
+ const queryClient = props.queryClient ?? (props.queryClientKey ? inject(props.queryClientKey, void 0) : void 0) ?? useQueryClient(props.queryClientId);
8
+ watch(() => unref(props.queryKey), (queryKey, _oldVal, onCleanup) => {
9
+ const unsubscribe = subscribeQueryCallbacks({
10
+ queryClient,
11
+ queryKey,
12
+ onSuccess: props.onSuccess,
13
+ onError: props.onError,
14
+ onSettled: props.onSettled
15
+ });
16
+ onCleanup(() => {
17
+ unsubscribe();
18
+ });
19
+ }, {
20
+ immediate: true,
21
+ flush: "sync"
22
+ });
23
+ }
24
+
25
+ //#endregion
26
+ export { useQueryCallbacks };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tanstack-query-callbacks",
3
3
  "type": "module",
4
- "version": "0.2.1",
4
+ "version": "0.3.0",
5
5
  "author": "zhong666 <hi@zhong666.me>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/aa900031/tanstack-query-callbacks#readme",
@@ -15,6 +15,7 @@
15
15
  "keywords": [
16
16
  "tanstack",
17
17
  "tanstack-query",
18
+ "svelte-query",
18
19
  "vue-query",
19
20
  "react-query"
20
21
  ],
@@ -22,29 +23,52 @@
22
23
  "exports": {
23
24
  ".": {
24
25
  "import": {
25
- "types": "./dist/core/index.d.mts",
26
- "default": "./dist/core/index.mjs"
26
+ "types": "./dist/core/index.d.ts",
27
+ "default": "./dist/core/index.js"
27
28
  },
28
29
  "require": {
29
30
  "types": "./dist/core/index.d.cts",
30
31
  "default": "./dist/core/index.cjs"
31
32
  }
32
33
  },
34
+ "./svelte": {
35
+ "import": {
36
+ "types": "./dist/svelte/index.d.ts",
37
+ "default": "./dist/svelte/index.js"
38
+ },
39
+ "require": {
40
+ "types": "./dist/svelte/index.d.cts",
41
+ "default": "./dist/svelte/index.cjs"
42
+ }
43
+ },
33
44
  "./vue": {
34
- "import": "./dist/vue/index.mjs",
35
- "require": "./dist/vue/index.cjs"
45
+ "import": {
46
+ "types": "./dist/vue/index.d.ts",
47
+ "default": "./dist/vue/index.js"
48
+ },
49
+ "require": {
50
+ "types": "./dist/vue/index.d.cts",
51
+ "default": "./dist/vue/index.cjs"
52
+ }
36
53
  },
37
54
  "./react": {
38
- "import": "./dist/react/index.mjs",
39
- "require": "./dist/react/index.cjs"
55
+ "import": {
56
+ "types": "./dist/react/index.d.ts",
57
+ "default": "./dist/react/index.js"
58
+ },
59
+ "require": {
60
+ "types": "./dist/react/index.d.cts",
61
+ "default": "./dist/react/index.cjs"
62
+ }
40
63
  }
41
64
  },
42
65
  "main": "dist/core/index.cjs",
43
- "module": "dist/core/index.mjs",
66
+ "module": "dist/core/index.js",
44
67
  "types": "dist/core/index.d.ts",
45
68
  "files": [
46
69
  "dist",
47
70
  "react.d.ts",
71
+ "svelte.d.ts",
48
72
  "vue.d.ts"
49
73
  ],
50
74
  "publishConfig": {
@@ -53,15 +77,20 @@
53
77
  "peerDependencies": {
54
78
  "@tanstack/query-core": "^4.* || ^5.*",
55
79
  "@tanstack/react-query": "^4.* || ^5.*",
80
+ "@tanstack/svelte-query": "^4.* || ^5.*",
56
81
  "@tanstack/vue-query": "^4.* || ^5.*",
57
82
  "@vue/composition-api": "^1.1.0",
58
83
  "react": "^18.0.0",
84
+ "svelte": "^3.0.0 || ^4.0.0 || ^5.0.0",
59
85
  "vue": "^2.6.12 || ^3.2.0"
60
86
  },
61
87
  "peerDependenciesMeta": {
62
88
  "@tanstack/react-query": {
63
89
  "optional": true
64
90
  },
91
+ "@tanstack/svelte-query": {
92
+ "optional": true
93
+ },
65
94
  "@tanstack/vue-query": {
66
95
  "optional": true
67
96
  },
@@ -71,48 +100,55 @@
71
100
  "react": {
72
101
  "optional": true
73
102
  },
103
+ "svelte": {
104
+ "optional": true
105
+ },
74
106
  "vue": {
75
107
  "optional": true
76
108
  }
77
109
  },
78
110
  "devDependencies": {
79
- "@aa900031/eslint-config": "^3.1.0",
80
- "@release-it/conventional-changelog": "^9.0.4",
81
- "@tanstack/query-core": "^5.72.1",
82
- "@tanstack/query-core-v4": "npm:@tanstack/query-core@^4.36.1",
83
- "@tanstack/react-query": "^5.72.1",
84
- "@tanstack/react-query-v4": "npm:@tanstack/react-query@^4.36.1",
85
- "@tanstack/vue-query": "^5.72.1",
86
- "@tanstack/vue-query-v4": "npm:@tanstack/vue-query@^4.37.1",
111
+ "@aa900031/eslint-config": "^6.0.0",
112
+ "@aa900031/release-it-config": "^0.1.1",
113
+ "@sveltejs/vite-plugin-svelte": "^5.1.1",
114
+ "@tanstack/query-core": "^5.90.20",
115
+ "@tanstack/query-core-v4": "npm:@tanstack/query-core@^4.41.0",
116
+ "@tanstack/react-query": "^5.90.10",
117
+ "@tanstack/react-query-v4": "npm:@tanstack/react-query@^4.42.0",
118
+ "@tanstack/svelte-query": "^6.0.18",
119
+ "@tanstack/vue-query": "^5.91.2",
120
+ "@tanstack/vue-query-v4": "npm:@tanstack/vue-query@^4.41.0",
87
121
  "@testing-library/react": "14.3.1",
122
+ "@testing-library/svelte": "^5.2.7",
88
123
  "@testing-library/vue": "^8.1.0",
89
- "@tsconfig/node18": "^18.2.4",
90
- "@types/node": "^18.19.86",
91
- "@types/react": "^18.3.20",
92
- "@types/react-dom": "^18.3.6",
93
- "@vitest/coverage-istanbul": "^3.1.1",
94
- "@vitest/ui": "^3.1.1",
124
+ "@tsconfig/node18": "^18.2.5",
125
+ "@types/node": "^18.19.130",
126
+ "@types/react": "^18.3.28",
127
+ "@types/react-dom": "^18.3.7",
128
+ "@vitest/coverage-istanbul": "^3.2.4",
129
+ "@vitest/ui": "^3.2.4",
95
130
  "@vue/composition-api": "^1.7.2",
96
- "conventional-changelog-unjs": "^1.0.0",
97
- "eslint": "^9.24.0",
98
- "happy-dom": "^17.4.4",
99
- "npm-run-all2": "^7.0.2",
131
+ "eslint": "^9.39.3",
132
+ "happy-dom": "^20.7.0",
133
+ "npm-run-all2": "^8.0.4",
100
134
  "react": "^18.3.1",
101
135
  "react-dom": "^18.3.1",
102
- "release-it": "^17.11.0",
103
- "typescript": "^5.8.3",
104
- "unbuild": "^3.5.0",
105
- "vitest": "^3.1.1",
106
- "vue": "^3.5.13",
107
- "vue-2": "npm:vue@~2.6.12",
136
+ "release-it": "^19.2.4",
137
+ "svelte": "^5.53.0",
138
+ "tsdown": "^0.20.0",
139
+ "typescript": "^5.9.3",
140
+ "typescript-svelte-plugin": "^0.3.50",
141
+ "vitest": "^3.2.4",
142
+ "vue": "^3.5.28",
143
+ "vue-2": "npm:vue@~2.6.14",
108
144
  "vue-2.7": "npm:vue@~2.7.16"
109
145
  },
110
146
  "resolutions": {
111
147
  "vue-demi": "latest"
112
148
  },
113
149
  "scripts": {
114
- "build": "unbuild",
115
- "test": "vitest run --coverage",
150
+ "build": "tsdown",
151
+ "test": "vitest run",
116
152
  "test:vue": "vue-demi-switch 3 && vitest run --dir ./src/vue",
117
153
  "test:vue-2": "vue-demi-switch 2 vue-2 && vitest run --dir ./src/vue",
118
154
  "test:vue-2.7": "vue-demi-switch 2.7 vue-2.7 && vitest run --dir ./src/vue",
@@ -120,7 +156,7 @@
120
156
  "dev:test-vue": "vue-demi-switch 3 vue && vitest --dir ./src/vue",
121
157
  "dev:test-vue-2": "vue-demi-switch 2 vue-2 && vitest --dir ./src/vue",
122
158
  "dev:test-vue-2.7": "vue-demi-switch 2.7 vue-2.7 && vitest --dir ./src/vue",
123
- "release": "release-it --ci",
159
+ "release": "release-it",
124
160
  "lint": "eslint . --cache --cache-location ./node_modules/.cache/eslint",
125
161
  "posttest:vue-2": "vue-demi-switch 3",
126
162
  "posttest:vue-2.7": "vue-demi-switch 3",
package/svelte.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ // eslint-disable-next-line ts/ban-ts-comment
2
+ // @ts-ignore
3
+ export * from './dist/svelte'
@@ -1,18 +0,0 @@
1
- import { QueryClient, QueryKey } from '@tanstack/query-core';
2
-
3
- type QuerySuccessHandler<TQueryFnData> = (data: TQueryFnData) => void;
4
- type QueryErrorHandler<TError> = (err: TError) => void;
5
- type QuerySettledHandler<TQueryFnData, TError> = (data: TQueryFnData | undefined, error: TError | null) => void;
6
- interface QueryCallbacks<TQueryFnData, TError> {
7
- onSuccess?: QuerySuccessHandler<TQueryFnData>;
8
- onError?: QueryErrorHandler<TError>;
9
- onSettled?: QuerySettledHandler<TQueryFnData, TError>;
10
- }
11
- interface SubscribeQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
12
- queryClient: QueryClient;
13
- queryKey: QueryKey;
14
- }
15
- declare function subscribeQueryCallbacks<TQueryFnData = unknown, TError = unknown>({ queryClient, queryKey, onSuccess, onError, onSettled, }: SubscribeQueryCallbacksProps<TQueryFnData, TError>): () => void;
16
-
17
- export { subscribeQueryCallbacks };
18
- export type { QueryCallbacks, QueryErrorHandler, QuerySettledHandler, QuerySuccessHandler, SubscribeQueryCallbacksProps };
@@ -1,40 +0,0 @@
1
- import { matchQuery, isCancelledError } from '@tanstack/query-core';
2
-
3
- function subscribeQueryCallbacks({
4
- queryClient,
5
- queryKey,
6
- onSuccess,
7
- onError,
8
- onSettled
9
- }) {
10
- if (!onSuccess && !onError && !onSettled)
11
- return noop;
12
- const cache = queryClient.getQueryCache();
13
- const unsubscribe = cache.subscribe((event) => {
14
- if (event.type !== "updated")
15
- return;
16
- if (!matchQuery({ queryKey, exact: true }, event.query))
17
- return;
18
- switch (event.action.type) {
19
- case "success": {
20
- if (!event.action.manual) {
21
- onSuccess?.(event.action.data);
22
- onSettled?.(event.action.data, null);
23
- }
24
- break;
25
- }
26
- case "error": {
27
- if (!isCancelledError(event.action.error)) {
28
- onError?.(event.action.error);
29
- onSettled?.(void 0, event.action.error);
30
- }
31
- break;
32
- }
33
- }
34
- });
35
- return unsubscribe;
36
- }
37
- function noop() {
38
- }
39
-
40
- export { subscribeQueryCallbacks };
@@ -1,20 +0,0 @@
1
- import { QueryKey, QueryClient } from '@tanstack/react-query';
2
- import { Context } from 'react';
3
- import { QueryCallbacks } from '../core/index.mjs';
4
- import '@tanstack/query-core';
5
-
6
- interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient context for v4
14
- */
15
- context?: Context<QueryClient | undefined>;
16
- }
17
- declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
18
-
19
- export { useQueryCallbacks };
20
- export type { UseQueryCallbacksProps };
@@ -1,27 +0,0 @@
1
- import { useQueryClient } from '@tanstack/react-query';
2
- import { useEffect } from 'react';
3
- import { subscribeQueryCallbacks } from '../core/index.mjs';
4
- import '@tanstack/query-core';
5
-
6
- function useQueryCallbacks(props) {
7
- const queryClient = useQueryClient(
8
- props.context ? { context: props.context } : props.queryClient
9
- // for v5
10
- );
11
- useEffect(() => {
12
- return subscribeQueryCallbacks({
13
- queryClient,
14
- queryKey: props.queryKey,
15
- onSuccess: props.onSuccess,
16
- onError: props.onError,
17
- onSettled: props.onSettled
18
- });
19
- }, [
20
- props.queryKey,
21
- props.onSuccess,
22
- props.onError,
23
- props.onSettled
24
- ]);
25
- }
26
-
27
- export { useQueryCallbacks };
@@ -1,24 +0,0 @@
1
- import { QueryKey, QueryClient } from '@tanstack/vue-query';
2
- import { Ref } from 'vue-demi';
3
- import { QueryCallbacks } from '../core/index.mjs';
4
- import '@tanstack/query-core';
5
-
6
- interface UseQueryCallbacksProps<TQueryFnData, TError> extends QueryCallbacks<TQueryFnData, TError> {
7
- queryKey: QueryKey | Ref<QueryKey>;
8
- /**
9
- * QueryClient instance
10
- */
11
- queryClient?: QueryClient;
12
- /**
13
- * QueryClient Context Key for inject
14
- */
15
- queryClientKey?: string;
16
- /**
17
- * QueryClient ID for v5 useQueryClient()
18
- */
19
- queryClientId?: string;
20
- }
21
- declare function useQueryCallbacks<TQueryFnData = unknown, TError = unknown>(props: UseQueryCallbacksProps<TQueryFnData, TError>): void;
22
-
23
- export { useQueryCallbacks };
24
- export type { UseQueryCallbacksProps };
@@ -1,26 +0,0 @@
1
- import { useQueryClient } from '@tanstack/vue-query';
2
- import { inject, watch, unref } from 'vue-demi';
3
- import { subscribeQueryCallbacks } from '../core/index.mjs';
4
- import '@tanstack/query-core';
5
-
6
- function useQueryCallbacks(props) {
7
- const queryClient = props.queryClient ?? (props.queryClientKey ? inject(props.queryClientKey, void 0) : void 0) ?? useQueryClient(props.queryClientId);
8
- watch(
9
- () => unref(props.queryKey),
10
- (queryKey, _oldVal, onCleanup) => {
11
- const unsubscribe = subscribeQueryCallbacks({
12
- queryClient,
13
- queryKey,
14
- onSuccess: props.onSuccess,
15
- onError: props.onError,
16
- onSettled: props.onSettled
17
- });
18
- onCleanup(() => {
19
- unsubscribe();
20
- });
21
- },
22
- { immediate: true }
23
- );
24
- }
25
-
26
- export { useQueryCallbacks };