clutchit 0.0.15 → 0.0.16

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/CHANGELOG.md CHANGED
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
14
  - `Try<T, E>` — synchronous typed error container with `Ok` and `Err` variants
15
15
  - `TryAsync<T, E>` — async equivalent backed by `Promise<Try<T, E>>`, implements `PromiseLike`
16
16
  - `ok()`, `err()`, `okAsync()`, `errAsync()` — standalone constructors
17
- - `Do` / `DoAsync` — generator-based Do notation with optional context parameter
17
+ - `attempt` — generator-based sequential chaining (overloaded for sync and async) with optional context parameter
18
18
  - Instance methods: `map`, `mapErr`, `andThen`, `orElse`, `tap`, `tapErr`, `andThrough`, `orThrough`, `catchFault`, `unwrapOr`, `match`
19
19
  - `fromPromise` — lift a `Promise` into a `TryAsync` with typed error mapping
20
20
  - `fromSafePromise` — lift a never-rejecting promise into `TryAsync<T, never>`
package/README.md CHANGED
@@ -19,7 +19,7 @@ npm install clutchit
19
19
 
20
20
  | Import | Description |
21
21
  |---|---|
22
- | `clutchit/unthrow` | `Try<T,E>`, `TryAsync<T,E>`, `Fault`, Do notation |
22
+ | `clutchit/unthrow` | `Try<T,E>`, `TryAsync<T,E>`, `Fault`, `attempt` |
23
23
  | `clutchit/schedule` | Composable timing schedules — exponential, linear, fibonacci, cron, … |
24
24
  | `clutchit/retry` | Retry policy with backoff schedules and abort signal support |
25
25
  | `clutchit/timeout` | Timeout wrapper with `AbortSignal` propagation |
@@ -151,15 +151,15 @@ combineWithAllErrors([ok(1), err('a'), err('b')]); // Err(['a', 'b'])
151
151
  race([okAsync(1), okAsync(2)]);
152
152
  ```
153
153
 
154
- ### Do notation
154
+ ### attempt
155
155
 
156
- Generator-based sequential chaining. `yield*` unwraps `Ok`; any `Err` short-circuits the entire block.
156
+ Generator-based sequential chaining. `yield*` unwraps `Ok`; any `Err` short-circuits the entire block. A single overloaded function handles both sync and async generators.
157
157
 
158
158
  ```ts
159
- import { Do, DoAsync } from 'clutchit/unthrow';
159
+ import { attempt } from 'clutchit/unthrow';
160
160
 
161
161
  // Sync
162
- const result = Do(function* () {
162
+ const result = attempt(function* () {
163
163
  const user = yield* findUser(id); // Try<User, NotFoundFault>
164
164
  const perms = yield* getPermissions(user); // Try<Perms, PermFault>
165
165
  return { user, perms };
@@ -167,7 +167,7 @@ const result = Do(function* () {
167
167
  });
168
168
 
169
169
  // Async
170
- const result = DoAsync(async function* () {
170
+ const result = attempt(async function* () {
171
171
  const user = yield* fetchUser(id); // TryAsync<User, NetworkFault>
172
172
  const order = yield* createOrder(user); // TryAsync<Order, OrderFault>
173
173
  return order;
@@ -175,7 +175,7 @@ const result = DoAsync(async function* () {
175
175
  });
176
176
 
177
177
  // With context
178
- const result = Do({ db }, function* (ctx) {
178
+ const result = attempt({ db }, function* (ctx) {
179
179
  const user = yield* ctx.db.findUser(id);
180
180
  return user;
181
181
  });
@@ -0,0 +1,9 @@
1
+ import { type Try, Err } from './try.js';
2
+ import { TryAsync } from './try.async.js';
3
+ type InferFault<Y> = Y extends Err<never, infer E> ? E : never;
4
+ export declare function attempt<T, Y extends Err<never, any>>(generator: () => Generator<Y, T>): Try<T, InferFault<Y>>;
5
+ export declare function attempt<C, T, Y extends Err<never, any>>(ctx: C, generator: (ctx: C) => Generator<Y, T>): Try<T, InferFault<Y>>;
6
+ export declare function attempt<T, Y extends Err<never, any>>(generator: () => AsyncGenerator<Y, T>): TryAsync<T, InferFault<Y>>;
7
+ export declare function attempt<C, T, Y extends Err<never, any>>(ctx: C, generator: (ctx: C) => AsyncGenerator<Y, T>): TryAsync<T, InferFault<Y>>;
8
+ export {};
9
+ //# sourceMappingURL=attempt.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { ok } from './try.js';
2
+ import { TryAsync } from './try.async.js';
3
+ // Implementation
4
+ export function attempt(ctxOrGenerator, generator) {
5
+ const gen = typeof ctxOrGenerator === 'function'
6
+ ? ctxOrGenerator()
7
+ : generator(ctxOrGenerator);
8
+ if (Symbol.asyncIterator in gen) {
9
+ return new TryAsync((async () => {
10
+ const next = await gen.next();
11
+ while (!next.done) {
12
+ return next.value;
13
+ }
14
+ return ok(next.value);
15
+ })());
16
+ }
17
+ const next = gen.next();
18
+ while (!next.done) {
19
+ return next.value;
20
+ }
21
+ return ok(next.value);
22
+ }
23
+ //# sourceMappingURL=attempt.js.map
@@ -11,5 +11,5 @@ export { type Try, Ok, Err, ok, err } from './try.js';
11
11
  export { TryAsync, okAsync, errAsync } from './try.async.js';
12
12
  export { type FaultOf } from './fault-of.js';
13
13
  export { fromPromise, fromSafePromise, fromThrowable, fromAsyncThrowable, combine, combineWithAllErrors, unwrap, flatten, race, } from './helpers.js';
14
- export { Do, DoAsync } from './do.js';
14
+ export { attempt } from './attempt.js';
15
15
  //# sourceMappingURL=index.d.ts.map
@@ -13,6 +13,6 @@ export var Fault;
13
13
  export { Ok, Err, ok, err } from './try.js';
14
14
  export { TryAsync, okAsync, errAsync } from './try.async.js';
15
15
  export { fromPromise, fromSafePromise, fromThrowable, fromAsyncThrowable, combine, combineWithAllErrors, unwrap, flatten, race, } from './helpers.js';
16
- // ── Do ──────────────────────────────────────────────────────────
17
- export { Do, DoAsync } from './do.js';
16
+ // ── Attempt ─────────────────────────────────────────────────────
17
+ export { attempt } from './attempt.js';
18
18
  //# sourceMappingURL=index.js.map
@@ -104,9 +104,9 @@ export class TryAsync {
104
104
  async *[Symbol.asyncIterator]() {
105
105
  const result = await this._promise;
106
106
  if (result.isErr()) {
107
- // @ts-expect-error -- structurally equivalent, safe for DoAsync notation
107
+ // @ts-expect-error -- structurally equivalent, safe for attempt
108
108
  yield result;
109
- throw new Error('Unreachable: DoAsync generator continued after Err yield');
109
+ throw new Error('Unreachable: attempt generator continued after Err yield');
110
110
  }
111
111
  return result.value;
112
112
  }
@@ -123,9 +123,9 @@ export class Err {
123
123
  return fn(this);
124
124
  }
125
125
  *[Symbol.iterator]() {
126
- // @ts-expect-error -- structurally equivalent, safe for Do notation
126
+ // @ts-expect-error -- structurally equivalent, safe for attempt
127
127
  yield this;
128
- throw new Error('Unreachable: Do notation generator continued after Err yield');
128
+ throw new Error('Unreachable: attempt generator continued after Err yield');
129
129
  }
130
130
  }
131
131
  export function ok(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clutchit",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "Resilience primitives for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,9 +0,0 @@
1
- import { type Try, Err } from './try.js';
2
- import { TryAsync } from './try.async.js';
3
- type InferFault<Y> = Y extends Err<never, infer E> ? E : never;
4
- export declare function Do<T, Y extends Err<never, any>>(generator: () => Generator<Y, T>): Try<T, InferFault<Y>>;
5
- export declare function Do<C, T, Y extends Err<never, any>>(ctx: C, generator: (ctx: C) => Generator<Y, T>): Try<T, InferFault<Y>>;
6
- export declare function DoAsync<T, Y extends Err<never, any>>(generator: () => AsyncGenerator<Y, T>): TryAsync<T, InferFault<Y>>;
7
- export declare function DoAsync<C, T, Y extends Err<never, any>>(ctx: C, generator: (ctx: C) => AsyncGenerator<Y, T>): TryAsync<T, InferFault<Y>>;
8
- export {};
9
- //# sourceMappingURL=do.d.ts.map
@@ -1,27 +0,0 @@
1
- import { ok } from './try.js';
2
- import { TryAsync } from './try.async.js';
3
- export function Do(ctxOrGenerator, generator) {
4
- const gen = typeof ctxOrGenerator === 'function'
5
- ? ctxOrGenerator()
6
- : generator(ctxOrGenerator);
7
- const next = gen.next();
8
- while (!next.done) {
9
- // Each yielded value is an Err — short-circuit
10
- return next.value;
11
- }
12
- return ok(next.value);
13
- }
14
- export function DoAsync(ctxOrGenerator, generator) {
15
- return new TryAsync((async () => {
16
- const gen = typeof ctxOrGenerator === 'function'
17
- ? ctxOrGenerator()
18
- : generator(ctxOrGenerator);
19
- const next = await gen.next();
20
- while (!next.done) {
21
- // Each yielded value is an Err — short-circuit
22
- return next.value;
23
- }
24
- return ok(next.value);
25
- })());
26
- }
27
- //# sourceMappingURL=do.js.map