@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,288 @@
|
|
|
1
|
+
(function(exports, _reause_shared, axios, react) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
//#endregion
|
|
25
|
+
axios = __toESM(axios, 1);
|
|
26
|
+
//#region useAxios/index.tsx
|
|
27
|
+
/**
|
|
28
|
+
* React port of VueUse's `useAxios` — wrapper for
|
|
29
|
+
* [`axios`](https://github.com/axios/axios).
|
|
30
|
+
*
|
|
31
|
+
* Map from @vueuse/integrations `useAxios`
|
|
32
|
+
* (`source/vueuse/packages/integrations/useAxios/`). All 12 upstream overloads
|
|
33
|
+
* are kept verbatim (`(url, config?, options?)`, `(url, instance?, options?)`,
|
|
34
|
+
* `(url, config, instance, options?)`, `(config)`, `(instance)`,
|
|
35
|
+
* `(config, instance)`); the url-ful forms expose `execute(url?, config?)`,
|
|
36
|
+
* the url-less forms require `execute(url, config?)`. The returned object
|
|
37
|
+
* mirrors the upstream members — `response`, `data`, `isFinished`,
|
|
38
|
+
* `isLoading`, `isAborted`/`isCanceled`, `error`, `abort`/`cancel`, `execute` —
|
|
39
|
+
* but the members are plain values backed by live getters instead of refs
|
|
40
|
+
* (no `.value`), and the object is thenable (`await useAxios(url)` resolves
|
|
41
|
+
* with the shell once the request settled, rejecting when it failed).
|
|
42
|
+
*
|
|
43
|
+
* Adjustment for React:
|
|
44
|
+
* - the returned object is built with `Object.create` over a getter base whose
|
|
45
|
+
* members read a synchronously-updated live mirror of the state, so a shell
|
|
46
|
+
* captured earlier (e.g. the value `await` resolved with) still exposes
|
|
47
|
+
* fresh values — the same shape `useAsyncState` uses. The base itself is
|
|
48
|
+
* deliberately NOT thenable, so resolving it can never re-adopt `then`;
|
|
49
|
+
* - `execute` returns the shared thenable shell (upstream `return promise`):
|
|
50
|
+
* `await execute()` resolves with the shell once the request finished,
|
|
51
|
+
* rejecting with the request error on failure, and a bare unawaited
|
|
52
|
+
* `execute()` never settles eagerly — so no unhandled rejection can
|
|
53
|
+
* escape. `immediate` requests fire from a mount effect (upstream fires
|
|
54
|
+
* during setup), and the shell is never left without a rejection handler
|
|
55
|
+
* there;
|
|
56
|
+
* - a pending request is aborted on unmount (React-safety deviation: upstream
|
|
57
|
+
* only relies on the `isAborted` guard), so a late response can never
|
|
58
|
+
* populate `data`/`response` after the component is gone;
|
|
59
|
+
* - `shallow` is accepted for API parity but has no effect: React state is
|
|
60
|
+
* never deep-wrapped, so there is no `shallowRef`/`ref` distinction here.
|
|
61
|
+
*
|
|
62
|
+
* @__NO_SIDE_EFFECTS__
|
|
63
|
+
* @example
|
|
64
|
+
* const { data, isLoading, isFinished, execute, abort } = useAxios('/api/posts')
|
|
65
|
+
* await execute('/api/posts/2')
|
|
66
|
+
*
|
|
67
|
+
* @see https://vueuse.org/integrations/useAxios
|
|
68
|
+
*/
|
|
69
|
+
function useAxios(...args) {
|
|
70
|
+
const url = typeof args[0] === "string" ? args[0] : void 0;
|
|
71
|
+
const argsPlaceholder = typeof url === "string" ? 1 : 0;
|
|
72
|
+
const defaultOptions = {
|
|
73
|
+
immediate: !!argsPlaceholder,
|
|
74
|
+
shallow: true,
|
|
75
|
+
abortPrevious: true
|
|
76
|
+
};
|
|
77
|
+
let defaultConfig = {};
|
|
78
|
+
let instance = axios.default;
|
|
79
|
+
let options = defaultOptions;
|
|
80
|
+
const isAxiosInstance = (val) => !!(val === null || val === void 0 ? void 0 : val.request);
|
|
81
|
+
if (args.length > 0 + argsPlaceholder) {
|
|
82
|
+
/**
|
|
83
|
+
* Unable to use `instanceof` here because of (https://github.com/axios/axios/issues/737)
|
|
84
|
+
* so instead we are checking if there is a `request` on the object to see if it is an
|
|
85
|
+
* axios instance
|
|
86
|
+
*/
|
|
87
|
+
if (isAxiosInstance(args[0 + argsPlaceholder])) instance = args[0 + argsPlaceholder];
|
|
88
|
+
else defaultConfig = args[0 + argsPlaceholder];
|
|
89
|
+
}
|
|
90
|
+
if (args.length > 1 + argsPlaceholder) {
|
|
91
|
+
if (isAxiosInstance(args[1 + argsPlaceholder])) instance = args[1 + argsPlaceholder];
|
|
92
|
+
}
|
|
93
|
+
if (args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]) || args.length === 3 + argsPlaceholder) options = args[args.length - 1] || defaultOptions;
|
|
94
|
+
const { onSuccess = _reause_shared.noop, onError = _reause_shared.noop, immediate, resetOnExecute = false } = options;
|
|
95
|
+
const initialData = options.initialData;
|
|
96
|
+
const urlRef = (0, react.useRef)(url);
|
|
97
|
+
urlRef.current = url;
|
|
98
|
+
const configRef = (0, react.useRef)(defaultConfig);
|
|
99
|
+
configRef.current = defaultConfig;
|
|
100
|
+
const instanceRef = (0, react.useRef)(instance);
|
|
101
|
+
instanceRef.current = instance;
|
|
102
|
+
const optionsRef = (0, react.useRef)(options);
|
|
103
|
+
optionsRef.current = options;
|
|
104
|
+
const immediateRef = (0, react.useRef)(immediate);
|
|
105
|
+
immediateRef.current = immediate;
|
|
106
|
+
const resetOnExecuteRef = (0, react.useRef)(resetOnExecute);
|
|
107
|
+
resetOnExecuteRef.current = resetOnExecute;
|
|
108
|
+
const initialDataRef = (0, react.useRef)(initialData);
|
|
109
|
+
initialDataRef.current = initialData;
|
|
110
|
+
const onSuccessRef = (0, react.useRef)(onSuccess);
|
|
111
|
+
onSuccessRef.current = onSuccess;
|
|
112
|
+
const onErrorRef = (0, react.useRef)(onError);
|
|
113
|
+
onErrorRef.current = onError;
|
|
114
|
+
const onFinishRef = (0, react.useRef)(options.onFinish);
|
|
115
|
+
onFinishRef.current = options.onFinish;
|
|
116
|
+
const [response, setResponse] = (0, react.useState)(void 0);
|
|
117
|
+
const [data, setData] = (0, react.useState)(initialData);
|
|
118
|
+
const [isFinished, setIsFinished] = (0, react.useState)(false);
|
|
119
|
+
const [isLoading, setIsLoading] = (0, react.useState)(false);
|
|
120
|
+
const [isAborted, setIsAborted] = (0, react.useState)(false);
|
|
121
|
+
const [error, setError] = (0, react.useState)(void 0);
|
|
122
|
+
const liveRef = (0, react.useRef)({
|
|
123
|
+
response,
|
|
124
|
+
data,
|
|
125
|
+
isFinished,
|
|
126
|
+
isLoading,
|
|
127
|
+
isAborted,
|
|
128
|
+
error
|
|
129
|
+
});
|
|
130
|
+
const abortControllerRef = (0, react.useRef)(null);
|
|
131
|
+
if (abortControllerRef.current === null) abortControllerRef.current = new AbortController();
|
|
132
|
+
const executeCounterRef = (0, react.useRef)(0);
|
|
133
|
+
const waitersRef = (0, react.useRef)([]);
|
|
134
|
+
const baseRef = (0, react.useRef)(null);
|
|
135
|
+
const shellRef = (0, react.useRef)(null);
|
|
136
|
+
const abort = (0, react.useCallback)((message) => {
|
|
137
|
+
var _abortControllerRef$c;
|
|
138
|
+
if (liveRef.current.isFinished || !liveRef.current.isLoading) return;
|
|
139
|
+
(_abortControllerRef$c = abortControllerRef.current) === null || _abortControllerRef$c === void 0 || _abortControllerRef$c.abort(message);
|
|
140
|
+
abortControllerRef.current = new AbortController();
|
|
141
|
+
liveRef.current.isAborted = true;
|
|
142
|
+
liveRef.current.isLoading = false;
|
|
143
|
+
liveRef.current.isFinished = false;
|
|
144
|
+
setIsAborted(true);
|
|
145
|
+
setIsLoading(false);
|
|
146
|
+
setIsFinished(false);
|
|
147
|
+
}, []);
|
|
148
|
+
const loading = (0, react.useCallback)((value) => {
|
|
149
|
+
liveRef.current.isLoading = value;
|
|
150
|
+
liveRef.current.isFinished = !value;
|
|
151
|
+
setIsLoading(value);
|
|
152
|
+
setIsFinished(!value);
|
|
153
|
+
}, []);
|
|
154
|
+
/**
|
|
155
|
+
* Reset data to initialData
|
|
156
|
+
*/
|
|
157
|
+
const resetData = (0, react.useCallback)(() => {
|
|
158
|
+
if (!resetOnExecuteRef.current) return;
|
|
159
|
+
liveRef.current.data = initialDataRef.current;
|
|
160
|
+
setData(initialDataRef.current);
|
|
161
|
+
}, []);
|
|
162
|
+
const drainWaiters = (0, react.useCallback)(() => {
|
|
163
|
+
const waiters = waitersRef.current;
|
|
164
|
+
if (waiters.length === 0) return;
|
|
165
|
+
waitersRef.current = [];
|
|
166
|
+
const currentError = liveRef.current.error;
|
|
167
|
+
const result = baseRef.current;
|
|
168
|
+
waiters.forEach(({ resolve, reject }) => {
|
|
169
|
+
if (currentError) reject(currentError);
|
|
170
|
+
else resolve(result);
|
|
171
|
+
});
|
|
172
|
+
}, []);
|
|
173
|
+
const execute = (0, react.useCallback)((executeUrl = urlRef.current, config = {}) => {
|
|
174
|
+
var _urlRef$current, _abortControllerRef$c2;
|
|
175
|
+
liveRef.current.error = void 0;
|
|
176
|
+
setError(void 0);
|
|
177
|
+
const _url = typeof executeUrl === "string" ? executeUrl : (_urlRef$current = urlRef.current) !== null && _urlRef$current !== void 0 ? _urlRef$current : config.url;
|
|
178
|
+
if (_url === void 0) {
|
|
179
|
+
const invalidUrlError = new axios.AxiosError(axios.AxiosError.ERR_INVALID_URL);
|
|
180
|
+
liveRef.current.error = invalidUrlError;
|
|
181
|
+
liveRef.current.isFinished = true;
|
|
182
|
+
setError(invalidUrlError);
|
|
183
|
+
setIsFinished(true);
|
|
184
|
+
return shellRef.current;
|
|
185
|
+
}
|
|
186
|
+
resetData();
|
|
187
|
+
if (optionsRef.current.abortPrevious !== false) abort();
|
|
188
|
+
loading(true);
|
|
189
|
+
executeCounterRef.current += 1;
|
|
190
|
+
const currentExecuteCounter = executeCounterRef.current;
|
|
191
|
+
liveRef.current.isAborted = false;
|
|
192
|
+
setIsAborted(false);
|
|
193
|
+
instanceRef.current(_url, {
|
|
194
|
+
...configRef.current,
|
|
195
|
+
...typeof executeUrl === "object" ? executeUrl : config,
|
|
196
|
+
signal: (_abortControllerRef$c2 = abortControllerRef.current) === null || _abortControllerRef$c2 === void 0 ? void 0 : _abortControllerRef$c2.signal
|
|
197
|
+
}).then((result) => {
|
|
198
|
+
if (liveRef.current.isAborted) return;
|
|
199
|
+
liveRef.current.response = result;
|
|
200
|
+
setResponse(result);
|
|
201
|
+
const payload = result === null || result === void 0 ? void 0 : result.data;
|
|
202
|
+
liveRef.current.data = payload;
|
|
203
|
+
setData(payload);
|
|
204
|
+
onSuccessRef.current(payload);
|
|
205
|
+
}).catch((e) => {
|
|
206
|
+
liveRef.current.error = e;
|
|
207
|
+
setError(e);
|
|
208
|
+
onErrorRef.current(e);
|
|
209
|
+
}).finally(() => {
|
|
210
|
+
var _onFinishRef$current;
|
|
211
|
+
(_onFinishRef$current = onFinishRef.current) === null || _onFinishRef$current === void 0 || _onFinishRef$current.call(onFinishRef);
|
|
212
|
+
if (currentExecuteCounter === executeCounterRef.current) {
|
|
213
|
+
loading(false);
|
|
214
|
+
drainWaiters();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
return shellRef.current;
|
|
218
|
+
}, [
|
|
219
|
+
abort,
|
|
220
|
+
drainWaiters,
|
|
221
|
+
loading,
|
|
222
|
+
resetData
|
|
223
|
+
]);
|
|
224
|
+
const base = {
|
|
225
|
+
get response() {
|
|
226
|
+
return liveRef.current.response;
|
|
227
|
+
},
|
|
228
|
+
get data() {
|
|
229
|
+
return liveRef.current.data;
|
|
230
|
+
},
|
|
231
|
+
get isFinished() {
|
|
232
|
+
return liveRef.current.isFinished;
|
|
233
|
+
},
|
|
234
|
+
get isLoading() {
|
|
235
|
+
return liveRef.current.isLoading;
|
|
236
|
+
},
|
|
237
|
+
get isAborted() {
|
|
238
|
+
return liveRef.current.isAborted;
|
|
239
|
+
},
|
|
240
|
+
get isCanceled() {
|
|
241
|
+
return liveRef.current.isAborted;
|
|
242
|
+
},
|
|
243
|
+
get error() {
|
|
244
|
+
return liveRef.current.error;
|
|
245
|
+
},
|
|
246
|
+
abort,
|
|
247
|
+
cancel: abort,
|
|
248
|
+
execute
|
|
249
|
+
};
|
|
250
|
+
baseRef.current = base;
|
|
251
|
+
const waitUntilFinished = (0, react.useCallback)(() => {
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
if (!liveRef.current.isLoading) {
|
|
254
|
+
const currentError = liveRef.current.error;
|
|
255
|
+
if (currentError) reject(currentError);
|
|
256
|
+
else resolve(baseRef.current);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
waitersRef.current.push({
|
|
260
|
+
resolve,
|
|
261
|
+
reject
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}, []);
|
|
265
|
+
(0, react.useEffect)(() => {
|
|
266
|
+
if (immediateRef.current && urlRef.current) execute().catch(_reause_shared.noop);
|
|
267
|
+
}, [execute]);
|
|
268
|
+
(0, react.useEffect)(() => () => {
|
|
269
|
+
var _abortControllerRef$c3;
|
|
270
|
+
(_abortControllerRef$c3 = abortControllerRef.current) === null || _abortControllerRef$c3 === void 0 || _abortControllerRef$c3.abort();
|
|
271
|
+
}, []);
|
|
272
|
+
(0, react.useEffect)(() => {
|
|
273
|
+
if (isLoading) return;
|
|
274
|
+
drainWaiters();
|
|
275
|
+
}, [isLoading, drainWaiters]);
|
|
276
|
+
const shell = Object.create(base);
|
|
277
|
+
shell.then = function then(onFulfilled, onRejected) {
|
|
278
|
+
return waitUntilFinished().then(onFulfilled, onRejected);
|
|
279
|
+
};
|
|
280
|
+
shell.catch = function catchFn(onRejected) {
|
|
281
|
+
return waitUntilFinished().catch(onRejected);
|
|
282
|
+
};
|
|
283
|
+
shellRef.current = shell;
|
|
284
|
+
return shell;
|
|
285
|
+
}
|
|
286
|
+
//#endregion
|
|
287
|
+
exports.useAxios = useAxios;
|
|
288
|
+
})(this.reause = this.reause || {}, reause, axios, React);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e};n=((e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule||!l.call(e,`default`)?a(n,`default`,{value:e,enumerable:!0}):n,e)))(n,1);function d(...e){let i=typeof e[0]==`string`?e[0]:void 0,a=+(typeof i==`string`),o={immediate:!!a,shallow:!0,abortPrevious:!0},s={},c=n.default,l=o,u=e=>!!(e!=null&&e.request);e.length>0+a&&(u(e[0+a])?c=e[0+a]:s=e[0+a]),e.length>1+a&&u(e[1+a])&&(c=e[1+a]),(e.length===2+a&&!u(e[1+a])||e.length===3+a)&&(l=e[e.length-1]||o);let{onSuccess:d=t.noop,onError:f=t.noop,immediate:p,resetOnExecute:m=!1}=l,h=l.initialData,g=(0,r.useRef)(i);g.current=i;let _=(0,r.useRef)(s);_.current=s;let v=(0,r.useRef)(c);v.current=c;let y=(0,r.useRef)(l);y.current=l;let b=(0,r.useRef)(p);b.current=p;let x=(0,r.useRef)(m);x.current=m;let S=(0,r.useRef)(h);S.current=h;let C=(0,r.useRef)(d);C.current=d;let w=(0,r.useRef)(f);w.current=f;let T=(0,r.useRef)(l.onFinish);T.current=l.onFinish;let[E,D]=(0,r.useState)(void 0),[O,k]=(0,r.useState)(h),[A,j]=(0,r.useState)(!1),[M,N]=(0,r.useState)(!1),[P,F]=(0,r.useState)(!1),[I,L]=(0,r.useState)(void 0),R=(0,r.useRef)({response:E,data:O,isFinished:A,isLoading:M,isAborted:P,error:I}),z=(0,r.useRef)(null);z.current===null&&(z.current=new AbortController);let B=(0,r.useRef)(0),V=(0,r.useRef)([]),H=(0,r.useRef)(null),U=(0,r.useRef)(null),W=(0,r.useCallback)(e=>{var t;!R.current.isFinished&&R.current.isLoading&&((t=z.current)==null||t.abort(e),z.current=new AbortController,R.current.isAborted=!0,R.current.isLoading=!1,R.current.isFinished=!1,F(!0),N(!1),j(!1))},[]),G=(0,r.useCallback)(e=>{R.current.isLoading=e,R.current.isFinished=!e,N(e),j(!e)},[]),K=(0,r.useCallback)(()=>{x.current&&(R.current.data=S.current,k(S.current))},[]),q=(0,r.useCallback)(()=>{let e=V.current;if(e.length===0)return;V.current=[];let t=R.current.error,n=H.current;e.forEach(({resolve:e,reject:r})=>{t?r(t):e(n)})},[]),J=(0,r.useCallback)((e=g.current,t={})=>{var r,i;R.current.error=void 0,L(void 0);let a=typeof e==`string`?e:(r=g.current)==null?t.url:r;if(a===void 0){let e=new n.AxiosError(n.AxiosError.ERR_INVALID_URL);return R.current.error=e,R.current.isFinished=!0,L(e),j(!0),U.current}K(),y.current.abortPrevious!==!1&&W(),G(!0),B.current+=1;let o=B.current;return R.current.isAborted=!1,F(!1),v.current(a,{..._.current,...typeof e==`object`?e:t,signal:(i=z.current)==null?void 0:i.signal}).then(e=>{if(R.current.isAborted)return;R.current.response=e,D(e);let t=e==null?void 0:e.data;R.current.data=t,k(t),C.current(t)}).catch(e=>{R.current.error=e,L(e),w.current(e)}).finally(()=>{var e;(e=T.current)==null||e.call(T),o===B.current&&(G(!1),q())}),U.current},[W,q,G,K]),Y={get response(){return R.current.response},get data(){return R.current.data},get isFinished(){return R.current.isFinished},get isLoading(){return R.current.isLoading},get isAborted(){return R.current.isAborted},get isCanceled(){return R.current.isAborted},get error(){return R.current.error},abort:W,cancel:W,execute:J};H.current=Y;let X=(0,r.useCallback)(()=>new Promise((e,t)=>{if(!R.current.isLoading){let n=R.current.error;n?t(n):e(H.current);return}V.current.push({resolve:e,reject:t})}),[]);(0,r.useEffect)(()=>{b.current&&g.current&&J().catch(t.noop)},[J]),(0,r.useEffect)(()=>()=>{var e;(e=z.current)==null||e.abort()},[]),(0,r.useEffect)(()=>{M||q()},[M,q]);let Z=Object.create(Y);return Z.then=function(e,t){return X().then(e,t)},Z.catch=function(e){return X().catch(e)},U.current=Z,Z}e.useAxios=d})(this.reause=this.reause||{},reause,axios,React);
|
package/dist/useAxios.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { noop } from "@reause/shared";
|
|
3
|
+
import axios, { AxiosError } from "axios";
|
|
4
|
+
//#region useAxios/index.tsx
|
|
5
|
+
/**
|
|
6
|
+
* React port of VueUse's `useAxios` — wrapper for
|
|
7
|
+
* [`axios`](https://github.com/axios/axios).
|
|
8
|
+
*
|
|
9
|
+
* Map from @vueuse/integrations `useAxios`
|
|
10
|
+
* (`source/vueuse/packages/integrations/useAxios/`). All 12 upstream overloads
|
|
11
|
+
* are kept verbatim (`(url, config?, options?)`, `(url, instance?, options?)`,
|
|
12
|
+
* `(url, config, instance, options?)`, `(config)`, `(instance)`,
|
|
13
|
+
* `(config, instance)`); the url-ful forms expose `execute(url?, config?)`,
|
|
14
|
+
* the url-less forms require `execute(url, config?)`. The returned object
|
|
15
|
+
* mirrors the upstream members — `response`, `data`, `isFinished`,
|
|
16
|
+
* `isLoading`, `isAborted`/`isCanceled`, `error`, `abort`/`cancel`, `execute` —
|
|
17
|
+
* but the members are plain values backed by live getters instead of refs
|
|
18
|
+
* (no `.value`), and the object is thenable (`await useAxios(url)` resolves
|
|
19
|
+
* with the shell once the request settled, rejecting when it failed).
|
|
20
|
+
*
|
|
21
|
+
* Adjustment for React:
|
|
22
|
+
* - the returned object is built with `Object.create` over a getter base whose
|
|
23
|
+
* members read a synchronously-updated live mirror of the state, so a shell
|
|
24
|
+
* captured earlier (e.g. the value `await` resolved with) still exposes
|
|
25
|
+
* fresh values — the same shape `useAsyncState` uses. The base itself is
|
|
26
|
+
* deliberately NOT thenable, so resolving it can never re-adopt `then`;
|
|
27
|
+
* - `execute` returns the shared thenable shell (upstream `return promise`):
|
|
28
|
+
* `await execute()` resolves with the shell once the request finished,
|
|
29
|
+
* rejecting with the request error on failure, and a bare unawaited
|
|
30
|
+
* `execute()` never settles eagerly — so no unhandled rejection can
|
|
31
|
+
* escape. `immediate` requests fire from a mount effect (upstream fires
|
|
32
|
+
* during setup), and the shell is never left without a rejection handler
|
|
33
|
+
* there;
|
|
34
|
+
* - a pending request is aborted on unmount (React-safety deviation: upstream
|
|
35
|
+
* only relies on the `isAborted` guard), so a late response can never
|
|
36
|
+
* populate `data`/`response` after the component is gone;
|
|
37
|
+
* - `shallow` is accepted for API parity but has no effect: React state is
|
|
38
|
+
* never deep-wrapped, so there is no `shallowRef`/`ref` distinction here.
|
|
39
|
+
*
|
|
40
|
+
* @__NO_SIDE_EFFECTS__
|
|
41
|
+
* @example
|
|
42
|
+
* const { data, isLoading, isFinished, execute, abort } = useAxios('/api/posts')
|
|
43
|
+
* await execute('/api/posts/2')
|
|
44
|
+
*
|
|
45
|
+
* @see https://vueuse.org/integrations/useAxios
|
|
46
|
+
*/
|
|
47
|
+
function useAxios(...args) {
|
|
48
|
+
const url = typeof args[0] === "string" ? args[0] : void 0;
|
|
49
|
+
const argsPlaceholder = typeof url === "string" ? 1 : 0;
|
|
50
|
+
const defaultOptions = {
|
|
51
|
+
immediate: !!argsPlaceholder,
|
|
52
|
+
shallow: true,
|
|
53
|
+
abortPrevious: true
|
|
54
|
+
};
|
|
55
|
+
let defaultConfig = {};
|
|
56
|
+
let instance = axios;
|
|
57
|
+
let options = defaultOptions;
|
|
58
|
+
const isAxiosInstance = (val) => !!(val === null || val === void 0 ? void 0 : val.request);
|
|
59
|
+
if (args.length > 0 + argsPlaceholder) {
|
|
60
|
+
/**
|
|
61
|
+
* Unable to use `instanceof` here because of (https://github.com/axios/axios/issues/737)
|
|
62
|
+
* so instead we are checking if there is a `request` on the object to see if it is an
|
|
63
|
+
* axios instance
|
|
64
|
+
*/
|
|
65
|
+
if (isAxiosInstance(args[0 + argsPlaceholder])) instance = args[0 + argsPlaceholder];
|
|
66
|
+
else defaultConfig = args[0 + argsPlaceholder];
|
|
67
|
+
}
|
|
68
|
+
if (args.length > 1 + argsPlaceholder) {
|
|
69
|
+
if (isAxiosInstance(args[1 + argsPlaceholder])) instance = args[1 + argsPlaceholder];
|
|
70
|
+
}
|
|
71
|
+
if (args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]) || args.length === 3 + argsPlaceholder) options = args[args.length - 1] || defaultOptions;
|
|
72
|
+
const { onSuccess = noop, onError = noop, immediate, resetOnExecute = false } = options;
|
|
73
|
+
const initialData = options.initialData;
|
|
74
|
+
const urlRef = useRef(url);
|
|
75
|
+
urlRef.current = url;
|
|
76
|
+
const configRef = useRef(defaultConfig);
|
|
77
|
+
configRef.current = defaultConfig;
|
|
78
|
+
const instanceRef = useRef(instance);
|
|
79
|
+
instanceRef.current = instance;
|
|
80
|
+
const optionsRef = useRef(options);
|
|
81
|
+
optionsRef.current = options;
|
|
82
|
+
const immediateRef = useRef(immediate);
|
|
83
|
+
immediateRef.current = immediate;
|
|
84
|
+
const resetOnExecuteRef = useRef(resetOnExecute);
|
|
85
|
+
resetOnExecuteRef.current = resetOnExecute;
|
|
86
|
+
const initialDataRef = useRef(initialData);
|
|
87
|
+
initialDataRef.current = initialData;
|
|
88
|
+
const onSuccessRef = useRef(onSuccess);
|
|
89
|
+
onSuccessRef.current = onSuccess;
|
|
90
|
+
const onErrorRef = useRef(onError);
|
|
91
|
+
onErrorRef.current = onError;
|
|
92
|
+
const onFinishRef = useRef(options.onFinish);
|
|
93
|
+
onFinishRef.current = options.onFinish;
|
|
94
|
+
const [response, setResponse] = useState(void 0);
|
|
95
|
+
const [data, setData] = useState(initialData);
|
|
96
|
+
const [isFinished, setIsFinished] = useState(false);
|
|
97
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
98
|
+
const [isAborted, setIsAborted] = useState(false);
|
|
99
|
+
const [error, setError] = useState(void 0);
|
|
100
|
+
const liveRef = useRef({
|
|
101
|
+
response,
|
|
102
|
+
data,
|
|
103
|
+
isFinished,
|
|
104
|
+
isLoading,
|
|
105
|
+
isAborted,
|
|
106
|
+
error
|
|
107
|
+
});
|
|
108
|
+
const abortControllerRef = useRef(null);
|
|
109
|
+
if (abortControllerRef.current === null) abortControllerRef.current = new AbortController();
|
|
110
|
+
const executeCounterRef = useRef(0);
|
|
111
|
+
const waitersRef = useRef([]);
|
|
112
|
+
const baseRef = useRef(null);
|
|
113
|
+
const shellRef = useRef(null);
|
|
114
|
+
const abort = useCallback((message) => {
|
|
115
|
+
var _abortControllerRef$c;
|
|
116
|
+
if (liveRef.current.isFinished || !liveRef.current.isLoading) return;
|
|
117
|
+
(_abortControllerRef$c = abortControllerRef.current) === null || _abortControllerRef$c === void 0 || _abortControllerRef$c.abort(message);
|
|
118
|
+
abortControllerRef.current = new AbortController();
|
|
119
|
+
liveRef.current.isAborted = true;
|
|
120
|
+
liveRef.current.isLoading = false;
|
|
121
|
+
liveRef.current.isFinished = false;
|
|
122
|
+
setIsAborted(true);
|
|
123
|
+
setIsLoading(false);
|
|
124
|
+
setIsFinished(false);
|
|
125
|
+
}, []);
|
|
126
|
+
const loading = useCallback((value) => {
|
|
127
|
+
liveRef.current.isLoading = value;
|
|
128
|
+
liveRef.current.isFinished = !value;
|
|
129
|
+
setIsLoading(value);
|
|
130
|
+
setIsFinished(!value);
|
|
131
|
+
}, []);
|
|
132
|
+
/**
|
|
133
|
+
* Reset data to initialData
|
|
134
|
+
*/
|
|
135
|
+
const resetData = useCallback(() => {
|
|
136
|
+
if (!resetOnExecuteRef.current) return;
|
|
137
|
+
liveRef.current.data = initialDataRef.current;
|
|
138
|
+
setData(initialDataRef.current);
|
|
139
|
+
}, []);
|
|
140
|
+
const drainWaiters = useCallback(() => {
|
|
141
|
+
const waiters = waitersRef.current;
|
|
142
|
+
if (waiters.length === 0) return;
|
|
143
|
+
waitersRef.current = [];
|
|
144
|
+
const currentError = liveRef.current.error;
|
|
145
|
+
const result = baseRef.current;
|
|
146
|
+
waiters.forEach(({ resolve, reject }) => {
|
|
147
|
+
if (currentError) reject(currentError);
|
|
148
|
+
else resolve(result);
|
|
149
|
+
});
|
|
150
|
+
}, []);
|
|
151
|
+
const execute = useCallback((executeUrl = urlRef.current, config = {}) => {
|
|
152
|
+
var _urlRef$current, _abortControllerRef$c2;
|
|
153
|
+
liveRef.current.error = void 0;
|
|
154
|
+
setError(void 0);
|
|
155
|
+
const _url = typeof executeUrl === "string" ? executeUrl : (_urlRef$current = urlRef.current) !== null && _urlRef$current !== void 0 ? _urlRef$current : config.url;
|
|
156
|
+
if (_url === void 0) {
|
|
157
|
+
const invalidUrlError = new AxiosError(AxiosError.ERR_INVALID_URL);
|
|
158
|
+
liveRef.current.error = invalidUrlError;
|
|
159
|
+
liveRef.current.isFinished = true;
|
|
160
|
+
setError(invalidUrlError);
|
|
161
|
+
setIsFinished(true);
|
|
162
|
+
return shellRef.current;
|
|
163
|
+
}
|
|
164
|
+
resetData();
|
|
165
|
+
if (optionsRef.current.abortPrevious !== false) abort();
|
|
166
|
+
loading(true);
|
|
167
|
+
executeCounterRef.current += 1;
|
|
168
|
+
const currentExecuteCounter = executeCounterRef.current;
|
|
169
|
+
liveRef.current.isAborted = false;
|
|
170
|
+
setIsAborted(false);
|
|
171
|
+
instanceRef.current(_url, {
|
|
172
|
+
...configRef.current,
|
|
173
|
+
...typeof executeUrl === "object" ? executeUrl : config,
|
|
174
|
+
signal: (_abortControllerRef$c2 = abortControllerRef.current) === null || _abortControllerRef$c2 === void 0 ? void 0 : _abortControllerRef$c2.signal
|
|
175
|
+
}).then((result) => {
|
|
176
|
+
if (liveRef.current.isAborted) return;
|
|
177
|
+
liveRef.current.response = result;
|
|
178
|
+
setResponse(result);
|
|
179
|
+
const payload = result === null || result === void 0 ? void 0 : result.data;
|
|
180
|
+
liveRef.current.data = payload;
|
|
181
|
+
setData(payload);
|
|
182
|
+
onSuccessRef.current(payload);
|
|
183
|
+
}).catch((e) => {
|
|
184
|
+
liveRef.current.error = e;
|
|
185
|
+
setError(e);
|
|
186
|
+
onErrorRef.current(e);
|
|
187
|
+
}).finally(() => {
|
|
188
|
+
var _onFinishRef$current;
|
|
189
|
+
(_onFinishRef$current = onFinishRef.current) === null || _onFinishRef$current === void 0 || _onFinishRef$current.call(onFinishRef);
|
|
190
|
+
if (currentExecuteCounter === executeCounterRef.current) {
|
|
191
|
+
loading(false);
|
|
192
|
+
drainWaiters();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
return shellRef.current;
|
|
196
|
+
}, [
|
|
197
|
+
abort,
|
|
198
|
+
drainWaiters,
|
|
199
|
+
loading,
|
|
200
|
+
resetData
|
|
201
|
+
]);
|
|
202
|
+
const base = {
|
|
203
|
+
get response() {
|
|
204
|
+
return liveRef.current.response;
|
|
205
|
+
},
|
|
206
|
+
get data() {
|
|
207
|
+
return liveRef.current.data;
|
|
208
|
+
},
|
|
209
|
+
get isFinished() {
|
|
210
|
+
return liveRef.current.isFinished;
|
|
211
|
+
},
|
|
212
|
+
get isLoading() {
|
|
213
|
+
return liveRef.current.isLoading;
|
|
214
|
+
},
|
|
215
|
+
get isAborted() {
|
|
216
|
+
return liveRef.current.isAborted;
|
|
217
|
+
},
|
|
218
|
+
get isCanceled() {
|
|
219
|
+
return liveRef.current.isAborted;
|
|
220
|
+
},
|
|
221
|
+
get error() {
|
|
222
|
+
return liveRef.current.error;
|
|
223
|
+
},
|
|
224
|
+
abort,
|
|
225
|
+
cancel: abort,
|
|
226
|
+
execute
|
|
227
|
+
};
|
|
228
|
+
baseRef.current = base;
|
|
229
|
+
const waitUntilFinished = useCallback(() => {
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
if (!liveRef.current.isLoading) {
|
|
232
|
+
const currentError = liveRef.current.error;
|
|
233
|
+
if (currentError) reject(currentError);
|
|
234
|
+
else resolve(baseRef.current);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
waitersRef.current.push({
|
|
238
|
+
resolve,
|
|
239
|
+
reject
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
}, []);
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (immediateRef.current && urlRef.current) execute().catch(noop);
|
|
245
|
+
}, [execute]);
|
|
246
|
+
useEffect(() => () => {
|
|
247
|
+
var _abortControllerRef$c3;
|
|
248
|
+
(_abortControllerRef$c3 = abortControllerRef.current) === null || _abortControllerRef$c3 === void 0 || _abortControllerRef$c3.abort();
|
|
249
|
+
}, []);
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
if (isLoading) return;
|
|
252
|
+
drainWaiters();
|
|
253
|
+
}, [isLoading, drainWaiters]);
|
|
254
|
+
const shell = Object.create(base);
|
|
255
|
+
shell.then = function then(onFulfilled, onRejected) {
|
|
256
|
+
return waitUntilFinished().then(onFulfilled, onRejected);
|
|
257
|
+
};
|
|
258
|
+
shell.catch = function catchFn(onRejected) {
|
|
259
|
+
return waitUntilFinished().catch(onRejected);
|
|
260
|
+
};
|
|
261
|
+
shellRef.current = shell;
|
|
262
|
+
return shell;
|
|
263
|
+
}
|
|
264
|
+
//#endregion
|
|
265
|
+
export { useAxios };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import { RefOrValue } from "@reause/shared";
|
|
3
|
+
import * as changeCase from "change-case";
|
|
4
|
+
import { Options } from "change-case";
|
|
5
|
+
//#region useChangeCase/index.d.ts
|
|
6
|
+
type EndsWithCase<T> = T extends `${infer _}Case` ? T : never;
|
|
7
|
+
type FilterKeys<T> = { [K in keyof T as K extends string ? K : never]: EndsWithCase<K>; };
|
|
8
|
+
type ChangeCaseKeys = FilterKeys<typeof changeCase>;
|
|
9
|
+
/**
|
|
10
|
+
* Union of the transformations `change-case` exports as `*Case` functions —
|
|
11
|
+
* derived from the module the same way VueUse does (`noCase`, `camelCase`,
|
|
12
|
+
* `capitalCase`, `constantCase`, `dotCase`, `kebabCase`, `pascalCase`,
|
|
13
|
+
* `pascalSnakeCase`, `pathCase`, `sentenceCase`, `snakeCase`, `trainCase`).
|
|
14
|
+
*/
|
|
15
|
+
export type ChangeCaseType = ChangeCaseKeys[keyof ChangeCaseKeys];
|
|
16
|
+
/**
|
|
17
|
+
* React return type: `[value, setValue]` tuple — the writable-side analog of
|
|
18
|
+
* the upstream `WritableComputedRef<string>` (issue §2B). `value` is the
|
|
19
|
+
* transformed string; `setValue` updates the internal input state.
|
|
20
|
+
*/
|
|
21
|
+
export type UseChangeCaseReturn = [string, Dispatch<SetStateAction<string>>];
|
|
22
|
+
/**
|
|
23
|
+
* React port of VueUse's `useChangeCase`.
|
|
24
|
+
*
|
|
25
|
+
* Map from @vueuse/integrations `useChangeCase`
|
|
26
|
+
* (`source/vueuse/packages/integrations/useChangeCase/`), a reactive wrapper
|
|
27
|
+
* around the `change-case` package. Upstream returns a writable
|
|
28
|
+
* `WritableComputedRef<string>`; here the writable computed ref maps to a
|
|
29
|
+
* `[value, setValue]` tuple: `value` is the transformed string (`change-case`
|
|
30
|
+
* applied to the internal input state with the current `type`), and
|
|
31
|
+
* `setValue` updates that internal input state like a controlled `useState`.
|
|
32
|
+
* `input` is the hook's **read-only value source** and takes a plain `string`
|
|
33
|
+
* (upstream: `MaybeRef<string>` / `MaybeRefOrGetter<string>`); `type` and
|
|
34
|
+
* `options` stay `RefOrValue` (format knobs, upstream `MaybeRefOrGetter`) and
|
|
35
|
+
* are resolved with `toValue` from `@reause/shared`.
|
|
36
|
+
*
|
|
37
|
+
* Adjustment for React:
|
|
38
|
+
* - upstream's writable computed captures a plain `input` once at setup. Here
|
|
39
|
+
* a changed `input` prop re-syncs the internal state on the next render, so
|
|
40
|
+
* a parent re-render with a new string is reflected; a `setValue` write is
|
|
41
|
+
* never clobbered while the `input` prop is unchanged (the baseline records
|
|
42
|
+
* the last externally synced value);
|
|
43
|
+
* - writes are **not** propagated back to the caller: `setValue` updates the
|
|
44
|
+
* internal input state only (upstream's writable computed writes through to
|
|
45
|
+
* a ref input). A changed `input` prop always wins over the internal state;
|
|
46
|
+
* - `setValue` writes the RAW string; the transform is re-applied on the next
|
|
47
|
+
* render (the upstream computed's `get` re-derives from the ref on access).
|
|
48
|
+
*
|
|
49
|
+
* @__NO_SIDE_EFFECTS__
|
|
50
|
+
* @example
|
|
51
|
+
* const [changeCase, setChangeCase] = useChangeCase('hello world', 'camelCase')
|
|
52
|
+
* changeCase // 'helloWorld'
|
|
53
|
+
* setChangeCase('vue use')
|
|
54
|
+
* changeCase // 'vueUse'
|
|
55
|
+
*/
|
|
56
|
+
export declare function useChangeCase(input: string, type: RefOrValue<ChangeCaseType>, options?: RefOrValue<Options> | undefined): UseChangeCaseReturn;
|
|
57
|
+
//#endregion
|