mulink 1.1.8 → 1.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/README.md +2 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/{chunk-AHGDKSOY.js → chunk-BMHPB646.js} +495 -252
- package/dist/lib/chunk-BMHPB646.js.map +1 -0
- package/dist/lib/{chunk-SSMOIVFN.cjs → chunk-IAS3P4RF.cjs} +495 -252
- package/dist/lib/chunk-IAS3P4RF.cjs.map +1 -0
- package/dist/lib/cli.cjs +57 -29
- package/dist/lib/cli.cjs.map +1 -1
- package/dist/lib/cli.js +42 -14
- package/dist/lib/cli.js.map +1 -1
- package/dist/lib/client.cjs +20 -20
- package/dist/lib/client.cjs.map +1 -1
- package/dist/lib/client.d.cts +4 -4
- package/dist/lib/client.d.ts +4 -4
- package/dist/lib/client.js +4 -4
- package/dist/lib/client.js.map +1 -1
- package/dist/lib/index.cjs +177 -15
- package/dist/lib/index.cjs.map +1 -1
- package/dist/lib/index.d.cts +211 -3
- package/dist/lib/index.d.ts +211 -3
- package/dist/lib/index.js +153 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/{version-checker-uF6o5ziX.d.cts → version-checker-DU88tpi8.d.cts} +152 -12
- package/dist/lib/{version-checker-uF6o5ziX.d.ts → version-checker-DU88tpi8.d.ts} +152 -12
- package/package.json +2 -2
- package/dist/lib/chunk-AHGDKSOY.js.map +0 -1
- package/dist/lib/chunk-SSMOIVFN.cjs.map +0 -1
package/dist/lib/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BridgeConfiguration,
|
|
2
|
-
export { A as ApiConfiguration,
|
|
1
|
+
import { B as BridgeConfiguration, s as GenerationContext, t as GeneratedFile } from './version-checker-DU88tpi8.cjs';
|
|
2
|
+
export { A as ApiConfiguration, j as ApiEndpointDefinition, l as ApiResponseDefinition, f as AuthConfiguration, a as BridgeCore, I as BridgeError, b as BridgeLogger, v as BridgePlugin, g as CacheConfiguration, q as CacheStrategy, C as ConfigurationLoader, D as DevelopmentConfiguration, E as EndpointMetadata, u as FileMetadata, F as FileSystemManager, e as FrameworkConfiguration, G as GenerationConfiguration, M as GenerationError, H as HttpMethod, L as LogLevel, h as OpenApiDocument, O as OpenApiSchemaParser, k as ParameterDefinition, i as ParsedApiSchema, P as PluginConfiguration, r as RateLimit, R as RequestBodyDefinition, S as SchemaDefinition, o as SchemaMetadata, K as SchemaParseError, p as SecurityRequirement, m as SecuritySchemeDefinition, n as ServerDefinition, J as ValidationError, V as VersionChecker, N as VersionInfo, d as checkAndNotifyUpdates, c as createBridgeVersionChecker } from './version-checker-DU88tpi8.cjs';
|
|
3
3
|
import 'openapi-types';
|
|
4
4
|
import 'zod';
|
|
5
5
|
|
|
@@ -12,4 +12,212 @@ declare class NextJsCodeGenerator {
|
|
|
12
12
|
private generateSafeActionClient;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Type guard utilities for runtime type checking
|
|
17
|
+
*
|
|
18
|
+
* These utilities provide type-safe runtime checks similar to libraries like Apollo Client.
|
|
19
|
+
* They ensure type safety at runtime and improve developer experience.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Type guard to check if a value is defined (not null or undefined)
|
|
23
|
+
*
|
|
24
|
+
* @param value - Value to check
|
|
25
|
+
* @returns True if value is defined
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const value: string | undefined = getValue();
|
|
30
|
+
* if (isDefined(value)) {
|
|
31
|
+
* // value is now string
|
|
32
|
+
* console.log(value.toUpperCase());
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard to check if a value is a string
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value to check
|
|
41
|
+
* @returns True if value is a string
|
|
42
|
+
*/
|
|
43
|
+
declare function isString(value: unknown): value is string;
|
|
44
|
+
/**
|
|
45
|
+
* Type guard to check if a value is a number
|
|
46
|
+
*
|
|
47
|
+
* @param value - Value to check
|
|
48
|
+
* @returns True if value is a number
|
|
49
|
+
*/
|
|
50
|
+
declare function isNumber(value: unknown): value is number;
|
|
51
|
+
/**
|
|
52
|
+
* Type guard to check if a value is an object (and not null)
|
|
53
|
+
*
|
|
54
|
+
* @param value - Value to check
|
|
55
|
+
* @returns True if value is an object
|
|
56
|
+
*/
|
|
57
|
+
declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
58
|
+
/**
|
|
59
|
+
* Type guard to check if a value is an array
|
|
60
|
+
*
|
|
61
|
+
* @param value - Value to check
|
|
62
|
+
* @returns True if value is an array
|
|
63
|
+
*/
|
|
64
|
+
declare function isArray<T>(value: unknown): value is T[];
|
|
65
|
+
/**
|
|
66
|
+
* Type guard to check if a value is a function
|
|
67
|
+
*
|
|
68
|
+
* @param value - Value to check
|
|
69
|
+
* @returns True if value is a function
|
|
70
|
+
*/
|
|
71
|
+
declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
|
|
72
|
+
/**
|
|
73
|
+
* Type guard to check if an error is an Error instance
|
|
74
|
+
*
|
|
75
|
+
* @param error - Error to check
|
|
76
|
+
* @returns True if error is an Error instance
|
|
77
|
+
*/
|
|
78
|
+
declare function isError(error: unknown): error is Error;
|
|
79
|
+
/**
|
|
80
|
+
* Type guard to check if a value is a Promise
|
|
81
|
+
*
|
|
82
|
+
* @param value - Value to check
|
|
83
|
+
* @returns True if value is a Promise
|
|
84
|
+
*/
|
|
85
|
+
declare function isPromise<T>(value: unknown): value is Promise<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Type guard to check if a value is a non-empty string
|
|
88
|
+
*
|
|
89
|
+
* @param value - Value to check
|
|
90
|
+
* @returns True if value is a non-empty string
|
|
91
|
+
*/
|
|
92
|
+
declare function isNonEmptyString(value: unknown): value is string;
|
|
93
|
+
/**
|
|
94
|
+
* Type guard to check if a value is a valid URL string
|
|
95
|
+
*
|
|
96
|
+
* @param value - Value to check
|
|
97
|
+
* @returns True if value is a valid URL
|
|
98
|
+
*/
|
|
99
|
+
declare function isValidUrl(value: unknown): value is string;
|
|
100
|
+
/**
|
|
101
|
+
* Type guard to check if a value is a valid file path
|
|
102
|
+
*
|
|
103
|
+
* @param value - Value to check
|
|
104
|
+
* @returns True if value appears to be a valid file path
|
|
105
|
+
*/
|
|
106
|
+
declare function isValidFilePath(value: unknown): value is string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Memoization utilities for performance optimization
|
|
110
|
+
*
|
|
111
|
+
* Similar to Apollo Client's caching strategies, these utilities provide
|
|
112
|
+
* efficient memoization for expensive computations.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* Memoization options
|
|
116
|
+
*/
|
|
117
|
+
interface MemoizeOptions {
|
|
118
|
+
/**
|
|
119
|
+
* Time to live in milliseconds
|
|
120
|
+
* If not provided, cache never expires
|
|
121
|
+
*/
|
|
122
|
+
ttl?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Maximum cache size
|
|
125
|
+
* If exceeded, oldest entries are removed
|
|
126
|
+
*/
|
|
127
|
+
maxSize?: number;
|
|
128
|
+
/**
|
|
129
|
+
* Custom key generator function
|
|
130
|
+
*/
|
|
131
|
+
keyGenerator?: (...args: unknown[]) => string;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Creates a memoized version of a function
|
|
135
|
+
*
|
|
136
|
+
* Caches function results based on arguments. Similar to Apollo Client's
|
|
137
|
+
* caching mechanism but for general-purpose functions.
|
|
138
|
+
*
|
|
139
|
+
* @param fn - Function to memoize
|
|
140
|
+
* @param options - Memoization options
|
|
141
|
+
* @returns Memoized function
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const expensiveFunction = (x: number, y: number) => {
|
|
146
|
+
* // Expensive computation
|
|
147
|
+
* return x * y;
|
|
148
|
+
* };
|
|
149
|
+
*
|
|
150
|
+
* const memoized = memoize(expensiveFunction, { ttl: 5000 });
|
|
151
|
+
* const result1 = memoized(2, 3); // Computes
|
|
152
|
+
* const result2 = memoized(2, 3); // Returns cached result
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function memoize<T extends (...args: unknown[]) => unknown>(fn: T, options?: MemoizeOptions): T;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a weak memoized version of a function
|
|
158
|
+
*
|
|
159
|
+
* Uses WeakMap for automatic garbage collection. Useful when cache keys
|
|
160
|
+
* are objects that may be garbage collected.
|
|
161
|
+
*
|
|
162
|
+
* @param fn - Function to memoize
|
|
163
|
+
* @returns Memoized function
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* const processObject = (obj: { id: number }) => {
|
|
168
|
+
* return obj.id * 2;
|
|
169
|
+
* };
|
|
170
|
+
*
|
|
171
|
+
* const memoized = weakMemoize(processObject);
|
|
172
|
+
* const result = memoized({ id: 5 });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function weakMemoize<T extends (arg: object) => unknown>(fn: T): T;
|
|
176
|
+
/**
|
|
177
|
+
* Debounce utility for rate limiting function calls
|
|
178
|
+
*
|
|
179
|
+
* Ensures a function is only called after a specified delay since
|
|
180
|
+
* the last invocation. Useful for API calls and user input handling.
|
|
181
|
+
*
|
|
182
|
+
* @param fn - Function to debounce
|
|
183
|
+
* @param delay - Delay in milliseconds
|
|
184
|
+
* @returns Debounced function
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const search = (query: string) => {
|
|
189
|
+
* console.log(`Searching for: ${query}`);
|
|
190
|
+
* };
|
|
191
|
+
*
|
|
192
|
+
* const debouncedSearch = debounce(search, 300);
|
|
193
|
+
* debouncedSearch('a'); // Not called
|
|
194
|
+
* debouncedSearch('ab'); // Not called
|
|
195
|
+
* debouncedSearch('abc'); // Called after 300ms
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function debounce<T extends (...args: unknown[]) => unknown>(fn: T, delay: number): T & {
|
|
199
|
+
cancel: () => void;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Throttle utility for rate limiting function calls
|
|
203
|
+
*
|
|
204
|
+
* Ensures a function is called at most once per specified interval.
|
|
205
|
+
* Unlike debounce, throttle guarantees execution at regular intervals.
|
|
206
|
+
*
|
|
207
|
+
* @param fn - Function to throttle
|
|
208
|
+
* @param interval - Interval in milliseconds
|
|
209
|
+
* @returns Throttled function
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const onScroll = () => {
|
|
214
|
+
* console.log('Scrolled');
|
|
215
|
+
* };
|
|
216
|
+
*
|
|
217
|
+
* const throttledScroll = throttle(onScroll, 100);
|
|
218
|
+
* // Will be called at most once every 100ms
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare function throttle<T extends (...args: unknown[]) => unknown>(fn: T, interval: number): T;
|
|
222
|
+
|
|
223
|
+
export { BridgeConfiguration, GeneratedFile, GenerationContext, NextJsCodeGenerator, debounce, isArray, isDefined, isError, isFunction, isNonEmptyString, isNumber, isObject, isPromise, isString, isValidFilePath, isValidUrl, memoize, throttle, weakMemoize };
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BridgeConfiguration,
|
|
2
|
-
export { A as ApiConfiguration,
|
|
1
|
+
import { B as BridgeConfiguration, s as GenerationContext, t as GeneratedFile } from './version-checker-DU88tpi8.js';
|
|
2
|
+
export { A as ApiConfiguration, j as ApiEndpointDefinition, l as ApiResponseDefinition, f as AuthConfiguration, a as BridgeCore, I as BridgeError, b as BridgeLogger, v as BridgePlugin, g as CacheConfiguration, q as CacheStrategy, C as ConfigurationLoader, D as DevelopmentConfiguration, E as EndpointMetadata, u as FileMetadata, F as FileSystemManager, e as FrameworkConfiguration, G as GenerationConfiguration, M as GenerationError, H as HttpMethod, L as LogLevel, h as OpenApiDocument, O as OpenApiSchemaParser, k as ParameterDefinition, i as ParsedApiSchema, P as PluginConfiguration, r as RateLimit, R as RequestBodyDefinition, S as SchemaDefinition, o as SchemaMetadata, K as SchemaParseError, p as SecurityRequirement, m as SecuritySchemeDefinition, n as ServerDefinition, J as ValidationError, V as VersionChecker, N as VersionInfo, d as checkAndNotifyUpdates, c as createBridgeVersionChecker } from './version-checker-DU88tpi8.js';
|
|
3
3
|
import 'openapi-types';
|
|
4
4
|
import 'zod';
|
|
5
5
|
|
|
@@ -12,4 +12,212 @@ declare class NextJsCodeGenerator {
|
|
|
12
12
|
private generateSafeActionClient;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Type guard utilities for runtime type checking
|
|
17
|
+
*
|
|
18
|
+
* These utilities provide type-safe runtime checks similar to libraries like Apollo Client.
|
|
19
|
+
* They ensure type safety at runtime and improve developer experience.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Type guard to check if a value is defined (not null or undefined)
|
|
23
|
+
*
|
|
24
|
+
* @param value - Value to check
|
|
25
|
+
* @returns True if value is defined
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const value: string | undefined = getValue();
|
|
30
|
+
* if (isDefined(value)) {
|
|
31
|
+
* // value is now string
|
|
32
|
+
* console.log(value.toUpperCase());
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard to check if a value is a string
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value to check
|
|
41
|
+
* @returns True if value is a string
|
|
42
|
+
*/
|
|
43
|
+
declare function isString(value: unknown): value is string;
|
|
44
|
+
/**
|
|
45
|
+
* Type guard to check if a value is a number
|
|
46
|
+
*
|
|
47
|
+
* @param value - Value to check
|
|
48
|
+
* @returns True if value is a number
|
|
49
|
+
*/
|
|
50
|
+
declare function isNumber(value: unknown): value is number;
|
|
51
|
+
/**
|
|
52
|
+
* Type guard to check if a value is an object (and not null)
|
|
53
|
+
*
|
|
54
|
+
* @param value - Value to check
|
|
55
|
+
* @returns True if value is an object
|
|
56
|
+
*/
|
|
57
|
+
declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
58
|
+
/**
|
|
59
|
+
* Type guard to check if a value is an array
|
|
60
|
+
*
|
|
61
|
+
* @param value - Value to check
|
|
62
|
+
* @returns True if value is an array
|
|
63
|
+
*/
|
|
64
|
+
declare function isArray<T>(value: unknown): value is T[];
|
|
65
|
+
/**
|
|
66
|
+
* Type guard to check if a value is a function
|
|
67
|
+
*
|
|
68
|
+
* @param value - Value to check
|
|
69
|
+
* @returns True if value is a function
|
|
70
|
+
*/
|
|
71
|
+
declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
|
|
72
|
+
/**
|
|
73
|
+
* Type guard to check if an error is an Error instance
|
|
74
|
+
*
|
|
75
|
+
* @param error - Error to check
|
|
76
|
+
* @returns True if error is an Error instance
|
|
77
|
+
*/
|
|
78
|
+
declare function isError(error: unknown): error is Error;
|
|
79
|
+
/**
|
|
80
|
+
* Type guard to check if a value is a Promise
|
|
81
|
+
*
|
|
82
|
+
* @param value - Value to check
|
|
83
|
+
* @returns True if value is a Promise
|
|
84
|
+
*/
|
|
85
|
+
declare function isPromise<T>(value: unknown): value is Promise<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Type guard to check if a value is a non-empty string
|
|
88
|
+
*
|
|
89
|
+
* @param value - Value to check
|
|
90
|
+
* @returns True if value is a non-empty string
|
|
91
|
+
*/
|
|
92
|
+
declare function isNonEmptyString(value: unknown): value is string;
|
|
93
|
+
/**
|
|
94
|
+
* Type guard to check if a value is a valid URL string
|
|
95
|
+
*
|
|
96
|
+
* @param value - Value to check
|
|
97
|
+
* @returns True if value is a valid URL
|
|
98
|
+
*/
|
|
99
|
+
declare function isValidUrl(value: unknown): value is string;
|
|
100
|
+
/**
|
|
101
|
+
* Type guard to check if a value is a valid file path
|
|
102
|
+
*
|
|
103
|
+
* @param value - Value to check
|
|
104
|
+
* @returns True if value appears to be a valid file path
|
|
105
|
+
*/
|
|
106
|
+
declare function isValidFilePath(value: unknown): value is string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Memoization utilities for performance optimization
|
|
110
|
+
*
|
|
111
|
+
* Similar to Apollo Client's caching strategies, these utilities provide
|
|
112
|
+
* efficient memoization for expensive computations.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* Memoization options
|
|
116
|
+
*/
|
|
117
|
+
interface MemoizeOptions {
|
|
118
|
+
/**
|
|
119
|
+
* Time to live in milliseconds
|
|
120
|
+
* If not provided, cache never expires
|
|
121
|
+
*/
|
|
122
|
+
ttl?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Maximum cache size
|
|
125
|
+
* If exceeded, oldest entries are removed
|
|
126
|
+
*/
|
|
127
|
+
maxSize?: number;
|
|
128
|
+
/**
|
|
129
|
+
* Custom key generator function
|
|
130
|
+
*/
|
|
131
|
+
keyGenerator?: (...args: unknown[]) => string;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Creates a memoized version of a function
|
|
135
|
+
*
|
|
136
|
+
* Caches function results based on arguments. Similar to Apollo Client's
|
|
137
|
+
* caching mechanism but for general-purpose functions.
|
|
138
|
+
*
|
|
139
|
+
* @param fn - Function to memoize
|
|
140
|
+
* @param options - Memoization options
|
|
141
|
+
* @returns Memoized function
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const expensiveFunction = (x: number, y: number) => {
|
|
146
|
+
* // Expensive computation
|
|
147
|
+
* return x * y;
|
|
148
|
+
* };
|
|
149
|
+
*
|
|
150
|
+
* const memoized = memoize(expensiveFunction, { ttl: 5000 });
|
|
151
|
+
* const result1 = memoized(2, 3); // Computes
|
|
152
|
+
* const result2 = memoized(2, 3); // Returns cached result
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function memoize<T extends (...args: unknown[]) => unknown>(fn: T, options?: MemoizeOptions): T;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a weak memoized version of a function
|
|
158
|
+
*
|
|
159
|
+
* Uses WeakMap for automatic garbage collection. Useful when cache keys
|
|
160
|
+
* are objects that may be garbage collected.
|
|
161
|
+
*
|
|
162
|
+
* @param fn - Function to memoize
|
|
163
|
+
* @returns Memoized function
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* const processObject = (obj: { id: number }) => {
|
|
168
|
+
* return obj.id * 2;
|
|
169
|
+
* };
|
|
170
|
+
*
|
|
171
|
+
* const memoized = weakMemoize(processObject);
|
|
172
|
+
* const result = memoized({ id: 5 });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function weakMemoize<T extends (arg: object) => unknown>(fn: T): T;
|
|
176
|
+
/**
|
|
177
|
+
* Debounce utility for rate limiting function calls
|
|
178
|
+
*
|
|
179
|
+
* Ensures a function is only called after a specified delay since
|
|
180
|
+
* the last invocation. Useful for API calls and user input handling.
|
|
181
|
+
*
|
|
182
|
+
* @param fn - Function to debounce
|
|
183
|
+
* @param delay - Delay in milliseconds
|
|
184
|
+
* @returns Debounced function
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const search = (query: string) => {
|
|
189
|
+
* console.log(`Searching for: ${query}`);
|
|
190
|
+
* };
|
|
191
|
+
*
|
|
192
|
+
* const debouncedSearch = debounce(search, 300);
|
|
193
|
+
* debouncedSearch('a'); // Not called
|
|
194
|
+
* debouncedSearch('ab'); // Not called
|
|
195
|
+
* debouncedSearch('abc'); // Called after 300ms
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function debounce<T extends (...args: unknown[]) => unknown>(fn: T, delay: number): T & {
|
|
199
|
+
cancel: () => void;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Throttle utility for rate limiting function calls
|
|
203
|
+
*
|
|
204
|
+
* Ensures a function is called at most once per specified interval.
|
|
205
|
+
* Unlike debounce, throttle guarantees execution at regular intervals.
|
|
206
|
+
*
|
|
207
|
+
* @param fn - Function to throttle
|
|
208
|
+
* @param interval - Interval in milliseconds
|
|
209
|
+
* @returns Throttled function
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const onScroll = () => {
|
|
214
|
+
* console.log('Scrolled');
|
|
215
|
+
* };
|
|
216
|
+
*
|
|
217
|
+
* const throttledScroll = throttle(onScroll, 100);
|
|
218
|
+
* // Will be called at most once every 100ms
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare function throttle<T extends (...args: unknown[]) => unknown>(fn: T, interval: number): T;
|
|
222
|
+
|
|
223
|
+
export { BridgeConfiguration, GeneratedFile, GenerationContext, NextJsCodeGenerator, debounce, isArray, isDefined, isError, isFunction, isNonEmptyString, isNumber, isObject, isPromise, isString, isValidFilePath, isValidUrl, memoize, throttle, weakMemoize };
|
package/dist/lib/index.js
CHANGED
|
@@ -1,3 +1,155 @@
|
|
|
1
|
-
|
|
1
|
+
import { __name } from './chunk-BMHPB646.js';
|
|
2
|
+
export { BridgeCore, BridgeError, BridgeLogger, ConfigurationLoader, FileSystemManager, GenerationError, LogLevel, NextJsCodeGenerator, OpenApiSchemaParser, SchemaParseError, ValidationError, VersionChecker, checkAndNotifyUpdates, createBridgeVersionChecker } from './chunk-BMHPB646.js';
|
|
3
|
+
|
|
4
|
+
// src/utils/type-guards.ts
|
|
5
|
+
function isDefined(value) {
|
|
6
|
+
return value !== null && value !== void 0;
|
|
7
|
+
}
|
|
8
|
+
__name(isDefined, "isDefined");
|
|
9
|
+
function isString(value) {
|
|
10
|
+
return typeof value === "string";
|
|
11
|
+
}
|
|
12
|
+
__name(isString, "isString");
|
|
13
|
+
function isNumber(value) {
|
|
14
|
+
return typeof value === "number" && !Number.isNaN(value);
|
|
15
|
+
}
|
|
16
|
+
__name(isNumber, "isNumber");
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
__name(isObject, "isObject");
|
|
21
|
+
function isArray(value) {
|
|
22
|
+
return Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
__name(isArray, "isArray");
|
|
25
|
+
function isFunction(value) {
|
|
26
|
+
return typeof value === "function";
|
|
27
|
+
}
|
|
28
|
+
__name(isFunction, "isFunction");
|
|
29
|
+
function isError(error) {
|
|
30
|
+
return error instanceof Error;
|
|
31
|
+
}
|
|
32
|
+
__name(isError, "isError");
|
|
33
|
+
function isPromise(value) {
|
|
34
|
+
return value instanceof Promise;
|
|
35
|
+
}
|
|
36
|
+
__name(isPromise, "isPromise");
|
|
37
|
+
function isNonEmptyString(value) {
|
|
38
|
+
return isString(value) && value.trim().length > 0;
|
|
39
|
+
}
|
|
40
|
+
__name(isNonEmptyString, "isNonEmptyString");
|
|
41
|
+
function isValidUrl(value) {
|
|
42
|
+
if (!isString(value)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
new URL(value);
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
__name(isValidUrl, "isValidUrl");
|
|
53
|
+
function isValidFilePath(value) {
|
|
54
|
+
if (!isString(value)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return value.length > 0 && !value.includes("\0");
|
|
58
|
+
}
|
|
59
|
+
__name(isValidFilePath, "isValidFilePath");
|
|
60
|
+
|
|
61
|
+
// src/utils/memoize.ts
|
|
62
|
+
function memoize(fn, options = {}) {
|
|
63
|
+
const cache = /* @__PURE__ */ new Map();
|
|
64
|
+
const { ttl, maxSize = 100, keyGenerator } = options;
|
|
65
|
+
const memoized = /* @__PURE__ */ __name((...args) => {
|
|
66
|
+
const key = keyGenerator ? keyGenerator(...args) : JSON.stringify(args);
|
|
67
|
+
const entry = cache.get(key);
|
|
68
|
+
const now = Date.now();
|
|
69
|
+
if (entry) {
|
|
70
|
+
if (ttl && entry.expiresAt <= now) {
|
|
71
|
+
cache.delete(key);
|
|
72
|
+
} else {
|
|
73
|
+
return entry.value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const value = fn(...args);
|
|
77
|
+
if (cache.size >= maxSize) {
|
|
78
|
+
const firstKey = cache.keys().next().value;
|
|
79
|
+
if (firstKey) {
|
|
80
|
+
cache.delete(firstKey);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
cache.set(key, {
|
|
84
|
+
value,
|
|
85
|
+
expiresAt: ttl ? now + ttl : Number.MAX_SAFE_INTEGER
|
|
86
|
+
});
|
|
87
|
+
return value;
|
|
88
|
+
}, "memoized");
|
|
89
|
+
memoized.clearCache = () => {
|
|
90
|
+
cache.clear();
|
|
91
|
+
};
|
|
92
|
+
memoized.getCacheSize = () => {
|
|
93
|
+
return cache.size;
|
|
94
|
+
};
|
|
95
|
+
return memoized;
|
|
96
|
+
}
|
|
97
|
+
__name(memoize, "memoize");
|
|
98
|
+
function weakMemoize(fn) {
|
|
99
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
100
|
+
return (arg) => {
|
|
101
|
+
if (cache.has(arg)) {
|
|
102
|
+
return cache.get(arg);
|
|
103
|
+
}
|
|
104
|
+
const value = fn(arg);
|
|
105
|
+
cache.set(arg, value);
|
|
106
|
+
return value;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
__name(weakMemoize, "weakMemoize");
|
|
110
|
+
function debounce(fn, delay) {
|
|
111
|
+
let timeoutId = null;
|
|
112
|
+
const debounced = /* @__PURE__ */ __name((...args) => {
|
|
113
|
+
if (timeoutId) {
|
|
114
|
+
clearTimeout(timeoutId);
|
|
115
|
+
}
|
|
116
|
+
timeoutId = setTimeout(() => {
|
|
117
|
+
fn(...args);
|
|
118
|
+
timeoutId = null;
|
|
119
|
+
}, delay);
|
|
120
|
+
}, "debounced");
|
|
121
|
+
debounced.cancel = () => {
|
|
122
|
+
if (timeoutId) {
|
|
123
|
+
clearTimeout(timeoutId);
|
|
124
|
+
timeoutId = null;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
return debounced;
|
|
128
|
+
}
|
|
129
|
+
__name(debounce, "debounce");
|
|
130
|
+
function throttle(fn, interval) {
|
|
131
|
+
let lastCall = 0;
|
|
132
|
+
let timeoutId = null;
|
|
133
|
+
return (...args) => {
|
|
134
|
+
const now = Date.now();
|
|
135
|
+
const timeSinceLastCall = now - lastCall;
|
|
136
|
+
if (timeSinceLastCall >= interval) {
|
|
137
|
+
lastCall = now;
|
|
138
|
+
fn(...args);
|
|
139
|
+
} else {
|
|
140
|
+
if (timeoutId) {
|
|
141
|
+
clearTimeout(timeoutId);
|
|
142
|
+
}
|
|
143
|
+
timeoutId = setTimeout(() => {
|
|
144
|
+
lastCall = Date.now();
|
|
145
|
+
fn(...args);
|
|
146
|
+
timeoutId = null;
|
|
147
|
+
}, interval - timeSinceLastCall);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
__name(throttle, "throttle");
|
|
152
|
+
|
|
153
|
+
export { debounce, isArray, isDefined, isError, isFunction, isNonEmptyString, isNumber, isObject, isPromise, isString, isValidFilePath, isValidUrl, memoize, throttle, weakMemoize };
|
|
2
154
|
//# sourceMappingURL=index.js.map
|
|
3
155
|
//# sourceMappingURL=index.js.map
|
package/dist/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
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G,IAAI,CAAA;AACV,QAAA,SAAA,GAAY,IAAA;AAAA,MACd,CAAA,EAAG,WAAW,iBAAiB,CAAA;AAAA,IACjC;AAAA,EACF,CAAA;AACF;AA1BgB,MAAA,CAAA,QAAA,EAAA,UAAA,CAAA","file":"index.js","sourcesContent":["/**\r\n * Type guard utilities for runtime type checking\r\n * \r\n * These utilities provide type-safe runtime checks similar to libraries like Apollo Client.\r\n * They ensure type safety at runtime and improve developer experience.\r\n */\r\n\r\n/**\r\n * Type guard to check if a value is defined (not null or undefined)\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is defined\r\n * \r\n * @example\r\n * ```typescript\r\n * const value: string | undefined = getValue();\r\n * if (isDefined(value)) {\r\n * // value is now string\r\n * console.log(value.toUpperCase());\r\n * }\r\n * ```\r\n */\r\nexport function isDefined<T>(value: T | null | undefined): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a string\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a string\r\n */\r\nexport function isString(value: unknown): value is string {\r\n return typeof value === 'string';\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a number\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a number\r\n */\r\nexport function isNumber(value: unknown): value is number {\r\n return typeof value === 'number' && !Number.isNaN(value);\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is an object (and not null)\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is an object\r\n */\r\nexport function isObject(value: unknown): value is Record<string, unknown> {\r\n return typeof value === 'object' && value !== null && !Array.isArray(value);\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is an array\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is an array\r\n */\r\nexport function isArray<T>(value: unknown): value is T[] {\r\n return Array.isArray(value);\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a function\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a function\r\n */\r\nexport function isFunction(value: unknown): value is (...args: unknown[]) => unknown {\r\n return typeof value === 'function';\r\n}\r\n\r\n/**\r\n * Type guard to check if an error is an Error instance\r\n * \r\n * @param error - Error to check\r\n * @returns True if error is an Error instance\r\n */\r\nexport function isError(error: unknown): error is Error {\r\n return error instanceof Error;\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a Promise\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a Promise\r\n */\r\nexport function isPromise<T>(value: unknown): value is Promise<T> {\r\n return value instanceof Promise;\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a non-empty string\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a non-empty string\r\n */\r\nexport function isNonEmptyString(value: unknown): value is string {\r\n return isString(value) && value.trim().length > 0;\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a valid URL string\r\n * \r\n * @param value - Value to check\r\n * @returns True if value is a valid URL\r\n */\r\nexport function isValidUrl(value: unknown): value is string {\r\n if (!isString(value)) {\r\n return false;\r\n }\r\n \r\n try {\r\n new URL(value);\r\n return true;\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a valid file path\r\n * \r\n * @param value - Value to check\r\n * @returns True if value appears to be a valid file path\r\n */\r\nexport function isValidFilePath(value: unknown): value is string {\r\n if (!isString(value)) {\r\n return false;\r\n }\r\n \r\n // Basic validation - can be enhanced based on OS\r\n return value.length > 0 && !value.includes('\\0');\r\n}\r\n\r\n","/**\r\n * Memoization utilities for performance optimization\r\n * \r\n * Similar to Apollo Client's caching strategies, these utilities provide\r\n * efficient memoization for expensive computations.\r\n */\r\n\r\n/**\r\n * Cache entry with expiration support\r\n */\r\ninterface CacheEntry<T> {\r\n value: T;\r\n expiresAt: number;\r\n}\r\n\r\n/**\r\n * Memoization options\r\n */\r\ninterface MemoizeOptions {\r\n /**\r\n * Time to live in milliseconds\r\n * If not provided, cache never expires\r\n */\r\n ttl?: number;\r\n \r\n /**\r\n * Maximum cache size\r\n * If exceeded, oldest entries are removed\r\n */\r\n maxSize?: number;\r\n \r\n /**\r\n * Custom key generator function\r\n */\r\n keyGenerator?: (...args: unknown[]) => string;\r\n}\r\n\r\n/**\r\n * Creates a memoized version of a function\r\n * \r\n * Caches function results based on arguments. Similar to Apollo Client's\r\n * caching mechanism but for general-purpose functions.\r\n * \r\n * @param fn - Function to memoize\r\n * @param options - Memoization options\r\n * @returns Memoized function\r\n * \r\n * @example\r\n * ```typescript\r\n * const expensiveFunction = (x: number, y: number) => {\r\n * // Expensive computation\r\n * return x * y;\r\n * };\r\n * \r\n * const memoized = memoize(expensiveFunction, { ttl: 5000 });\r\n * const result1 = memoized(2, 3); // Computes\r\n * const result2 = memoized(2, 3); // Returns cached result\r\n * ```\r\n */\r\nexport function memoize<T extends (...args: unknown[]) => unknown>(\r\n fn: T,\r\n options: MemoizeOptions = {}\r\n): T {\r\n const cache = new Map<string, CacheEntry<ReturnType<T>>>();\r\n const { ttl, maxSize = 100, keyGenerator } = options;\r\n\r\n const memoized = ((...args: Parameters<T>): ReturnType<T> => {\r\n // Generate cache key\r\n const key = keyGenerator \r\n ? keyGenerator(...args)\r\n : JSON.stringify(args);\r\n\r\n // Check cache\r\n const entry = cache.get(key);\r\n const now = Date.now();\r\n\r\n if (entry) {\r\n // Check if expired\r\n if (ttl && entry.expiresAt <= now) {\r\n cache.delete(key);\r\n } else {\r\n return entry.value;\r\n }\r\n }\r\n\r\n // Compute value\r\n const value = fn(...args) as ReturnType<T>;\r\n\r\n // Store in cache\r\n if (cache.size >= maxSize) {\r\n // Remove oldest entry (first in Map)\r\n const firstKey = cache.keys().next().value;\r\n if (firstKey) {\r\n cache.delete(firstKey);\r\n }\r\n }\r\n\r\n cache.set(key, {\r\n value,\r\n expiresAt: ttl ? now + ttl : Number.MAX_SAFE_INTEGER,\r\n });\r\n\r\n return value;\r\n }) as T;\r\n\r\n // Add cache management methods\r\n (memoized as T & { clearCache: () => void }).clearCache = () => {\r\n cache.clear();\r\n };\r\n\r\n (memoized as T & { getCacheSize: () => number }).getCacheSize = () => {\r\n return cache.size;\r\n };\r\n\r\n return memoized;\r\n}\r\n\r\n/**\r\n * Creates a weak memoized version of a function\r\n * \r\n * Uses WeakMap for automatic garbage collection. Useful when cache keys\r\n * are objects that may be garbage collected.\r\n * \r\n * @param fn - Function to memoize\r\n * @returns Memoized function\r\n * \r\n * @example\r\n * ```typescript\r\n * const processObject = (obj: { id: number }) => {\r\n * return obj.id * 2;\r\n * };\r\n * \r\n * const memoized = weakMemoize(processObject);\r\n * const result = memoized({ id: 5 });\r\n * ```\r\n */\r\nexport function weakMemoize<T extends (arg: object) => unknown>(\r\n fn: T\r\n): T {\r\n const cache = new WeakMap<object, ReturnType<T>>();\r\n\r\n return ((arg: Parameters<T>[0]): ReturnType<T> => {\r\n if (cache.has(arg)) {\r\n return cache.get(arg)!;\r\n }\r\n\r\n const value = fn(arg) as ReturnType<T>;\r\n cache.set(arg, value);\r\n return value;\r\n }) as T;\r\n}\r\n\r\n/**\r\n * Debounce utility for rate limiting function calls\r\n * \r\n * Ensures a function is only called after a specified delay since\r\n * the last invocation. Useful for API calls and user input handling.\r\n * \r\n * @param fn - Function to debounce\r\n * @param delay - Delay in milliseconds\r\n * @returns Debounced function\r\n * \r\n * @example\r\n * ```typescript\r\n * const search = (query: string) => {\r\n * console.log(`Searching for: ${query}`);\r\n * };\r\n * \r\n * const debouncedSearch = debounce(search, 300);\r\n * debouncedSearch('a'); // Not called\r\n * debouncedSearch('ab'); // Not called\r\n * debouncedSearch('abc'); // Called after 300ms\r\n * ```\r\n */\r\nexport function debounce<T extends (...args: unknown[]) => unknown>(\r\n fn: T,\r\n delay: number\r\n): T & { cancel: () => void } {\r\n let timeoutId: NodeJS.Timeout | null = null;\r\n\r\n const debounced = ((...args: Parameters<T>) => {\r\n if (timeoutId) {\r\n clearTimeout(timeoutId);\r\n }\r\n\r\n timeoutId = setTimeout(() => {\r\n fn(...args);\r\n timeoutId = null;\r\n }, delay);\r\n }) as T & { cancel: () => void };\r\n\r\n debounced.cancel = () => {\r\n if (timeoutId) {\r\n clearTimeout(timeoutId);\r\n timeoutId = null;\r\n }\r\n };\r\n\r\n return debounced;\r\n}\r\n\r\n/**\r\n * Throttle utility for rate limiting function calls\r\n * \r\n * Ensures a function is called at most once per specified interval.\r\n * Unlike debounce, throttle guarantees execution at regular intervals.\r\n * \r\n * @param fn - Function to throttle\r\n * @param interval - Interval in milliseconds\r\n * @returns Throttled function\r\n * \r\n * @example\r\n * ```typescript\r\n * const onScroll = () => {\r\n * console.log('Scrolled');\r\n * };\r\n * \r\n * const throttledScroll = throttle(onScroll, 100);\r\n * // Will be called at most once every 100ms\r\n * ```\r\n */\r\nexport function throttle<T extends (...args: unknown[]) => unknown>(\r\n fn: T,\r\n interval: number\r\n): T {\r\n let lastCall = 0;\r\n let timeoutId: NodeJS.Timeout | null = null;\r\n\r\n return ((...args: Parameters<T>) => {\r\n const now = Date.now();\r\n const timeSinceLastCall = now - lastCall;\r\n\r\n if (timeSinceLastCall >= interval) {\r\n lastCall = now;\r\n fn(...args);\r\n } else {\r\n if (timeoutId) {\r\n clearTimeout(timeoutId);\r\n }\r\n\r\n timeoutId = setTimeout(() => {\r\n lastCall = Date.now();\r\n fn(...args);\r\n timeoutId = null;\r\n }, interval - timeSinceLastCall);\r\n }\r\n }) as T;\r\n}\r\n\r\n"]}
|