try-ok 0.1.2 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025 Sangun Kang <sangun950@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -20,86 +20,77 @@ That's why I created `try-ok`—to fix these habits with a tiny, zero-dependency
20
20
  ## Installation
21
21
 
22
22
  ```bash
23
- npm install @sangun-kang/try-ok
23
+ npm install try-ok
24
24
  ```
25
25
 
26
- ## How to use
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
- import { tryOk } from "@sangun-kang/try-ok";
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); // Typed as unknown (or your custom type)
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
- `try-ok` works well inside React components, especially when calling an existing async function:
41
+ ## API
42
+
43
+ ### `tryOk(promise)` — Async
44
+
45
+ ```ts
46
+ const result = await tryOk(fetchUser());
47
+ ```
48
+
49
+ ### `tryOkSync(fn)` — Sync
50
+
51
+ ```ts
52
+ const result = tryOkSync(() => JSON.parse(jsonString));
53
+ ```
64
54
 
65
- ```tsx
66
- import { tryOk } from "@sangun-kang/try-ok";
55
+ ### `unwrap(result, fallback)`
67
56
 
68
- export default async function Page() {
69
- const result = await tryOk(getData());
57
+ ```ts
58
+ const user = unwrap(result, defaultUser); // Returns data or fallback
59
+ ```
70
60
 
71
- if (result.isError) {
72
- return <div>Oops!</div>;
73
- }
61
+ ### `ok(data)` / `err(error)` — Create Result directly
74
62
 
75
- return <div>I'm so happy</div>;
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
- ## Types
72
+ ```ts
73
+ if (isOk(result)) { /* result.data available */ }
74
+ if (isErr(result)) { /* result.error available */ }
75
+ ```
82
76
 
83
- The implementation is minimal. No magic.
77
+ ## Types
84
78
 
85
79
  ```ts
86
- export type Ok<T> = { isError: false; data: T };
87
- export type Err<E> = { isError: true; error: E };
88
- export type Result<T, E = unknown> = Ok<T> | Err<E>;
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 s(r){try{let t=await r;return o(t)}catch(t){return e(t)}}exports.tryOk=s;
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 s(r){try{let t=await r;return o(t)}catch(t){return e(t)}}export{s as tryOk};
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.1.2",
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
  }