@varlet/axle 1.0.0 → 1.0.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/lib/use.cjs DELETED
@@ -1,351 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
- var __export = (target, all) => {
21
- for (var name in all)
22
- __defProp(target, name, { get: all[name], enumerable: true });
23
- };
24
- var __copyProps = (to, from, except, desc) => {
25
- if (from && typeof from === "object" || typeof from === "function") {
26
- for (let key of __getOwnPropNames(from))
27
- if (!__hasOwnProp.call(to, key) && key !== except)
28
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
- }
30
- return to;
31
- };
32
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
-
34
- // src/use.ts
35
- var use_exports = {};
36
- __export(use_exports, {
37
- cleanupCacheBuffer: () => cleanupCacheBuffer,
38
- createUseAxle: () => createUseAxle,
39
- normalizeValueGetter: () => normalizeValueGetter
40
- });
41
- module.exports = __toCommonJS(use_exports);
42
- var import_vue = require("vue");
43
- var import_rattail = require("rattail");
44
- function normalizeValueGetter(valueGetter) {
45
- return (0, import_rattail.isFunction)(valueGetter) ? valueGetter() : valueGetter;
46
- }
47
- var cacheBuffer = /* @__PURE__ */ new Map();
48
- function cleanupCacheBuffer() {
49
- cacheBuffer.forEach((value, key) => {
50
- if (value.expiredTime && Date.now() >= value.expiredTime) {
51
- cacheBuffer.delete(key);
52
- }
53
- });
54
- }
55
- function createUseAxle(options) {
56
- const {
57
- axle,
58
- onTransform: defaultOnTransform,
59
- immediate: defaultImmediate = true,
60
- abortOnUnmount: defaultAbortOnUnmount = true,
61
- cacheTime: defaultCacheTime = Infinity
62
- } = options;
63
- const useAxle = (options2) => {
64
- const {
65
- url: initialUrlOrGetter,
66
- method,
67
- immediate = defaultImmediate,
68
- abortOnUnmount = defaultAbortOnUnmount,
69
- cacheTime = defaultCacheTime,
70
- cacheKey,
71
- pollingInterval,
72
- pollingOnHidden = true,
73
- pollingOnDeactivated = false,
74
- refreshOnWindowFocus = false,
75
- value: initialValue,
76
- resetValue: initialResetValue,
77
- cloneResetValue: initialCloneResetValue,
78
- params: initialParamsOrGetter,
79
- config: initialConfigOrGetter,
80
- watch: watchOptions,
81
- runnable = () => true,
82
- onBefore = () => {
83
- },
84
- onAfter = () => {
85
- },
86
- onTransform = defaultOnTransform != null ? defaultOnTransform : (response) => response,
87
- onSuccess = () => {
88
- },
89
- onError = () => {
90
- }
91
- } = options2;
92
- const value = (0, import_vue.ref)(initialValue);
93
- const loading = (0, import_vue.ref)(false);
94
- const error = (0, import_vue.ref)();
95
- const downloadProgress = (0, import_vue.ref)(0);
96
- const uploadProgress = (0, import_vue.ref)(0);
97
- const pollingCanceled = (0, import_vue.ref)(false);
98
- const refs = {
99
- value,
100
- loading,
101
- error,
102
- downloadProgress,
103
- uploadProgress,
104
- pollingCanceled
105
- };
106
- const extra = {
107
- uploadProgress,
108
- downloadProgress,
109
- loading,
110
- error,
111
- pollingCanceled,
112
- abort,
113
- resetValue,
114
- invalidateCache,
115
- cancelPolling
116
- };
117
- let controller = new AbortController();
118
- let pollingTimer = null;
119
- let deactivated = false;
120
- let hidden = false;
121
- let unmounted = false;
122
- const run = Object.assign(async (options3 = {}) => {
123
- var _a, _b, _c, _d, _e;
124
- if (!runnable() || abortOnUnmount && unmounted) {
125
- return;
126
- }
127
- pollingCanceled.value = false;
128
- if (controller.signal.aborted) {
129
- controller = new AbortController();
130
- }
131
- const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
132
- if (resetValueOption === true) {
133
- resetValue(options3);
134
- }
135
- uploadProgress.value = 0;
136
- downloadProgress.value = 0;
137
- const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
138
- const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
139
- const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
140
- const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
141
- onBefore(refs);
142
- loading.value = true;
143
- try {
144
- cleanupCacheBuffer();
145
- const response = await getResponse();
146
- if (shouldSetCacheResponse(normalizedCacheKey)) {
147
- const cache = cacheBuffer.get(normalizedCacheKey);
148
- cache.response = response;
149
- cache.expiredTime = Date.now() + cacheTime;
150
- }
151
- value.value = await onTransform(response, refs);
152
- error.value = void 0;
153
- onSuccess(response, refs);
154
- loading.value = false;
155
- onAfter(refs);
156
- return response;
157
- } catch (responseError) {
158
- error.value = responseError;
159
- onError(responseError, refs);
160
- loading.value = false;
161
- onAfter(refs);
162
- throw responseError;
163
- } finally {
164
- clearPolling();
165
- startPolling();
166
- }
167
- function getResponse() {
168
- if (shouldUseCache(normalizedCacheKey)) {
169
- return JSON.parse(JSON.stringify(cacheBuffer.get(normalizedCacheKey).response));
170
- }
171
- if (shouldAwaitCachePromise(normalizedCacheKey)) {
172
- return cacheBuffer.get(normalizedCacheKey).promise;
173
- }
174
- const promise = fetchResponse();
175
- if (shouldSetCachePromise(normalizedCacheKey)) {
176
- cacheBuffer.set(normalizedCacheKey, { promise });
177
- }
178
- return promise;
179
- }
180
- function fetchResponse() {
181
- return axle[method](url, params, __spreadValues({
182
- signal: controller.signal,
183
- onUploadProgress(event) {
184
- var _a2;
185
- uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
186
- },
187
- onDownloadProgress(event) {
188
- var _a2;
189
- downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
190
- }
191
- }, config));
192
- }
193
- function shouldUseCache(key) {
194
- if (!key || !cacheBuffer.has(key)) {
195
- return false;
196
- }
197
- const buffer = cacheBuffer.get(key);
198
- return buffer.response != null;
199
- }
200
- function shouldAwaitCachePromise(key) {
201
- if (!key || !cacheBuffer.has(key)) {
202
- return false;
203
- }
204
- const buffer = cacheBuffer.get(key);
205
- return buffer.promise;
206
- }
207
- function shouldSetCachePromise(key) {
208
- if (!key || cacheBuffer.has(key)) {
209
- return false;
210
- }
211
- return true;
212
- }
213
- function shouldSetCacheResponse(key) {
214
- if (!key || !cacheBuffer.has(key)) {
215
- return false;
216
- }
217
- const buffer = cacheBuffer.get(key);
218
- return buffer.response == null;
219
- }
220
- }, extra);
221
- if (immediate) {
222
- runWithInitialConfig();
223
- }
224
- if ((0, import_vue.getCurrentInstance)()) {
225
- (0, import_vue.onActivated)(() => {
226
- deactivated = false;
227
- addVisibilityChangeListener();
228
- addWindowFocusListener();
229
- startPolling();
230
- });
231
- (0, import_vue.onMounted)(() => {
232
- addVisibilityChangeListener();
233
- addWindowFocusListener();
234
- });
235
- (0, import_vue.onDeactivated)(() => {
236
- deactivated = true;
237
- removeVisibilityChangeListener();
238
- removeWindowFocusListener();
239
- clearPolling();
240
- });
241
- (0, import_vue.onUnmounted)(() => {
242
- removeVisibilityChangeListener();
243
- removeWindowFocusListener();
244
- cancelPolling();
245
- });
246
- (0, import_vue.onUnmounted)(() => {
247
- unmounted = true;
248
- if (abortOnUnmount) {
249
- abort();
250
- }
251
- });
252
- if (watchOptions) {
253
- const normalizedWatchOptions = watchOptions === true ? { params: true, config: true } : __spreadValues({ params: false, config: false }, watchOptions);
254
- const enableWatchParams = (0, import_rattail.isFunction)(initialParamsOrGetter) && normalizedWatchOptions.params;
255
- const enableWatchConfig = (0, import_rattail.isFunction)(initialConfigOrGetter) && normalizedWatchOptions.config;
256
- (0, import_vue.watch)(
257
- () => [
258
- enableWatchParams ? normalizeValueGetter(initialParamsOrGetter) : void 0,
259
- enableWatchConfig ? normalizeValueGetter(initialConfigOrGetter) : void 0,
260
- normalizedWatchOptions._custom ? normalizedWatchOptions._custom() : void 0
261
- ],
262
- () => {
263
- runWithInitialConfig();
264
- }
265
- );
266
- }
267
- }
268
- function runWithInitialConfig() {
269
- return run({
270
- url: normalizeValueGetter(initialUrlOrGetter),
271
- params: normalizeValueGetter(initialParamsOrGetter),
272
- config: normalizeValueGetter(initialConfigOrGetter)
273
- });
274
- }
275
- function resetValue(options3 = {}) {
276
- var _a, _b;
277
- const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
278
- const cloneFn = cloneResetValue === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : (0, import_rattail.isFunction)(initialCloneResetValue) ? initialCloneResetValue : (v) => v;
279
- value.value = cloneFn(initialValue);
280
- }
281
- function abort() {
282
- controller.abort();
283
- }
284
- function addWindowFocusListener() {
285
- if (typeof window !== "undefined") {
286
- window.addEventListener("focus", handleWindowFocus);
287
- }
288
- }
289
- function removeWindowFocusListener() {
290
- if (typeof window !== "undefined") {
291
- window.removeEventListener("focus", handleWindowFocus);
292
- }
293
- }
294
- function handleWindowFocus() {
295
- if (refreshOnWindowFocus) {
296
- runWithInitialConfig();
297
- }
298
- }
299
- function addVisibilityChangeListener() {
300
- if (typeof document !== "undefined") {
301
- document.addEventListener("visibilitychange", handleVisibilityChange);
302
- }
303
- }
304
- function removeVisibilityChangeListener() {
305
- if (typeof document !== "undefined") {
306
- document.removeEventListener("visibilitychange", handleVisibilityChange);
307
- }
308
- }
309
- function handleVisibilityChange() {
310
- hidden = document.visibilityState === "hidden";
311
- if (pollingOnHidden) {
312
- return;
313
- }
314
- hidden ? clearPolling() : startPolling();
315
- }
316
- function startPolling() {
317
- if (pollingInterval == null || pollingCanceled.value || pollingTimer != null || deactivated && !pollingOnDeactivated || hidden && !pollingOnHidden) {
318
- return;
319
- }
320
- pollingTimer = setTimeout(() => {
321
- runWithInitialConfig();
322
- }, pollingInterval);
323
- }
324
- function clearPolling() {
325
- if (pollingTimer == null) {
326
- return;
327
- }
328
- clearTimeout(pollingTimer);
329
- pollingTimer = null;
330
- }
331
- function cancelPolling() {
332
- clearPolling();
333
- pollingCanceled.value = true;
334
- }
335
- function invalidateCache() {
336
- const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
337
- if (!normalizedCacheKey || !cacheBuffer.has(normalizedCacheKey)) {
338
- return;
339
- }
340
- cacheBuffer.delete(normalizedCacheKey);
341
- }
342
- return [value, run, extra];
343
- };
344
- return useAxle;
345
- }
346
- // Annotate the CommonJS export names for ESM import in node:
347
- 0 && (module.exports = {
348
- cleanupCacheBuffer,
349
- createUseAxle,
350
- normalizeValueGetter
351
- });
package/lib/use.d.cts DELETED
@@ -1,85 +0,0 @@
1
- import { Ref } from 'vue';
2
- import { A as AxleRequestConfig, d as RunnerMethod, e as AxleInstance } from './instance-857fe07c.js';
3
- import 'axios';
4
-
5
- type Runnable = () => boolean;
6
- interface RunOptions<V, P, D> {
7
- url?: string;
8
- params?: P;
9
- config?: AxleRequestConfig<D>;
10
- resetValue?: boolean;
11
- cloneResetValue?: boolean | ((value: V) => V);
12
- }
13
- type UseAxleExtra<V> = {
14
- loading: Ref<boolean>;
15
- error: Ref<Error | undefined>;
16
- uploadProgress: Ref<number>;
17
- downloadProgress: Ref<number>;
18
- pollingCanceled: Ref<boolean>;
19
- abort(): void;
20
- resetValue(options?: ResetValueOptions<V>): void;
21
- invalidateCache(): void;
22
- cancelPolling(): void;
23
- };
24
- interface UseAxleRefs<V> {
25
- value: Ref<V>;
26
- loading: Ref<boolean>;
27
- error: Ref<Error | undefined>;
28
- uploadProgress: Ref<number>;
29
- downloadProgress: Ref<number>;
30
- pollingCanceled: Ref<boolean>;
31
- }
32
- type Run<V, R, P, D> = {
33
- (options?: RunOptions<V, P, D>): Promise<R>;
34
- } & UseAxleExtra<V>;
35
- type WatchOptions = boolean | {
36
- params?: boolean;
37
- config?: boolean;
38
- _custom?: () => any;
39
- };
40
- interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<string, any>> {
41
- url: string | (() => string);
42
- method: RunnerMethod;
43
- value?: V;
44
- immediate?: boolean;
45
- resetValue?: boolean;
46
- cloneResetValue?: boolean | ((value: V) => V);
47
- abortOnUnmount?: boolean;
48
- cacheKey?: string | (() => string);
49
- cacheTime?: number;
50
- config?: AxleRequestConfig<D> | (() => AxleRequestConfig<D>);
51
- watch?: WatchOptions;
52
- pollingInterval?: number;
53
- pollingOnHidden?: boolean;
54
- pollingOnDeactivated?: boolean;
55
- refreshOnWindowFocus?: boolean;
56
- params?: P | (() => P);
57
- onBefore?(refs: UseAxleRefs<V>): void;
58
- onAfter?(refs: UseAxleRefs<V>): void;
59
- onTransform?(response: R, refs: UseAxleRefs<V>): V | Promise<V>;
60
- onSuccess?(response: R, refs: UseAxleRefs<V>): void;
61
- onError?(error: Error, refs: UseAxleRefs<V>): void;
62
- }
63
- interface ResetValueOptions<V> {
64
- cloneResetValue?: boolean | ((value: V) => V);
65
- }
66
- type UseAxleInstance<V, R, P, D> = [value: Ref<V>, run: Run<V, R, P, D>, extra: UseAxleExtra<V>];
67
- interface CreateUseAxleOptions {
68
- axle: AxleInstance;
69
- immediate?: boolean;
70
- abortOnUnmount?: boolean;
71
- cacheTime?: number;
72
- onTransform?(response: any, refs: any): any;
73
- }
74
- type UseAxleOptionsWithRunnable<V = any, R = any, P = Record<string, any>, D = Record<string, any>> = UseAxleOptions<V, R, P, D> & {
75
- runnable: Runnable;
76
- };
77
- interface UseAxle {
78
- <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(options: UseAxleOptionsWithRunnable<V, R, P, D>): UseAxleInstance<V, R | undefined, P, D>;
79
- <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(options: UseAxleOptions<V, R, P, D>): UseAxleInstance<V, R, P, D>;
80
- }
81
- declare function normalizeValueGetter<T>(valueGetter: T | (() => T)): T;
82
- declare function cleanupCacheBuffer(): void;
83
- declare function createUseAxle(options: CreateUseAxleOptions): UseAxle;
84
-
85
- export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, Runnable, UseAxle, UseAxleExtra, UseAxleInstance, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxleRefs, WatchOptions, cleanupCacheBuffer, createUseAxle, normalizeValueGetter };
package/lib/use.d.ts DELETED
@@ -1,85 +0,0 @@
1
- import { Ref } from 'vue';
2
- import { A as AxleRequestConfig, d as RunnerMethod, e as AxleInstance } from './instance-857fe07c.js';
3
- import 'axios';
4
-
5
- type Runnable = () => boolean;
6
- interface RunOptions<V, P, D> {
7
- url?: string;
8
- params?: P;
9
- config?: AxleRequestConfig<D>;
10
- resetValue?: boolean;
11
- cloneResetValue?: boolean | ((value: V) => V);
12
- }
13
- type UseAxleExtra<V> = {
14
- loading: Ref<boolean>;
15
- error: Ref<Error | undefined>;
16
- uploadProgress: Ref<number>;
17
- downloadProgress: Ref<number>;
18
- pollingCanceled: Ref<boolean>;
19
- abort(): void;
20
- resetValue(options?: ResetValueOptions<V>): void;
21
- invalidateCache(): void;
22
- cancelPolling(): void;
23
- };
24
- interface UseAxleRefs<V> {
25
- value: Ref<V>;
26
- loading: Ref<boolean>;
27
- error: Ref<Error | undefined>;
28
- uploadProgress: Ref<number>;
29
- downloadProgress: Ref<number>;
30
- pollingCanceled: Ref<boolean>;
31
- }
32
- type Run<V, R, P, D> = {
33
- (options?: RunOptions<V, P, D>): Promise<R>;
34
- } & UseAxleExtra<V>;
35
- type WatchOptions = boolean | {
36
- params?: boolean;
37
- config?: boolean;
38
- _custom?: () => any;
39
- };
40
- interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<string, any>> {
41
- url: string | (() => string);
42
- method: RunnerMethod;
43
- value?: V;
44
- immediate?: boolean;
45
- resetValue?: boolean;
46
- cloneResetValue?: boolean | ((value: V) => V);
47
- abortOnUnmount?: boolean;
48
- cacheKey?: string | (() => string);
49
- cacheTime?: number;
50
- config?: AxleRequestConfig<D> | (() => AxleRequestConfig<D>);
51
- watch?: WatchOptions;
52
- pollingInterval?: number;
53
- pollingOnHidden?: boolean;
54
- pollingOnDeactivated?: boolean;
55
- refreshOnWindowFocus?: boolean;
56
- params?: P | (() => P);
57
- onBefore?(refs: UseAxleRefs<V>): void;
58
- onAfter?(refs: UseAxleRefs<V>): void;
59
- onTransform?(response: R, refs: UseAxleRefs<V>): V | Promise<V>;
60
- onSuccess?(response: R, refs: UseAxleRefs<V>): void;
61
- onError?(error: Error, refs: UseAxleRefs<V>): void;
62
- }
63
- interface ResetValueOptions<V> {
64
- cloneResetValue?: boolean | ((value: V) => V);
65
- }
66
- type UseAxleInstance<V, R, P, D> = [value: Ref<V>, run: Run<V, R, P, D>, extra: UseAxleExtra<V>];
67
- interface CreateUseAxleOptions {
68
- axle: AxleInstance;
69
- immediate?: boolean;
70
- abortOnUnmount?: boolean;
71
- cacheTime?: number;
72
- onTransform?(response: any, refs: any): any;
73
- }
74
- type UseAxleOptionsWithRunnable<V = any, R = any, P = Record<string, any>, D = Record<string, any>> = UseAxleOptions<V, R, P, D> & {
75
- runnable: Runnable;
76
- };
77
- interface UseAxle {
78
- <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(options: UseAxleOptionsWithRunnable<V, R, P, D>): UseAxleInstance<V, R | undefined, P, D>;
79
- <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(options: UseAxleOptions<V, R, P, D>): UseAxleInstance<V, R, P, D>;
80
- }
81
- declare function normalizeValueGetter<T>(valueGetter: T | (() => T)): T;
82
- declare function cleanupCacheBuffer(): void;
83
- declare function createUseAxle(options: CreateUseAxleOptions): UseAxle;
84
-
85
- export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, Runnable, UseAxle, UseAxleExtra, UseAxleInstance, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxleRefs, WatchOptions, cleanupCacheBuffer, createUseAxle, normalizeValueGetter };