@sudobility/types 1.9.53 → 1.9.55
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/types/blockchain/index.cjs +30 -0
- package/dist/types/blockchain/index.d.ts +14 -0
- package/dist/types/blockchain/index.d.ts.map +1 -0
- package/dist/types/blockchain/index.js +30 -0
- package/dist/types/blockchain/index.js.map +1 -0
- package/dist/types/blockchain/validation.cjs +57 -1
- package/dist/types/blockchain/validation.d.ts +57 -1
- package/dist/types/blockchain/validation.d.ts.map +1 -1
- package/dist/types/blockchain/validation.js +57 -1
- package/dist/types/blockchain/validation.js.map +1 -1
- package/dist/types/business/enums.cjs +110 -25
- package/dist/types/business/enums.d.ts +110 -2
- package/dist/types/business/enums.d.ts.map +1 -1
- package/dist/types/business/enums.js +110 -25
- package/dist/types/business/enums.js.map +1 -1
- package/dist/types/common.cjs +7 -1
- package/dist/types/common.d.ts +134 -20
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/common.js +7 -1
- package/dist/types/common.js.map +1 -1
- package/dist/utils/async-helpers.cjs +124 -10
- package/dist/utils/async-helpers.d.ts +129 -8
- package/dist/utils/async-helpers.d.ts.map +1 -1
- package/dist/utils/async-helpers.js +124 -10
- package/dist/utils/async-helpers.js.map +1 -1
- package/dist/utils/formatting/currency.cjs +5 -2
- package/dist/utils/formatting/currency.d.ts +5 -1
- package/dist/utils/formatting/currency.d.ts.map +1 -1
- package/dist/utils/formatting/currency.js +5 -2
- package/dist/utils/formatting/currency.js.map +1 -1
- package/dist/utils/formatting/date.cjs +67 -8
- package/dist/utils/formatting/date.d.ts +67 -8
- package/dist/utils/formatting/date.d.ts.map +1 -1
- package/dist/utils/formatting/date.js +67 -8
- package/dist/utils/formatting/date.js.map +1 -1
- package/dist/utils/formatting/string.cjs +150 -17
- package/dist/utils/formatting/string.d.ts +150 -17
- package/dist/utils/formatting/string.d.ts.map +1 -1
- package/dist/utils/formatting/string.js +150 -17
- package/dist/utils/formatting/string.js.map +1 -1
- package/dist/utils/validation/type-validation.cjs +94 -11
- package/dist/utils/validation/type-validation.d.ts +94 -11
- package/dist/utils/validation/type-validation.d.ts.map +1 -1
- package/dist/utils/validation/type-validation.js +94 -11
- package/dist/utils/validation/type-validation.js.map +1 -1
- package/package.json +6 -1
package/dist/types/common.d.ts
CHANGED
|
@@ -1,15 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Common utility types used throughout the application
|
|
2
|
+
* Common utility types used throughout the application.
|
|
3
|
+
*
|
|
4
|
+
* Provides foundational types like {@link Optional}, {@link Result},
|
|
5
|
+
* {@link ValidationResult}, and API response structures used across
|
|
6
|
+
* every package in the ecosystem.
|
|
7
|
+
*
|
|
8
|
+
* @since 1.0.0
|
|
3
9
|
*/
|
|
4
10
|
import { ChainType } from './business/enums';
|
|
5
11
|
/**
|
|
6
|
-
* Utility type for values that can be T, undefined, or null
|
|
7
|
-
*
|
|
12
|
+
* Utility type for values that can be T, undefined, or null.
|
|
13
|
+
* Use instead of manual `T | null | undefined` unions for consistency.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The base type
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* interface User {
|
|
21
|
+
* name: string;
|
|
22
|
+
* bio?: Optional<string>;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
8
25
|
*/
|
|
9
26
|
export type Optional<T> = T | undefined | null;
|
|
10
27
|
/**
|
|
11
|
-
* Base wallet data structure containing address and chain type
|
|
12
|
-
* Used across all wallet-related interfaces to ensure consistency
|
|
28
|
+
* Base wallet data structure containing address and chain type.
|
|
29
|
+
* Used across all wallet-related interfaces to ensure consistency.
|
|
30
|
+
*
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const wallet: WalletData = {
|
|
36
|
+
* walletAddress: '0x742d35Cc6634C0532925a3b8D2C36B7f1234567',
|
|
37
|
+
* chainType: ChainType.EVM,
|
|
38
|
+
* };
|
|
39
|
+
* ```
|
|
13
40
|
*/
|
|
14
41
|
export interface WalletData {
|
|
15
42
|
/** Wallet address (EVM 0x format or Solana Base58) */
|
|
@@ -18,8 +45,10 @@ export interface WalletData {
|
|
|
18
45
|
chainType: ChainType;
|
|
19
46
|
}
|
|
20
47
|
/**
|
|
21
|
-
* Base folder data structure containing common folder properties
|
|
22
|
-
* Used across all folder/mailbox-related interfaces to ensure consistency
|
|
48
|
+
* Base folder data structure containing common folder properties.
|
|
49
|
+
* Used across all folder/mailbox-related interfaces to ensure consistency.
|
|
50
|
+
*
|
|
51
|
+
* @since 1.0.0
|
|
23
52
|
*/
|
|
24
53
|
export interface FolderBase {
|
|
25
54
|
/** Folder name */
|
|
@@ -30,8 +59,10 @@ export interface FolderBase {
|
|
|
30
59
|
unreadCount: number;
|
|
31
60
|
}
|
|
32
61
|
/**
|
|
33
|
-
* Base message structure containing core message fields
|
|
34
|
-
* Used across all message/email-related interfaces to ensure consistency
|
|
62
|
+
* Base message structure containing core message fields.
|
|
63
|
+
* Used across all message/email-related interfaces to ensure consistency.
|
|
64
|
+
*
|
|
65
|
+
* @since 1.0.0
|
|
35
66
|
*/
|
|
36
67
|
export interface MessageBase {
|
|
37
68
|
/** Message sender */
|
|
@@ -44,8 +75,20 @@ export interface MessageBase {
|
|
|
44
75
|
body: string;
|
|
45
76
|
}
|
|
46
77
|
/**
|
|
47
|
-
* Base response structure for all API operations
|
|
48
|
-
* Unified response interface for consistency across the entire application
|
|
78
|
+
* Base response structure for all API operations.
|
|
79
|
+
* Unified response interface for consistency across the entire application.
|
|
80
|
+
*
|
|
81
|
+
* @template T - The response data type (defaults to `unknown`)
|
|
82
|
+
* @since 1.0.0
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const response: BaseResponse<User> = {
|
|
87
|
+
* success: true,
|
|
88
|
+
* data: { name: 'Alice' },
|
|
89
|
+
* timestamp: new Date().toISOString(),
|
|
90
|
+
* };
|
|
91
|
+
* ```
|
|
49
92
|
*/
|
|
50
93
|
export interface BaseResponse<T = unknown> {
|
|
51
94
|
/** Operation success status */
|
|
@@ -58,7 +101,19 @@ export interface BaseResponse<T = unknown> {
|
|
|
58
101
|
timestamp: string;
|
|
59
102
|
}
|
|
60
103
|
/**
|
|
61
|
-
* Unified pagination options for querying
|
|
104
|
+
* Unified pagination options for querying.
|
|
105
|
+
* Supports both offset-based and cursor-based pagination.
|
|
106
|
+
*
|
|
107
|
+
* @since 1.0.0
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const opts: PaginationOptions = {
|
|
112
|
+
* limit: 20,
|
|
113
|
+
* cursor: 'abc123',
|
|
114
|
+
* sortOrder: 'desc',
|
|
115
|
+
* };
|
|
116
|
+
* ```
|
|
62
117
|
*/
|
|
63
118
|
export interface PaginationOptions {
|
|
64
119
|
/** Number of items per page */
|
|
@@ -71,7 +126,9 @@ export interface PaginationOptions {
|
|
|
71
126
|
sortOrder?: Optional<'asc' | 'desc'>;
|
|
72
127
|
}
|
|
73
128
|
/**
|
|
74
|
-
* Unified pagination metadata for responses
|
|
129
|
+
* Unified pagination metadata for responses.
|
|
130
|
+
*
|
|
131
|
+
* @since 1.0.0
|
|
75
132
|
*/
|
|
76
133
|
export interface PaginationInfo {
|
|
77
134
|
/** Whether there are more items after current page */
|
|
@@ -88,14 +145,42 @@ export interface PaginationInfo {
|
|
|
88
145
|
pageSize: number;
|
|
89
146
|
}
|
|
90
147
|
/**
|
|
91
|
-
* Paginated response structure
|
|
148
|
+
* Paginated response structure extending {@link BaseResponse} with
|
|
149
|
+
* pagination metadata.
|
|
150
|
+
*
|
|
151
|
+
* @template T - The item type in the paginated list
|
|
152
|
+
* @since 1.0.0
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const page: PaginatedResponse<User> = {
|
|
157
|
+
* success: true,
|
|
158
|
+
* data: [{ name: 'Alice' }],
|
|
159
|
+
* timestamp: new Date().toISOString(),
|
|
160
|
+
* pagination: { hasNextPage: true, pageSize: 20 },
|
|
161
|
+
* };
|
|
162
|
+
* ```
|
|
92
163
|
*/
|
|
93
164
|
export interface PaginatedResponse<T> extends BaseResponse<T[]> {
|
|
94
165
|
/** Pagination metadata */
|
|
95
166
|
pagination: PaginationInfo;
|
|
96
167
|
}
|
|
97
168
|
/**
|
|
98
|
-
* Unified error structure for all operations
|
|
169
|
+
* Unified error structure for all operations.
|
|
170
|
+
* Provides typed error categories, human-readable messages,
|
|
171
|
+
* and optional suggested actions for the user.
|
|
172
|
+
*
|
|
173
|
+
* @since 1.0.0
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const error: UnifiedError = {
|
|
178
|
+
* type: 'ValidationError',
|
|
179
|
+
* message: 'Invalid email address',
|
|
180
|
+
* details: { field: 'email' },
|
|
181
|
+
* suggestedAction: 'Please enter a valid email.',
|
|
182
|
+
* };
|
|
183
|
+
* ```
|
|
99
184
|
*/
|
|
100
185
|
export interface UnifiedError {
|
|
101
186
|
/** Error type/code */
|
|
@@ -116,9 +201,25 @@ export interface UnifiedError {
|
|
|
116
201
|
suggestedAction?: Optional<string>;
|
|
117
202
|
}
|
|
118
203
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
204
|
+
* Discriminated union for operations that can succeed or fail.
|
|
205
|
+
* Prefer this over throwing exceptions for recoverable errors.
|
|
206
|
+
*
|
|
207
|
+
* @template T - Success result type
|
|
208
|
+
* @template E - Error type (defaults to {@link UnifiedError})
|
|
209
|
+
* @since 1.0.0
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* function divide(a: number, b: number): Result<number> {
|
|
214
|
+
* if (b === 0) {
|
|
215
|
+
* return {
|
|
216
|
+
* success: false,
|
|
217
|
+
* error: { type: 'ValidationError', message: 'Division by zero' },
|
|
218
|
+
* };
|
|
219
|
+
* }
|
|
220
|
+
* return { success: true, data: a / b };
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
122
223
|
*/
|
|
123
224
|
export type Result<T, E = UnifiedError> = {
|
|
124
225
|
success: true;
|
|
@@ -128,8 +229,21 @@ export type Result<T, E = UnifiedError> = {
|
|
|
128
229
|
error: E;
|
|
129
230
|
};
|
|
130
231
|
/**
|
|
131
|
-
* Unified validation result pattern
|
|
132
|
-
* @
|
|
232
|
+
* Unified validation result pattern.
|
|
233
|
+
* Used by validators and {@link createValidator} to return typed results.
|
|
234
|
+
*
|
|
235
|
+
* @template T - The validated data type
|
|
236
|
+
* @since 1.0.0
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* function validateAge(value: unknown): ValidationResult<number> {
|
|
241
|
+
* if (typeof value === 'number' && value >= 0) {
|
|
242
|
+
* return { isValid: true, data: value };
|
|
243
|
+
* }
|
|
244
|
+
* return { isValid: false, error: 'Age must be a non-negative number' };
|
|
245
|
+
* }
|
|
246
|
+
* ```
|
|
133
247
|
*/
|
|
134
248
|
export type ValidationResult<T = unknown> = {
|
|
135
249
|
isValid: true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,GAAG,IAAI,CAAC;AAE/C;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,UAAU;IACzB,sDAAsD;IACtD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,CAAC,GAAG,YAAY,IAClC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GAC1B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAEjC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,GAAG,OAAO,IACpC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GAC1B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;CAAE,CAAC"}
|
package/dist/types/common.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Common utility types used throughout the application
|
|
3
|
+
* Common utility types used throughout the application.
|
|
4
|
+
*
|
|
5
|
+
* Provides foundational types like {@link Optional}, {@link Result},
|
|
6
|
+
* {@link ValidationResult}, and API response structures used across
|
|
7
|
+
* every package in the ecosystem.
|
|
8
|
+
*
|
|
9
|
+
* @since 1.0.0
|
|
4
10
|
*/
|
|
5
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
12
|
//# sourceMappingURL=common.js.map
|
package/dist/types/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG"}
|
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Common async operation patterns and helpers
|
|
4
|
-
* Reduces boilerplate code for common async operations
|
|
3
|
+
* Common async operation patterns and helpers.
|
|
4
|
+
* Reduces boilerplate code for common async operations.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0
|
|
5
7
|
*/
|
|
6
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
9
|
exports.debounceAsync = exports.clearExpiredCache = exports.withCache = exports.withTimeout = exports.safeParallel = exports.withLoadingState = exports.safeAsync = void 0;
|
|
8
10
|
const logger_1 = require("./logging/logger");
|
|
9
11
|
/**
|
|
10
|
-
* Safely execute an async operation with error handling
|
|
11
|
-
* Returns a result object instead of throwing
|
|
12
|
+
* Safely execute an async operation with error handling.
|
|
13
|
+
* Returns a result object instead of throwing.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The success data type
|
|
16
|
+
* @param operation - Async function to execute
|
|
17
|
+
* @param context - Optional context string for logging
|
|
18
|
+
* @returns A promise resolving to an {@link AsyncResult}
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const result = await safeAsync(() => fetchUser(id), 'fetchUser');
|
|
24
|
+
* if (result.success) {
|
|
25
|
+
* console.log(result.data);
|
|
26
|
+
* } else {
|
|
27
|
+
* console.error(result.error?.message);
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
12
30
|
*/
|
|
13
31
|
const safeAsync = async (operation, context) => {
|
|
14
32
|
try {
|
|
@@ -23,7 +41,26 @@ const safeAsync = async (operation, context) => {
|
|
|
23
41
|
};
|
|
24
42
|
exports.safeAsync = safeAsync;
|
|
25
43
|
/**
|
|
26
|
-
* Execute async operation with loading state tracking
|
|
44
|
+
* Execute async operation with loading state tracking.
|
|
45
|
+
* Manages `setLoading` and `setError` callbacks automatically.
|
|
46
|
+
*
|
|
47
|
+
* @template T - The success data type
|
|
48
|
+
* @param operation - Async function to execute
|
|
49
|
+
* @param setLoading - Callback to update loading state
|
|
50
|
+
* @param setError - Callback to update error state
|
|
51
|
+
* @param context - Optional context string for logging
|
|
52
|
+
* @returns The operation result, or undefined on error
|
|
53
|
+
* @since 1.0.0
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const data = await withLoadingState(
|
|
58
|
+
* () => api.getUsers(),
|
|
59
|
+
* setIsLoading,
|
|
60
|
+
* setErrorMsg,
|
|
61
|
+
* 'getUsers'
|
|
62
|
+
* );
|
|
63
|
+
* ```
|
|
27
64
|
*/
|
|
28
65
|
const withLoadingState = async (operation, setLoading, setError, context) => {
|
|
29
66
|
setLoading(true);
|
|
@@ -44,7 +81,22 @@ const withLoadingState = async (operation, setLoading, setError, context) => {
|
|
|
44
81
|
};
|
|
45
82
|
exports.withLoadingState = withLoadingState;
|
|
46
83
|
/**
|
|
47
|
-
* Execute multiple async operations in parallel with error handling
|
|
84
|
+
* Execute multiple async operations in parallel with error handling.
|
|
85
|
+
* All operations run concurrently via `Promise.all`.
|
|
86
|
+
*
|
|
87
|
+
* @template T - Tuple of result types
|
|
88
|
+
* @param operations - Array of async functions to execute in parallel
|
|
89
|
+
* @param context - Optional context string for logging
|
|
90
|
+
* @returns A promise resolving to an {@link AsyncResult} of the tuple
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const result = await safeParallel(
|
|
96
|
+
* [() => fetchUser(1), () => fetchPosts(1)] as const,
|
|
97
|
+
* 'loadProfile'
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
48
100
|
*/
|
|
49
101
|
const safeParallel = async (operations, context) => {
|
|
50
102
|
try {
|
|
@@ -59,7 +111,25 @@ const safeParallel = async (operations, context) => {
|
|
|
59
111
|
};
|
|
60
112
|
exports.safeParallel = safeParallel;
|
|
61
113
|
/**
|
|
62
|
-
* Execute async operation with timeout
|
|
114
|
+
* Execute async operation with a timeout.
|
|
115
|
+
* Rejects if the operation does not complete within `timeoutMs`.
|
|
116
|
+
*
|
|
117
|
+
* @template T - The success data type
|
|
118
|
+
* @param operation - Async function to execute
|
|
119
|
+
* @param timeoutMs - Maximum time in milliseconds
|
|
120
|
+
* @param context - Optional context string for logging
|
|
121
|
+
* @returns The operation result
|
|
122
|
+
* @throws Error if the operation times out
|
|
123
|
+
* @since 1.0.0
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const data = await withTimeout(
|
|
128
|
+
* () => fetch('/api/data').then((r) => r.json()),
|
|
129
|
+
* 5000,
|
|
130
|
+
* 'fetchData'
|
|
131
|
+
* );
|
|
132
|
+
* ```
|
|
63
133
|
*/
|
|
64
134
|
const withTimeout = async (operation, timeoutMs, context) => {
|
|
65
135
|
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error(`Operation timed out after ${timeoutMs}ms`)), timeoutMs));
|
|
@@ -73,9 +143,30 @@ const withTimeout = async (operation, timeoutMs, context) => {
|
|
|
73
143
|
};
|
|
74
144
|
exports.withTimeout = withTimeout;
|
|
75
145
|
/**
|
|
76
|
-
*
|
|
146
|
+
* In-memory cache for {@link withCache}.
|
|
77
147
|
*/
|
|
78
148
|
const cache = new Map();
|
|
149
|
+
/**
|
|
150
|
+
* Cache async operation results with TTL (time-to-live).
|
|
151
|
+
* Returns cached data if still valid; otherwise executes the operation
|
|
152
|
+
* and stores the result.
|
|
153
|
+
*
|
|
154
|
+
* @template T - The cached data type
|
|
155
|
+
* @param key - Unique cache key
|
|
156
|
+
* @param operation - Async function whose result is cached
|
|
157
|
+
* @param ttlMs - Cache TTL in milliseconds (default: 5 minutes)
|
|
158
|
+
* @returns The cached or freshly fetched data
|
|
159
|
+
* @since 1.0.0
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const user = await withCache(
|
|
164
|
+
* `user-${id}`,
|
|
165
|
+
* () => api.getUser(id),
|
|
166
|
+
* 60_000 // 1 minute
|
|
167
|
+
* );
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
79
170
|
const withCache = async (key, operation, ttlMs = 5 * 60 * 1000 // 5 minutes default
|
|
80
171
|
) => {
|
|
81
172
|
const cached = cache.get(key);
|
|
@@ -89,7 +180,8 @@ const withCache = async (key, operation, ttlMs = 5 * 60 * 1000 // 5 minutes defa
|
|
|
89
180
|
};
|
|
90
181
|
exports.withCache = withCache;
|
|
91
182
|
/**
|
|
92
|
-
* Clear expired cache entries
|
|
183
|
+
* Clear expired cache entries from the in-memory cache.
|
|
184
|
+
* @since 1.0.0
|
|
93
185
|
*/
|
|
94
186
|
const clearExpiredCache = () => {
|
|
95
187
|
const now = Date.now();
|
|
@@ -101,9 +193,31 @@ const clearExpiredCache = () => {
|
|
|
101
193
|
};
|
|
102
194
|
exports.clearExpiredCache = clearExpiredCache;
|
|
103
195
|
/**
|
|
104
|
-
*
|
|
196
|
+
* Active debounce timers keyed by operation key.
|
|
105
197
|
*/
|
|
106
198
|
const debounceMap = new Map();
|
|
199
|
+
/**
|
|
200
|
+
* Debounce async function calls by key.
|
|
201
|
+
* Only the last call within the delay window will execute.
|
|
202
|
+
*
|
|
203
|
+
* @template T - Argument types
|
|
204
|
+
* @template R - Return type
|
|
205
|
+
* @param fn - The async function to debounce
|
|
206
|
+
* @param delay - Debounce delay in milliseconds
|
|
207
|
+
* @param key - Unique key identifying this debounced operation
|
|
208
|
+
* @returns A debounced version of `fn`
|
|
209
|
+
* @since 1.0.0
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const debouncedSearch = debounceAsync(
|
|
214
|
+
* (query: string) => api.search(query),
|
|
215
|
+
* 300,
|
|
216
|
+
* 'search'
|
|
217
|
+
* );
|
|
218
|
+
* await debouncedSearch('hello');
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
107
221
|
const debounceAsync = (fn, delay, key) => {
|
|
108
222
|
return (...args) => {
|
|
109
223
|
return new Promise(resolve => {
|
|
@@ -1,35 +1,156 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Common async operation patterns and helpers
|
|
3
|
-
* Reduces boilerplate code for common async operations
|
|
2
|
+
* Common async operation patterns and helpers.
|
|
3
|
+
* Reduces boilerplate code for common async operations.
|
|
4
|
+
*
|
|
5
|
+
* @since 1.0.0
|
|
4
6
|
*/
|
|
5
7
|
import { Optional } from '../types/common';
|
|
8
|
+
/**
|
|
9
|
+
* Result type for safe async operations.
|
|
10
|
+
* Contains either `data` on success or `error` on failure.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The success data type
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
6
15
|
type AsyncResult<T> = {
|
|
7
16
|
data?: T;
|
|
8
17
|
error?: Error;
|
|
9
18
|
success: boolean;
|
|
10
19
|
};
|
|
11
20
|
/**
|
|
12
|
-
* Safely execute an async operation with error handling
|
|
13
|
-
* Returns a result object instead of throwing
|
|
21
|
+
* Safely execute an async operation with error handling.
|
|
22
|
+
* Returns a result object instead of throwing.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The success data type
|
|
25
|
+
* @param operation - Async function to execute
|
|
26
|
+
* @param context - Optional context string for logging
|
|
27
|
+
* @returns A promise resolving to an {@link AsyncResult}
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const result = await safeAsync(() => fetchUser(id), 'fetchUser');
|
|
33
|
+
* if (result.success) {
|
|
34
|
+
* console.log(result.data);
|
|
35
|
+
* } else {
|
|
36
|
+
* console.error(result.error?.message);
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
14
39
|
*/
|
|
15
40
|
declare const safeAsync: <T>(operation: () => Promise<T>, context?: string) => Promise<AsyncResult<T>>;
|
|
16
41
|
/**
|
|
17
|
-
* Execute async operation with loading state tracking
|
|
42
|
+
* Execute async operation with loading state tracking.
|
|
43
|
+
* Manages `setLoading` and `setError` callbacks automatically.
|
|
44
|
+
*
|
|
45
|
+
* @template T - The success data type
|
|
46
|
+
* @param operation - Async function to execute
|
|
47
|
+
* @param setLoading - Callback to update loading state
|
|
48
|
+
* @param setError - Callback to update error state
|
|
49
|
+
* @param context - Optional context string for logging
|
|
50
|
+
* @returns The operation result, or undefined on error
|
|
51
|
+
* @since 1.0.0
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const data = await withLoadingState(
|
|
56
|
+
* () => api.getUsers(),
|
|
57
|
+
* setIsLoading,
|
|
58
|
+
* setErrorMsg,
|
|
59
|
+
* 'getUsers'
|
|
60
|
+
* );
|
|
61
|
+
* ```
|
|
18
62
|
*/
|
|
19
63
|
declare const withLoadingState: <T>(operation: () => Promise<T>, setLoading: (loading: boolean) => void, setError: (error: Optional<string>) => void, context?: string) => Promise<Optional<T>>;
|
|
20
64
|
/**
|
|
21
|
-
* Execute multiple async operations in parallel with error handling
|
|
65
|
+
* Execute multiple async operations in parallel with error handling.
|
|
66
|
+
* All operations run concurrently via `Promise.all`.
|
|
67
|
+
*
|
|
68
|
+
* @template T - Tuple of result types
|
|
69
|
+
* @param operations - Array of async functions to execute in parallel
|
|
70
|
+
* @param context - Optional context string for logging
|
|
71
|
+
* @returns A promise resolving to an {@link AsyncResult} of the tuple
|
|
72
|
+
* @since 1.0.0
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const result = await safeParallel(
|
|
77
|
+
* [() => fetchUser(1), () => fetchPosts(1)] as const,
|
|
78
|
+
* 'loadProfile'
|
|
79
|
+
* );
|
|
80
|
+
* ```
|
|
22
81
|
*/
|
|
23
82
|
declare const safeParallel: <T extends readonly unknown[]>(operations: readonly [...{ [K in keyof T]: () => Promise<T[K]>; }], context?: string) => Promise<AsyncResult<T>>;
|
|
24
83
|
/**
|
|
25
|
-
* Execute async operation with timeout
|
|
84
|
+
* Execute async operation with a timeout.
|
|
85
|
+
* Rejects if the operation does not complete within `timeoutMs`.
|
|
86
|
+
*
|
|
87
|
+
* @template T - The success data type
|
|
88
|
+
* @param operation - Async function to execute
|
|
89
|
+
* @param timeoutMs - Maximum time in milliseconds
|
|
90
|
+
* @param context - Optional context string for logging
|
|
91
|
+
* @returns The operation result
|
|
92
|
+
* @throws Error if the operation times out
|
|
93
|
+
* @since 1.0.0
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const data = await withTimeout(
|
|
98
|
+
* () => fetch('/api/data').then((r) => r.json()),
|
|
99
|
+
* 5000,
|
|
100
|
+
* 'fetchData'
|
|
101
|
+
* );
|
|
102
|
+
* ```
|
|
26
103
|
*/
|
|
27
104
|
declare const withTimeout: <T>(operation: () => Promise<T>, timeoutMs: number, context?: string) => Promise<T>;
|
|
105
|
+
/**
|
|
106
|
+
* Cache async operation results with TTL (time-to-live).
|
|
107
|
+
* Returns cached data if still valid; otherwise executes the operation
|
|
108
|
+
* and stores the result.
|
|
109
|
+
*
|
|
110
|
+
* @template T - The cached data type
|
|
111
|
+
* @param key - Unique cache key
|
|
112
|
+
* @param operation - Async function whose result is cached
|
|
113
|
+
* @param ttlMs - Cache TTL in milliseconds (default: 5 minutes)
|
|
114
|
+
* @returns The cached or freshly fetched data
|
|
115
|
+
* @since 1.0.0
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const user = await withCache(
|
|
120
|
+
* `user-${id}`,
|
|
121
|
+
* () => api.getUser(id),
|
|
122
|
+
* 60_000 // 1 minute
|
|
123
|
+
* );
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
28
126
|
declare const withCache: <T>(key: string, operation: () => Promise<T>, ttlMs?: number) => Promise<T>;
|
|
29
127
|
/**
|
|
30
|
-
* Clear expired cache entries
|
|
128
|
+
* Clear expired cache entries from the in-memory cache.
|
|
129
|
+
* @since 1.0.0
|
|
31
130
|
*/
|
|
32
131
|
declare const clearExpiredCache: () => void;
|
|
132
|
+
/**
|
|
133
|
+
* Debounce async function calls by key.
|
|
134
|
+
* Only the last call within the delay window will execute.
|
|
135
|
+
*
|
|
136
|
+
* @template T - Argument types
|
|
137
|
+
* @template R - Return type
|
|
138
|
+
* @param fn - The async function to debounce
|
|
139
|
+
* @param delay - Debounce delay in milliseconds
|
|
140
|
+
* @param key - Unique key identifying this debounced operation
|
|
141
|
+
* @returns A debounced version of `fn`
|
|
142
|
+
* @since 1.0.0
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const debouncedSearch = debounceAsync(
|
|
147
|
+
* (query: string) => api.search(query),
|
|
148
|
+
* 300,
|
|
149
|
+
* 'search'
|
|
150
|
+
* );
|
|
151
|
+
* await debouncedSearch('hello');
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
33
154
|
declare const debounceAsync: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, delay: number, key: string) => ((...args: T) => Promise<Optional<R>>);
|
|
34
155
|
export { safeAsync, withLoadingState, safeParallel, withTimeout, withCache, clearExpiredCache, debounceAsync, type AsyncResult, };
|
|
35
156
|
//# sourceMappingURL=async-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-helpers.d.ts","sourceRoot":"","sources":["../../src/utils/async-helpers.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"async-helpers.d.ts","sourceRoot":"","sources":["../../src/utils/async-helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C;;;;;;GAMG;AACH,KAAK,WAAW,CAAC,CAAC,IAAI;IACpB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,QAAA,MAAM,SAAS,GAAU,CAAC,EACxB,WAAW,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,UAAU,MAAM,KACf,OA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