neverpanic 0.0.5 → 1.0.0-beta.1
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 +16 -13
- package/dist/index.d.ts +5 -10
- package/dist/index.js +32 -16
- package/package.json +14 -9
package/README.md
CHANGED
|
@@ -19,20 +19,23 @@ Create a safe function from an unsafe one:
|
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
21
|
const getUser = n.safeFn(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
async (id: string) => {
|
|
23
|
+
const res = await fetch(
|
|
24
|
+
`https://example.com/users/${id}`,
|
|
25
|
+
);
|
|
26
|
+
if (!res.ok)
|
|
27
|
+
return { success: false, error: "FAILED_TO_FETCH" };
|
|
28
|
+
|
|
29
|
+
return { success: true, data: await res.json() };
|
|
30
|
+
},
|
|
31
|
+
(err) => "FAILED_TO_GET_USER",
|
|
29
32
|
);
|
|
30
33
|
|
|
31
34
|
const getUserResult = await getUser("some-user-id");
|
|
32
35
|
if (!getUserResult.success) {
|
|
33
|
-
|
|
36
|
+
console.error(getUserResult.error);
|
|
34
37
|
} else {
|
|
35
|
-
|
|
38
|
+
console.log(getUserResult.data);
|
|
36
39
|
}
|
|
37
40
|
```
|
|
38
41
|
|
|
@@ -43,12 +46,12 @@ Runs the provided callback function, catching any thrown errors and returning a
|
|
|
43
46
|
|
|
44
47
|
```ts
|
|
45
48
|
const user = await n.fromUnsafe(
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
() => db.findUser("some-user-id"),
|
|
50
|
+
(err) => "FAILED_T0_FIND_USER",
|
|
48
51
|
);
|
|
49
52
|
if (!user.success) {
|
|
50
|
-
|
|
53
|
+
console.error(user.error);
|
|
51
54
|
} else {
|
|
52
|
-
|
|
55
|
+
console.log(user.data);
|
|
53
56
|
}
|
|
54
57
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -5,14 +5,6 @@ export type Result<T = unknown, E = unknown> = {
|
|
|
5
5
|
success: false;
|
|
6
6
|
error: E;
|
|
7
7
|
};
|
|
8
|
-
type DataOf<R extends Result> = R extends {
|
|
9
|
-
success: true;
|
|
10
|
-
data: infer D;
|
|
11
|
-
} ? D : never;
|
|
12
|
-
type ErrorOf<R extends Result> = R extends {
|
|
13
|
-
success: false;
|
|
14
|
-
error: infer E;
|
|
15
|
-
} ? E : never;
|
|
16
8
|
/**
|
|
17
9
|
* Create a safe function from an unsafe one.
|
|
18
10
|
*
|
|
@@ -55,10 +47,13 @@ declare function safeFn<T extends Result | Promise<Result>, A extends unknown[],
|
|
|
55
47
|
* }
|
|
56
48
|
*/
|
|
57
49
|
declare function fromUnsafe<T, E = null, R = T extends Promise<unknown> ? Promise<Result<Awaited<T>, E>> : Result<T, E>>(cb: () => T, eh?: (err: unknown) => E): R;
|
|
58
|
-
declare function resultsToResult<R extends Result[]>(results: R): Result<DataOf<R[number]>[], ErrorOf<R[number]>[]>;
|
|
59
50
|
export declare const n: {
|
|
60
51
|
safeFn: typeof safeFn;
|
|
61
52
|
fromUnsafe: typeof fromUnsafe;
|
|
62
|
-
resultsToResult:
|
|
53
|
+
resultsToResult: <T extends Result[], D extends Extract<T[number], {
|
|
54
|
+
success: true;
|
|
55
|
+
}>, E extends Extract<T[number], {
|
|
56
|
+
success: false;
|
|
57
|
+
}>>(results: T) => Result<D[], E[]>;
|
|
63
58
|
};
|
|
64
59
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -67,26 +67,42 @@ function fromUnsafe(cb, eh) {
|
|
|
67
67
|
try {
|
|
68
68
|
const result = cb();
|
|
69
69
|
if (result instanceof Promise)
|
|
70
|
-
return result
|
|
70
|
+
return result
|
|
71
|
+
.then(createSuccessResult)
|
|
72
|
+
.catch(createErrorResult);
|
|
71
73
|
return createSuccessResult(result);
|
|
72
74
|
}
|
|
73
75
|
catch (e) {
|
|
74
76
|
return createErrorResult(e);
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Convert a list of results into a single result.
|
|
81
|
+
*
|
|
82
|
+
* @param results - A list of Results.
|
|
83
|
+
* @returns A single result containing the data / errors of the input results.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* const findUserResults = userIds.map((userId) =>
|
|
87
|
+
* n.fromUnsafe(
|
|
88
|
+
* () => db.findUser(userId),
|
|
89
|
+
* () => "FAILED_TO_FIND_USER" as const,
|
|
90
|
+
* ),
|
|
91
|
+
* );
|
|
92
|
+
*
|
|
93
|
+
* const result = n.resultsToResult(findUserResults)
|
|
94
|
+
*/
|
|
95
|
+
const resultsToResult = (results) => {
|
|
96
|
+
const errors = results.filter((result) => !result.success);
|
|
97
|
+
if (errors.length)
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: errors,
|
|
101
|
+
};
|
|
102
|
+
const successes = results.filter((result) => result.success);
|
|
103
|
+
return {
|
|
104
|
+
success: true,
|
|
105
|
+
data: successes,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
92
108
|
export const n = { safeFn, fromUnsafe, resultsToResult };
|
package/package.json
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neverpanic",
|
|
3
|
-
"
|
|
4
|
-
"version": "0.0.5",
|
|
5
|
-
"type": "module",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
6
4
|
"repository": {
|
|
7
5
|
"url": "https://github.com/bgrcs/neverpanic"
|
|
8
6
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"module": "index.ts",
|
|
14
12
|
"exports": {
|
|
15
13
|
".": "./dist/index.js"
|
|
16
14
|
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "bun test",
|
|
18
|
+
"fmt": "oxfmt --check",
|
|
19
|
+
"fmt:fix": "oxfmt --write"
|
|
20
|
+
},
|
|
17
21
|
"devDependencies": {
|
|
18
|
-
"@types/bun": "latest"
|
|
22
|
+
"@types/bun": "latest",
|
|
23
|
+
"oxfmt": "0.32.0"
|
|
19
24
|
},
|
|
20
25
|
"peerDependencies": {
|
|
21
26
|
"typescript": "5"
|