functype 0.8.61 → 0.8.62

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.
Files changed (81) hide show
  1. package/dist/Map-BNxKlujn.mjs +46 -0
  2. package/dist/Map-BNxKlujn.mjs.map +1 -0
  3. package/dist/branded/Brand.d.ts +46 -0
  4. package/dist/branded/index.d.ts +1 -48
  5. package/dist/branded/index.mjs +23 -2
  6. package/dist/branded/index.mjs.map +1 -1
  7. package/dist/collections/index.d.ts +8 -0
  8. package/dist/companion/Companion.d.ts +23 -0
  9. package/dist/companion/index.d.ts +1 -0
  10. package/dist/core/base/Base.d.ts +10 -0
  11. package/dist/core/base/index.d.ts +1 -0
  12. package/dist/core/index.d.ts +4 -0
  13. package/dist/core/info/Info.d.ts +26 -0
  14. package/dist/core/info/index.d.ts +1 -0
  15. package/dist/core/task/Task.d.ts +57 -0
  16. package/dist/core/task/index.d.ts +1 -0
  17. package/dist/core/throwable/Throwable.d.ts +18 -0
  18. package/dist/core/throwable/index.d.ts +1 -0
  19. package/dist/either/Either.d.ts +57 -0
  20. package/dist/either/index.d.ts +1 -3
  21. package/dist/either/index.mjs +13 -2
  22. package/dist/either/index.mjs.map +1 -1
  23. package/dist/error/ParseError.d.ts +6 -0
  24. package/dist/error/index.d.ts +1 -0
  25. package/dist/fpromise/FPromise.d.ts +369 -0
  26. package/dist/fpromise/index.d.ts +2 -3
  27. package/dist/fpromise/index.mjs +6 -2
  28. package/dist/fpromise/index.mjs.map +1 -1
  29. package/dist/fpromise/retry.d.ts +106 -0
  30. package/dist/functor/index.d.ts +18 -0
  31. package/dist/hkt/index.d.ts +49 -0
  32. package/dist/identity/Identity.d.ts +5 -0
  33. package/dist/identity/index.d.ts +1 -0
  34. package/dist/index-CgMYHgdG.mjs +1286 -0
  35. package/dist/index-CgMYHgdG.mjs.map +1 -0
  36. package/dist/index.d.ts +28 -3
  37. package/dist/index.mjs +174 -2
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/iterable/index.d.ts +25 -0
  40. package/dist/list/List.d.ts +45 -0
  41. package/dist/list/index.d.ts +1 -3
  42. package/dist/list/index.mjs +5 -2
  43. package/dist/list/index.mjs.map +1 -1
  44. package/dist/map/Map.d.ts +19 -0
  45. package/dist/map/index.d.ts +2 -3
  46. package/dist/map/index.mjs +7 -2
  47. package/dist/map/index.mjs.map +1 -1
  48. package/dist/map/shim.d.ts +2 -0
  49. package/dist/option/Option.d.ts +164 -0
  50. package/dist/option/index.d.ts +1 -870
  51. package/dist/option/index.mjs +7 -2
  52. package/dist/option/index.mjs.map +1 -1
  53. package/dist/serializable/Serializable.d.ts +15 -0
  54. package/dist/serializable/index.d.ts +1 -0
  55. package/dist/set/Set.d.ts +16 -0
  56. package/dist/set/index.d.ts +2 -3
  57. package/dist/set/index.mjs +5 -2
  58. package/dist/set/index.mjs.map +1 -1
  59. package/dist/set/shim.d.ts +2 -0
  60. package/dist/try/Try.d.ts +20 -0
  61. package/dist/try/index.d.ts +1 -3
  62. package/dist/try/index.mjs +44 -2
  63. package/dist/try/index.mjs.map +1 -1
  64. package/dist/tuple/Tuple.d.ts +11 -0
  65. package/dist/tuple/index.d.ts +1 -1
  66. package/dist/tuple/index.mjs +27 -2
  67. package/dist/tuple/index.mjs.map +1 -1
  68. package/dist/typeable/Typeable.d.ts +10 -0
  69. package/dist/typeable/index.d.ts +1 -0
  70. package/dist/util/index.d.ts +3 -0
  71. package/dist/util/isIterable.d.ts +1 -0
  72. package/dist/valuable/Valuable.d.ts +13 -0
  73. package/dist/valuable/index.d.ts +1 -0
  74. package/package.json +7 -6
  75. package/dist/Tuple-GXgoHfiN.d.ts +0 -54
  76. package/dist/chunk-A3EHNBZM.mjs +0 -2
  77. package/dist/chunk-A3EHNBZM.mjs.map +0 -1
  78. package/dist/chunk-OQWAJKZJ.mjs +0 -2
  79. package/dist/chunk-OQWAJKZJ.mjs.map +0 -1
  80. package/dist/chunk-TQJDL6YW.mjs +0 -2
  81. package/dist/chunk-TQJDL6YW.mjs.map +0 -1
@@ -0,0 +1,369 @@
1
+ import { Either } from '../either/Either';
2
+ import { Type } from '../functor';
3
+ /**
4
+ * Error context information that provides additional metadata about errors.
5
+ * This context is passed to error mapping functions to provide more information
6
+ * about the error that occurred.
7
+ *
8
+ * @property originalError - The original error that was thrown
9
+ * @property stack - The stack trace of the error, if available
10
+ * @property timestamp - The timestamp when the error occurred
11
+ */
12
+ export type ErrorContext = {
13
+ originalError: unknown;
14
+ stack?: string;
15
+ timestamp: number;
16
+ };
17
+ /**
18
+ * FPromise is a functional wrapper around JavaScript's Promise with enhanced error handling.
19
+ * It implements the Functor and AsyncFunctor interfaces, providing map and flatMap operations
20
+ * for functional composition.
21
+ *
22
+ * FPromise adds several features not available in standard Promises:
23
+ * - Generic error typing for better type safety
24
+ * - Error recovery mechanisms (recover, recoverWith, recoverWithF)
25
+ * - Error filtering and categorization
26
+ * - Side effect methods (tap, tapError)
27
+ * - Error context preservation
28
+ * - Conversion to/from Either for more functional error handling
29
+ *
30
+ * @template T - The type of the value that the FPromise resolves to
31
+ * @template E - The type of the error that the FPromise may reject with (defaults to unknown)
32
+ */
33
+ /**
34
+ * FPromise type that defines the function signature and methods
35
+ */
36
+ export interface FPromise<T extends Type, E extends Type = unknown> extends PromiseLike<T> {
37
+ readonly _tag: "FPromise";
38
+ tap: (f: (value: T) => void) => FPromise<T, E>;
39
+ mapError: <E2>(f: (error: E, context: ErrorContext) => E2) => FPromise<T, E2>;
40
+ tapError: (f: (error: E) => void) => FPromise<T, E>;
41
+ recover: (fallback: T) => FPromise<T, never>;
42
+ recoverWith: (f: (error: E) => T) => FPromise<T, never>;
43
+ recoverWithF: <E2>(f: (error: E) => FPromise<T, E2>) => FPromise<T, E2>;
44
+ filterError: <E2 extends E>(predicate: (error: E) => boolean, handler: (error: E) => FPromise<T, E2>) => FPromise<T, E>;
45
+ logError: (logger: (error: E, context: ErrorContext) => void) => FPromise<T, E>;
46
+ toPromise: () => Promise<T>;
47
+ toEither: () => Promise<T>;
48
+ map: <U extends Type>(f: (value: T) => U) => FPromise<U, E>;
49
+ flatMap: <U extends Type>(f: (value: T) => FPromise<U, E> | PromiseLike<U>) => FPromise<U, E>;
50
+ flatMapAsync: <U extends Type>(f: (value: T) => PromiseLike<U>) => Promise<U>;
51
+ }
52
+ /**
53
+ * Static utility methods for FPromise using the Companion pattern.
54
+ * These methods provide factory functions and utilities for working with FPromises.
55
+ */
56
+ export declare const FPromiseCompanion: {
57
+ /**
58
+ * Creates an FPromise that resolves to the provided value.
59
+ *
60
+ * @template T - The type of the value
61
+ * @template E - The type of the error (defaults to never since this FPromise won't reject)
62
+ * @param value - The value to resolve with
63
+ * @returns An FPromise that resolves to the value
64
+ *
65
+ * @example
66
+ * const promise = FPromise.resolve(42)
67
+ * // promise resolves to 42
68
+ */
69
+ resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
70
+ /**
71
+ * Creates an FPromise that rejects with the provided reason.
72
+ *
73
+ * @template T - The type of the value (which will never be produced)
74
+ * @template E - The type of the error
75
+ * @param reason - The reason for rejection
76
+ * @returns An FPromise that rejects with the reason
77
+ *
78
+ * @example
79
+ * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
80
+ * // promise rejects with Error("Something went wrong")
81
+ */
82
+ reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
83
+ /**
84
+ * Creates an FPromise from a regular Promise.
85
+ *
86
+ * @template T - The type of the value
87
+ * @template E - The type of the error
88
+ * @param promise - The Promise to convert
89
+ * @returns An FPromise that resolves or rejects with the same value or error
90
+ *
91
+ * @example
92
+ * const promise = FPromise.from(fetch("https://api.example.com/data"))
93
+ * // promise is an FPromise that resolves or rejects based on the fetch result
94
+ */
95
+ from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
96
+ /**
97
+ * Creates an FPromise from an Either.
98
+ * If the Either is a Right, the FPromise resolves with the Right value.
99
+ * If the Either is a Left, the FPromise rejects with the Left value.
100
+ *
101
+ * @template L - The type of the Left value (error)
102
+ * @template R - The type of the Right value (success)
103
+ * @param either - The Either to convert
104
+ * @returns An FPromise that resolves or rejects based on the Either
105
+ *
106
+ * @example
107
+ * const either = Right<Error, number>(42)
108
+ * const promise = FPromise.fromEither(either)
109
+ * // promise resolves to 42
110
+ */
111
+ fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
112
+ /**
113
+ * Runs multiple FPromises in parallel and returns an array of results.
114
+ * Similar to Promise.all, this will reject if any of the promises reject.
115
+ *
116
+ * @template T - The type of the values
117
+ * @template E - The type of the error
118
+ * @param promises - An array of FPromises, Promises, or values
119
+ * @returns An FPromise that resolves to an array of results
120
+ *
121
+ * @example
122
+ * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
123
+ * const result = await FPromise.all(promises).toPromise()
124
+ * // result is [1, 2, 3]
125
+ */
126
+ all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
127
+ /**
128
+ * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
129
+ * This will always resolve, never reject.
130
+ *
131
+ * @template T - The type of the values
132
+ * @template E - The type of the errors
133
+ * @param promises - An array of FPromises or Promises
134
+ * @returns An FPromise that resolves to an array of Either results
135
+ *
136
+ * @example
137
+ * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
138
+ * const result = await FPromise.allSettled(promises).toPromise()
139
+ * // result is [Right(1), Left(Error("Failed"))]
140
+ */
141
+ allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
142
+ /**
143
+ * Like Promise.race, returns the first promise to settle (either resolve or reject).
144
+ *
145
+ * @template T - The type of the values
146
+ * @template E - The type of the errors
147
+ * @param promises - An array of FPromises or Promises
148
+ * @returns An FPromise that resolves or rejects with the result of the first promise to settle
149
+ *
150
+ * @example
151
+ * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
152
+ * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
153
+ * const result = await FPromise.race([slow, fast]).toPromise()
154
+ * // result is 2
155
+ */
156
+ race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
157
+ /**
158
+ * Like Promise.any, returns the first promise to fulfill.
159
+ * This will only reject if all promises reject.
160
+ *
161
+ * @template T - The type of the values
162
+ * @template E - The type of the errors
163
+ * @param promises - An array of FPromises or Promises
164
+ * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
165
+ *
166
+ * @example
167
+ * const promises = [
168
+ * FPromise.reject<number, Error>(new Error("First failed")),
169
+ * FPromise.resolve(2),
170
+ * FPromise.reject<number, Error>(new Error("Third failed"))
171
+ * ]
172
+ * const result = await FPromise.any(promises).toPromise()
173
+ * // result is 2
174
+ */
175
+ any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
176
+ /**
177
+ * Retries an operation with exponential backoff.
178
+ * This is useful for operations that may fail temporarily, such as network requests.
179
+ *
180
+ * @template T - The type of the value
181
+ * @template E - The type of the error
182
+ * @param operation - A function that returns an FPromise
183
+ * @param options - Configuration options for the retry
184
+ * @param options.maxRetries - Maximum number of retry attempts
185
+ * @param options.baseDelay - Base delay in milliseconds (default: 100)
186
+ * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
187
+ * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
188
+ *
189
+ * @example
190
+ * const operation = () => {
191
+ * if (Math.random() > 0.8) {
192
+ * return FPromise.resolve("Success!")
193
+ * }
194
+ * return FPromise.reject<string, Error>(new Error("Temporary failure"))
195
+ * }
196
+ *
197
+ * const result = await FPromise.retryWithBackoff(operation, {
198
+ * maxRetries: 3,
199
+ * baseDelay: 100,
200
+ * shouldRetry: (error) => error.message === "Temporary failure"
201
+ * }).toPromise()
202
+ */
203
+ retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
204
+ maxRetries: number;
205
+ baseDelay?: number;
206
+ shouldRetry?: (error: E, attempt: number) => boolean;
207
+ }) => FPromise<T, E>;
208
+ };
209
+ /**
210
+ * Creates an FPromise from an executor function.
211
+ *
212
+ * @template T - The type of the value that the FPromise resolves to
213
+ * @template E - The type of the error that the FPromise may reject with
214
+ * @param executor - A function that receives resolve and reject functions
215
+ * @returns An FPromise instance
216
+ */
217
+ export declare const FPromise: (<T extends Type, E = unknown>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: E) => void) => void) => FPromise<T, E>) & {
218
+ /**
219
+ * Creates an FPromise that resolves to the provided value.
220
+ *
221
+ * @template T - The type of the value
222
+ * @template E - The type of the error (defaults to never since this FPromise won't reject)
223
+ * @param value - The value to resolve with
224
+ * @returns An FPromise that resolves to the value
225
+ *
226
+ * @example
227
+ * const promise = FPromise.resolve(42)
228
+ * // promise resolves to 42
229
+ */
230
+ resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
231
+ /**
232
+ * Creates an FPromise that rejects with the provided reason.
233
+ *
234
+ * @template T - The type of the value (which will never be produced)
235
+ * @template E - The type of the error
236
+ * @param reason - The reason for rejection
237
+ * @returns An FPromise that rejects with the reason
238
+ *
239
+ * @example
240
+ * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
241
+ * // promise rejects with Error("Something went wrong")
242
+ */
243
+ reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
244
+ /**
245
+ * Creates an FPromise from a regular Promise.
246
+ *
247
+ * @template T - The type of the value
248
+ * @template E - The type of the error
249
+ * @param promise - The Promise to convert
250
+ * @returns An FPromise that resolves or rejects with the same value or error
251
+ *
252
+ * @example
253
+ * const promise = FPromise.from(fetch("https://api.example.com/data"))
254
+ * // promise is an FPromise that resolves or rejects based on the fetch result
255
+ */
256
+ from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
257
+ /**
258
+ * Creates an FPromise from an Either.
259
+ * If the Either is a Right, the FPromise resolves with the Right value.
260
+ * If the Either is a Left, the FPromise rejects with the Left value.
261
+ *
262
+ * @template L - The type of the Left value (error)
263
+ * @template R - The type of the Right value (success)
264
+ * @param either - The Either to convert
265
+ * @returns An FPromise that resolves or rejects based on the Either
266
+ *
267
+ * @example
268
+ * const either = Right<Error, number>(42)
269
+ * const promise = FPromise.fromEither(either)
270
+ * // promise resolves to 42
271
+ */
272
+ fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
273
+ /**
274
+ * Runs multiple FPromises in parallel and returns an array of results.
275
+ * Similar to Promise.all, this will reject if any of the promises reject.
276
+ *
277
+ * @template T - The type of the values
278
+ * @template E - The type of the error
279
+ * @param promises - An array of FPromises, Promises, or values
280
+ * @returns An FPromise that resolves to an array of results
281
+ *
282
+ * @example
283
+ * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
284
+ * const result = await FPromise.all(promises).toPromise()
285
+ * // result is [1, 2, 3]
286
+ */
287
+ all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
288
+ /**
289
+ * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
290
+ * This will always resolve, never reject.
291
+ *
292
+ * @template T - The type of the values
293
+ * @template E - The type of the errors
294
+ * @param promises - An array of FPromises or Promises
295
+ * @returns An FPromise that resolves to an array of Either results
296
+ *
297
+ * @example
298
+ * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
299
+ * const result = await FPromise.allSettled(promises).toPromise()
300
+ * // result is [Right(1), Left(Error("Failed"))]
301
+ */
302
+ allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
303
+ /**
304
+ * Like Promise.race, returns the first promise to settle (either resolve or reject).
305
+ *
306
+ * @template T - The type of the values
307
+ * @template E - The type of the errors
308
+ * @param promises - An array of FPromises or Promises
309
+ * @returns An FPromise that resolves or rejects with the result of the first promise to settle
310
+ *
311
+ * @example
312
+ * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
313
+ * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
314
+ * const result = await FPromise.race([slow, fast]).toPromise()
315
+ * // result is 2
316
+ */
317
+ race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
318
+ /**
319
+ * Like Promise.any, returns the first promise to fulfill.
320
+ * This will only reject if all promises reject.
321
+ *
322
+ * @template T - The type of the values
323
+ * @template E - The type of the errors
324
+ * @param promises - An array of FPromises or Promises
325
+ * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
326
+ *
327
+ * @example
328
+ * const promises = [
329
+ * FPromise.reject<number, Error>(new Error("First failed")),
330
+ * FPromise.resolve(2),
331
+ * FPromise.reject<number, Error>(new Error("Third failed"))
332
+ * ]
333
+ * const result = await FPromise.any(promises).toPromise()
334
+ * // result is 2
335
+ */
336
+ any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
337
+ /**
338
+ * Retries an operation with exponential backoff.
339
+ * This is useful for operations that may fail temporarily, such as network requests.
340
+ *
341
+ * @template T - The type of the value
342
+ * @template E - The type of the error
343
+ * @param operation - A function that returns an FPromise
344
+ * @param options - Configuration options for the retry
345
+ * @param options.maxRetries - Maximum number of retry attempts
346
+ * @param options.baseDelay - Base delay in milliseconds (default: 100)
347
+ * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
348
+ * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
349
+ *
350
+ * @example
351
+ * const operation = () => {
352
+ * if (Math.random() > 0.8) {
353
+ * return FPromise.resolve("Success!")
354
+ * }
355
+ * return FPromise.reject<string, Error>(new Error("Temporary failure"))
356
+ * }
357
+ *
358
+ * const result = await FPromise.retryWithBackoff(operation, {
359
+ * maxRetries: 3,
360
+ * baseDelay: 100,
361
+ * shouldRetry: (error) => error.message === "Temporary failure"
362
+ * }).toPromise()
363
+ */
364
+ retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
365
+ maxRetries: number;
366
+ baseDelay?: number;
367
+ shouldRetry?: (error: E, attempt: number) => boolean;
368
+ }) => FPromise<T, E>;
369
+ };
@@ -1,3 +1,2 @@
1
- export { f as ErrorContext, F as FPromise, g as FPromiseCompanion } from '../option/index.js';
2
- import '../Tuple-GXgoHfiN.js';
3
- import '../branded/index.js';
1
+ export * from './FPromise';
2
+ export * from './retry';
@@ -1,2 +1,6 @@
1
- export{s as FPromise,r as FPromiseCompanion}from'../chunk-A3EHNBZM.mjs';import'../chunk-OQWAJKZJ.mjs';import'../chunk-TQJDL6YW.mjs';//# sourceMappingURL=index.mjs.map
2
- //# sourceMappingURL=index.mjs.map
1
+ import { F as r, d as s } from "../index-CgMYHgdG.mjs";
2
+ export {
3
+ r as FPromise,
4
+ s as FPromiseCompanion
5
+ };
6
+ //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"index.mjs"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,106 @@
1
+ import { FPromise } from './FPromise';
2
+ /**
3
+ * A utility function to retry an operation multiple times until it succeeds.
4
+ * This function will attempt the operation and retry it up to maxRetries times if it fails.
5
+ * Unlike retryWithBackoff, this function retries immediately without any delay between attempts.
6
+ *
7
+ * @template T - The type of the value that the operation produces
8
+ * @template E - The type of the error that the operation may produce
9
+ * @param operation - A function that returns an FPromise
10
+ * @param maxRetries - Maximum number of retry attempts
11
+ * @returns An FPromise that will be resolved when the operation succeeds or rejected after all retries fail
12
+ *
13
+ * @example
14
+ * const unreliableOperation = () => {
15
+ * if (Math.random() < 0.7) {
16
+ * return FPromise.reject(new Error("Operation failed"));
17
+ * }
18
+ * return FPromise.resolve("Success!");
19
+ * };
20
+ *
21
+ * retry(unreliableOperation, 3)
22
+ * .toPromise()
23
+ * .then(result => console.log(result))
24
+ * .catch(error => console.error("All attempts failed:", error));
25
+ */
26
+ export declare const retry: <T, E = unknown>(operation: () => FPromise<T, E>, maxRetries: number) => FPromise<T, E>;
27
+ /**
28
+ * A version of retry that uses exponential backoff between retry attempts.
29
+ * This is useful for operations that may fail due to rate limiting or temporary network issues.
30
+ * The delay between retries increases exponentially: baseDelay * 2^attempt.
31
+ *
32
+ * @template T - The type of the value that the operation produces
33
+ * @template E - The type of the error that the operation may produce
34
+ * @param operation - A function that returns an FPromise
35
+ * @param maxRetries - Maximum number of retry attempts
36
+ * @param baseDelay - Base delay in milliseconds (default: 100ms)
37
+ * @param shouldRetry - Optional function to determine if a retry should be attempted based on the error
38
+ * @returns An FPromise that will be resolved when the operation succeeds or rejected after all retries fail
39
+ *
40
+ * @example
41
+ * const fetchData = () => {
42
+ * return FPromise.from(fetch('https://api.example.com/data'))
43
+ * .flatMap(response => {
44
+ * if (!response.ok) {
45
+ * return FPromise.reject(new Error(`HTTP error: ${response.status}`));
46
+ * }
47
+ * return FPromise.from(response.json());
48
+ * });
49
+ * };
50
+ *
51
+ * retryWithBackoff(fetchData, 3, 200, (error) => {
52
+ * // Only retry on network errors or 429 (Too Many Requests)
53
+ * return error.name === 'NetworkError' ||
54
+ * (error.message && error.message.includes('HTTP error: 429'));
55
+ * })
56
+ * .toPromise()
57
+ * .then(data => console.log("Data:", data))
58
+ * .catch(error => console.error("Failed to fetch data:", error));
59
+ */
60
+ export declare const retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, maxRetries: number, baseDelay?: number, shouldRetry?: (error: E, attempt: number) => boolean) => FPromise<T, E>;
61
+ /**
62
+ * A highly configurable version of retry that allows for custom delay calculation,
63
+ * conditional retries, and retry event callbacks.
64
+ *
65
+ * @template T - The type of the value that the operation produces
66
+ * @template E - The type of the error that the operation may produce
67
+ * @param operation - A function that returns an FPromise
68
+ * @param options - Configuration options for the retry
69
+ * @param options.maxRetries - Maximum number of retry attempts
70
+ * @param options.delayFn - Optional function to calculate delay between retries (default: no delay)
71
+ * @param options.shouldRetry - Optional function to determine if a retry should be attempted (default: always retry)
72
+ * @param options.onRetry - Optional callback that is called before each retry attempt
73
+ * @returns An FPromise that will be resolved when the operation succeeds or rejected after all retries fail
74
+ *
75
+ * @example
76
+ * const fetchWithAuth = () => {
77
+ * return FPromise.from(fetch('https://api.example.com/protected', {
78
+ * headers: { 'Authorization': `Bearer ${getToken()}` }
79
+ * }))
80
+ * .flatMap(response => {
81
+ * if (response.status === 401) {
82
+ * return FPromise.reject(new AuthError("Unauthorized"));
83
+ * }
84
+ * return FPromise.from(response.json());
85
+ * });
86
+ * };
87
+ *
88
+ * retryWithOptions(fetchWithAuth, {
89
+ * maxRetries: 2,
90
+ * delayFn: (attempt) => 500 * attempt, // Linear backoff: 500ms, 1000ms
91
+ * shouldRetry: (error) => error instanceof AuthError,
92
+ * onRetry: (error, attempt) => {
93
+ * console.log(`Retry attempt ${attempt} after auth error, refreshing token...`);
94
+ * refreshAuthToken();
95
+ * }
96
+ * })
97
+ * .toPromise()
98
+ * .then(data => console.log("Data:", data))
99
+ * .catch(error => console.error("Failed after retries:", error));
100
+ */
101
+ export declare const retryWithOptions: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
102
+ maxRetries: number;
103
+ delayFn?: (attempt: number) => number;
104
+ shouldRetry?: (error: E, attempt: number) => boolean;
105
+ onRetry?: (error: E, attempt: number) => void;
106
+ }) => FPromise<T, E>;
@@ -0,0 +1,18 @@
1
+ export type SingleType = unknown;
2
+ export type ArrayType = SingleType[];
3
+ export type Type = SingleType | ArrayType;
4
+ export type AbstractFunctor<A extends Type> = {
5
+ map(f: (value: A) => Type): AbstractFunctor<Type>;
6
+ flatMap(f: (value: A) => AbstractFunctor<Type>): AbstractFunctor<Type>;
7
+ };
8
+ export type Functor<A extends Type> = AbstractFunctor<A> & {
9
+ map<B extends Type>(f: (value: A) => B): Functor<B>;
10
+ flatMap<B extends Type>(f: (value: A) => Functor<B>): Functor<B>;
11
+ };
12
+ export type AsyncFunctor<A extends Type> = {
13
+ flatMapAsync(f: (value: A) => PromiseLike<AsyncFunctor<A>>): PromiseLike<AsyncFunctor<A>>;
14
+ };
15
+ export type ArrayFunctor<A extends ArrayType> = AbstractFunctor<A> & {
16
+ map<U extends ArrayType>(f: (value: A) => U): ArrayFunctor<U>;
17
+ flatMap<U extends ArrayType>(f: (value: A) => ArrayFunctor<U>): ArrayFunctor<U>;
18
+ };
@@ -0,0 +1,49 @@
1
+ import { Either } from '../either/Either';
2
+ import { Type } from '../functor';
3
+ import { List } from '../list/List';
4
+ import { Option } from '../option/Option';
5
+ import { Try } from '../try/Try';
6
+ /**
7
+ * Type function for representing higher-kinded types
8
+ */
9
+ export type Kind<F, A> = F extends (arg: infer T) => infer R ? R : never;
10
+ /**
11
+ * Type constructors for common Functype data types
12
+ */
13
+ export type OptionKind = <A>(a: A) => Option<A>;
14
+ export type ListKind = <A>(a: A) => List<A>;
15
+ export type EitherKind<E> = <A>(a: A) => Either<E, A>;
16
+ export type TryKind = <A>(a: A) => Try<A>;
17
+ /**
18
+ * Universal type that includes all potential return types from the HKT functions
19
+ * Used to avoid 'any' usage which the linter prohibits
20
+ */
21
+ export type UniversalContainer = Option<unknown> | List<unknown> | Either<unknown, unknown> | Try<unknown>;
22
+ /**
23
+ * HKT provides utilities for working with higher-kinded types
24
+ * This allows writing generic code that works across different
25
+ * container types like Option, List, Either, etc.
26
+ */
27
+ export declare const HKT: {
28
+ (): {
29
+ _type: string;
30
+ map: <F, A, B>(fa: unknown, f: (a: A) => B) => unknown;
31
+ flatten: <F, A>(ffa: unknown) => unknown;
32
+ flatMap: <F, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
33
+ ap: <F, A, B>(ff: unknown, fa: unknown) => unknown;
34
+ sequence: <F, G, A>(fga: unknown) => unknown;
35
+ traverse: <F, G, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
36
+ toString(): string;
37
+ _tag: string;
38
+ };
39
+ map<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => B): unknown;
40
+ flatten<F = unknown, A = unknown>(ffa: unknown): unknown;
41
+ flatMap<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
42
+ ap<F = unknown, A = unknown, B = unknown>(ff: unknown, fa: unknown): unknown;
43
+ sequence<F = unknown, G = unknown, A = unknown>(fga: unknown): unknown;
44
+ traverse<F = unknown, G = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
45
+ isOption: <T extends Type>(value: unknown) => value is Option<T>;
46
+ isList: <T extends Type>(value: unknown) => value is List<T>;
47
+ isEither: <E extends Type, A extends Type>(value: unknown) => value is Either<E, A>;
48
+ isTry: <T extends Type>(value: unknown) => value is Try<T>;
49
+ };
@@ -0,0 +1,5 @@
1
+ export type Identity<T> = {
2
+ id: T;
3
+ isSame?: (other: Identity<T>) => boolean;
4
+ };
5
+ export declare function Identity<T>(value: T): Identity<T>;
@@ -0,0 +1 @@
1
+ export * from './Identity';