@kikiutils/shared 18.0.1 → 18.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/buffer.d.ts +19 -21
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js +18 -19
- package/dist/buffer.js.map +1 -1
- package/dist/classes/enhanced-date.d.ts +457 -0
- package/dist/classes/enhanced-date.d.ts.map +1 -0
- package/dist/classes/enhanced-date.js +635 -0
- package/dist/classes/enhanced-date.js.map +1 -0
- package/dist/classes/path.d.ts +211 -48
- package/dist/classes/path.d.ts.map +1 -1
- package/dist/classes/path.js +188 -54
- package/dist/classes/path.js.map +1 -1
- package/dist/classes/precision-number.d.ts +44 -39
- package/dist/classes/precision-number.d.ts.map +1 -1
- package/dist/classes/precision-number.js +51 -45
- package/dist/classes/precision-number.js.map +1 -1
- package/dist/classes/ssh-client.d.ts +3 -4
- package/dist/classes/ssh-client.d.ts.map +1 -1
- package/dist/classes/ssh-client.js +2 -2
- package/dist/classes/ssh-client.js.map +1 -1
- package/dist/clipboard.d.ts +25 -26
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +23 -23
- package/dist/clipboard.js.map +1 -1
- package/dist/consola.d.ts +12 -15
- package/dist/consola.d.ts.map +1 -1
- package/dist/consola.js +10 -12
- package/dist/consola.js.map +1 -1
- package/dist/crypto-hash.d.ts +65 -56
- package/dist/crypto-hash.d.ts.map +1 -1
- package/dist/crypto-hash.js +55 -45
- package/dist/crypto-hash.js.map +1 -1
- package/dist/datetime.d.ts +41 -53
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +53 -66
- package/dist/datetime.js.map +1 -1
- package/dist/element-plus.d.ts +13 -14
- package/dist/element-plus.d.ts.map +1 -1
- package/dist/element-plus.js +11 -11
- package/dist/element-plus.js.map +1 -1
- package/dist/enum.d.ts +39 -36
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.js +36 -32
- package/dist/enum.js.map +1 -1
- package/dist/env.d.ts +22 -15
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +20 -12
- package/dist/env.js.map +1 -1
- package/dist/event-awaiter.d.ts +47 -52
- package/dist/event-awaiter.d.ts.map +1 -1
- package/dist/event-awaiter.js +46 -50
- package/dist/event-awaiter.js.map +1 -1
- package/dist/general.d.ts +22 -17
- package/dist/general.d.ts.map +1 -1
- package/dist/general.js.map +1 -1
- package/dist/hash.d.ts +24 -21
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +20 -16
- package/dist/hash.js.map +1 -1
- package/dist/math.d.ts +20 -19
- package/dist/math.d.ts.map +1 -1
- package/dist/math.js +14 -12
- package/dist/math.js.map +1 -1
- package/dist/number.d.ts +10 -9
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js +9 -7
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts +22 -10
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +21 -8
- package/dist/object.js.map +1 -1
- package/dist/pino.d.ts +13 -15
- package/dist/pino.d.ts.map +1 -1
- package/dist/pino.js +10 -15
- package/dist/pino.js.map +1 -1
- package/dist/random.d.ts +17 -15
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +16 -13
- package/dist/random.js.map +1 -1
- package/dist/redis/script-runner.d.ts +57 -0
- package/dist/redis/script-runner.d.ts.map +1 -0
- package/dist/redis/script-runner.js +46 -0
- package/dist/redis/script-runner.js.map +1 -0
- package/dist/storages/lru/keyed-store.d.ts +18 -14
- package/dist/storages/lru/keyed-store.d.ts.map +1 -1
- package/dist/storages/lru/keyed-store.js +17 -12
- package/dist/storages/lru/keyed-store.js.map +1 -1
- package/dist/storages/redis/keyed-store.d.ts +11 -21
- package/dist/storages/redis/keyed-store.d.ts.map +1 -1
- package/dist/storages/redis/keyed-store.js +10 -19
- package/dist/storages/redis/keyed-store.js.map +1 -1
- package/dist/storages/redis/msgpack.d.ts +33 -13
- package/dist/storages/redis/msgpack.d.ts.map +1 -1
- package/dist/storages/redis/msgpack.js +32 -11
- package/dist/storages/redis/msgpack.js.map +1 -1
- package/dist/storages/redis/types.d.ts +34 -17
- package/dist/storages/redis/types.d.ts.map +1 -1
- package/dist/string.d.ts +12 -11
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +20 -15
- package/dist/string.js.map +1 -1
- package/dist/time.d.ts +26 -21
- package/dist/time.d.ts.map +1 -1
- package/dist/time.js +24 -18
- package/dist/time.js.map +1 -1
- package/dist/types/filtered-key-path.d.ts +4 -15
- package/dist/types/filtered-key-path.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/vue.d.ts +7 -7
- package/dist/types/vue.d.ts.map +1 -1
- package/dist/url.d.ts +15 -14
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +12 -10
- package/dist/url.js.map +1 -1
- package/dist/vue.d.ts +19 -21
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +15 -16
- package/dist/vue.js.map +1 -1
- package/dist/web.d.ts +14 -11
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +12 -8
- package/dist/web.js.map +1 -1
- package/package.json +21 -21
- package/src/buffer.ts +18 -19
- package/src/classes/enhanced-date.ts +715 -0
- package/src/classes/path.ts +248 -96
- package/src/classes/precision-number.ts +61 -53
- package/src/classes/ssh-client.ts +8 -5
- package/src/clipboard.ts +23 -23
- package/src/consola.ts +10 -12
- package/src/crypto-hash.ts +55 -45
- package/src/datetime.ts +65 -85
- package/src/element-plus.ts +11 -11
- package/src/enum.ts +36 -32
- package/src/env.ts +20 -12
- package/src/event-awaiter.ts +49 -50
- package/src/general.ts +20 -14
- package/src/hash.ts +20 -16
- package/src/math.ts +18 -16
- package/src/number.ts +9 -7
- package/src/object.ts +21 -8
- package/src/pino.ts +11 -15
- package/src/random.ts +16 -13
- package/src/redis/script-runner.ts +107 -0
- package/src/storages/lru/keyed-store.ts +17 -12
- package/src/storages/redis/keyed-store.ts +10 -19
- package/src/storages/redis/msgpack.ts +32 -11
- package/src/storages/redis/types.ts +32 -14
- package/src/string.ts +10 -8
- package/src/time.ts +24 -18
- package/src/types/filtered-key-path.ts +9 -14
- package/src/types/vue.ts +6 -5
- package/src/url.ts +12 -10
- package/src/vue.ts +15 -16
- package/src/web.ts +12 -8
|
@@ -1,69 +1,87 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* A caller-owned asynchronous adapter for Redis-like binary operations.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Each callback performs one operation and rejects its promise on backend failure.
|
|
6
|
+
* TTL results use Redis conventions: `-1` means no expiry, and `-2` means the key is missing.
|
|
3
7
|
*/
|
|
4
8
|
export interface RedisLikeAdapter {
|
|
5
9
|
/**
|
|
6
|
-
* Deletes a key and
|
|
10
|
+
* Deletes a key and resolves to the number of keys deleted.
|
|
7
11
|
*/
|
|
8
12
|
delete: (key: string) => Promise<number>;
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
|
-
*
|
|
15
|
+
* Reads a key and resolves to its raw bytes, or `null` when the key is missing.
|
|
12
16
|
*/
|
|
13
17
|
getBuffer: (key: string) => Promise<null | Uint8Array>;
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
|
-
* Checks
|
|
20
|
+
* Checks a key and resolves to whether it exists.
|
|
17
21
|
*/
|
|
18
22
|
has: (key: string) => Promise<boolean>;
|
|
19
23
|
|
|
20
24
|
/**
|
|
21
|
-
*
|
|
25
|
+
* Writes raw bytes and resolves to the backend acknowledgment, or no value.
|
|
22
26
|
*/
|
|
23
27
|
setBuffer: (key: string, value: Uint8Array) => Promise<'OK' | void>;
|
|
24
28
|
|
|
25
29
|
/**
|
|
26
|
-
*
|
|
30
|
+
* Writes raw bytes with an expiry and resolves to the backend acknowledgment, or no value.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The storage key.
|
|
33
|
+
* @param ttlSeconds - The expiry duration in seconds.
|
|
34
|
+
* @param value - The raw bytes to store.
|
|
27
35
|
*/
|
|
28
36
|
setBufferEx: (key: string, ttlSeconds: number, value: Uint8Array) => Promise<'OK' | void>;
|
|
29
37
|
|
|
30
38
|
/**
|
|
31
|
-
*
|
|
39
|
+
* Reads a key's remaining TTL in seconds, or `-1` for no expiry and `-2` for a missing key.
|
|
32
40
|
*/
|
|
33
41
|
ttl: (key: string) => Promise<number>;
|
|
34
42
|
}
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
|
-
*
|
|
45
|
+
* An asynchronous Redis-like storage contract with typed reads and serialized writes.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* Operations reject on backend or serialization failure. TTL values are in seconds;
|
|
49
|
+
* `-1` means no expiry, and `-2` means the key is missing. Connection cleanup remains caller-owned.
|
|
38
50
|
*/
|
|
39
51
|
export interface RedisLikeStorage {
|
|
40
52
|
/**
|
|
41
|
-
*
|
|
53
|
+
* Reads a deserialized value and resolves to `null` when the key is missing.
|
|
54
|
+
*
|
|
55
|
+
* @typeParam T - The expected value type; deserialization does not validate this type.
|
|
42
56
|
*/
|
|
43
57
|
getItem: <T = unknown>(key: string) => Promise<null | T>;
|
|
44
58
|
|
|
45
59
|
/**
|
|
46
|
-
*
|
|
60
|
+
* Reads a key's remaining TTL in seconds, or `-1` for no expiry and `-2` for a missing key.
|
|
47
61
|
*/
|
|
48
62
|
getItemTtl: (key: string) => Promise<number>;
|
|
49
63
|
|
|
50
64
|
/**
|
|
51
|
-
* Checks
|
|
65
|
+
* Checks a key and resolves to whether it exists.
|
|
52
66
|
*/
|
|
53
67
|
hasItem: (key: string) => Promise<boolean>;
|
|
54
68
|
|
|
55
69
|
/**
|
|
56
|
-
* Removes a key.
|
|
70
|
+
* Removes a key and resolves to whether a key was deleted.
|
|
57
71
|
*/
|
|
58
72
|
removeItem: (key: string) => Promise<boolean>;
|
|
59
73
|
|
|
60
74
|
/**
|
|
61
|
-
*
|
|
75
|
+
* Stores a value and resolves to whether the backend acknowledges the write.
|
|
62
76
|
*/
|
|
63
77
|
setItem: (key: string, value: any) => Promise<boolean>;
|
|
64
78
|
|
|
65
79
|
/**
|
|
66
|
-
*
|
|
80
|
+
* Stores a value with an expiry and resolves to whether the backend acknowledges the write.
|
|
81
|
+
*
|
|
82
|
+
* @param key - The storage key.
|
|
83
|
+
* @param ttlSeconds - The expiry duration in seconds.
|
|
84
|
+
* @param value - The value to serialize and store.
|
|
67
85
|
*/
|
|
68
86
|
setItemWithTtl: (key: string, ttlSeconds: number, value: any) => Promise<boolean>;
|
|
69
87
|
}
|
package/src/string.ts
CHANGED
|
@@ -26,23 +26,25 @@ const CHARSETS: Record<RandomStringMode, string> = {
|
|
|
26
26
|
/**
|
|
27
27
|
* Generates a random string of a given length using a predefined character set.
|
|
28
28
|
*
|
|
29
|
+
* @remarks
|
|
29
30
|
* Uses `Math.random`, so this helper is suitable for display tokens, placeholders,
|
|
30
31
|
* and test data, but not for passwords, API keys, or other cryptographic secrets.
|
|
31
32
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* @param
|
|
33
|
+
* @param length - The positive integer length of the string to generate.
|
|
34
|
+
* @param mode - The character set to use (default: `'alphabetic'`).
|
|
34
35
|
*
|
|
35
|
-
* @returns
|
|
36
|
+
* @returns The generated random string.
|
|
36
37
|
*
|
|
37
|
-
* @throws
|
|
38
|
+
* @throws Error if the length is not a positive integer or the mode is unsupported.
|
|
38
39
|
*
|
|
39
40
|
* @example
|
|
40
|
-
*
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
41
43
|
* import { randomString } from '@kikiutils/shared/string';
|
|
42
44
|
*
|
|
43
|
-
* randomString(8); //
|
|
44
|
-
* randomString(6, 'numeric'); //
|
|
45
|
-
* randomString(10, 'alphanumeric'); //
|
|
45
|
+
* randomString(8); // For example, 'aZbXwTyQ'.
|
|
46
|
+
* randomString(6, 'numeric'); // For example, '402398'.
|
|
47
|
+
* randomString(10, 'alphanumeric'); // For example, 'a9Z4pQ8xY2'.
|
|
46
48
|
* ```
|
|
47
49
|
*/
|
|
48
50
|
export function randomString(length: number, mode: RandomStringMode = 'alphabetic') {
|
package/src/time.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Waits until a duration elapses or cancellation occurs without rejecting on cancellation.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The promise resolves without a value when the timer completes or the signal aborts.
|
|
6
|
+
* An already aborted signal resolves immediately; completion removes the abort listener and clears the timer.
|
|
5
7
|
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
*
|
|
9
|
-
* @returns {Promise<void>} Resolves when the delay completes or the signal is aborted
|
|
8
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
9
|
+
* @param signal - The signal that ends the wait early.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
|
-
*
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
13
14
|
* import { delay } from '@kikiutils/shared/time';
|
|
14
15
|
*
|
|
15
16
|
* const controller = new AbortController();
|
|
16
|
-
*
|
|
17
|
-
* await delay(5000, controller.signal);
|
|
17
|
+
* controller.abort();
|
|
18
|
+
* await delay(5000, controller.signal);
|
|
18
19
|
* ```
|
|
19
20
|
*/
|
|
20
21
|
export function delay(ms: number, signal?: AbortSignal): Promise<void> {
|
|
@@ -37,22 +38,27 @@ export function delay(ms: number, signal?: AbortSignal): Promise<void> {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
+
* Waits until a duration elapses and rejects on cancellation.
|
|
41
42
|
*
|
|
42
|
-
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* The promise resolves without a value when the timer completes and rejects with `signal.reason` if aborted.
|
|
45
|
+
* An already aborted signal rejects immediately. Aborting clears the timer; timer completion removes the listener.
|
|
43
46
|
*
|
|
44
|
-
* @param
|
|
45
|
-
* @param
|
|
46
|
-
*
|
|
47
|
-
* @returns {Promise<void>} Resolves when the delay completes or rejects when the signal is aborted
|
|
47
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
48
|
+
* @param signal - The signal that cancels the wait.
|
|
48
49
|
*
|
|
49
50
|
* @example
|
|
50
|
-
*
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
51
53
|
* import { delayOrThrow } from '@kikiutils/shared/time';
|
|
52
54
|
*
|
|
53
55
|
* const controller = new AbortController();
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
+
* controller.abort(new Error('Canceled'));
|
|
57
|
+
* try {
|
|
58
|
+
* await delayOrThrow(5000, controller.signal);
|
|
59
|
+
* } catch (error) {
|
|
60
|
+
* console.error(error);
|
|
61
|
+
* }
|
|
56
62
|
* ```
|
|
57
63
|
*/
|
|
58
64
|
export function delayOrThrow(ms: number, signal?: AbortSignal): Promise<void> {
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
import type { ObjectId } from 'bson';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Source: https://github.com/element-plus/element-plus
|
|
6
|
-
* License: MIT (https://opensource.org/licenses/MIT)
|
|
7
|
-
*
|
|
8
|
-
* Original types might have been modified to suit this project’s needs.
|
|
9
|
-
*/
|
|
3
|
+
// Types adapted from Element Plus: https://github.com/element-plus/element-plus.
|
|
4
|
+
// Original license: MIT (https://opensource.org/licenses/MIT).
|
|
10
5
|
|
|
11
|
-
/***/
|
|
12
6
|
type ConditionalPath<K extends number | string, V, U> = V extends U ? `${K}` : DefaultPath<K, V, U, never>;
|
|
13
7
|
type DefaultPath<
|
|
14
8
|
K extends number | string,
|
|
@@ -17,16 +11,17 @@ type DefaultPath<
|
|
|
17
11
|
RK = `${K}`,
|
|
18
12
|
> = V extends TerminalType ? RK : `${K}.${FilteredKeyPath<V, U>}`;
|
|
19
13
|
|
|
14
|
+
/* eslint-disable style/max-len -- The existing conditional type is kept on one source line. */
|
|
15
|
+
|
|
20
16
|
/**
|
|
21
|
-
* A
|
|
22
|
-
* filtered by a specific type `U`. If `U` is
|
|
23
|
-
* not provided, it defaults to including all key paths.
|
|
17
|
+
* A union of nested object key paths optionally filtered by the matching value type.
|
|
24
18
|
*
|
|
25
|
-
* @
|
|
26
|
-
* @
|
|
19
|
+
* @typeParam T - The object type to traverse.
|
|
20
|
+
* @typeParam U - The value type to select; `never` disables filtering.
|
|
27
21
|
*/
|
|
28
|
-
// eslint-disable-next-line style/max-len
|
|
29
22
|
export type FilteredKeyPath<T, U = never> = T extends ReadonlyArray<infer V> ? (IsTuple<T> extends true ? { [K in TupleKey<T>]-?: PathImpl<Exclude<K, symbol>, T[K], U> }[TupleKey<T>] : PathImpl<number, V, U>) : { [K in keyof T]-?: PathImpl<Exclude<K, symbol>, T[K], U> }[keyof T];
|
|
23
|
+
/* eslint-enable style/max-len */
|
|
24
|
+
|
|
30
25
|
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
31
26
|
type PathImpl<K extends number | string, V, U> = [U] extends [never] ? DefaultPath<K, V> : ConditionalPath<K, V, U>;
|
|
32
27
|
type TerminalType =
|
package/src/types/vue.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { GlobalComponents } from 'vue';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* A
|
|
5
|
-
* The reference can be either an instance of the specified component or null.
|
|
4
|
+
* A nullable reference to an instance of a globally registered Vue component.
|
|
6
5
|
*
|
|
7
|
-
* @
|
|
6
|
+
* @typeParam K - The component name in Vue's `GlobalComponents` registry.
|
|
8
7
|
*
|
|
9
8
|
* @example
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { ComponentRef } from '@kikiutils/shared/types/vue';
|
|
12
|
+
* import { ref } from 'vue';
|
|
12
13
|
*
|
|
13
14
|
* const keepAliveRef = ref<ComponentRef<'KeepAlive'>>(null);
|
|
14
15
|
* ```
|
package/src/url.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Appends or updates the `redirect` query parameter on a given URL.
|
|
3
3
|
*
|
|
4
|
+
* @remarks
|
|
4
5
|
* Typically used to preserve the user's current path for post-login navigation.
|
|
5
6
|
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* (must be a safe same-origin application path starting with `/`)
|
|
7
|
+
* @param url - The target URL to modify.
|
|
8
|
+
* @param redirectPath - The safe same-origin application path starting with `/` to use as the redirect destination.
|
|
9
9
|
*
|
|
10
|
-
* @returns
|
|
10
|
+
* @returns A new URL string with the `redirect` query parameter.
|
|
11
11
|
*
|
|
12
|
-
* @throws
|
|
12
|
+
* @throws Error if `redirectPath` is not a safe same-origin application path.
|
|
13
13
|
*/
|
|
14
14
|
export function appendRedirectParamToUrl(url: string, redirectPath: string) {
|
|
15
15
|
// eslint-disable-next-line style/max-len
|
|
@@ -26,13 +26,14 @@ export function appendRedirectParamToUrl(url: string, redirectPath: string) {
|
|
|
26
26
|
/**
|
|
27
27
|
* Returns whether a value is a safe same-origin redirect path.
|
|
28
28
|
*
|
|
29
|
+
* @remarks
|
|
29
30
|
* Safe redirect paths are absolute application paths such as `/dashboard`.
|
|
30
31
|
* Protocol-relative URLs (`//example.com`), absolute URLs, backslash paths,
|
|
31
32
|
* and non-string values are rejected.
|
|
32
33
|
*
|
|
33
|
-
* @param
|
|
34
|
+
* @param value - The value to check.
|
|
34
35
|
*
|
|
35
|
-
* @returns
|
|
36
|
+
* @returns Whether the value is safe to use as an application redirect path.
|
|
36
37
|
*/
|
|
37
38
|
export function isSafeRedirectPath(value: unknown): value is string {
|
|
38
39
|
if (typeof value !== 'string') return false;
|
|
@@ -45,13 +46,14 @@ export function isSafeRedirectPath(value: unknown): value is string {
|
|
|
45
46
|
/**
|
|
46
47
|
* Normalizes a redirect value into a safe same-origin application path.
|
|
47
48
|
*
|
|
49
|
+
* @remarks
|
|
48
50
|
* If an array is provided, the first value is checked. Unsafe values fall back
|
|
49
51
|
* to the provided fallback path.
|
|
50
52
|
*
|
|
51
|
-
* @param
|
|
52
|
-
* @param
|
|
53
|
+
* @param value - The redirect value to normalize.
|
|
54
|
+
* @param fallback - The caller-supplied safe fallback path; it is returned unchanged and is not validated.
|
|
53
55
|
*
|
|
54
|
-
* @returns
|
|
56
|
+
* @returns The validated input path, or the unvalidated fallback path.
|
|
55
57
|
*/
|
|
56
58
|
export function normalizeRedirectPath(value: unknown, fallback = '/') {
|
|
57
59
|
const redirectPath = Array.isArray(value) ? value[0] : value;
|
package/src/vue.ts
CHANGED
|
@@ -8,20 +8,20 @@ import {
|
|
|
8
8
|
import { appendRedirectParamToUrl } from './url';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Appends the current Vue Router route's fullPath as the `redirect` query parameter to the given URL.
|
|
11
|
+
* Appends the current Vue Router route's `fullPath` as the `redirect` query parameter to the given URL.
|
|
12
12
|
*
|
|
13
|
-
* @param
|
|
13
|
+
* @param url - The base URL to modify.
|
|
14
14
|
*
|
|
15
|
-
* @returns
|
|
15
|
+
* @returns A new URL with the current route `fullPath` as the `redirect` parameter.
|
|
16
16
|
*/
|
|
17
17
|
export function appendRedirectParamFromCurrentRouteToUrl(url: string) {
|
|
18
18
|
return appendRedirectParamToUrl(url, useRoute().fullPath);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* Clears an interval referenced by a Vue ref and sets it to null
|
|
22
|
+
* Clears an interval referenced by a Vue ref and sets it to `null`.
|
|
23
23
|
*
|
|
24
|
-
* @param
|
|
24
|
+
* @param intervalRef - A Vue ref holding a timer handle or `null`.
|
|
25
25
|
*/
|
|
26
26
|
export function clearIntervalRef(intervalRef: Ref<null | ReturnType<typeof setInterval>>) {
|
|
27
27
|
if (intervalRef.value) clearInterval(intervalRef.value);
|
|
@@ -29,9 +29,9 @@ export function clearIntervalRef(intervalRef: Ref<null | ReturnType<typeof setIn
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* Clears a timeout referenced by a Vue ref and sets it to null
|
|
32
|
+
* Clears a timeout referenced by a Vue ref and sets it to `null`.
|
|
33
33
|
*
|
|
34
|
-
* @param
|
|
34
|
+
* @param timeoutRef - A Vue ref holding a timer handle or `null`.
|
|
35
35
|
*/
|
|
36
36
|
export function clearTimeoutRef(timeoutRef: Ref<null | ReturnType<typeof setTimeout>>) {
|
|
37
37
|
if (timeoutRef.value) clearTimeout(timeoutRef.value);
|
|
@@ -39,18 +39,17 @@ export function clearTimeoutRef(timeoutRef: Ref<null | ReturnType<typeof setTime
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
* of a scrollable container across route changes and keep-alive activation.
|
|
42
|
+
* Registers hooks that save and restore a container's scroll position across kept-alive route changes.
|
|
44
43
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* Call within Vue component setup with Vue Router and `KeepAlive`.
|
|
46
|
+
* Route leave saves the current offsets, and each activation restores them to the currently referenced element.
|
|
47
|
+
* A missing element saves zero offsets and skips restoration. Without `KeepAlive`, unmounting discards the state.
|
|
48
|
+
* The hooks follow the component lifecycle; there is no separate cleanup handle.
|
|
47
49
|
*
|
|
48
|
-
*
|
|
49
|
-
* so scroll position will not be preserved.
|
|
50
|
+
* @typeParam T - The scrollable element type.
|
|
50
51
|
*
|
|
51
|
-
* @
|
|
52
|
-
*
|
|
53
|
-
* @param {Ref<null | T>} containerRef - A ref to the scrollable HTML element
|
|
52
|
+
* @param containerRef - The reference to the scrollable element, or `null` while it is unavailable.
|
|
54
53
|
*/
|
|
55
54
|
export function usePreserveScroll<T extends Element = HTMLElement>(containerRef: Ref<null | T>) {
|
|
56
55
|
let scrollLeft = 0;
|
package/src/web.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { appendRedirectParamToUrl } from './url';
|
|
|
3
3
|
/**
|
|
4
4
|
* Appends the current browser URL (including path, query, and hash) as the `redirect` query parameter to the given URL.
|
|
5
5
|
*
|
|
6
|
-
* @param
|
|
6
|
+
* @param url - The base URL to modify.
|
|
7
7
|
*
|
|
8
|
-
* @returns
|
|
8
|
+
* @returns A new URL with the current location as the `redirect` parameter.
|
|
9
9
|
*/
|
|
10
10
|
export function appendRedirectParamFromCurrentLocationToUrl(url: string) {
|
|
11
11
|
const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
|
@@ -13,14 +13,18 @@ export function appendRedirectParamFromCurrentLocationToUrl(url: string) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Navigates to
|
|
17
|
-
* (path, query, and hash) as the `redirect` query parameter.
|
|
16
|
+
* Navigates to a URL with the current browser path, query, and hash as its redirect parameter.
|
|
18
17
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Omitting `delayMs` navigates immediately. Providing a delay, including `0`, schedules navigation;
|
|
20
|
+
* the current location is read when the timer fires, not when the timer is created.
|
|
21
|
+
* Requires a browser environment. Redirect-path validation errors propagate from the URL helper.
|
|
21
22
|
*
|
|
22
|
-
* @param
|
|
23
|
-
* @param
|
|
23
|
+
* @param url - The destination URL.
|
|
24
|
+
* @param delayMs - The navigation delay in milliseconds, using native `setTimeout` timing rules.
|
|
25
|
+
*
|
|
26
|
+
* @returns A timer handle when delayed, which the caller can cancel with `clearTimeout`, or `undefined`
|
|
27
|
+
* when navigation is immediate.
|
|
24
28
|
*/
|
|
25
29
|
export function assignUrlWithRedirectParamFromCurrentLocation(url: string, delayMs?: number) {
|
|
26
30
|
if (delayMs === undefined) window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url));
|