runtry 0.1.0 → 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 +19 -4
- package/README.md +165 -7
- package/package.json +8 -2
package/LICENSE
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
1
|
+
MIT License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
Copyright (c) 2026 Sebastian Sala
|
|
5
4
|
|
|
6
|
-
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,173 @@
|
|
|
1
|
-
#
|
|
1
|
+
# runtry
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Run async functions and return a typed `Result` **instead of throwing**.
|
|
4
|
+
|
|
5
|
+
- ✅ No repetitive `try/catch` in UI code
|
|
6
|
+
- ✅ Typed success/error handling
|
|
7
|
+
- ✅ Pluggable error normalization (matchers / adapters)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Install
|
|
4
12
|
|
|
5
13
|
```bash
|
|
6
|
-
|
|
14
|
+
npm i runtry
|
|
15
|
+
# or
|
|
16
|
+
bun add runtry
|
|
17
|
+
# or
|
|
18
|
+
pnpm add runtry
|
|
7
19
|
```
|
|
8
20
|
|
|
9
|
-
|
|
21
|
+
---
|
|
10
22
|
|
|
11
|
-
|
|
12
|
-
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { run } from "runtry";
|
|
27
|
+
|
|
28
|
+
const result = await run(async () => {
|
|
29
|
+
// any async work
|
|
30
|
+
return 42;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
if (result.ok) {
|
|
34
|
+
console.log("data:", result.data);
|
|
35
|
+
} else {
|
|
36
|
+
console.error("error:", result.error.code, result.error.message);
|
|
37
|
+
}
|
|
13
38
|
```
|
|
14
39
|
|
|
15
|
-
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `run(fn, options?)`
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import type { AppError, RunOptions, RunResult } from "runtry";
|
|
48
|
+
|
|
49
|
+
declare function run<T, E extends AppError = AppError>(
|
|
50
|
+
fn: () => Promise<T>,
|
|
51
|
+
options?: RunOptions<T, E>
|
|
52
|
+
): Promise<RunResult<T, E>>;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- Returns `{ ok: true, data, error: null }` on success
|
|
56
|
+
- Returns `{ ok: false, data: null, error }` on failure
|
|
57
|
+
- Never throws (unless your callbacks throw)
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Normalizing errors (matchers)
|
|
62
|
+
|
|
63
|
+
`runtry` can normalize unknown thrown values (`unknown`) into a consistent `AppError` shape.
|
|
64
|
+
|
|
65
|
+
### 1) Create matchers (adapters)
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import {
|
|
69
|
+
createNormalizer,
|
|
70
|
+
abortMatcher,
|
|
71
|
+
instanceOfMatcher,
|
|
72
|
+
defaultFallback,
|
|
73
|
+
} from "runtry";
|
|
74
|
+
import type { AppError } from "runtry";
|
|
75
|
+
import { HttpError } from "./http-error"; // your app error class
|
|
76
|
+
|
|
77
|
+
type MyMeta = { url?: string; body?: unknown };
|
|
78
|
+
|
|
79
|
+
const httpMatcher = instanceOfMatcher(HttpError, (e) => ({
|
|
80
|
+
code: "HTTP",
|
|
81
|
+
message: e.message,
|
|
82
|
+
status: e.status,
|
|
83
|
+
meta: { url: e.url, body: e.body } as MyMeta,
|
|
84
|
+
cause: e,
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
const toError = createNormalizer<AppError<MyMeta>>(
|
|
88
|
+
[abortMatcher, httpMatcher],
|
|
89
|
+
(e) => defaultFallback(e) as AppError<MyMeta>
|
|
90
|
+
);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 2) Use the normalizer in `run`
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { run } from "runtry";
|
|
97
|
+
|
|
98
|
+
const res = await run(getDocuments, {
|
|
99
|
+
toError,
|
|
100
|
+
onError: (e) => console.log(e.status, e.message),
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## `createClient()` (configure once, reuse everywhere)
|
|
107
|
+
|
|
108
|
+
If you don't want to pass `toError` every time, create a client:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import {
|
|
112
|
+
createClient,
|
|
113
|
+
abortMatcher,
|
|
114
|
+
instanceOfMatcher,
|
|
115
|
+
defaultFallback,
|
|
116
|
+
} from "runtry";
|
|
117
|
+
import { HttpError } from "./http-error";
|
|
118
|
+
|
|
119
|
+
const client = createClient({
|
|
120
|
+
matchers: [
|
|
121
|
+
abortMatcher,
|
|
122
|
+
instanceOfMatcher(HttpError, (e) => ({
|
|
123
|
+
code: "HTTP",
|
|
124
|
+
message: e.message,
|
|
125
|
+
status: e.status,
|
|
126
|
+
meta: { url: e.url, body: e.body },
|
|
127
|
+
cause: e,
|
|
128
|
+
})),
|
|
129
|
+
],
|
|
130
|
+
fallback: defaultFallback,
|
|
131
|
+
ignoreAbort: true, // default
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const result = await client.run(getDocuments, {
|
|
135
|
+
onError: (e) => console.log(e.code, e.message),
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## React example (loading + toast, no try/catch)
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { createClient, abortMatcher, defaultFallback } from "runtry";
|
|
145
|
+
|
|
146
|
+
const client = createClient({
|
|
147
|
+
matchers: [abortMatcher],
|
|
148
|
+
fallback: defaultFallback,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
let cancelled = false;
|
|
153
|
+
setIsLoading(true);
|
|
154
|
+
|
|
155
|
+
client.run(getDocuments, {
|
|
156
|
+
onSuccess: (docs) => {
|
|
157
|
+
if (cancelled) return;
|
|
158
|
+
setUploadedFiles(docs.map(mapper));
|
|
159
|
+
},
|
|
160
|
+
onError: (e) => {
|
|
161
|
+
if (e.code === "ABORTED") return;
|
|
162
|
+
toast.error(e.message);
|
|
163
|
+
},
|
|
164
|
+
onFinally: () => {
|
|
165
|
+
if (!cancelled) setIsLoading(false);
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return () => {
|
|
170
|
+
cancelled = true;
|
|
171
|
+
};
|
|
172
|
+
}, []);
|
|
173
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "runtry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Run async functions and return a typed Result instead of throwing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,5 +24,11 @@
|
|
|
24
24
|
"@types/bun": "latest",
|
|
25
25
|
"typescript": "^5.0.0"
|
|
26
26
|
},
|
|
27
|
-
"license": "MIT"
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"homepage": "https://github.com/sebastiansala/trysome#readme",
|
|
29
|
+
"bugs": "https://github.com/sebastiansala/trysome/issues",
|
|
30
|
+
"author": {
|
|
31
|
+
"name": "sebasxsala",
|
|
32
|
+
"url": "https://github.com/sebasxsala"
|
|
33
|
+
}
|
|
28
34
|
}
|