unwrapped 0.1.2 → 0.1.4

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 CHANGED
@@ -1,2 +1,848 @@
1
- # unwrapped
2
- Library to deal with result types, both synchronous and asynchronous, along with utilities like caches
1
+ # Unwrapped
2
+
3
+ A TypeScript library for elegant error handling and asynchronous state management, inspired by Rust's `Result` type and designed with Vue 3 integration in mind.
4
+
5
+ ## Overview
6
+
7
+ **Unwrapped** provides a robust alternative to `try/catch` blocks and promise chains, making error handling explicit, type-safe, and composable. It consists of two main parts:
8
+
9
+ - **Core**: Framework-agnostic utilities for managing results and async operations
10
+ - **Vue**: Vue 3 composables and components for reactive async state management
11
+
12
+ You can take a look at the Real world example section at the end of this document to see how Unwrapped can simplify your development.
13
+
14
+ A brief comparison with other libraries can be found at the "Why Unwrapped ?" section at the end of the document.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install unwrapped
20
+ ```
21
+
22
+
23
+ ## Core Concepts
24
+
25
+ ### `Result<T, E>`
26
+
27
+ A `Result` represents a synchronous operation that can either succeed with a value of type `T` or fail with an error of type `E`.
28
+
29
+ #### **Basic Usage:**
30
+
31
+ ```typescript
32
+ import { Result, ErrorBase } from 'unwrapped/core';
33
+
34
+ function divide(a: number, b: number): Result<number> {
35
+ if (b === 0) {
36
+ return Result.errTag("division_by_zero", "Can't divide by 0 !");
37
+ }
38
+
39
+ return Result.ok(a / b);
40
+ }
41
+
42
+ const shouldSucceed = divide(10, 2);
43
+ const shouldError = divide(10, 0);
44
+
45
+ // Checking status
46
+ if (shouldSucceed.isSuccess()) {
47
+ console.log("Success !");
48
+ }
49
+ if (shouldError.isError()) {
50
+ console.log("Error !");
51
+ }
52
+
53
+ // Unwrapping values
54
+ const value = shouldSucceed.unwrapOrNull(); // 5
55
+ const valueOrDefault = shouldError.unwrapOr(0); // Returns 0 since it's an error
56
+ const valueOrThrow = shouldSucceed.unwrapOrThrow();
57
+ ```
58
+
59
+ #### **Working with Promises:**
60
+
61
+ ```typescript
62
+ // Wrap a promise and catch errors
63
+ const result = await Result.tryPromise(
64
+ fetch("/api/data").then(r => r.json(),
65
+ (error) => new ErrorBase("fetch_error", "Failed to fetch data", error)
66
+ );
67
+
68
+ // Execute an async function
69
+ const result = await Result.tryFunction(
70
+ async () => {
71
+ const response = await fetch("/api/data");
72
+ return response.json();
73
+ },
74
+ (error) => new ErrorBase("fetch_error", "Failed to fetch data", error)
75
+ );
76
+ ```
77
+
78
+ #### **Chaining Operations:**
79
+
80
+ ```typescript
81
+ function validateAge(age: number): Result<number, ErrorBase> {
82
+ if (age < 0) {
83
+ return Result.err(new ErrorBase("invalid_age", "Age must be positive"));
84
+ }
85
+ return Result.ok(age);
86
+ }
87
+
88
+ function categorizeAge(age: number): Result<string, ErrorBase> {
89
+ if (age < 18) return Result.ok('minor');
90
+ if (age < 65) return Result.ok('adult');
91
+ return Result.ok('senior');
92
+ }
93
+
94
+ // Chain operations - stops at first error
95
+ const valid = Result.ok(25)
96
+ .flatChain(validateAge) // 25 is a valid age so execution continues
97
+ .flatChain(categorizeAge); // 25 is passed to categorizeAge
98
+
99
+ const invalid = Result.ok(-1)
100
+ .flatChain(validateAge) // -1 is not a valid age, so the chain short-circuits and returns a Result containing the error given by validateAge
101
+ .flatChain(categorizeAge) // categorizeAge does not get called
102
+
103
+ console.log(valid.state); // { status: "success", value: "adult" }
104
+ console.log(invalid.state); // { status: "error", value: <ErrorBase> }
105
+ ```
106
+
107
+ #### **Generator Syntax for Complex Flows:**
108
+
109
+ The same way async/await allows to write asynchronous code in a synchronous-looking way, generators can be used to write result chaining in a more imperative-looking manner via `Result.run()`.
110
+
111
+ Think of `function*` as `async function` and `yield*` as `await`. Inside a generator function executed by `Result.run()`, yielding a `Result` with `yield*` unwraps the `Result` and allows you to get its value if it is successful. If the `Result` contains an error, the whole generator will terminate and `Result.run()` will return a `Result` containing the error.
112
+
113
+ Note that in the case of `Result.run()`, everything is synchronous. For performing the same kind of operations on asynchronous tasks, use `AsyncResult.run()`.
114
+
115
+ ```typescript
116
+
117
+ const valid = Result.run(function* () {
118
+ const validatedAge = yield* validateAge(10); // 10 is a valid age so validatedAge is set to 10 and execution continues
119
+ const category = yield* categorizeAge(validateAge); // yield* unwraps the value so category is set to "minor"
120
+
121
+ return category;
122
+ });
123
+
124
+ const invalid = Result.run(function* () {
125
+ const validatedAge = yield* validateAge(-1); // -1 is not a valid age so the run terminates early and returns a Result containing the error given by validateAge
126
+ const category = yield* categorizeAge(validateAge); // this never gets reached
127
+
128
+ return category;
129
+ })
130
+
131
+ // If any step fails, the error is automatically propagated
132
+ if (invalid.isError()) {
133
+ console.error(result.state.error);
134
+ }
135
+ ```
136
+
137
+ ### `AsyncResult<T, E>`
138
+
139
+ An `AsyncResult` represents an asynchronous operation with four possible states: `idle`, `loading`, `success`, or `error`.
140
+
141
+ #### **Basic Usage:**
142
+
143
+ ```typescript
144
+ import { AsyncResult, Result } from 'unwrapped/core';
145
+
146
+ // Create from a promise that returns a Result
147
+ const asyncResult = AsyncResult.fromResultPromise(
148
+ fetch('/api/user')
149
+ .then(r => r.json())
150
+ .then(data => Result.ok(data))
151
+ .catch(err => Result.err(new ErrorBase('API_ERROR', 'Failed to fetch', err)))
152
+ );
153
+
154
+ // Create from a plain promise
155
+ const asyncResult = AsyncResult.fromValuePromise(
156
+ fetch('/api/user').then(r => r.json())
157
+ );
158
+
159
+ // Check current state
160
+ console.log(asyncResult.isLoading()); // true
161
+ console.log(asyncResult.isSuccess()); // false
162
+
163
+ // Listen to state changes
164
+ asyncResult.listen((result) => {
165
+ if (result.isSuccess()) {
166
+ console.log('Data loaded:', result.unwrapOrNull());
167
+ } else if (result.isError()) {
168
+ console.error('Error:', result.state.error);
169
+ }
170
+ });
171
+
172
+ // Wait for completion
173
+ const settledResult = await asyncResult.waitForSettled();
174
+ const value = settledResult.unwrapOrNull();
175
+ ```
176
+
177
+ #### **Lazy Actions:**
178
+
179
+ ```typescript
180
+ // Create an action that doesn't execute until triggered
181
+ const { trigger, result } = AsyncResult.makeLazyAction(async () => {
182
+ const response = await fetch('/api/data');
183
+ const data = await response.json();
184
+ return Result.ok(data);
185
+ });
186
+
187
+ // Listen for changes
188
+ result.listen((r) => {
189
+ console.log('State:', r.state.status);
190
+ });
191
+
192
+ // Trigger execution
193
+ trigger();
194
+ ```
195
+
196
+ #### **Chaining Async Operations:**
197
+
198
+ ```typescript
199
+ const userResult = AsyncResult.fromValuePromise(fetch('/api/user/1').then(r => r.json()));
200
+
201
+ // Chain with another async operation
202
+ const postsResult = userResult.chain(async (user) => {
203
+ const response = await fetch(`/api/posts?userId=${user.id}`);
204
+ const posts = await response.json();
205
+ return Result.ok(posts);
206
+ });
207
+
208
+ // FlatChain with AsyncResult
209
+ const enrichedPosts = postsResult.flatChain((posts) => {
210
+ return AsyncResult.fromValuePromise(
211
+ Promise.all(posts.map(p => enrichPost(p)))
212
+ );
213
+ });
214
+ ```
215
+
216
+ #### **Generator Syntax for Async Operations:**
217
+
218
+ ```typescript
219
+ function fetchUser(id: string): AsyncResult<User> {
220
+ // ...
221
+ }
222
+
223
+ function fetchProfile(id: string): AsyncResult<Profile> {
224
+ // ...
225
+ }
226
+
227
+ const result = AsyncResult.run(function* () {
228
+ const user = yield* fetchUser(userId);
229
+ const profile = yield* fetchProfile(user.profileId);
230
+
231
+ return { user, profile };
232
+ });
233
+
234
+ // result is an AsyncResult that automatically tracks loading/success/error states
235
+ result.debug("Profile fetcher");
236
+ ```
237
+
238
+ #### **Ensuring Multiple AsyncResults:**
239
+
240
+ ```typescript
241
+ // Wait for multiple AsyncResults to complete
242
+ const user: AsyncResult<User> = fetchUser(userId);
243
+ const settings: AsyncResult<Settings> = AsyncResult.fromValuePromise(fetchSettings());
244
+
245
+ const all = AsyncResult.ensureAvailable([user, settings]);
246
+
247
+ all.listen((result) => {
248
+ if (result.isSuccess()) {
249
+ const [userData, settingsData] = result.unwrapOrThrow();
250
+ console.log('All data loaded', { userData, settingsData });
251
+ }
252
+ });
253
+ ```
254
+
255
+ ### `KeyedAsyncCache<P, V, E>`
256
+
257
+ A cache for asynchronous operations that maps parameters to their results, with support for automatic refetching.
258
+
259
+ **Usage:**
260
+
261
+ ```typescript
262
+ import { KeyedAsyncCache, Result } from 'unwrapped/core';
263
+
264
+ // Create a cache with a fetcher function
265
+ const userCache = new KeyedAsyncCache(
266
+ async (userId: number) => Result.tryFunction(
267
+ async () => {
268
+ const response = await fetch(`/api/users/${userId}`);
269
+ const data = await response.json();
270
+ return data;
271
+ },
272
+ (e) => new ErrorBase("fetch_error", "Error on fetch", e)
273
+ ),
274
+ (userId) => `user-${userId}`, // Key generator
275
+ 60000 // TTL: 60 seconds
276
+ );
277
+
278
+ // Get cached or fetch
279
+ const userResult = userCache.get(123); // Returns AsyncResult
280
+
281
+ // Get with refetch policy
282
+ const freshUser = userCache.get(123, { policy: 'refetch' });
283
+ const errorRetry = userCache.get(456, { policy: 'if-error' });
284
+
285
+ // Check if any request is loading
286
+ if (userCache.anyLoading()) {
287
+ console.log('Loading data...');
288
+ }
289
+
290
+ // Invalidate cache
291
+ userCache.invalidateParams(123);
292
+ userCache.invalidateAll();
293
+ userCache.clear();
294
+ ```
295
+
296
+ ### `ErrorBase`
297
+
298
+ A structured error class that provides consistent error handling with codes, messages, and automatic logging.
299
+
300
+ ```typescript
301
+ import { ErrorBase } from 'unwrapped/core';
302
+
303
+ // Create an error
304
+ const error = new ErrorBase(
305
+ 'VALIDATION_ERROR',
306
+ 'Email address is invalid',
307
+ originalError, // Optional: the caught error
308
+ true // Optional: whether to log immediately (default: true)
309
+ );
310
+
311
+ // Access error properties
312
+ console.log(error.code); // 'VALIDATION_ERROR'
313
+ console.log(error.message); // 'Email address is invalid'
314
+ console.log(error.toString()); // 'Error VALIDATION_ERROR: Email address is invalid'
315
+
316
+ // Log the error
317
+ error.logError();
318
+
319
+ // Use with Result
320
+ const result = Result.err(error);
321
+ ```
322
+
323
+ ## Vue Integration
324
+
325
+ The Vue package provides composables and components for seamless integration with Vue 3's reactivity system.
326
+
327
+ ### Composables
328
+
329
+ #### `useAsyncResultRef(asyncResult)`
330
+
331
+ Makes an `AsyncResult` reactive by wrapping it in a Vue ref:
332
+
333
+ ```typescript
334
+ import { AsyncResult } from 'unwrapped/core';
335
+ import { useAsyncResultRef } from 'unwrapped/vue';
336
+
337
+ const asyncResult = AsyncResult.fromValuePromise(fetch('/api/data').then(r => r.json()));
338
+ const resultRef = useAsyncResultRef(asyncResult);
339
+ ```
340
+
341
+ ```vue
342
+ <template>
343
+ <div v-if="resultRef.isLoading()">Loading...</div>
344
+ <div v-else-if="resultRef.isSuccess()">
345
+ Data: {{ resultRef.unwrapOrNull() }}
346
+ </div>
347
+ <div v-else-if="resultRef.isError()">
348
+ Error: {{ resultRef.state.error.message }}
349
+ </div>
350
+ </template>
351
+ ```
352
+
353
+ #### `useAction(action)`
354
+
355
+ Executes an action immediately and returns a reactive AsyncResult:
356
+
357
+ ```typescript
358
+ import { useAction } from 'unwrapped/vue';
359
+ import { Result } from 'unwrapped/core';
360
+
361
+ const resultRef = useAction(async () => Result.tryFunction(
362
+ async () => {
363
+ const response = await fetch('/api/data');
364
+ const data = await response.json();
365
+ return data;
366
+ },
367
+ (e) => new ErrorBase("fetch_error", "Error on fetch", e)
368
+ ));
369
+
370
+ ```
371
+
372
+ #### `useLazyAction(action)`
373
+
374
+ Creates a lazy action that can be triggered manually:
375
+
376
+ ```typescript
377
+ import { useLazyAction } from 'unwrapped/vue';
378
+ import { Result } from 'unwrapped/core';
379
+
380
+ const { resultRef, trigger } = useLazyAction(async () => Result.tryFunction(
381
+ async () => {
382
+ const response = await fetch('/api/data');
383
+ const data = await response.json();
384
+ return data;
385
+ },
386
+ (e) => new ErrorBase("fetch_error", "Error on fetch", e)
387
+ ));
388
+ ```
389
+
390
+ ```vue
391
+ <template>
392
+ <button @click="trigger">Load Data</button>
393
+ <div v-if="resultRef.isLoading()">Loading...</div>
394
+ <div v-else-if="resultRef.isSuccess()">{{ resultRef.unwrapOrNull() }}</div>
395
+ </template>
396
+ ```
397
+
398
+ #### `useReactiveChain(source, pipe, options)`
399
+
400
+ Creates a reactive pipeline that automatically updates when the source changes:
401
+
402
+ ```typescript
403
+ import { ref } from 'vue';
404
+ import { useReactiveChain } from 'unwrapped/vue';
405
+ import { AsyncResult, Result } from 'unwrapped/core';
406
+
407
+ const userId = ref(1);
408
+
409
+ const userResultRef = useReactiveChain(
410
+ () => userId.value,
411
+ (id) => AsyncResult.fromValuePromise(
412
+ fetch(`/api/users/${id}`).then(r => r.json())
413
+ ),
414
+ { immediate: true }
415
+ );
416
+ ```
417
+
418
+ #### `useGenerator(generatorFunc)` / `useLazyGenerator(generatorFunc)`
419
+
420
+ Run generator functions with reactive AsyncResults:
421
+
422
+ ```typescript
423
+ import { useGenerator } from 'unwrapped/vue';
424
+ import { AsyncResult } from 'unwrapped/core';
425
+
426
+ const resultRef = useGenerator(function* () {
427
+ const user = yield* AsyncResult.fromValuePromise(fetchUser());
428
+ const posts = yield* AsyncResult.fromValuePromise(fetchPosts(user.id));
429
+ return { user, posts };
430
+ });
431
+ ```
432
+
433
+ #### `useReactiveGenerator(source, generatorFunc, options)`
434
+
435
+ Reactive generator that reruns when source changes:
436
+
437
+ ```typescript
438
+ import { ref } from 'vue';
439
+ import { useReactiveGenerator } from 'unwrapped/vue';
440
+ import { AsyncResult } from 'unwrapped/core';
441
+
442
+ const searchQuery = ref('');
443
+
444
+ const resultsRef = useReactiveGenerator(
445
+ () => searchQuery.value,
446
+ function* (query) {
447
+ if (!query) return [];
448
+
449
+ const results = yield* AsyncResult.fromValuePromise(
450
+ fetch(`/api/search?q=${query}`).then(r => r.json())
451
+ );
452
+
453
+ return results;
454
+ }
455
+ );
456
+ ```
457
+
458
+ ### Components
459
+
460
+ #### `<AsyncResultLoader>`
461
+
462
+ A component that renders different content based on AsyncResult state:
463
+
464
+ ```vue
465
+ <template>
466
+ <AsyncResultLoader :result="dataResult">
467
+ <template #loading>
468
+ <div class="spinner">Loading...</div>
469
+ </template>
470
+
471
+ <template #error="{ error }">
472
+ <div class="error-message">
473
+ Error {{ error.code }}: {{ error.message }}
474
+ </div>
475
+ </template>
476
+
477
+ <template #default="{ value }">
478
+ <div class="data">{{ value }}</div>
479
+ </template>
480
+
481
+ <template #idle>
482
+ <div>Click the button to load data</div>
483
+ </template>
484
+ </AsyncResultLoader>
485
+ </template>
486
+
487
+ <script setup>
488
+ import { AsyncResultLoader } from 'unwrapped/vue';
489
+ import { useLazyAction } from 'unwrapped/vue';
490
+ import { Result } from 'unwrapped/core';
491
+
492
+ const { resultRef: dataResult, trigger: loadData } = useLazyAction(async () => {
493
+ const response = await fetch('/api/data');
494
+ const data = await response.json();
495
+ return Result.ok(data);
496
+ });
497
+ </script>
498
+ ```
499
+
500
+ #### `buildCustomAsyncResultLoader(slots)`
501
+
502
+ Create reusable loaders with consistent loading and error UI:
503
+
504
+ ```typescript
505
+ import { buildCustomAsyncResultLoader } from 'unwrapped/vue';
506
+ import { h } from 'vue';
507
+ import Spinner from './Spinner.vue';
508
+ import ErrorAlert from './ErrorAlert.vue';
509
+
510
+ export const CustomLoader = buildCustomAsyncResultLoader({
511
+ loading: () => h(Spinner),
512
+ error: ({ error }) => h(ErrorAlert, { error })
513
+ });
514
+ ```
515
+
516
+ ```vue
517
+ <template>
518
+ <CustomLoader :result="myAsyncResult">
519
+ <template #default="{ value }">
520
+ <!-- Your success content -->
521
+ <div>{{ value }}</div>
522
+ </template>
523
+ </CustomLoader>
524
+ </template>
525
+ ```
526
+
527
+ ## Real-World Examples
528
+
529
+ ### Simple data fetching (Vue 3)
530
+
531
+ #### **Without** Unwrapped
532
+
533
+ ```vue
534
+ <template>
535
+ <div>
536
+ <!-- Manually handle each state -->
537
+ <div v-if="loading">Loading user...</div>
538
+ <div v-else-if="error" class="error">
539
+ Error: {{ error.message }}
540
+ </div>
541
+ <div v-else-if="user">
542
+ <h2>{{ user.name }}</h2>
543
+ <p>{{ user.email }}</p>
544
+ </div>
545
+ </div>
546
+ </template>
547
+
548
+ <script setup>
549
+ import { ref, onMounted } from 'vue';
550
+
551
+ // Need separate refs for each state
552
+ const user = ref(null);
553
+ const loading = ref(false);
554
+ const error = ref(null);
555
+
556
+ onMounted(async () => {
557
+ // Manually manage loading state
558
+ loading.value = true;
559
+ error.value = null;
560
+
561
+ try {
562
+ const response = await fetch('/api/user/1');
563
+ if (!response.ok) throw new Error('Failed to fetch');
564
+ user.value = await response.json();
565
+ } catch (e) {
566
+ // Manually handle errors
567
+ error.value = e;
568
+ } finally {
569
+ // Don't forget to set loading to false!
570
+ loading.value = false;
571
+ }
572
+ });
573
+ </script>
574
+ ```
575
+
576
+ #### **With** Unwrapped
577
+
578
+ ```vue
579
+ <template>
580
+ <div>
581
+ <!-- Single component handles all states automatically -->
582
+ <!-- You can make your own custom reusable version with buildCustomAsyncResultLoader to avoid repeating the loading and error slots -->
583
+ <AsyncResultLoader :result="userResult">
584
+ <template #loading>Loading user...</template>
585
+
586
+ <template #error="{ error }">
587
+ <div class="error">Error: {{ error.message }}</div>
588
+ </template>
589
+
590
+ <!-- Only renders when data is successfully loaded -->
591
+ <template #default="{ value: user }">
592
+ <h2>{{ user.name }}</h2>
593
+ <p>{{ user.email }}</p>
594
+ </template>
595
+ </AsyncResultLoader>
596
+ </div>
597
+ </template>
598
+
599
+ <script setup>
600
+ import { AsyncResultLoader, useAction } from 'unwrapped/vue';
601
+ import { Result, ErrorBase } from 'unwrapped/core';
602
+
603
+ // Single composable handles loading, success, and error states automatically
604
+ // No need for separate refs or manual state management
605
+ const userResult = useAction(async () =>
606
+ Result.tryFunction(
607
+ async () => {
608
+ const response = await fetch('/api/user/1');
609
+ if (!response.ok) return Result.errTag("fetch_error", "response.ok is false");
610
+ return response.json();
611
+ },
612
+ (e) => new ErrorBase('unknown_fetch_error', 'Failed to load user', e)
613
+ )
614
+ );
615
+
616
+ // That's it! Loading state, error handling, and success state are all managed
617
+ // userResult automatically transitions: idle -> loading -> success/error
618
+ </script>
619
+ ```
620
+
621
+
622
+ ### Reactive search (Vue 3)
623
+
624
+ #### **Without** Unwrapped
625
+
626
+ ```vue
627
+ <template>
628
+ <div>
629
+ <input v-model="searchQuery" placeholder="Search users..." />
630
+
631
+ <!-- Multiple loading states to manage -->
632
+ <div v-if="isSearching">Searching...</div>
633
+ <div v-else-if="isLoadingDetails">Loading user details...</div>
634
+
635
+ <div v-if="searchError" class="error">{{ searchError }}</div>
636
+ <div v-if="detailsError" class="error">{{ detailsError }}</div>
637
+
638
+ <div v-if="searchResults && !selectedUser">
639
+ <div v-for="user in searchResults" :key="user.id"
640
+ @click="loadUserDetails(user.id)">
641
+ {{ user.name }}
642
+ </div>
643
+ </div>
644
+
645
+ <div v-if="selectedUser">
646
+ <h2>{{ selectedUser.name }}</h2>
647
+ <p>Posts: {{ selectedUser.posts?.length || 0 }}</p>
648
+ </div>
649
+ </div>
650
+ </template>
651
+
652
+ <script setup>
653
+ import { ref, watch } from 'vue';
654
+
655
+ const searchQuery = ref('');
656
+ const searchResults = ref(null);
657
+ const selectedUser = ref(null);
658
+
659
+ // Separate loading/error states for each operation
660
+ const isSearching = ref(false);
661
+ const isLoadingDetails = ref(false);
662
+ const searchError = ref(null);
663
+ const detailsError = ref(null);
664
+
665
+ // Watch for search query changes
666
+ watch(searchQuery, async (query) => {
667
+ if (!query) {
668
+ searchResults.value = null;
669
+ return;
670
+ }
671
+
672
+ isSearching.value = true;
673
+ searchError.value = null;
674
+
675
+ try {
676
+ const response = await fetch(`/api/users/search?q=${query}`);
677
+ searchResults.value = await response.json();
678
+ } catch (e) {
679
+ searchError.value = e.message;
680
+ } finally {
681
+ isSearching.value = false;
682
+ }
683
+ });
684
+
685
+ async function loadUserDetails(userId) {
686
+ isLoadingDetails.value = true;
687
+ detailsError.value = null;
688
+
689
+ try {
690
+ // Chain two requests manually
691
+ const userRes = await fetch(`/api/users/${userId}`);
692
+ const user = await userRes.json();
693
+
694
+ const postsRes = await fetch(`/api/posts?userId=${userId}`);
695
+ const posts = await postsRes.json();
696
+
697
+ selectedUser.value = { ...user, posts };
698
+ } catch (e) {
699
+ detailsError.value = e.message;
700
+ } finally {
701
+ isLoadingDetails.value = false;
702
+ }
703
+ }
704
+ </script>
705
+ ```
706
+
707
+
708
+ #### **With** Unwrapped
709
+
710
+ ```vue
711
+ <template>
712
+ <div>
713
+ <input v-model="searchQuery" placeholder="Search users..." />
714
+
715
+ <!-- Search results with automatic state management -->
716
+ <CustomAsyncResultLoader :result="searchResults">
717
+ <template #default="{ value: users }">
718
+ <div v-for="user in users" :key="user.id"
719
+ @click="selectedUserId = user.id">
720
+ {{ user.name }}
721
+ </div>
722
+ </template>
723
+
724
+ <template #idle>
725
+ <div>Enter a search query</div>
726
+ </template>
727
+ </CustomAsyncResultLoader>
728
+
729
+ <!-- User details with chained operations -->
730
+ <CustomAsyncResultLoader v-if="selectedUserId" :result="userDetails">
731
+ <template #default="{ value: userData }">
732
+ <h2>{{ userData.user.name }}</h2>
733
+ <p>Email: {{ userData.user.email }}</p>
734
+ <p>Posts: {{ userData.posts.length }}</p>
735
+ </template>
736
+ </CustomAsyncResultLoader>
737
+ </div>
738
+ </template>
739
+
740
+ <script setup>
741
+ import { ref } from 'vue';
742
+ import { AsyncResultLoader, useReactiveChain, useReactiveGenerator }
743
+ from 'unwrapped/vue';
744
+ import { AsyncResult, ErrorBase } from 'unwrapped/core';
745
+ import CustomAsyncResultLoader from 'src/your/own/component'; // Made with buildCustomAsyncResultLoader()
746
+
747
+ const searchQuery = ref('');
748
+ const selectedUserId = ref(null);
749
+
750
+ // Automatically re-fetches when searchQuery changes
751
+ // No need for manual watch() or state management
752
+ const searchResults = useReactiveChain(
753
+ () => searchQuery.value, // Reactive source
754
+ (query) => {
755
+ // Return idle state if no query
756
+ if (!query) return AsyncResult.idle();
757
+
758
+ // Otherwise fetch - loading/error states handled automatically
759
+ return AsyncResult.fromValuePromise(
760
+ fetch(`/api/users/search?q=${query}`).then(r => r.json())
761
+ );
762
+ },
763
+ { immediate: true }
764
+ );
765
+
766
+ // Generator syntax makes chaining multiple async operations elegant
767
+ // Automatically re-runs when selectedUserId changes
768
+ const userDetails = useReactiveGenerator(
769
+ () => selectedUserId.value, // Reactive source
770
+ function* (userId) {
771
+ if (!userId) return null;
772
+
773
+ // yield* unwraps AsyncResults - if any fail, whole chain fails
774
+ // No manual error handling needed for each step!
775
+ const user = yield* AsyncResult.fromValuePromise(
776
+ fetch(`/api/users/${userId}`).then(r => r.json())
777
+ );
778
+
779
+ const posts = yield* AsyncResult.fromValuePromise(
780
+ fetch(`/api/posts?userId=${userId}`).then(r => r.json())
781
+ );
782
+
783
+ // Return combined result - automatically wrapped in success state
784
+ return { user, posts };
785
+ }
786
+ );
787
+
788
+ // That's it! No manual:
789
+ // - loading state tracking
790
+ // - error state tracking
791
+ // - try/catch blocks
792
+ // - watch() cleanup
793
+ // - state reset on new requests
794
+ // All handled automatically by Unwrapped!
795
+ </script>
796
+ ```
797
+
798
+ ## Why Unwrapped ?
799
+
800
+ Traditional error handling in TypeScript relies on thrown errors with try/catch blocks. While this is great for "catstrophic failures" to make the whole app explode. This is great for simple scripts (which was, to be fair, the original intended purpose of JavaScript), but having your whole app panic because of a random JSON.parse() burried in your code is not ideal. Since the advent of Promises, this basic pattern basically became an absolute requirement for every serious app, and we're stuck developping complex apps with sub-par tools for handling the state of our asynchronous operations, having no really good way to track loading and error states provided by the language.Forgetting to set loading = false in a finally block, or missing an error case, are common sources of bugs. Complex async flows with multiple dependent operations become nested and difficult to follow.
801
+
802
+ Unwrapped addresses these pain points by making error handling explicit and composable through Result types, inspired by the more modern takes on these issues offered by newer languages/tools. A Result<T, E> forces you to acknowledge both success and error cases, with full type safety for both. For async operations, AsyncResult automatically manages the full lifecycle (idle → loading → success/error) so you don't need separate state variables. Generator syntax (yield*) lets you write complex async flows that read sequentially while remaining fully type-safe and composable.
803
+
804
+ The goal is not to reinvent TypeScript or impose a new paradigm, but to reduce boilerplate and eliminate common error-handling bugs while staying close to familiar patterns. If you already use async/await and promises, Unwrapped feels natural—just more robust and explicit about errors.
805
+
806
+
807
+ ### Why not use Tanstack Query instead ?
808
+
809
+ On the frontend, errors and pending states are encountered while fetching data. A very good library for this is Tanstack Query, which has versions for most front-end frameworks. It's great at handling what we talked about earlier, from the basics of loading and error states, to more advanced concepts like caching, invalidation, and retries. It's however not meant as a general purpose error handling mechanism and lacks a proper Result type able to be used in other contexts in your codebase, and instead leans on the thrown errors pattern. It's thus more focused on a very specific part of your frontend application.
810
+
811
+ Unwrapped takes a more general approach, and instead of starting from the top (the useQuery primitive of Tanstack Query), starts from the bottom, with error and result types. These build primitives that can be easily built upon to compose features that catch up with the capabilities of Tanstack Query, especially via the framework specific bindings (`unwrapped/vue` for instance). Results and AsyncResults can be chained (via their `.chain()` and `.flatChain()` methods) for general synchronous or asynchronous computations that may fail at each step, and those chains can be written in a more imperative-looking way with the generators. These features get composed to allow the `KeyedAsyncCache` to perform automatically deduping, invalidations, and retries of any asynchronous operations, which can be easily used in your state management library of choice, like zustand or pinia. While Unwrapped is not yet at feature parity with Tanstack Query for the specific area it covers, its primitives are more composables and can be used in every context when needed. In fact, while first thought for front-end development, the `unwrapped/core` sub-module is pure typescript and can be used on the backend.
812
+
813
+ ### Why not use Effect instead ?
814
+
815
+ Effect is an incredibly powerful library (that could even be called a framework) that has the ambition to "Fix TypeScript". It succeeds very well in this in its own way, but at the cost of almost becoming its own language. Some projects can benefit immensly from this, but it is overkill for simpler, smaller projects. Sometimes you just want your existing tools (TypeScript), but made a little more convenient, and that's where Unwrapped comes into play.
816
+
817
+ Unwrapped draws a lot of inspiration from Effect and its concepts (especially for the generators) while trying to make them more accessible, with a much more minimal set of APIs. Unwrapped also aims to play nicely with the existing UI frameworks (React, Vue, etc...) by providing integrations that bring Unwrapped's features into the common way of writing apps in those frameworks. In short, it aims to bring some of Effect's concepts to the Vue/React/Svelte way of writing, without disrupting existing patterns.
818
+
819
+ ## Next planned features
820
+
821
+ Unwrapped is in very active development and a lot of features are still planned, such as :
822
+ - Abort and retries on AsyncResult
823
+ - Better support for concurrency
824
+ - Common utilities (like fetch()) using AsyncResult so you don't have to wrap them in a AsyncResult.fromValuePromise()
825
+ - Debounce on relevant utilities
826
+
827
+
828
+ ## API Reference
829
+
830
+ ### Core Module (`unwrapped/core`)
831
+
832
+ - **`Result<T, E>`**: Synchronous result type
833
+ - **`AsyncResult<T, E>`**: Asynchronous result with state tracking
834
+ - **`ErrorBase`**: Base error class with structured logging
835
+ - **`KeyedAsyncCache<P, V, E>`**: Cache for async operations
836
+
837
+ ### Vue Module (`unwrapped/vue`)
838
+
839
+ - **Composables**: `useAsyncResultRef`, `useAction`, `useLazyAction`, `useReactiveChain`, `useGenerator`, `useLazyGenerator`, `useReactiveGenerator`
840
+ - **Components**: `AsyncResultLoader`, `buildCustomAsyncResultLoader`
841
+
842
+ ## License
843
+
844
+ LGPL-3.0-or-later
845
+
846
+ ## Contributing
847
+
848
+ Contributions are welcome! Please feel free to submit issues or pull requests.