try-ok 0.1.3 → 0.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 +36 -45
- package/dist/try-ok.cjs +1 -1
- package/dist/try-ok.d.cts +7 -1
- package/dist/try-ok.d.ts +7 -1
- package/dist/try-ok.js +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -23,83 +23,74 @@ That's why I created `try-ok`—to fix these habits with a tiny, zero-dependency
|
|
|
23
23
|
npm install try-ok
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
##
|
|
27
|
-
|
|
28
|
-
### The Problem (`try-catch`)
|
|
29
|
-
|
|
30
|
-
Using `try-catch` often leads to nested code and loose typing:
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
try {
|
|
34
|
-
const data = await fetch("/api/user").then(r => r.json());
|
|
35
|
-
// ...logic
|
|
36
|
-
} catch (error) {
|
|
37
|
-
// ❌ What is 'error'? We don't know.
|
|
38
|
-
// ❌ strict typing is lost here.
|
|
39
|
-
console.error(error);
|
|
40
|
-
}
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### The Solution (`try-ok`)
|
|
44
|
-
|
|
45
|
-
With `try-ok`, you handle errors explicitly as return values:
|
|
26
|
+
## Quick Start
|
|
46
27
|
|
|
47
28
|
```ts
|
|
48
29
|
import { tryOk } from "try-ok";
|
|
49
30
|
|
|
50
31
|
const result = await tryOk(fetch("/api/user").then(r => r.json()));
|
|
51
32
|
|
|
52
|
-
// 1. Handle Error First (Type Guard)
|
|
53
33
|
if (result.isError) {
|
|
54
|
-
console.error(result.error);
|
|
34
|
+
console.error(result.error);
|
|
55
35
|
return;
|
|
56
36
|
}
|
|
57
37
|
|
|
58
|
-
// 2. Safe to proceed
|
|
59
|
-
// 'result.data' is now guaranteed to be valid
|
|
60
38
|
console.log(result.data);
|
|
61
39
|
```
|
|
62
40
|
|
|
63
|
-
|
|
41
|
+
## API
|
|
64
42
|
|
|
65
|
-
|
|
66
|
-
import { tryOk } from "try-ok";
|
|
43
|
+
### `tryOk(promise)` — Async
|
|
67
44
|
|
|
68
|
-
|
|
69
|
-
|
|
45
|
+
```ts
|
|
46
|
+
const result = await tryOk(fetchUser());
|
|
47
|
+
```
|
|
70
48
|
|
|
71
|
-
|
|
72
|
-
return <div>Oops!</div>;
|
|
73
|
-
}
|
|
49
|
+
### `tryOkSync(fn)` — Sync
|
|
74
50
|
|
|
75
|
-
|
|
51
|
+
```ts
|
|
52
|
+
const result = tryOkSync(() => JSON.parse(jsonString));
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `unwrap(result, fallback)`
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const user = unwrap(result, defaultUser); // Returns data or fallback
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### `ok(data)` / `err(error)` — Create Result directly
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
function divide(a: number, b: number): Result<number, string> {
|
|
65
|
+
if (b === 0) return err("Division by zero");
|
|
66
|
+
return ok(a / b);
|
|
76
67
|
}
|
|
77
68
|
```
|
|
78
69
|
|
|
79
|
-
|
|
70
|
+
### `isOk(result)` / `isErr(result)` — Type Guards
|
|
80
71
|
|
|
81
|
-
|
|
72
|
+
```ts
|
|
73
|
+
if (isOk(result)) { /* result.data available */ }
|
|
74
|
+
if (isErr(result)) { /* result.error available */ }
|
|
75
|
+
```
|
|
82
76
|
|
|
83
|
-
|
|
77
|
+
## Types
|
|
84
78
|
|
|
85
79
|
```ts
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
type Ok<T> = { isError: false; data: T };
|
|
81
|
+
type Err<E> = { isError: true; error: E };
|
|
82
|
+
type Result<T, E = unknown> = Ok<T> | Err<E>;
|
|
89
83
|
```
|
|
90
84
|
|
|
91
85
|
## Custom Error Types
|
|
92
86
|
|
|
93
|
-
You can strictly type your errors if needed:
|
|
94
|
-
|
|
95
87
|
```ts
|
|
96
88
|
type ApiError = { status: number; message: string };
|
|
97
89
|
|
|
98
90
|
const result = await tryOk<User, ApiError>(getUser());
|
|
99
91
|
|
|
100
92
|
if (result.isError) {
|
|
101
|
-
// TypeScript knows this is ApiError
|
|
102
|
-
console.log(result.error.status);
|
|
93
|
+
console.log(result.error.status); // TypeScript knows this is ApiError
|
|
103
94
|
}
|
|
104
95
|
```
|
|
105
96
|
|
|
@@ -112,8 +103,8 @@ Still, try-ok has a slightly different goal:
|
|
|
112
103
|
it focuses on stronger type safety and explicit error handling using a clean Result pattern.
|
|
113
104
|
|
|
114
105
|
If you prefer predictable control flow and safer TypeScript,
|
|
115
|
-
this library might fit your style.
|
|
106
|
+
this library might fit your style.
|
|
116
107
|
|
|
117
|
-
|
|
108
|
+
---
|
|
118
109
|
|
|
119
110
|
MIT
|
package/dist/try-ok.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var o=r=>({isError:false,data:r}),e=r=>({isError:true,error:r});async function
|
|
1
|
+
'use strict';var o=r=>({isError:false,data:r}),e=r=>({isError:true,error:r}),s=r=>!r.isError,E=r=>r.isError,n=(r,t)=>r.isError?t:r.data;async function p(r){try{let t=await r;return o(t)}catch(t){return e(t)}}function T(r){try{let t=r();return o(t)}catch(t){return e(t)}}exports.err=e;exports.isErr=E;exports.isOk=s;exports.ok=o;exports.tryOk=p;exports.tryOkSync=T;exports.unwrap=n;
|
package/dist/try-ok.d.cts
CHANGED
|
@@ -7,7 +7,13 @@ type Err<E = unknown> = {
|
|
|
7
7
|
error: E;
|
|
8
8
|
};
|
|
9
9
|
type Result<T, E = unknown> = Ok<T> | Err<E>;
|
|
10
|
+
declare const ok: <T>(data: T) => Ok<T>;
|
|
11
|
+
declare const err: <E>(error: E) => Err<E>;
|
|
12
|
+
declare const isOk: <T, E>(r: Result<T, E>) => r is Ok<T>;
|
|
13
|
+
declare const isErr: <T, E>(r: Result<T, E>) => r is Err<E>;
|
|
14
|
+
declare const unwrap: <T, E, F = T>(result: Result<T, E>, fallback: F) => T | F;
|
|
10
15
|
|
|
11
16
|
declare function tryOk<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;
|
|
17
|
+
declare function tryOkSync<T, E = unknown>(fn: () => T): Result<T, E>;
|
|
12
18
|
|
|
13
|
-
export { type Err, type Ok, type Result, tryOk };
|
|
19
|
+
export { type Err, type Ok, type Result, err, isErr, isOk, ok, tryOk, tryOkSync, unwrap };
|
package/dist/try-ok.d.ts
CHANGED
|
@@ -7,7 +7,13 @@ type Err<E = unknown> = {
|
|
|
7
7
|
error: E;
|
|
8
8
|
};
|
|
9
9
|
type Result<T, E = unknown> = Ok<T> | Err<E>;
|
|
10
|
+
declare const ok: <T>(data: T) => Ok<T>;
|
|
11
|
+
declare const err: <E>(error: E) => Err<E>;
|
|
12
|
+
declare const isOk: <T, E>(r: Result<T, E>) => r is Ok<T>;
|
|
13
|
+
declare const isErr: <T, E>(r: Result<T, E>) => r is Err<E>;
|
|
14
|
+
declare const unwrap: <T, E, F = T>(result: Result<T, E>, fallback: F) => T | F;
|
|
10
15
|
|
|
11
16
|
declare function tryOk<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;
|
|
17
|
+
declare function tryOkSync<T, E = unknown>(fn: () => T): Result<T, E>;
|
|
12
18
|
|
|
13
|
-
export { type Err, type Ok, type Result, tryOk };
|
|
19
|
+
export { type Err, type Ok, type Result, err, isErr, isOk, ok, tryOk, tryOkSync, unwrap };
|
package/dist/try-ok.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var o=r=>({isError:false,data:r}),e=r=>({isError:true,error:r});async function
|
|
1
|
+
var o=r=>({isError:false,data:r}),e=r=>({isError:true,error:r}),s=r=>!r.isError,E=r=>r.isError,n=(r,t)=>r.isError?t:r.data;async function p(r){try{let t=await r;return o(t)}catch(t){return e(t)}}function T(r){try{let t=r();return o(t)}catch(t){return e(t)}}export{e as err,E as isErr,s as isOk,o as ok,p as tryOk,T as tryOkSync,n as unwrap};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "try-ok",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Type-safe error handling for async operations using Result pattern",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Type-safe error handling for async and sync operations using Result pattern",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/try-ok.cjs",
|
|
7
7
|
"module": "./dist/try-ok.js",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/try-ok.d.ts",
|
|
12
12
|
"import": "./dist/try-ok.js",
|
|
13
|
-
"require": "./dist/try-ok.cjs"
|
|
13
|
+
"require": "./dist/try-ok.cjs",
|
|
14
|
+
"default": "./dist/try-ok.js"
|
|
14
15
|
}
|
|
15
16
|
},
|
|
16
17
|
"files": ["dist"],
|
|
@@ -43,6 +44,7 @@
|
|
|
43
44
|
"result",
|
|
44
45
|
"type-safe",
|
|
45
46
|
"async",
|
|
47
|
+
"sync",
|
|
46
48
|
"promise"
|
|
47
49
|
]
|
|
48
50
|
}
|