@reause/integrations 0.1.2
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 +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.iife.js +1621 -0
- package/dist/index.iife.min.js +1 -0
- package/dist/index.js +13 -0
- package/dist/useAsyncValidator.d.ts +83 -0
- package/dist/useAsyncValidator.iife.js +192 -0
- package/dist/useAsyncValidator.iife.min.js +1 -0
- package/dist/useAsyncValidator.js +168 -0
- package/dist/useAxios.d.ts +125 -0
- package/dist/useAxios.iife.js +288 -0
- package/dist/useAxios.iife.min.js +1 -0
- package/dist/useAxios.js +265 -0
- package/dist/useChangeCase.d.ts +57 -0
- package/dist/useChangeCase.iife.js +91 -0
- package/dist/useChangeCase.iife.min.js +1 -0
- package/dist/useChangeCase.js +68 -0
- package/dist/useCookies.d.ts +117 -0
- package/dist/useCookies.iife.js +142 -0
- package/dist/useCookies.iife.min.js +1 -0
- package/dist/useCookies.js +117 -0
- package/dist/useDrauu.d.ts +152 -0
- package/dist/useDrauu.iife.js +221 -0
- package/dist/useDrauu.iife.min.js +1 -0
- package/dist/useDrauu.js +221 -0
- package/dist/useFocusTrap.d.ts +116 -0
- package/dist/useFocusTrap.iife.js +125 -0
- package/dist/useFocusTrap.iife.min.js +1 -0
- package/dist/useFocusTrap.js +125 -0
- package/dist/useFuse.d.ts +86 -0
- package/dist/useFuse.iife.js +95 -0
- package/dist/useFuse.iife.min.js +1 -0
- package/dist/useFuse.js +72 -0
- package/dist/useIDBKeyval.d.ts +139 -0
- package/dist/useIDBKeyval.iife.js +175 -0
- package/dist/useIDBKeyval.iife.min.js +1 -0
- package/dist/useIDBKeyval.js +174 -0
- package/dist/useJwt.d.ts +52 -0
- package/dist/useJwt.iife.js +54 -0
- package/dist/useJwt.iife.min.js +1 -0
- package/dist/useJwt.js +53 -0
- package/dist/useNProgress.d.ts +115 -0
- package/dist/useNProgress.iife.js +148 -0
- package/dist/useNProgress.iife.min.js +1 -0
- package/dist/useNProgress.js +125 -0
- package/dist/useQRCode.d.ts +38 -0
- package/dist/useQRCode.iife.js +78 -0
- package/dist/useQRCode.iife.min.js +1 -0
- package/dist/useQRCode.js +55 -0
- package/dist/useSortable.d.ts +143 -0
- package/dist/useSortable.iife.js +199 -0
- package/dist/useSortable.iife.min.js +1 -0
- package/dist/useSortable.js +173 -0
- package/package.json +92 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
//#region useIDBKeyval/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Custom (de)serialization between the value held in state and the raw value
|
|
4
|
+
* stored in IndexedDB. Defaults to an identity pair.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseIDBKeyvalSerializer<T> {
|
|
7
|
+
read: (raw: unknown) => T;
|
|
8
|
+
write: (value: T) => unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface UseIDBOptions<T> {
|
|
11
|
+
/**
|
|
12
|
+
* Allow a custom `window` instance, e.g. working with iframes or in testing
|
|
13
|
+
* environments.
|
|
14
|
+
*/
|
|
15
|
+
window?: Window;
|
|
16
|
+
/**
|
|
17
|
+
* On error callback.
|
|
18
|
+
*
|
|
19
|
+
* @default (error) => console.error(error)
|
|
20
|
+
*/
|
|
21
|
+
onError?: (error: unknown) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Write the default value to the store when the key does not exist.
|
|
24
|
+
*
|
|
25
|
+
* @default true
|
|
26
|
+
*/
|
|
27
|
+
writeDefaults?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Custom data serialization.
|
|
30
|
+
*/
|
|
31
|
+
serializer?: UseIDBKeyvalSerializer<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Listen to changes from other tabs through a `BroadcastChannel`, useful for
|
|
34
|
+
* multi-tab applications.
|
|
35
|
+
*
|
|
36
|
+
* @default true
|
|
37
|
+
*/
|
|
38
|
+
listenToStorageChanges?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Watch for deep changes.
|
|
41
|
+
*
|
|
42
|
+
* Accepted for upstream parity only — **no effect**: React has no deep
|
|
43
|
+
* observation, so writes happen explicitly through `setData` (see the
|
|
44
|
+
* divergence notes on `useIDBKeyval`).
|
|
45
|
+
*
|
|
46
|
+
* @default true
|
|
47
|
+
*/
|
|
48
|
+
deep?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Use a shallow reference.
|
|
51
|
+
*
|
|
52
|
+
* Accepted for upstream parity only — **no effect**: React state is always
|
|
53
|
+
* replaced wholesale.
|
|
54
|
+
*
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
shallow?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* The flush timing of the (upstream) watcher.
|
|
60
|
+
*
|
|
61
|
+
* Accepted for upstream parity only — **no effect**: React has no watcher to
|
|
62
|
+
* flush, so writes happen explicitly through `setData` (see the divergence
|
|
63
|
+
* notes on `useIDBKeyval`).
|
|
64
|
+
*
|
|
65
|
+
* @default 'pre'
|
|
66
|
+
*/
|
|
67
|
+
flush?: 'pre' | 'post' | 'sync' | 'async';
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Reactive companion state of `useIDBKeyval` — the React replacement for the
|
|
71
|
+
* upstream `isFinished` / `isSupported` refs (issue §2B tuple family).
|
|
72
|
+
*/
|
|
73
|
+
export interface UseIDBKeyvalControls {
|
|
74
|
+
/**
|
|
75
|
+
* Whether the initial read from the store has finished (successfully or
|
|
76
|
+
* with an error).
|
|
77
|
+
*/
|
|
78
|
+
isFinished: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Whether the `BroadcastChannel` API is available in the current browser.
|
|
81
|
+
*/
|
|
82
|
+
isSupported: boolean;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* React return type: `[data, setData, controls]` — the state-like tuple family
|
|
86
|
+
* used by `useStateWithControl` and `useStorage` (issue §2B). `data` is
|
|
87
|
+
* `T | null` where `null` means the key was removed from the store.
|
|
88
|
+
*/
|
|
89
|
+
export type UseIDBKeyvalReturn<T> = [data: T | null, setData: (value: T | null) => Promise<void>, controls: UseIDBKeyvalControls];
|
|
90
|
+
/**
|
|
91
|
+
* Reactive [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
|
92
|
+
* store — React port of VueUse's `useIDBKeyval`.
|
|
93
|
+
*
|
|
94
|
+
* Map from @vueuse/integrations `useIDBKeyval`
|
|
95
|
+
* (`source/vueuse/packages/integrations/useIDBKeyval/`), a reactive wrapper
|
|
96
|
+
* around [`idb-keyval`](https://github.com/jakearchibald/idb-keyval). The value
|
|
97
|
+
* is persisted under `key`, read once on mount and kept in sync across tabs
|
|
98
|
+
* through a `BroadcastChannel`.
|
|
99
|
+
*
|
|
100
|
+
* React divergences:
|
|
101
|
+
* - the upstream object return `{ data, isFinished, isSupported, set }` becomes
|
|
102
|
+
* the state-like tuple `[data, setData, controls]` (§2B), mirroring
|
|
103
|
+
* `useStorage`: `data` is `T | null` (`null` = removed) and `setData(null)`
|
|
104
|
+
* deletes the key through `del`;
|
|
105
|
+
* - **there is no deep watcher.** Upstream writes on *any* mutation of
|
|
106
|
+
* `data.value` (`watchPausable(data, write, { deep: true })`), so
|
|
107
|
+
* `data.value.count++` persists by itself. React state has no deep
|
|
108
|
+
* observation, so writes happen **explicitly through `setData`** — that is
|
|
109
|
+
* the React contract. Mutating an object held in `data` in place does *not*
|
|
110
|
+
* persist; call `setData(next)` with a new value instead. The `deep` /
|
|
111
|
+
* `shallow` / `flush` options are accepted for parity and have no effect;
|
|
112
|
+
* - `isFinished` / `isSupported` live in the third tuple slot as plain
|
|
113
|
+
* booleans (upstream: `ShallowRef` / `ComputedRef`), and `isSupported` is
|
|
114
|
+
* computed synchronously (`typeof window !== 'undefined' && 'BroadcastChannel'
|
|
115
|
+
* in window`) instead of going through `useSupported`, so a `BroadcastChannel`
|
|
116
|
+
* is available on the first mount effect;
|
|
117
|
+
* - `initialValue` is the hook's **read-only value source** and takes a plain
|
|
118
|
+
* `T` (upstream: `MaybeRefOrGetter<T>`); it is resolved once at mount, as
|
|
119
|
+
* upstream's `toValue(initialValue)` is — the hook owns writes, so later
|
|
120
|
+
* prop changes are ignored;
|
|
121
|
+
* - a `delete` message from another tab resets `data` to the initial value
|
|
122
|
+
* (upstream parity) but does not re-write the store: incoming syncs never
|
|
123
|
+
* write back, mirroring upstream's paused watcher;
|
|
124
|
+
* - the async read and the channel listener never set state after unmount.
|
|
125
|
+
*
|
|
126
|
+
* @__NO_SIDE_EFFECTS__
|
|
127
|
+
* @example
|
|
128
|
+
* const [data, setData, { isFinished, isSupported }] = useIDBKeyval('my-store', { count: 0 })
|
|
129
|
+
*
|
|
130
|
+
* // explicit write (no deep watcher)
|
|
131
|
+
* setData({ count: 1 })
|
|
132
|
+
*
|
|
133
|
+
* // remove the key
|
|
134
|
+
* await setData(null)
|
|
135
|
+
*
|
|
136
|
+
* @see https://vueuse.org/integrations/useIDBKeyval/
|
|
137
|
+
*/
|
|
138
|
+
export declare function useIDBKeyval<T>(key: IDBValidKey, initialValue: T, options?: UseIDBOptions<T>): UseIDBKeyvalReturn<T>;
|
|
139
|
+
//#endregion
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
(function(exports, idb_keyval, react) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region useIDBKeyval/index.tsx
|
|
4
|
+
function defaultOnError(error) {
|
|
5
|
+
console.error(error);
|
|
6
|
+
}
|
|
7
|
+
function defaultSerializer() {
|
|
8
|
+
return {
|
|
9
|
+
read: (raw) => raw,
|
|
10
|
+
write: (value) => value
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reactive [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
|
15
|
+
* store — React port of VueUse's `useIDBKeyval`.
|
|
16
|
+
*
|
|
17
|
+
* Map from @vueuse/integrations `useIDBKeyval`
|
|
18
|
+
* (`source/vueuse/packages/integrations/useIDBKeyval/`), a reactive wrapper
|
|
19
|
+
* around [`idb-keyval`](https://github.com/jakearchibald/idb-keyval). The value
|
|
20
|
+
* is persisted under `key`, read once on mount and kept in sync across tabs
|
|
21
|
+
* through a `BroadcastChannel`.
|
|
22
|
+
*
|
|
23
|
+
* React divergences:
|
|
24
|
+
* - the upstream object return `{ data, isFinished, isSupported, set }` becomes
|
|
25
|
+
* the state-like tuple `[data, setData, controls]` (§2B), mirroring
|
|
26
|
+
* `useStorage`: `data` is `T | null` (`null` = removed) and `setData(null)`
|
|
27
|
+
* deletes the key through `del`;
|
|
28
|
+
* - **there is no deep watcher.** Upstream writes on *any* mutation of
|
|
29
|
+
* `data.value` (`watchPausable(data, write, { deep: true })`), so
|
|
30
|
+
* `data.value.count++` persists by itself. React state has no deep
|
|
31
|
+
* observation, so writes happen **explicitly through `setData`** — that is
|
|
32
|
+
* the React contract. Mutating an object held in `data` in place does *not*
|
|
33
|
+
* persist; call `setData(next)` with a new value instead. The `deep` /
|
|
34
|
+
* `shallow` / `flush` options are accepted for parity and have no effect;
|
|
35
|
+
* - `isFinished` / `isSupported` live in the third tuple slot as plain
|
|
36
|
+
* booleans (upstream: `ShallowRef` / `ComputedRef`), and `isSupported` is
|
|
37
|
+
* computed synchronously (`typeof window !== 'undefined' && 'BroadcastChannel'
|
|
38
|
+
* in window`) instead of going through `useSupported`, so a `BroadcastChannel`
|
|
39
|
+
* is available on the first mount effect;
|
|
40
|
+
* - `initialValue` is the hook's **read-only value source** and takes a plain
|
|
41
|
+
* `T` (upstream: `MaybeRefOrGetter<T>`); it is resolved once at mount, as
|
|
42
|
+
* upstream's `toValue(initialValue)` is — the hook owns writes, so later
|
|
43
|
+
* prop changes are ignored;
|
|
44
|
+
* - a `delete` message from another tab resets `data` to the initial value
|
|
45
|
+
* (upstream parity) but does not re-write the store: incoming syncs never
|
|
46
|
+
* write back, mirroring upstream's paused watcher;
|
|
47
|
+
* - the async read and the channel listener never set state after unmount.
|
|
48
|
+
*
|
|
49
|
+
* @__NO_SIDE_EFFECTS__
|
|
50
|
+
* @example
|
|
51
|
+
* const [data, setData, { isFinished, isSupported }] = useIDBKeyval('my-store', { count: 0 })
|
|
52
|
+
*
|
|
53
|
+
* // explicit write (no deep watcher)
|
|
54
|
+
* setData({ count: 1 })
|
|
55
|
+
*
|
|
56
|
+
* // remove the key
|
|
57
|
+
* await setData(null)
|
|
58
|
+
*
|
|
59
|
+
* @see https://vueuse.org/integrations/useIDBKeyval/
|
|
60
|
+
*/
|
|
61
|
+
function useIDBKeyval(key, initialValue, options = {}) {
|
|
62
|
+
var _rawInitRef$current, _optionsRef$current$w;
|
|
63
|
+
const optionsRef = (0, react.useRef)(options);
|
|
64
|
+
const rawInitRef = (0, react.useRef)(void 0);
|
|
65
|
+
(_rawInitRef$current = rawInitRef.current) !== null && _rawInitRef$current !== void 0 || (rawInitRef.current = { value: initialValue });
|
|
66
|
+
const [data, setDataState] = (0, react.useState)(() => initialValue !== null && initialValue !== void 0 ? initialValue : null);
|
|
67
|
+
const [isFinished, setIsFinished] = (0, react.useState)(false);
|
|
68
|
+
const dataRef = (0, react.useRef)(data);
|
|
69
|
+
const channelRef = (0, react.useRef)(void 0);
|
|
70
|
+
const mountedRef = (0, react.useRef)(true);
|
|
71
|
+
const targetWindow = (_optionsRef$current$w = optionsRef.current.window) !== null && _optionsRef$current$w !== void 0 ? _optionsRef$current$w : typeof window !== "undefined" ? window : void 0;
|
|
72
|
+
const isSupported = !!targetWindow && "BroadcastChannel" in targetWindow;
|
|
73
|
+
const keyRef = (0, react.useRef)(key);
|
|
74
|
+
keyRef.current = key;
|
|
75
|
+
const getSerializer = (0, react.useCallback)(() => {
|
|
76
|
+
var _optionsRef$current$s;
|
|
77
|
+
return (_optionsRef$current$s = optionsRef.current.serializer) !== null && _optionsRef$current$s !== void 0 ? _optionsRef$current$s : defaultSerializer();
|
|
78
|
+
}, []);
|
|
79
|
+
const read = (0, react.useCallback)(async () => {
|
|
80
|
+
const { onError = defaultOnError, writeDefaults = true } = optionsRef.current;
|
|
81
|
+
const rawInit = rawInitRef.current.value;
|
|
82
|
+
const currentKey = key;
|
|
83
|
+
try {
|
|
84
|
+
const rawValue = await (0, idb_keyval.get)(currentKey);
|
|
85
|
+
if (!mountedRef.current || keyRef.current !== currentKey) return;
|
|
86
|
+
if (rawValue === void 0) {
|
|
87
|
+
if (rawInit !== void 0 && rawInit !== null && writeDefaults) await (0, idb_keyval.set)(currentKey, getSerializer().write(rawInit));
|
|
88
|
+
} else {
|
|
89
|
+
const value = getSerializer().read(rawValue);
|
|
90
|
+
dataRef.current = value;
|
|
91
|
+
setDataState(value);
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (mountedRef.current) onError(error);
|
|
95
|
+
}
|
|
96
|
+
if (mountedRef.current) setIsFinished(true);
|
|
97
|
+
}, [key, getSerializer]);
|
|
98
|
+
const write = (0, react.useCallback)(async (value) => {
|
|
99
|
+
const { onError = defaultOnError } = optionsRef.current;
|
|
100
|
+
try {
|
|
101
|
+
if (value == null) {
|
|
102
|
+
var _channelRef$current;
|
|
103
|
+
await (0, idb_keyval.del)(key);
|
|
104
|
+
(_channelRef$current = channelRef.current) === null || _channelRef$current === void 0 || _channelRef$current.postMessage({ type: "delete" });
|
|
105
|
+
} else {
|
|
106
|
+
var _channelRef$current2;
|
|
107
|
+
const serializedValue = getSerializer().write(value);
|
|
108
|
+
await (0, idb_keyval.update)(key, () => serializedValue);
|
|
109
|
+
(_channelRef$current2 = channelRef.current) === null || _channelRef$current2 === void 0 || _channelRef$current2.postMessage({
|
|
110
|
+
type: "set",
|
|
111
|
+
value: serializedValue
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
onError(error);
|
|
116
|
+
}
|
|
117
|
+
}, [key, getSerializer]);
|
|
118
|
+
const setData = (0, react.useCallback)(async (value) => {
|
|
119
|
+
dataRef.current = value;
|
|
120
|
+
if (mountedRef.current) setDataState(value);
|
|
121
|
+
await write(value);
|
|
122
|
+
}, [write]);
|
|
123
|
+
(0, react.useEffect)(() => {
|
|
124
|
+
mountedRef.current = true;
|
|
125
|
+
read();
|
|
126
|
+
return () => {
|
|
127
|
+
mountedRef.current = false;
|
|
128
|
+
};
|
|
129
|
+
}, [read]);
|
|
130
|
+
(0, react.useEffect)(() => {
|
|
131
|
+
const { onError = defaultOnError, listenToStorageChanges = true } = optionsRef.current;
|
|
132
|
+
if (!listenToStorageChanges || !isSupported) return;
|
|
133
|
+
const channel = new BroadcastChannel(`vueuse-idb-${JSON.stringify(key)}`);
|
|
134
|
+
channelRef.current = channel;
|
|
135
|
+
const onMessageEvent = (event) => {
|
|
136
|
+
var _event$data;
|
|
137
|
+
const { type, value } = (_event$data = event.data) !== null && _event$data !== void 0 ? _event$data : {};
|
|
138
|
+
try {
|
|
139
|
+
if (type === "delete") {
|
|
140
|
+
var _value;
|
|
141
|
+
const rawInit = (_value = rawInitRef.current.value) !== null && _value !== void 0 ? _value : null;
|
|
142
|
+
dataRef.current = rawInit;
|
|
143
|
+
setDataState(rawInit);
|
|
144
|
+
} else if (type === "set") {
|
|
145
|
+
const next = getSerializer().read(value);
|
|
146
|
+
dataRef.current = next;
|
|
147
|
+
setDataState(next);
|
|
148
|
+
}
|
|
149
|
+
} catch (error) {
|
|
150
|
+
onError(error);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
channel.addEventListener("message", onMessageEvent, { passive: true });
|
|
154
|
+
return () => {
|
|
155
|
+
channel.removeEventListener("message", onMessageEvent);
|
|
156
|
+
if (channelRef.current === channel) channelRef.current = void 0;
|
|
157
|
+
channel.close();
|
|
158
|
+
};
|
|
159
|
+
}, [
|
|
160
|
+
key,
|
|
161
|
+
isSupported,
|
|
162
|
+
getSerializer
|
|
163
|
+
]);
|
|
164
|
+
return [
|
|
165
|
+
data,
|
|
166
|
+
setData,
|
|
167
|
+
{
|
|
168
|
+
isFinished,
|
|
169
|
+
isSupported
|
|
170
|
+
}
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
exports.useIDBKeyval = useIDBKeyval;
|
|
175
|
+
})(this.reause = this.reause || {}, idbKeyval, React);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e){console.error(e)}function i(){return{read:e=>e,write:e=>e}}function a(e,a,o={}){var s;let c=(0,n.useRef)(o),l=(0,n.useRef)(void 0);l.current!=null||(l.current={value:a});let[u,d]=(0,n.useState)(()=>a==null?null:a),[f,p]=(0,n.useState)(!1),m=(0,n.useRef)(u),h=(0,n.useRef)(void 0),g=(0,n.useRef)(!0),_=(s=c.current.window)==null?typeof window<`u`?window:void 0:s,v=!!_&&`BroadcastChannel`in _,y=(0,n.useRef)(e);y.current=e;let b=(0,n.useCallback)(()=>{var e;return(e=c.current.serializer)==null?i():e},[]),x=(0,n.useCallback)(async()=>{let{onError:n=r,writeDefaults:i=!0}=c.current,a=l.current.value,o=e;try{let e=await(0,t.get)(o);if(!g.current||y.current!==o)return;if(e===void 0)a!=null&&i&&await(0,t.set)(o,b().write(a));else{let t=b().read(e);m.current=t,d(t)}}catch(e){g.current&&n(e)}g.current&&p(!0)},[e,b]),S=(0,n.useCallback)(async n=>{let{onError:i=r}=c.current;try{if(n==null){var a;await(0,t.del)(e),(a=h.current)==null||a.postMessage({type:`delete`})}else{var o;let r=b().write(n);await(0,t.update)(e,()=>r),(o=h.current)==null||o.postMessage({type:`set`,value:r})}}catch(e){i(e)}},[e,b]),C=(0,n.useCallback)(async e=>{m.current=e,g.current&&d(e),await S(e)},[S]);return(0,n.useEffect)(()=>(g.current=!0,x(),()=>{g.current=!1}),[x]),(0,n.useEffect)(()=>{let{onError:t=r,listenToStorageChanges:n=!0}=c.current;if(!n||!v)return;let i=new BroadcastChannel(`vueuse-idb-${JSON.stringify(e)}`);h.current=i;let a=e=>{var n;let{type:r,value:i}=(n=e.data)==null?{}:n;try{if(r===`delete`){var a;let e=(a=l.current.value)==null?null:a;m.current=e,d(e)}else if(r===`set`){let e=b().read(i);m.current=e,d(e)}}catch(e){t(e)}};return i.addEventListener(`message`,a,{passive:!0}),()=>{i.removeEventListener(`message`,a),h.current===i&&(h.current=void 0),i.close()}},[e,v,b]),[u,C,{isFinished:f,isSupported:v}]}e.useIDBKeyval=a})(this.reause=this.reause||{},idbKeyval,React);
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { del, get, set, update } from "idb-keyval";
|
|
3
|
+
//#region useIDBKeyval/index.tsx
|
|
4
|
+
function defaultOnError(error) {
|
|
5
|
+
console.error(error);
|
|
6
|
+
}
|
|
7
|
+
function defaultSerializer() {
|
|
8
|
+
return {
|
|
9
|
+
read: (raw) => raw,
|
|
10
|
+
write: (value) => value
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reactive [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
|
15
|
+
* store — React port of VueUse's `useIDBKeyval`.
|
|
16
|
+
*
|
|
17
|
+
* Map from @vueuse/integrations `useIDBKeyval`
|
|
18
|
+
* (`source/vueuse/packages/integrations/useIDBKeyval/`), a reactive wrapper
|
|
19
|
+
* around [`idb-keyval`](https://github.com/jakearchibald/idb-keyval). The value
|
|
20
|
+
* is persisted under `key`, read once on mount and kept in sync across tabs
|
|
21
|
+
* through a `BroadcastChannel`.
|
|
22
|
+
*
|
|
23
|
+
* React divergences:
|
|
24
|
+
* - the upstream object return `{ data, isFinished, isSupported, set }` becomes
|
|
25
|
+
* the state-like tuple `[data, setData, controls]` (§2B), mirroring
|
|
26
|
+
* `useStorage`: `data` is `T | null` (`null` = removed) and `setData(null)`
|
|
27
|
+
* deletes the key through `del`;
|
|
28
|
+
* - **there is no deep watcher.** Upstream writes on *any* mutation of
|
|
29
|
+
* `data.value` (`watchPausable(data, write, { deep: true })`), so
|
|
30
|
+
* `data.value.count++` persists by itself. React state has no deep
|
|
31
|
+
* observation, so writes happen **explicitly through `setData`** — that is
|
|
32
|
+
* the React contract. Mutating an object held in `data` in place does *not*
|
|
33
|
+
* persist; call `setData(next)` with a new value instead. The `deep` /
|
|
34
|
+
* `shallow` / `flush` options are accepted for parity and have no effect;
|
|
35
|
+
* - `isFinished` / `isSupported` live in the third tuple slot as plain
|
|
36
|
+
* booleans (upstream: `ShallowRef` / `ComputedRef`), and `isSupported` is
|
|
37
|
+
* computed synchronously (`typeof window !== 'undefined' && 'BroadcastChannel'
|
|
38
|
+
* in window`) instead of going through `useSupported`, so a `BroadcastChannel`
|
|
39
|
+
* is available on the first mount effect;
|
|
40
|
+
* - `initialValue` is the hook's **read-only value source** and takes a plain
|
|
41
|
+
* `T` (upstream: `MaybeRefOrGetter<T>`); it is resolved once at mount, as
|
|
42
|
+
* upstream's `toValue(initialValue)` is — the hook owns writes, so later
|
|
43
|
+
* prop changes are ignored;
|
|
44
|
+
* - a `delete` message from another tab resets `data` to the initial value
|
|
45
|
+
* (upstream parity) but does not re-write the store: incoming syncs never
|
|
46
|
+
* write back, mirroring upstream's paused watcher;
|
|
47
|
+
* - the async read and the channel listener never set state after unmount.
|
|
48
|
+
*
|
|
49
|
+
* @__NO_SIDE_EFFECTS__
|
|
50
|
+
* @example
|
|
51
|
+
* const [data, setData, { isFinished, isSupported }] = useIDBKeyval('my-store', { count: 0 })
|
|
52
|
+
*
|
|
53
|
+
* // explicit write (no deep watcher)
|
|
54
|
+
* setData({ count: 1 })
|
|
55
|
+
*
|
|
56
|
+
* // remove the key
|
|
57
|
+
* await setData(null)
|
|
58
|
+
*
|
|
59
|
+
* @see https://vueuse.org/integrations/useIDBKeyval/
|
|
60
|
+
*/
|
|
61
|
+
function useIDBKeyval(key, initialValue, options = {}) {
|
|
62
|
+
var _rawInitRef$current, _optionsRef$current$w;
|
|
63
|
+
const optionsRef = useRef(options);
|
|
64
|
+
const rawInitRef = useRef(void 0);
|
|
65
|
+
(_rawInitRef$current = rawInitRef.current) !== null && _rawInitRef$current !== void 0 || (rawInitRef.current = { value: initialValue });
|
|
66
|
+
const [data, setDataState] = useState(() => initialValue !== null && initialValue !== void 0 ? initialValue : null);
|
|
67
|
+
const [isFinished, setIsFinished] = useState(false);
|
|
68
|
+
const dataRef = useRef(data);
|
|
69
|
+
const channelRef = useRef(void 0);
|
|
70
|
+
const mountedRef = useRef(true);
|
|
71
|
+
const targetWindow = (_optionsRef$current$w = optionsRef.current.window) !== null && _optionsRef$current$w !== void 0 ? _optionsRef$current$w : typeof window !== "undefined" ? window : void 0;
|
|
72
|
+
const isSupported = !!targetWindow && "BroadcastChannel" in targetWindow;
|
|
73
|
+
const keyRef = useRef(key);
|
|
74
|
+
keyRef.current = key;
|
|
75
|
+
const getSerializer = useCallback(() => {
|
|
76
|
+
var _optionsRef$current$s;
|
|
77
|
+
return (_optionsRef$current$s = optionsRef.current.serializer) !== null && _optionsRef$current$s !== void 0 ? _optionsRef$current$s : defaultSerializer();
|
|
78
|
+
}, []);
|
|
79
|
+
const read = useCallback(async () => {
|
|
80
|
+
const { onError = defaultOnError, writeDefaults = true } = optionsRef.current;
|
|
81
|
+
const rawInit = rawInitRef.current.value;
|
|
82
|
+
const currentKey = key;
|
|
83
|
+
try {
|
|
84
|
+
const rawValue = await get(currentKey);
|
|
85
|
+
if (!mountedRef.current || keyRef.current !== currentKey) return;
|
|
86
|
+
if (rawValue === void 0) {
|
|
87
|
+
if (rawInit !== void 0 && rawInit !== null && writeDefaults) await set(currentKey, getSerializer().write(rawInit));
|
|
88
|
+
} else {
|
|
89
|
+
const value = getSerializer().read(rawValue);
|
|
90
|
+
dataRef.current = value;
|
|
91
|
+
setDataState(value);
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (mountedRef.current) onError(error);
|
|
95
|
+
}
|
|
96
|
+
if (mountedRef.current) setIsFinished(true);
|
|
97
|
+
}, [key, getSerializer]);
|
|
98
|
+
const write = useCallback(async (value) => {
|
|
99
|
+
const { onError = defaultOnError } = optionsRef.current;
|
|
100
|
+
try {
|
|
101
|
+
if (value == null) {
|
|
102
|
+
var _channelRef$current;
|
|
103
|
+
await del(key);
|
|
104
|
+
(_channelRef$current = channelRef.current) === null || _channelRef$current === void 0 || _channelRef$current.postMessage({ type: "delete" });
|
|
105
|
+
} else {
|
|
106
|
+
var _channelRef$current2;
|
|
107
|
+
const serializedValue = getSerializer().write(value);
|
|
108
|
+
await update(key, () => serializedValue);
|
|
109
|
+
(_channelRef$current2 = channelRef.current) === null || _channelRef$current2 === void 0 || _channelRef$current2.postMessage({
|
|
110
|
+
type: "set",
|
|
111
|
+
value: serializedValue
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
onError(error);
|
|
116
|
+
}
|
|
117
|
+
}, [key, getSerializer]);
|
|
118
|
+
const setData = useCallback(async (value) => {
|
|
119
|
+
dataRef.current = value;
|
|
120
|
+
if (mountedRef.current) setDataState(value);
|
|
121
|
+
await write(value);
|
|
122
|
+
}, [write]);
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
mountedRef.current = true;
|
|
125
|
+
read();
|
|
126
|
+
return () => {
|
|
127
|
+
mountedRef.current = false;
|
|
128
|
+
};
|
|
129
|
+
}, [read]);
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
const { onError = defaultOnError, listenToStorageChanges = true } = optionsRef.current;
|
|
132
|
+
if (!listenToStorageChanges || !isSupported) return;
|
|
133
|
+
const channel = new BroadcastChannel(`vueuse-idb-${JSON.stringify(key)}`);
|
|
134
|
+
channelRef.current = channel;
|
|
135
|
+
const onMessageEvent = (event) => {
|
|
136
|
+
var _event$data;
|
|
137
|
+
const { type, value } = (_event$data = event.data) !== null && _event$data !== void 0 ? _event$data : {};
|
|
138
|
+
try {
|
|
139
|
+
if (type === "delete") {
|
|
140
|
+
var _value;
|
|
141
|
+
const rawInit = (_value = rawInitRef.current.value) !== null && _value !== void 0 ? _value : null;
|
|
142
|
+
dataRef.current = rawInit;
|
|
143
|
+
setDataState(rawInit);
|
|
144
|
+
} else if (type === "set") {
|
|
145
|
+
const next = getSerializer().read(value);
|
|
146
|
+
dataRef.current = next;
|
|
147
|
+
setDataState(next);
|
|
148
|
+
}
|
|
149
|
+
} catch (error) {
|
|
150
|
+
onError(error);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
channel.addEventListener("message", onMessageEvent, { passive: true });
|
|
154
|
+
return () => {
|
|
155
|
+
channel.removeEventListener("message", onMessageEvent);
|
|
156
|
+
if (channelRef.current === channel) channelRef.current = void 0;
|
|
157
|
+
channel.close();
|
|
158
|
+
};
|
|
159
|
+
}, [
|
|
160
|
+
key,
|
|
161
|
+
isSupported,
|
|
162
|
+
getSerializer
|
|
163
|
+
]);
|
|
164
|
+
return [
|
|
165
|
+
data,
|
|
166
|
+
setData,
|
|
167
|
+
{
|
|
168
|
+
isFinished,
|
|
169
|
+
isSupported
|
|
170
|
+
}
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
export { useIDBKeyval };
|
package/dist/useJwt.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { JwtHeader, JwtPayload } from "jwt-decode";
|
|
2
|
+
//#region useJwt/index.d.ts
|
|
3
|
+
export interface UseJwtOptions<Fallback> {
|
|
4
|
+
/**
|
|
5
|
+
* Value returned when encounter error on decoding
|
|
6
|
+
*
|
|
7
|
+
* @default null
|
|
8
|
+
*/
|
|
9
|
+
fallbackValue?: Fallback;
|
|
10
|
+
/**
|
|
11
|
+
* Error callback for decoding
|
|
12
|
+
*/
|
|
13
|
+
onError?: (error: unknown) => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* React return type: a plain object with two distinct-typed named fields —
|
|
17
|
+
* the value analog of the upstream `ComputedRef`s. `header` and `payload` are
|
|
18
|
+
* decoded values (or `Fallback`), NOT refs.
|
|
19
|
+
*/
|
|
20
|
+
export interface UseJwtReturn<Payload, Header, Fallback> {
|
|
21
|
+
header: Header | Fallback;
|
|
22
|
+
payload: Payload | Fallback;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* React port of VueUse's `useJwt`.
|
|
26
|
+
*
|
|
27
|
+
* Map from @vueuse/integrations `useJwt`
|
|
28
|
+
* (`source/vueuse/packages/integrations/useJwt/`), a wrapper for
|
|
29
|
+
* [`jwt-decode`](https://github.com/auth0/jwt-decode). `encodedJwt` is the
|
|
30
|
+
* hook's **read-only value source** and takes a plain string (upstream:
|
|
31
|
+
* `MaybeRefOrGetter<string>`); the decode is memoized on the token, so a
|
|
32
|
+
* stable token keeps `header`/`payload` referentially stable across renders.
|
|
33
|
+
*
|
|
34
|
+
* Adjustment for React:
|
|
35
|
+
* - upstream returns `{ header, payload }` as `ComputedRef`s; here the two
|
|
36
|
+
* fields are plain decoded values (the issue's own `const { header, payload }
|
|
37
|
+
* = useJwt(encodedJwt)` shape), so read them directly instead of `.value`;
|
|
38
|
+
* - `fallbackValue` defaults to `null`, and `onError` is kept in a ref so an
|
|
39
|
+
* inline arrow callback does not churn the memo dependencies;
|
|
40
|
+
* - decoding happens during render, so under React StrictMode's dev
|
|
41
|
+
* double-render a bad token may invoke `onError` twice. This is unavoidable
|
|
42
|
+
* while decoding during render; guard the callback if it must fire once.
|
|
43
|
+
*
|
|
44
|
+
* @__NO_SIDE_EFFECTS__
|
|
45
|
+
* @see https://vueuse.org/useJwt
|
|
46
|
+
* @example
|
|
47
|
+
* const { header, payload } = useJwt(encodedJwt)
|
|
48
|
+
* header.alg // 'HS256'
|
|
49
|
+
* payload.sub // '1234567890'
|
|
50
|
+
*/
|
|
51
|
+
export declare function useJwt<Payload extends object = JwtPayload, Header extends object = JwtHeader, Fallback = null>(encodedJwt: string, options?: UseJwtOptions<Fallback>): UseJwtReturn<Payload, Header, Fallback>;
|
|
52
|
+
//#endregion
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
(function(exports, jwt_decode, react) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region useJwt/index.tsx
|
|
4
|
+
/**
|
|
5
|
+
* React port of VueUse's `useJwt`.
|
|
6
|
+
*
|
|
7
|
+
* Map from @vueuse/integrations `useJwt`
|
|
8
|
+
* (`source/vueuse/packages/integrations/useJwt/`), a wrapper for
|
|
9
|
+
* [`jwt-decode`](https://github.com/auth0/jwt-decode). `encodedJwt` is the
|
|
10
|
+
* hook's **read-only value source** and takes a plain string (upstream:
|
|
11
|
+
* `MaybeRefOrGetter<string>`); the decode is memoized on the token, so a
|
|
12
|
+
* stable token keeps `header`/`payload` referentially stable across renders.
|
|
13
|
+
*
|
|
14
|
+
* Adjustment for React:
|
|
15
|
+
* - upstream returns `{ header, payload }` as `ComputedRef`s; here the two
|
|
16
|
+
* fields are plain decoded values (the issue's own `const { header, payload }
|
|
17
|
+
* = useJwt(encodedJwt)` shape), so read them directly instead of `.value`;
|
|
18
|
+
* - `fallbackValue` defaults to `null`, and `onError` is kept in a ref so an
|
|
19
|
+
* inline arrow callback does not churn the memo dependencies;
|
|
20
|
+
* - decoding happens during render, so under React StrictMode's dev
|
|
21
|
+
* double-render a bad token may invoke `onError` twice. This is unavoidable
|
|
22
|
+
* while decoding during render; guard the callback if it must fire once.
|
|
23
|
+
*
|
|
24
|
+
* @__NO_SIDE_EFFECTS__
|
|
25
|
+
* @see https://vueuse.org/useJwt
|
|
26
|
+
* @example
|
|
27
|
+
* const { header, payload } = useJwt(encodedJwt)
|
|
28
|
+
* header.alg // 'HS256'
|
|
29
|
+
* payload.sub // '1234567890'
|
|
30
|
+
*/
|
|
31
|
+
function useJwt(encodedJwt, options = {}) {
|
|
32
|
+
const { onError, fallbackValue = null } = options;
|
|
33
|
+
const onErrorRef = (0, react.useRef)(onError);
|
|
34
|
+
onErrorRef.current = onError;
|
|
35
|
+
const fallbackValueRef = (0, react.useRef)(fallbackValue);
|
|
36
|
+
fallbackValueRef.current = fallbackValue;
|
|
37
|
+
const token = encodedJwt;
|
|
38
|
+
const decodeWithFallback = (value, jwtOptions) => {
|
|
39
|
+
try {
|
|
40
|
+
return (0, jwt_decode.jwtDecode)(value, jwtOptions);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
var _onErrorRef$current;
|
|
43
|
+
(_onErrorRef$current = onErrorRef.current) === null || _onErrorRef$current === void 0 || _onErrorRef$current.call(onErrorRef, err);
|
|
44
|
+
return fallbackValueRef.current;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
header: (0, react.useMemo)(() => decodeWithFallback(token, { header: true }), [token]),
|
|
49
|
+
payload: (0, react.useMemo)(() => decodeWithFallback(token), [token])
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
exports.useJwt = useJwt;
|
|
54
|
+
})(this.reause = this.reause || {}, jwt_decode, React);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e,r={}){let{onError:i,fallbackValue:a=null}=r,o=(0,n.useRef)(i);o.current=i;let s=(0,n.useRef)(a);s.current=a;let c=e,l=(e,n)=>{try{return(0,t.jwtDecode)(e,n)}catch(e){var r;return(r=o.current)==null||r.call(o,e),s.current}};return{header:(0,n.useMemo)(()=>l(c,{header:!0}),[c]),payload:(0,n.useMemo)(()=>l(c),[c])}}e.useJwt=r})(this.reause=this.reause||{},jwt_decode,React);
|
package/dist/useJwt.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
import { jwtDecode } from "jwt-decode";
|
|
3
|
+
//#region useJwt/index.tsx
|
|
4
|
+
/**
|
|
5
|
+
* React port of VueUse's `useJwt`.
|
|
6
|
+
*
|
|
7
|
+
* Map from @vueuse/integrations `useJwt`
|
|
8
|
+
* (`source/vueuse/packages/integrations/useJwt/`), a wrapper for
|
|
9
|
+
* [`jwt-decode`](https://github.com/auth0/jwt-decode). `encodedJwt` is the
|
|
10
|
+
* hook's **read-only value source** and takes a plain string (upstream:
|
|
11
|
+
* `MaybeRefOrGetter<string>`); the decode is memoized on the token, so a
|
|
12
|
+
* stable token keeps `header`/`payload` referentially stable across renders.
|
|
13
|
+
*
|
|
14
|
+
* Adjustment for React:
|
|
15
|
+
* - upstream returns `{ header, payload }` as `ComputedRef`s; here the two
|
|
16
|
+
* fields are plain decoded values (the issue's own `const { header, payload }
|
|
17
|
+
* = useJwt(encodedJwt)` shape), so read them directly instead of `.value`;
|
|
18
|
+
* - `fallbackValue` defaults to `null`, and `onError` is kept in a ref so an
|
|
19
|
+
* inline arrow callback does not churn the memo dependencies;
|
|
20
|
+
* - decoding happens during render, so under React StrictMode's dev
|
|
21
|
+
* double-render a bad token may invoke `onError` twice. This is unavoidable
|
|
22
|
+
* while decoding during render; guard the callback if it must fire once.
|
|
23
|
+
*
|
|
24
|
+
* @__NO_SIDE_EFFECTS__
|
|
25
|
+
* @see https://vueuse.org/useJwt
|
|
26
|
+
* @example
|
|
27
|
+
* const { header, payload } = useJwt(encodedJwt)
|
|
28
|
+
* header.alg // 'HS256'
|
|
29
|
+
* payload.sub // '1234567890'
|
|
30
|
+
*/
|
|
31
|
+
function useJwt(encodedJwt, options = {}) {
|
|
32
|
+
const { onError, fallbackValue = null } = options;
|
|
33
|
+
const onErrorRef = useRef(onError);
|
|
34
|
+
onErrorRef.current = onError;
|
|
35
|
+
const fallbackValueRef = useRef(fallbackValue);
|
|
36
|
+
fallbackValueRef.current = fallbackValue;
|
|
37
|
+
const token = encodedJwt;
|
|
38
|
+
const decodeWithFallback = (value, jwtOptions) => {
|
|
39
|
+
try {
|
|
40
|
+
return jwtDecode(value, jwtOptions);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
var _onErrorRef$current;
|
|
43
|
+
(_onErrorRef$current = onErrorRef.current) === null || _onErrorRef$current === void 0 || _onErrorRef$current.call(onErrorRef, err);
|
|
44
|
+
return fallbackValueRef.current;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
header: useMemo(() => decodeWithFallback(token, { header: true }), [token]),
|
|
49
|
+
payload: useMemo(() => decodeWithFallback(token), [token])
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { useJwt };
|