@nlozgachev/pipelined 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Task-ChKyH0pF.d.mts → Task-Bd3gXPRQ.d.mts} +1 -1
- package/dist/{Task-BB8Wmc1J.d.ts → Task-BjAkkD6t.d.ts} +1 -1
- package/dist/chunk-2XKWSZEU.mjs +549 -0
- package/dist/chunk-BYWKZLHM.mjs +10 -0
- package/dist/chunk-FAZN3IWZ.mjs +554 -0
- package/dist/chunk-KXAWFKQQ.mjs +242 -0
- package/dist/composition.d.mts +10 -18
- package/dist/composition.d.ts +10 -18
- package/dist/composition.js +116 -10
- package/dist/composition.mjs +29 -106
- package/dist/core.d.mts +215 -120
- package/dist/core.d.ts +215 -120
- package/dist/core.js +67 -31
- package/dist/core.mjs +18 -491
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1563 -0
- package/dist/index.mjs +122 -0
- package/dist/types.mjs +3 -7
- package/dist/utils.d.mts +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.mjs +9 -544
- package/package.json +5 -2
package/dist/core.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { D as Deferred, E as Err, N as None,
|
|
1
|
+
import { a as Option, W as WithValue, b as WithLog, R as Result, c as WithKind, d as WithError, T as Task, e as WithErrors, f as WithFirst, g as WithSecond } from './Task-Bd3gXPRQ.mjs';
|
|
2
|
+
export { D as Deferred, E as Err, N as None, O as Ok, S as Some } from './Task-Bd3gXPRQ.mjs';
|
|
3
3
|
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.mjs';
|
|
4
4
|
|
|
5
5
|
/** Keys of T for which undefined is assignable (i.e. optional fields). */
|
|
@@ -1045,6 +1045,218 @@ declare namespace RemoteData {
|
|
|
1045
1045
|
const toResult: <E>(onNotReady: () => E) => <A>(data: RemoteData<E, A>) => Result<E, A>;
|
|
1046
1046
|
}
|
|
1047
1047
|
|
|
1048
|
+
/**
|
|
1049
|
+
* A Task that can fail with an error of type E or succeed with a value of type A.
|
|
1050
|
+
* Combines async operations with typed error handling.
|
|
1051
|
+
*
|
|
1052
|
+
* @example
|
|
1053
|
+
* ```ts
|
|
1054
|
+
* const fetchUser = (id: string): TaskResult<Error, User> =>
|
|
1055
|
+
* TaskResult.tryCatch(
|
|
1056
|
+
* () => fetch(`/users/${id}`).then(r => r.json()),
|
|
1057
|
+
* (e) => new Error(`Failed to fetch user: ${e}`)
|
|
1058
|
+
* );
|
|
1059
|
+
* ```
|
|
1060
|
+
*/
|
|
1061
|
+
type TaskResult<E, A> = Task<Result<E, A>>;
|
|
1062
|
+
declare namespace TaskResult {
|
|
1063
|
+
/**
|
|
1064
|
+
* Wraps a value in a successful TaskResult.
|
|
1065
|
+
*/
|
|
1066
|
+
const ok: <E, A>(value: A) => TaskResult<E, A>;
|
|
1067
|
+
/**
|
|
1068
|
+
* Creates a failed TaskResult with the given error.
|
|
1069
|
+
*/
|
|
1070
|
+
const err: <E, A>(error: E) => TaskResult<E, A>;
|
|
1071
|
+
/**
|
|
1072
|
+
* Creates a TaskResult from a function that may throw.
|
|
1073
|
+
* Catches any errors and transforms them using the onError function.
|
|
1074
|
+
*
|
|
1075
|
+
* @example
|
|
1076
|
+
* ```ts
|
|
1077
|
+
* const parseJson = (s: string): TaskResult<string, unknown> =>
|
|
1078
|
+
* TaskResult.tryCatch(
|
|
1079
|
+
* async () => JSON.parse(s),
|
|
1080
|
+
* (e) => `Parse error: ${e}`
|
|
1081
|
+
* );
|
|
1082
|
+
* ```
|
|
1083
|
+
*/
|
|
1084
|
+
const tryCatch: <E, A>(f: () => Promise<A>, onError: (e: unknown) => E) => TaskResult<E, A>;
|
|
1085
|
+
/**
|
|
1086
|
+
* Transforms the success value inside a TaskResult.
|
|
1087
|
+
*/
|
|
1088
|
+
const map: <E, A, B>(f: (a: A) => B) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1089
|
+
/**
|
|
1090
|
+
* Transforms the error value inside a TaskResult.
|
|
1091
|
+
*/
|
|
1092
|
+
const mapError: <E, F, A>(f: (e: E) => F) => (data: TaskResult<E, A>) => TaskResult<F, A>;
|
|
1093
|
+
/**
|
|
1094
|
+
* Chains TaskResult computations. If the first succeeds, passes the value to f.
|
|
1095
|
+
* If the first fails, propagates the error.
|
|
1096
|
+
*/
|
|
1097
|
+
const chain: <E, A, B>(f: (a: A) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1098
|
+
/**
|
|
1099
|
+
* Extracts the value from a TaskResult by providing handlers for both cases.
|
|
1100
|
+
*/
|
|
1101
|
+
const fold: <E, A, B>(onErr: (e: E) => B, onOk: (a: A) => B) => (data: TaskResult<E, A>) => Task<B>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Pattern matches on a TaskResult, returning a Task of the result.
|
|
1104
|
+
*/
|
|
1105
|
+
const match: <E, A, B>(cases: {
|
|
1106
|
+
err: (e: E) => B;
|
|
1107
|
+
ok: (a: A) => B;
|
|
1108
|
+
}) => (data: TaskResult<E, A>) => Task<B>;
|
|
1109
|
+
/**
|
|
1110
|
+
* Recovers from an error by providing a fallback TaskResult.
|
|
1111
|
+
* The fallback can produce a different success type, widening the result to `TaskResult<E, A | B>`.
|
|
1112
|
+
*/
|
|
1113
|
+
const recover: <E, A, B>(fallback: (e: E) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, A | B>;
|
|
1114
|
+
/**
|
|
1115
|
+
* Returns the success value or a default value if the TaskResult is an error.
|
|
1116
|
+
* The default can be a different type, widening the result to `Task<A | B>`.
|
|
1117
|
+
*/
|
|
1118
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: TaskResult<E, A>) => Task<A | B>;
|
|
1119
|
+
/**
|
|
1120
|
+
* Executes a side effect on the success value without changing the TaskResult.
|
|
1121
|
+
* Useful for logging or debugging.
|
|
1122
|
+
*/
|
|
1123
|
+
const tap: <E, A>(f: (a: A) => void) => (data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1124
|
+
/**
|
|
1125
|
+
* Re-runs a TaskResult on `Err` with configurable attempts, backoff, and retry condition.
|
|
1126
|
+
*
|
|
1127
|
+
* @param options.attempts - Total number of attempts (1 = no retry, 3 = up to 3 tries)
|
|
1128
|
+
* @param options.backoff - Fixed delay in ms, or a function `(attempt) => ms` for computed delay
|
|
1129
|
+
* @param options.when - Only retry when this returns true; defaults to always retry on Err
|
|
1130
|
+
*
|
|
1131
|
+
* @example
|
|
1132
|
+
* ```ts
|
|
1133
|
+
* // Retry up to 3 times with exponential backoff
|
|
1134
|
+
* pipe(
|
|
1135
|
+
* fetchUser,
|
|
1136
|
+
* TaskResult.retry({ attempts: 3, backoff: n => n * 1000 })
|
|
1137
|
+
* );
|
|
1138
|
+
*
|
|
1139
|
+
* // Only retry on network errors, not auth errors
|
|
1140
|
+
* pipe(
|
|
1141
|
+
* fetchUser,
|
|
1142
|
+
* TaskResult.retry({ attempts: 3, when: e => e instanceof NetworkError })
|
|
1143
|
+
* );
|
|
1144
|
+
* ```
|
|
1145
|
+
*/
|
|
1146
|
+
const retry: <E>(options: {
|
|
1147
|
+
attempts: number;
|
|
1148
|
+
backoff?: number | ((attempt: number) => number);
|
|
1149
|
+
when?: (error: E) => boolean;
|
|
1150
|
+
}) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1151
|
+
/**
|
|
1152
|
+
* Fails a TaskResult with a typed error if it does not resolve within the given time.
|
|
1153
|
+
*
|
|
1154
|
+
* @example
|
|
1155
|
+
* ```ts
|
|
1156
|
+
* pipe(
|
|
1157
|
+
* fetchUser,
|
|
1158
|
+
* TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
|
|
1159
|
+
* );
|
|
1160
|
+
* ```
|
|
1161
|
+
*/
|
|
1162
|
+
const timeout: <E>(ms: number, onTimeout: () => E) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* A Resource pairs an async acquisition step with a guaranteed cleanup step.
|
|
1167
|
+
*
|
|
1168
|
+
* Use it whenever something must be explicitly closed, released, or torn down
|
|
1169
|
+
* after you are done with it — database connections, file handles, locks,
|
|
1170
|
+
* temporary directories, or any object with a lifecycle.
|
|
1171
|
+
*
|
|
1172
|
+
* The key guarantee: `release` always runs after `Resource.use`, even when
|
|
1173
|
+
* the work function returns an error. If `acquire` itself fails, `release` is
|
|
1174
|
+
* skipped — there is nothing to clean up.
|
|
1175
|
+
*
|
|
1176
|
+
* Build a Resource with `Resource.make` or `Resource.fromTask`, then run it
|
|
1177
|
+
* with `Resource.use`.
|
|
1178
|
+
*
|
|
1179
|
+
* @example
|
|
1180
|
+
* ```ts
|
|
1181
|
+
* const dbResource = Resource.make(
|
|
1182
|
+
* TaskResult.tryCatch(() => openConnection(config), (e) => new DbError(e)),
|
|
1183
|
+
* (conn) => Task.from(() => conn.close())
|
|
1184
|
+
* );
|
|
1185
|
+
*
|
|
1186
|
+
* const result = await pipe(
|
|
1187
|
+
* dbResource,
|
|
1188
|
+
* Resource.use((conn) => queryUser(conn, userId))
|
|
1189
|
+
* )();
|
|
1190
|
+
* // conn.close() is called whether queryUser succeeds or fails
|
|
1191
|
+
* ```
|
|
1192
|
+
*/
|
|
1193
|
+
type Resource<E, A> = {
|
|
1194
|
+
readonly acquire: TaskResult<E, A>;
|
|
1195
|
+
readonly release: (a: A) => Task<void>;
|
|
1196
|
+
};
|
|
1197
|
+
declare namespace Resource {
|
|
1198
|
+
/**
|
|
1199
|
+
* Creates a Resource from an acquire operation that may fail and a release function.
|
|
1200
|
+
*
|
|
1201
|
+
* @example
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* const fileResource = Resource.make(
|
|
1204
|
+
* TaskResult.tryCatch(() => fs.promises.open("data.csv", "r"), toFileError),
|
|
1205
|
+
* (handle) => Task.from(() => handle.close())
|
|
1206
|
+
* );
|
|
1207
|
+
* ```
|
|
1208
|
+
*/
|
|
1209
|
+
const make: <E, A>(acquire: TaskResult<E, A>, release: (a: A) => Task<void>) => Resource<E, A>;
|
|
1210
|
+
/**
|
|
1211
|
+
* Creates a Resource from an acquire operation that cannot fail.
|
|
1212
|
+
* Use this when opening the resource is guaranteed to succeed, such as
|
|
1213
|
+
* in-memory locks, counters, or timers.
|
|
1214
|
+
*
|
|
1215
|
+
* @example
|
|
1216
|
+
* ```ts
|
|
1217
|
+
* const timerResource = Resource.fromTask<never, Timer>(
|
|
1218
|
+
* Task.from(() => Promise.resolve(startTimer())),
|
|
1219
|
+
* (timer) => Task.from(() => Promise.resolve(timer.stop()))
|
|
1220
|
+
* );
|
|
1221
|
+
* ```
|
|
1222
|
+
*/
|
|
1223
|
+
const fromTask: <E, A>(acquire: Task<A>, release: (a: A) => Task<void>) => Resource<E, A>;
|
|
1224
|
+
/**
|
|
1225
|
+
* Acquires the resource, runs `f` with it, then releases it.
|
|
1226
|
+
*
|
|
1227
|
+
* Release always runs, even when `f` returns an error.
|
|
1228
|
+
* If acquire fails, `f` and release are both skipped and the error is returned.
|
|
1229
|
+
*
|
|
1230
|
+
* @example
|
|
1231
|
+
* ```ts
|
|
1232
|
+
* const rows = await pipe(
|
|
1233
|
+
* dbResource,
|
|
1234
|
+
* Resource.use((conn) => runQuery(conn, "SELECT * FROM users"))
|
|
1235
|
+
* )();
|
|
1236
|
+
* // conn is closed whether the query succeeds or fails
|
|
1237
|
+
* ```
|
|
1238
|
+
*/
|
|
1239
|
+
const use: <E, A, B>(f: (a: A) => TaskResult<E, B>) => (resource: Resource<E, A>) => TaskResult<E, B>;
|
|
1240
|
+
/**
|
|
1241
|
+
* Acquires two resources in sequence and presents them as a tuple.
|
|
1242
|
+
* Resources are released in reverse order: the second is released before the first.
|
|
1243
|
+
*
|
|
1244
|
+
* If the second resource fails to acquire, the first is released immediately
|
|
1245
|
+
* before returning the error.
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* ```ts
|
|
1249
|
+
* const combined = Resource.combine(dbResource, cacheResource);
|
|
1250
|
+
*
|
|
1251
|
+
* const result = await pipe(
|
|
1252
|
+
* combined,
|
|
1253
|
+
* Resource.use(([conn, cache]) => lookupWithFallback(conn, cache, userId))
|
|
1254
|
+
* )();
|
|
1255
|
+
* ```
|
|
1256
|
+
*/
|
|
1257
|
+
const combine: <E, A, B>(resourceA: Resource<E, A>, resourceB: Resource<E, B>) => Resource<E, readonly [A, B]>;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1048
1260
|
/**
|
|
1049
1261
|
* A synchronous computation that threads a piece of mutable state `S` through
|
|
1050
1262
|
* a pipeline without exposing mutation at call sites.
|
|
@@ -1237,123 +1449,6 @@ declare namespace State {
|
|
|
1237
1449
|
const execute: <S>(initialState: S) => <A>(st: State<S, A>) => S;
|
|
1238
1450
|
}
|
|
1239
1451
|
|
|
1240
|
-
/**
|
|
1241
|
-
* A Task that can fail with an error of type E or succeed with a value of type A.
|
|
1242
|
-
* Combines async operations with typed error handling.
|
|
1243
|
-
*
|
|
1244
|
-
* @example
|
|
1245
|
-
* ```ts
|
|
1246
|
-
* const fetchUser = (id: string): TaskResult<Error, User> =>
|
|
1247
|
-
* TaskResult.tryCatch(
|
|
1248
|
-
* () => fetch(`/users/${id}`).then(r => r.json()),
|
|
1249
|
-
* (e) => new Error(`Failed to fetch user: ${e}`)
|
|
1250
|
-
* );
|
|
1251
|
-
* ```
|
|
1252
|
-
*/
|
|
1253
|
-
type TaskResult<E, A> = Task<Result<E, A>>;
|
|
1254
|
-
declare namespace TaskResult {
|
|
1255
|
-
/**
|
|
1256
|
-
* Wraps a value in a successful TaskResult.
|
|
1257
|
-
*/
|
|
1258
|
-
const ok: <E, A>(value: A) => TaskResult<E, A>;
|
|
1259
|
-
/**
|
|
1260
|
-
* Creates a failed TaskResult with the given error.
|
|
1261
|
-
*/
|
|
1262
|
-
const err: <E, A>(error: E) => TaskResult<E, A>;
|
|
1263
|
-
/**
|
|
1264
|
-
* Creates a TaskResult from a function that may throw.
|
|
1265
|
-
* Catches any errors and transforms them using the onError function.
|
|
1266
|
-
*
|
|
1267
|
-
* @example
|
|
1268
|
-
* ```ts
|
|
1269
|
-
* const parseJson = (s: string): TaskResult<string, unknown> =>
|
|
1270
|
-
* TaskResult.tryCatch(
|
|
1271
|
-
* async () => JSON.parse(s),
|
|
1272
|
-
* (e) => `Parse error: ${e}`
|
|
1273
|
-
* );
|
|
1274
|
-
* ```
|
|
1275
|
-
*/
|
|
1276
|
-
const tryCatch: <E, A>(f: () => Promise<A>, onError: (e: unknown) => E) => TaskResult<E, A>;
|
|
1277
|
-
/**
|
|
1278
|
-
* Transforms the success value inside a TaskResult.
|
|
1279
|
-
*/
|
|
1280
|
-
const map: <E, A, B>(f: (a: A) => B) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1281
|
-
/**
|
|
1282
|
-
* Transforms the error value inside a TaskResult.
|
|
1283
|
-
*/
|
|
1284
|
-
const mapError: <E, F, A>(f: (e: E) => F) => (data: TaskResult<E, A>) => TaskResult<F, A>;
|
|
1285
|
-
/**
|
|
1286
|
-
* Chains TaskResult computations. If the first succeeds, passes the value to f.
|
|
1287
|
-
* If the first fails, propagates the error.
|
|
1288
|
-
*/
|
|
1289
|
-
const chain: <E, A, B>(f: (a: A) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1290
|
-
/**
|
|
1291
|
-
* Extracts the value from a TaskResult by providing handlers for both cases.
|
|
1292
|
-
*/
|
|
1293
|
-
const fold: <E, A, B>(onErr: (e: E) => B, onOk: (a: A) => B) => (data: TaskResult<E, A>) => Task<B>;
|
|
1294
|
-
/**
|
|
1295
|
-
* Pattern matches on a TaskResult, returning a Task of the result.
|
|
1296
|
-
*/
|
|
1297
|
-
const match: <E, A, B>(cases: {
|
|
1298
|
-
err: (e: E) => B;
|
|
1299
|
-
ok: (a: A) => B;
|
|
1300
|
-
}) => (data: TaskResult<E, A>) => Task<B>;
|
|
1301
|
-
/**
|
|
1302
|
-
* Recovers from an error by providing a fallback TaskResult.
|
|
1303
|
-
* The fallback can produce a different success type, widening the result to `TaskResult<E, A | B>`.
|
|
1304
|
-
*/
|
|
1305
|
-
const recover: <E, A, B>(fallback: (e: E) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, A | B>;
|
|
1306
|
-
/**
|
|
1307
|
-
* Returns the success value or a default value if the TaskResult is an error.
|
|
1308
|
-
* The default can be a different type, widening the result to `Task<A | B>`.
|
|
1309
|
-
*/
|
|
1310
|
-
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: TaskResult<E, A>) => Task<A | B>;
|
|
1311
|
-
/**
|
|
1312
|
-
* Executes a side effect on the success value without changing the TaskResult.
|
|
1313
|
-
* Useful for logging or debugging.
|
|
1314
|
-
*/
|
|
1315
|
-
const tap: <E, A>(f: (a: A) => void) => (data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1316
|
-
/**
|
|
1317
|
-
* Re-runs a TaskResult on `Err` with configurable attempts, backoff, and retry condition.
|
|
1318
|
-
*
|
|
1319
|
-
* @param options.attempts - Total number of attempts (1 = no retry, 3 = up to 3 tries)
|
|
1320
|
-
* @param options.backoff - Fixed delay in ms, or a function `(attempt) => ms` for computed delay
|
|
1321
|
-
* @param options.when - Only retry when this returns true; defaults to always retry on Err
|
|
1322
|
-
*
|
|
1323
|
-
* @example
|
|
1324
|
-
* ```ts
|
|
1325
|
-
* // Retry up to 3 times with exponential backoff
|
|
1326
|
-
* pipe(
|
|
1327
|
-
* fetchUser,
|
|
1328
|
-
* TaskResult.retry({ attempts: 3, backoff: n => n * 1000 })
|
|
1329
|
-
* );
|
|
1330
|
-
*
|
|
1331
|
-
* // Only retry on network errors, not auth errors
|
|
1332
|
-
* pipe(
|
|
1333
|
-
* fetchUser,
|
|
1334
|
-
* TaskResult.retry({ attempts: 3, when: e => e instanceof NetworkError })
|
|
1335
|
-
* );
|
|
1336
|
-
* ```
|
|
1337
|
-
*/
|
|
1338
|
-
const retry: <E>(options: {
|
|
1339
|
-
attempts: number;
|
|
1340
|
-
backoff?: number | ((attempt: number) => number);
|
|
1341
|
-
when?: (error: E) => boolean;
|
|
1342
|
-
}) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1343
|
-
/**
|
|
1344
|
-
* Fails a TaskResult with a typed error if it does not resolve within the given time.
|
|
1345
|
-
*
|
|
1346
|
-
* @example
|
|
1347
|
-
* ```ts
|
|
1348
|
-
* pipe(
|
|
1349
|
-
* fetchUser,
|
|
1350
|
-
* TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
|
|
1351
|
-
* );
|
|
1352
|
-
* ```
|
|
1353
|
-
*/
|
|
1354
|
-
const timeout: <E>(ms: number, onTimeout: () => E) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
1452
|
/**
|
|
1358
1453
|
* A Task that resolves to an optional value.
|
|
1359
1454
|
* Combines async operations with the Option type for values that may not exist.
|
|
@@ -2167,4 +2262,4 @@ declare namespace Tuple {
|
|
|
2167
2262
|
const tap: <A, B>(f: (a: A, b: B) => void) => (tuple: Tuple<A, B>) => Tuple<A, B>;
|
|
2168
2263
|
}
|
|
2169
2264
|
|
|
2170
|
-
export { type Failure, type Invalid, Lens, type Loading, Logged, type NotAsked, Option, Optional, Predicate, Reader, Refinement, RemoteData, Result, State, type Success, Task, TaskOption, TaskResult, TaskValidation, These, type TheseBoth, type TheseFirst, type TheseSecond, Tuple, type Valid, Validation };
|
|
2265
|
+
export { type Failure, type Invalid, Lens, type Loading, Logged, type NotAsked, Option, Optional, Predicate, Reader, Refinement, RemoteData, Resource, Result, State, type Success, Task, TaskOption, TaskResult, TaskValidation, These, type TheseBoth, type TheseFirst, type TheseSecond, Tuple, type Valid, Validation };
|