@ahoo-wang/fetcher-react 1.8.5 → 1.8.6
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/dist/fetcher/index.d.ts +1 -0
- package/dist/fetcher/index.d.ts.map +1 -1
- package/dist/fetcher/useFetcher.d.ts +41 -0
- package/dist/fetcher/useFetcher.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +57 -16
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +4 -3
package/dist/fetcher/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetcher/index.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetcher/index.ts"],"names":[],"mappings":"AAaA,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FetcherCapable, FetchExchange, FetchRequest, RequestOptions } from '@ahoo-wang/fetcher';
|
|
2
|
+
import { DependencyList } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the useFetcher hook.
|
|
5
|
+
* Extends RequestOptions and FetcherCapable interfaces.
|
|
6
|
+
*/
|
|
7
|
+
export interface UseFetcherOptions extends RequestOptions, FetcherCapable {
|
|
8
|
+
/**
|
|
9
|
+
* Dependencies list for the fetch operation.
|
|
10
|
+
* When provided, the hook will re-fetch when any of these values change.
|
|
11
|
+
*/
|
|
12
|
+
readonly deps?: DependencyList;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the fetch operation should execute immediately upon component mount.
|
|
15
|
+
* Defaults to true.
|
|
16
|
+
*/
|
|
17
|
+
readonly immediate?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The result object returned by the useFetcher hook.
|
|
21
|
+
* @template R - The type of the data returned by the fetch operation
|
|
22
|
+
*/
|
|
23
|
+
export interface UseFetcherResult<R> {
|
|
24
|
+
/** Indicates if the fetch operation is currently in progress */
|
|
25
|
+
loading: boolean;
|
|
26
|
+
/** The FetchExchange object representing the ongoing fetch operation */
|
|
27
|
+
exchange?: FetchExchange;
|
|
28
|
+
/** The data returned by the fetch operation, or undefined if not yet loaded or an error occurred */
|
|
29
|
+
result?: R;
|
|
30
|
+
/** Any error that occurred during the fetch operation, or undefined if no error */
|
|
31
|
+
error?: unknown;
|
|
32
|
+
/**
|
|
33
|
+
* Function to manually trigger the fetch operation.
|
|
34
|
+
* Useful for fetching data on demand rather than automatically.
|
|
35
|
+
*/
|
|
36
|
+
execute: () => Promise<void>;
|
|
37
|
+
/** Function to cancel the ongoing fetch operation */
|
|
38
|
+
cancel: () => void;
|
|
39
|
+
}
|
|
40
|
+
export declare function useFetcher<R>(request: FetchRequest, options?: UseFetcherOptions): UseFetcherResult<R>;
|
|
41
|
+
//# sourceMappingURL=useFetcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,cAAc,EACd,aAAa,EACb,YAAY,EACZ,cAAc,EACf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAA4C,MAAM,OAAO,CAAC;AAGjF;;;GAGG;AACH,MAAM,WAAW,iBAAkB,SAAQ,cAAc,EAAE,cAAc;IACvE;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,cAAc,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IAEjB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,aAAa,CAAC;IAEzB,oGAAoG;IACpG,MAAM,CAAC,EAAE,CAAC,CAAC;IAEX,mFAAmF;IACnF,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;OAGG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7B,qDAAqD;IACrD,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAmErG"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,22 +1,63 @@
|
|
|
1
|
-
import { useCallback as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { useCallback as o, useSyncExternalStore as F, useRef as v, useEffect as m, useState as i } from "react";
|
|
2
|
+
import { fetcher as A, getFetcher as L } from "@ahoo-wang/fetcher";
|
|
3
|
+
function V(t) {
|
|
4
|
+
const e = o(
|
|
5
|
+
(s) => t.addListener(s),
|
|
6
|
+
[t]
|
|
7
|
+
), u = o(
|
|
8
|
+
() => t.get(),
|
|
9
|
+
[t]
|
|
10
|
+
), f = F(
|
|
10
11
|
e,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
),
|
|
14
|
-
(
|
|
15
|
-
[
|
|
12
|
+
u,
|
|
13
|
+
u
|
|
14
|
+
), a = o(
|
|
15
|
+
(s) => t.set(s),
|
|
16
|
+
[t]
|
|
16
17
|
);
|
|
17
|
-
return [
|
|
18
|
+
return [f, a];
|
|
19
|
+
}
|
|
20
|
+
function M() {
|
|
21
|
+
var t = v(!1), e = o(function() {
|
|
22
|
+
return t.current;
|
|
23
|
+
}, []);
|
|
24
|
+
return m(function() {
|
|
25
|
+
return t.current = !0, function() {
|
|
26
|
+
t.current = !1;
|
|
27
|
+
};
|
|
28
|
+
}, []), e;
|
|
29
|
+
}
|
|
30
|
+
function $(t, e) {
|
|
31
|
+
const { deps: u = [], fetcher: f = A, immediate: a = !0 } = e || {}, [s, d] = i(!1), [x, b] = i(void 0), [g, C] = i(void 0), [E, R] = i(void 0), n = M(), r = v(), h = L(f), l = o(async () => {
|
|
32
|
+
r.current && r.current.abort(), r.current = t.abortController ?? new AbortController(), t.abortController = r.current, n() && (d(!0), b(void 0));
|
|
33
|
+
try {
|
|
34
|
+
const c = await h.exchange(t, e);
|
|
35
|
+
n() && C(c);
|
|
36
|
+
const w = await c.extractResult();
|
|
37
|
+
n() && R(w);
|
|
38
|
+
} catch (c) {
|
|
39
|
+
if (c instanceof Error && c.name === "AbortError")
|
|
40
|
+
return;
|
|
41
|
+
n() && b(c);
|
|
42
|
+
} finally {
|
|
43
|
+
n() && d(!1), r.current === t.abortController && (r.current = void 0);
|
|
44
|
+
}
|
|
45
|
+
}, [h, n, e, ...u]), p = o(() => {
|
|
46
|
+
r.current?.abort();
|
|
47
|
+
}, []);
|
|
48
|
+
return m(() => (a && l(), () => {
|
|
49
|
+
r.current?.abort(), r.current = void 0;
|
|
50
|
+
}), [l, a, ...u]), {
|
|
51
|
+
loading: s,
|
|
52
|
+
exchange: g,
|
|
53
|
+
result: E,
|
|
54
|
+
error: x,
|
|
55
|
+
execute: l,
|
|
56
|
+
cancel: p
|
|
57
|
+
};
|
|
18
58
|
}
|
|
19
59
|
export {
|
|
20
|
-
|
|
60
|
+
$ as useFetcher,
|
|
61
|
+
V as useKeyStorage
|
|
21
62
|
};
|
|
22
63
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/storage/useKeyStorage.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useCallback, useSyncExternalStore } from 'react';\nimport { KeyStorage } from '@ahoo-wang/fetcher-storage';\n\n/**\n * A React hook that provides state management for a KeyStorage instance.\n * Subscribes to storage changes and returns the current value along with a setter function.\n *\n * @template T - The type of value stored in the key storage\n * @param keyStorage - The KeyStorage instance to subscribe to and manage\n * @returns A tuple containing the current stored value and a function to update it\n */\nexport function useKeyStorage<T>(\n keyStorage: KeyStorage<T>,\n): [T | null, (value: T) => void] {\n const subscribe = useCallback(\n (callback: () => void) => keyStorage.addListener(callback),\n [keyStorage],\n );\n const getSnapshot = useCallback(\n () => keyStorage.get(),\n [keyStorage],\n );\n const value = useSyncExternalStore(\n subscribe,\n getSnapshot,\n getSnapshot,\n );\n const setValue = useCallback(\n (value: T) => keyStorage.set(value),\n [keyStorage],\n );\n return [value, setValue];\n}"],"names":["useKeyStorage","keyStorage","subscribe","useCallback","callback","getSnapshot","value","useSyncExternalStore","setValue"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/storage/useKeyStorage.ts","../../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMountedState.js","../src/fetcher/useFetcher.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useCallback, useSyncExternalStore } from 'react';\nimport { KeyStorage } from '@ahoo-wang/fetcher-storage';\n\n/**\n * A React hook that provides state management for a KeyStorage instance.\n * Subscribes to storage changes and returns the current value along with a setter function.\n *\n * @template T - The type of value stored in the key storage\n * @param keyStorage - The KeyStorage instance to subscribe to and manage\n * @returns A tuple containing the current stored value and a function to update it\n */\nexport function useKeyStorage<T>(\n keyStorage: KeyStorage<T>,\n): [T | null, (value: T) => void] {\n const subscribe = useCallback(\n (callback: () => void) => keyStorage.addListener(callback),\n [keyStorage],\n );\n const getSnapshot = useCallback(\n () => keyStorage.get(),\n [keyStorage],\n );\n const value = useSyncExternalStore(\n subscribe,\n getSnapshot,\n getSnapshot,\n );\n const setValue = useCallback(\n (value: T) => keyStorage.set(value),\n [keyStorage],\n );\n return [value, setValue];\n}","import { useCallback, useEffect, useRef } from 'react';\nexport default function useMountedState() {\n var mountedRef = useRef(false);\n var get = useCallback(function () { return mountedRef.current; }, []);\n useEffect(function () {\n mountedRef.current = true;\n return function () {\n mountedRef.current = false;\n };\n }, []);\n return get;\n}\n","/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n fetcher as defaultFetcher,\n FetcherCapable,\n FetchExchange,\n FetchRequest, getFetcher,\n RequestOptions,\n} from '@ahoo-wang/fetcher';\nimport { DependencyList, useRef, useCallback, useEffect, useState } from 'react';\nimport { useMountedState } from 'react-use';\n\n/**\n * Configuration options for the useFetcher hook.\n * Extends RequestOptions and FetcherCapable interfaces.\n */\nexport interface UseFetcherOptions extends RequestOptions, FetcherCapable {\n /**\n * Dependencies list for the fetch operation.\n * When provided, the hook will re-fetch when any of these values change.\n */\n readonly deps?: DependencyList;\n\n /**\n * Whether the fetch operation should execute immediately upon component mount.\n * Defaults to true.\n */\n readonly immediate?: boolean;\n}\n\n/**\n * The result object returned by the useFetcher hook.\n * @template R - The type of the data returned by the fetch operation\n */\nexport interface UseFetcherResult<R> {\n /** Indicates if the fetch operation is currently in progress */\n loading: boolean;\n\n /** The FetchExchange object representing the ongoing fetch operation */\n exchange?: FetchExchange;\n\n /** The data returned by the fetch operation, or undefined if not yet loaded or an error occurred */\n result?: R;\n\n /** Any error that occurred during the fetch operation, or undefined if no error */\n error?: unknown;\n\n /**\n * Function to manually trigger the fetch operation.\n * Useful for fetching data on demand rather than automatically.\n */\n execute: () => Promise<void>;\n\n /** Function to cancel the ongoing fetch operation */\n cancel: () => void;\n}\n\nexport function useFetcher<R>(request: FetchRequest, options?: UseFetcherOptions): UseFetcherResult<R> {\n const { deps = [], fetcher = defaultFetcher, immediate = true } = options || {};\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<unknown>(undefined);\n const [exchange, setExchange] = useState<FetchExchange | undefined>(undefined);\n const [result, setResult] = useState<R | undefined>(undefined);\n const isMounted = useMountedState();\n const abortControllerRef = useRef<AbortController | undefined>();\n\n const currentFetcher = getFetcher(fetcher);\n /**\n * Execute the fetch operation.\n * Cancels any ongoing fetch before starting a new one.\n */\n const execute = useCallback(async () => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n abortControllerRef.current = request.abortController ?? new AbortController();\n request.abortController = abortControllerRef.current;\n if (isMounted()) {\n setLoading(true);\n setError(undefined);\n }\n try {\n const exchange = await currentFetcher.exchange(request, options);\n if (isMounted()) {\n setExchange(exchange);\n }\n const result = await exchange.extractResult<R>();\n if (isMounted()) {\n setResult(result);\n }\n } catch (error) {\n if (error instanceof Error && error.name === 'AbortError') {\n return;\n }\n if (isMounted()) {\n setError(error);\n }\n } finally {\n if (isMounted()) {\n setLoading(false);\n }\n if (abortControllerRef.current === request.abortController) {\n abortControllerRef.current = undefined;\n }\n }\n }, [currentFetcher, isMounted, options, ...deps]);\n /**\n * Cancel the ongoing fetch operation if one is in progress.\n */\n const cancel = useCallback(() => {\n abortControllerRef.current?.abort();\n }, []);\n useEffect(() => {\n if (immediate) {\n execute();\n }\n return () => {\n abortControllerRef.current?.abort();\n abortControllerRef.current = undefined;\n };\n }, [execute, immediate, ...deps]);\n return {\n loading, exchange, result, error, execute, cancel,\n };\n}"],"names":["useKeyStorage","keyStorage","subscribe","useCallback","callback","getSnapshot","value","useSyncExternalStore","setValue","useMountedState","mountedRef","useRef","get","useEffect","useFetcher","request","options","deps","fetcher","defaultFetcher","immediate","loading","setLoading","useState","error","setError","exchange","setExchange","result","setResult","isMounted","abortControllerRef","currentFetcher","getFetcher","execute","cancel"],"mappings":";;AAwBO,SAASA,EACdC,GACgC;AAChC,QAAMC,IAAYC;AAAA,IAChB,CAACC,MAAyBH,EAAW,YAAYG,CAAQ;AAAA,IACzD,CAACH,CAAU;AAAA,EAAA,GAEPI,IAAcF;AAAA,IAClB,MAAMF,EAAW,IAAA;AAAA,IACjB,CAACA,CAAU;AAAA,EAAA,GAEPK,IAAQC;AAAA,IACZL;AAAA,IACAG;AAAA,IACAA;AAAA,EAAA,GAEIG,IAAWL;AAAA,IACf,CAACG,MAAaL,EAAW,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AA;AACE,MAAIM,OACFR,EAAW,EAAK,GAEdS,EAAmB,YAAYhB,EAAQ,oBACzCgB,EAAmB,UAAU;AAAA,IAEjC;AAAA,EACF,GAAG,CAACC,GAAgBF,GAAWd,GAAS,GAAGC,CAAI,CAAC,GAI1CkB,IAAShC,EAAY,MAAM;AAC/B,IAAA4B,EAAmB,SAAS,MAAA;AAAA,EAC9B,GAAG,CAAA,CAAE;AACL,SAAAlB,EAAU,OACJO,KACFc,EAAA,GAEK,MAAM;AACX,IAAAH,EAAmB,SAAS,MAAA,GAC5BA,EAAmB,UAAU;AAAA,EAC/B,IACC,CAACG,GAASd,GAAW,GAAGH,CAAI,CAAC,GACzB;AAAA,IACL,SAAAI;AAAA,IAAS,UAAAK;AAAA,IAAU,QAAAE;AAAA,IAAQ,OAAAJ;AAAA,IAAO,SAAAU;AAAA,IAAS,QAAAC;AAAA,EAAA;AAE/C;","x_google_ignoreList":[1]}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","react","@ahoo-wang/fetcher"],e):(r=typeof globalThis<"u"?globalThis:r||self,e(r.FetcherReact={},r.React,r.Fetcher))})(this,(function(r,e,d){"use strict";function v(t){const u=e.useCallback(i=>t.addListener(i),[t]),s=e.useCallback(()=>t.get(),[t]),a=e.useSyncExternalStore(u,s,s),f=e.useCallback(i=>t.set(i),[t]);return[a,f]}function g(){var t=e.useRef(!1),u=e.useCallback(function(){return t.current},[]);return e.useEffect(function(){return t.current=!0,function(){t.current=!1}},[]),u}function x(t,u){const{deps:s=[],fetcher:a=d.fetcher,immediate:f=!0}=u||{},[i,h]=e.useState(!1),[m,b]=e.useState(void 0),[E,S]=e.useState(void 0),[p,F]=e.useState(void 0),o=g(),n=e.useRef(),C=d.getFetcher(a),l=e.useCallback(async()=>{n.current&&n.current.abort(),n.current=t.abortController??new AbortController,t.abortController=n.current,o()&&(h(!0),b(void 0));try{const c=await C.exchange(t,u);o()&&S(c);const y=await c.extractResult();o()&&F(y)}catch(c){if(c instanceof Error&&c.name==="AbortError")return;o()&&b(c)}finally{o()&&h(!1),n.current===t.abortController&&(n.current=void 0)}},[C,o,u,...s]),w=e.useCallback(()=>{n.current?.abort()},[]);return e.useEffect(()=>(f&&l(),()=>{n.current?.abort(),n.current=void 0}),[l,f,...s]),{loading:i,exchange:E,result:p,error:m,execute:l,cancel:w}}r.useFetcher=x,r.useKeyStorage=v,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/storage/useKeyStorage.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useCallback, useSyncExternalStore } from 'react';\nimport { KeyStorage } from '@ahoo-wang/fetcher-storage';\n\n/**\n * A React hook that provides state management for a KeyStorage instance.\n * Subscribes to storage changes and returns the current value along with a setter function.\n *\n * @template T - The type of value stored in the key storage\n * @param keyStorage - The KeyStorage instance to subscribe to and manage\n * @returns A tuple containing the current stored value and a function to update it\n */\nexport function useKeyStorage<T>(\n keyStorage: KeyStorage<T>,\n): [T | null, (value: T) => void] {\n const subscribe = useCallback(\n (callback: () => void) => keyStorage.addListener(callback),\n [keyStorage],\n );\n const getSnapshot = useCallback(\n () => keyStorage.get(),\n [keyStorage],\n );\n const value = useSyncExternalStore(\n subscribe,\n getSnapshot,\n getSnapshot,\n );\n const setValue = useCallback(\n (value: T) => keyStorage.set(value),\n [keyStorage],\n );\n return [value, setValue];\n}"],"names":["useKeyStorage","keyStorage","subscribe","useCallback","callback","getSnapshot","value","useSyncExternalStore","setValue"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/storage/useKeyStorage.ts","../../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMountedState.js","../src/fetcher/useFetcher.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useCallback, useSyncExternalStore } from 'react';\nimport { KeyStorage } from '@ahoo-wang/fetcher-storage';\n\n/**\n * A React hook that provides state management for a KeyStorage instance.\n * Subscribes to storage changes and returns the current value along with a setter function.\n *\n * @template T - The type of value stored in the key storage\n * @param keyStorage - The KeyStorage instance to subscribe to and manage\n * @returns A tuple containing the current stored value and a function to update it\n */\nexport function useKeyStorage<T>(\n keyStorage: KeyStorage<T>,\n): [T | null, (value: T) => void] {\n const subscribe = useCallback(\n (callback: () => void) => keyStorage.addListener(callback),\n [keyStorage],\n );\n const getSnapshot = useCallback(\n () => keyStorage.get(),\n [keyStorage],\n );\n const value = useSyncExternalStore(\n subscribe,\n getSnapshot,\n getSnapshot,\n );\n const setValue = useCallback(\n (value: T) => keyStorage.set(value),\n [keyStorage],\n );\n return [value, setValue];\n}","import { useCallback, useEffect, useRef } from 'react';\nexport default function useMountedState() {\n var mountedRef = useRef(false);\n var get = useCallback(function () { return mountedRef.current; }, []);\n useEffect(function () {\n mountedRef.current = true;\n return function () {\n mountedRef.current = false;\n };\n }, []);\n return get;\n}\n","/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n fetcher as defaultFetcher,\n FetcherCapable,\n FetchExchange,\n FetchRequest, getFetcher,\n RequestOptions,\n} from '@ahoo-wang/fetcher';\nimport { DependencyList, useRef, useCallback, useEffect, useState } from 'react';\nimport { useMountedState } from 'react-use';\n\n/**\n * Configuration options for the useFetcher hook.\n * Extends RequestOptions and FetcherCapable interfaces.\n */\nexport interface UseFetcherOptions extends RequestOptions, FetcherCapable {\n /**\n * Dependencies list for the fetch operation.\n * When provided, the hook will re-fetch when any of these values change.\n */\n readonly deps?: DependencyList;\n\n /**\n * Whether the fetch operation should execute immediately upon component mount.\n * Defaults to true.\n */\n readonly immediate?: boolean;\n}\n\n/**\n * The result object returned by the useFetcher hook.\n * @template R - The type of the data returned by the fetch operation\n */\nexport interface UseFetcherResult<R> {\n /** Indicates if the fetch operation is currently in progress */\n loading: boolean;\n\n /** The FetchExchange object representing the ongoing fetch operation */\n exchange?: FetchExchange;\n\n /** The data returned by the fetch operation, or undefined if not yet loaded or an error occurred */\n result?: R;\n\n /** Any error that occurred during the fetch operation, or undefined if no error */\n error?: unknown;\n\n /**\n * Function to manually trigger the fetch operation.\n * Useful for fetching data on demand rather than automatically.\n */\n execute: () => Promise<void>;\n\n /** Function to cancel the ongoing fetch operation */\n cancel: () => void;\n}\n\nexport function useFetcher<R>(request: FetchRequest, options?: UseFetcherOptions): UseFetcherResult<R> {\n const { deps = [], fetcher = defaultFetcher, immediate = true } = options || {};\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<unknown>(undefined);\n const [exchange, setExchange] = useState<FetchExchange | undefined>(undefined);\n const [result, setResult] = useState<R | undefined>(undefined);\n const isMounted = useMountedState();\n const abortControllerRef = useRef<AbortController | undefined>();\n\n const currentFetcher = getFetcher(fetcher);\n /**\n * Execute the fetch operation.\n * Cancels any ongoing fetch before starting a new one.\n */\n const execute = useCallback(async () => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n abortControllerRef.current = request.abortController ?? new AbortController();\n request.abortController = abortControllerRef.current;\n if (isMounted()) {\n setLoading(true);\n setError(undefined);\n }\n try {\n const exchange = await currentFetcher.exchange(request, options);\n if (isMounted()) {\n setExchange(exchange);\n }\n const result = await exchange.extractResult<R>();\n if (isMounted()) {\n setResult(result);\n }\n } catch (error) {\n if (error instanceof Error && error.name === 'AbortError') {\n return;\n }\n if (isMounted()) {\n setError(error);\n }\n } finally {\n if (isMounted()) {\n setLoading(false);\n }\n if (abortControllerRef.current === request.abortController) {\n abortControllerRef.current = undefined;\n }\n }\n }, [currentFetcher, isMounted, options, ...deps]);\n /**\n * Cancel the ongoing fetch operation if one is in progress.\n */\n const cancel = useCallback(() => {\n abortControllerRef.current?.abort();\n }, []);\n useEffect(() => {\n if (immediate) {\n execute();\n }\n return () => {\n abortControllerRef.current?.abort();\n abortControllerRef.current = undefined;\n };\n }, [execute, immediate, ...deps]);\n return {\n loading, exchange, result, error, execute, cancel,\n };\n}"],"names":["useKeyStorage","keyStorage","subscribe","useCallback","callback","getSnapshot","value","useSyncExternalStore","setValue","useMountedState","mountedRef","useRef","get","useEffect","useFetcher","request","options","deps","fetcher","defaultFetcher","immediate","loading","setLoading","useState","error","setError","exchange","setExchange","result","setResult","isMounted","abortControllerRef","currentFetcher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QACzCD,MACFR,EAAW,EAAI,EACfG,EAAS,MAAS,GAEpB,GAAI,CACF,MAAMC,EAAW,MAAMM,EAAe,SAASjB,EAASC,CAAO,EAC3Dc,KACFH,EAAYD,CAAQ,EAEtB,MAAME,EAAS,MAAMF,EAAS,cAAA,EAC1BI,KACFD,EAAUD,CAAM,CAEpB,OAASJ,EAAO,CACd,GAAIA,aAAiB,OAASA,EAAM,OAAS,aAC3C,OAEEM,KACFL,EAASD,CAAK,CAElB,QAAA,CACMM,KACFR,EAAW,EAAK,EAEdS,EAAmB,UAAYhB,EAAQ,kBACzCgB,EAAmB,QAAU,OAEjC,CACF,EAAG,CAACC,EAAgBF,EAAWd,EAAS,GAAGC,CAAI,CAAC,EAI1CkB,EAAShC,EAAAA,YAAY,IAAM,CAC/B4B,EAAmB,SAAS,MAAA,CAC9B,EAAG,CAAA,CAAE,EACLlB,OAAAA,EAAAA,UAAU,KACJO,GACFc,EAAA,EAEK,IAAM,CACXH,EAAmB,SAAS,MAAA,EAC5BA,EAAmB,QAAU,MAC/B,GACC,CAACG,EAASd,EAAW,GAAGH,CAAI,CAAC,EACzB,CACL,QAAAI,EAAS,SAAAK,EAAU,OAAAE,EAAQ,MAAAJ,EAAO,QAAAU,EAAS,OAAAC,CAAA,CAE/C","x_google_ignoreList":[1]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-react",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.6",
|
|
4
4
|
"description": "React integration for Fetcher HTTP client. Provides React Hooks and components for seamless data fetching with automatic re-rendering and loading states.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -37,8 +37,9 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"
|
|
41
|
-
"@ahoo-wang/fetcher
|
|
40
|
+
"react-use": "^17.6.0",
|
|
41
|
+
"@ahoo-wang/fetcher": "1.8.6",
|
|
42
|
+
"@ahoo-wang/fetcher-storage": "1.8.6"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
45
|
"react": ">=16.8.0",
|