try-ok 0.1.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 ADDED
@@ -0,0 +1,119 @@
1
+ # try-ok
2
+
3
+ **Predictable, type-safe error handling for TypeScript.**
4
+ Stop throwing. Start returning.
5
+
6
+ ## Motivation (Why I built this)
7
+
8
+ In my projects, I noticed that `try-catch` was creating more problems than it solved.
9
+
10
+ As our codebase grew, we faced the same issues repeatedly:
11
+
12
+ * Inside `catch(e)`, the error is always `unknown`, so TypeScript can't help us.
13
+ * `throw` breaks the control flow, making logic hard to follow.
14
+ * It's easy to forget error handling when it's hidden in a `catch` block.
15
+
16
+ I wanted a way to write safer code without introducing a heavy framework. I needed something simple that treats errors as **values**, just like in Go or Rust.
17
+
18
+ That's why I created `try-ok`β€”to fix these habits with a tiny, zero-dependency tool.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install @sangun-kang/try-ok
24
+ ```
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:
46
+
47
+ ```ts
48
+ import { tryResult } from "@sangun-kang/try-ok";
49
+
50
+ const result = await tryResult(fetch("/api/user").then(r => r.json()));
51
+
52
+ // 1. Handle Error First (Type Guard)
53
+ if (result.isError) {
54
+ console.error(result.error); // Typed as unknown (or your custom type)
55
+ return;
56
+ }
57
+
58
+ // 2. Safe to proceed
59
+ // 'result.data' is now guaranteed to be valid
60
+ console.log(result.data);
61
+ ```
62
+
63
+ `try-ok` works well inside React components, especially when calling an existing async function:
64
+
65
+ ```tsx
66
+ import { tryResult } from "@sangun-kang/try-ok";
67
+
68
+ export default async function Page() {
69
+ const result = await tryResult(getData());
70
+
71
+ if (result.isError) {
72
+ return <div>Oops!</div>;
73
+ }
74
+
75
+ return <div>I'm so happy</div>;
76
+ }
77
+ ```
78
+
79
+ -----
80
+
81
+ ## Types
82
+
83
+ The implementation is minimal. No magic.
84
+
85
+ ```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>;
89
+ ```
90
+
91
+ ## Custom Error Types
92
+
93
+ You can strictly type your errors if needed:
94
+
95
+ ```ts
96
+ type ApiError = { status: number; message: string };
97
+
98
+ const result = await tryResult<User, ApiError>(getUser());
99
+
100
+ if (result.isError) {
101
+ // TypeScript knows this is ApiError
102
+ console.log(result.error.status);
103
+ }
104
+ ```
105
+
106
+ ## Why another library?
107
+
108
+ I actually found a lot of similar OSS!
109
+ Seems like developers everywhere have had the same idea haha.
110
+
111
+ Still, try-result has a slightly different goal:
112
+ it focuses on stronger type safety and explicit error handling using a clean Result pattern.
113
+
114
+ If you prefer predictable control flow and safer TypeScript,
115
+ this library might fit your style. 😊
116
+
117
+ -----
118
+
119
+ MIT
@@ -0,0 +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;
@@ -0,0 +1,13 @@
1
+ type Ok<T> = {
2
+ isError: false;
3
+ data: T;
4
+ };
5
+ type Err<E = unknown> = {
6
+ isError: true;
7
+ error: E;
8
+ };
9
+ type Result<T, E = unknown> = Ok<T> | Err<E>;
10
+
11
+ declare function tryOk<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;
12
+
13
+ export { type Err, type Ok, type Result, tryOk };
@@ -0,0 +1,13 @@
1
+ type Ok<T> = {
2
+ isError: false;
3
+ data: T;
4
+ };
5
+ type Err<E = unknown> = {
6
+ isError: true;
7
+ error: E;
8
+ };
9
+ type Result<T, E = unknown> = Ok<T> | Err<E>;
10
+
11
+ declare function tryOk<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;
12
+
13
+ export { type Err, type Ok, type Result, tryOk };
package/dist/try-ok.js ADDED
@@ -0,0 +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};
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "try-ok",
3
+ "version": "0.1.0",
4
+ "description": "Type-safe error handling for async operations using Result pattern",
5
+ "type": "module",
6
+ "main": "./dist/try-ok.cjs",
7
+ "module": "./dist/try-ok.js",
8
+ "types": "./dist/try-ok.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/try-ok.d.ts",
12
+ "import": "./dist/try-ok.js",
13
+ "require": "./dist/try-ok.cjs"
14
+ }
15
+ },
16
+ "files": ["dist"],
17
+ "scripts": {
18
+ "build": "tsup",
19
+ "typecheck": "tsc --noEmit",
20
+ "test": "vitest",
21
+ "test:run": "vitest run"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.6.2",
25
+ "tsup": "^8.0.0",
26
+ "vitest": "^1.0.0"
27
+ },
28
+ "author": {
29
+ "name": "Sangun Kang",
30
+ "email": "sangun950@gmail.com"
31
+ },
32
+ "license": "MIT",
33
+ "engines": {
34
+ "node": ">=14.16"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/sangun-kang/try-ok.git"
39
+ },
40
+ "keywords": [
41
+ "typescript",
42
+ "error-handling",
43
+ "result",
44
+ "type-safe",
45
+ "async",
46
+ "promise"
47
+ ]
48
+ }