neverpanic 1.0.0-beta.2 → 1.0.0-beta.3
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/dist/index.d.ts +23 -0
- package/dist/index.js +79 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,29 @@ export type Result<T = unknown, E = unknown> = {
|
|
|
5
5
|
success: false;
|
|
6
6
|
error: E;
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Create a typesafe instance of neverpanic.
|
|
10
|
+
*
|
|
11
|
+
* @returns An instance of neverpanic that conforms to the types specified in the generic arguments.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const getUser = n.safeFn(
|
|
15
|
+
* async (id: string) => {
|
|
16
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
17
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
18
|
+
*
|
|
19
|
+
* return { success: true, data: await res.json() };
|
|
20
|
+
* },
|
|
21
|
+
* () => "FAILED_TO_GET_USER"
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* const getUserResult = await getUser("some-user-id");
|
|
25
|
+
* if (!getUserResult.success) {
|
|
26
|
+
* console.error(getUserResult.error);
|
|
27
|
+
* } else {
|
|
28
|
+
* console.log(getUserResult.data);
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
8
31
|
export declare const createNeverpanic: <D = unknown, E = unknown>() => {
|
|
9
32
|
ok: <const T extends D>(data: T) => Result<T, never>;
|
|
10
33
|
err: <const T extends E>(error: T) => Result<never, T>;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a typesafe instance of neverpanic.
|
|
3
|
+
*
|
|
4
|
+
* @returns An instance of neverpanic that conforms to the types specified in the generic arguments.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const getUser = n.safeFn(
|
|
8
|
+
* async (id: string) => {
|
|
9
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
10
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
11
|
+
*
|
|
12
|
+
* return { success: true, data: await res.json() };
|
|
13
|
+
* },
|
|
14
|
+
* () => "FAILED_TO_GET_USER"
|
|
15
|
+
* );
|
|
16
|
+
*
|
|
17
|
+
* const getUserResult = await getUser("some-user-id");
|
|
18
|
+
* if (!getUserResult.success) {
|
|
19
|
+
* console.error(getUserResult.error);
|
|
20
|
+
* } else {
|
|
21
|
+
* console.log(getUserResult.data);
|
|
22
|
+
* }
|
|
23
|
+
*/
|
|
1
24
|
export const createNeverpanic = () => {
|
|
2
25
|
const ok = (data) => ({
|
|
3
26
|
success: true,
|
|
@@ -7,6 +30,31 @@ export const createNeverpanic = () => {
|
|
|
7
30
|
success: false,
|
|
8
31
|
error,
|
|
9
32
|
});
|
|
33
|
+
/**
|
|
34
|
+
* Create a safe function from an unsafe one.
|
|
35
|
+
*
|
|
36
|
+
* @param cb - The async function to wrap.
|
|
37
|
+
* @param [eh] - Optional fallback error handler.
|
|
38
|
+
* @returns A new function that returns a typesafe Result.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const getUser = n.safeFn(
|
|
42
|
+
* async (id: string) => {
|
|
43
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
44
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
45
|
+
*
|
|
46
|
+
* return { success: true, data: await res.json() };
|
|
47
|
+
* },
|
|
48
|
+
* () => "FAILED_TO_GET_USER"
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* const getUserResult = await getUser("some-user-id");
|
|
52
|
+
* if (!getUserResult.success) {
|
|
53
|
+
* console.error(getUserResult.error);
|
|
54
|
+
* } else {
|
|
55
|
+
* console.log(getUserResult.data);
|
|
56
|
+
* }
|
|
57
|
+
*/
|
|
10
58
|
const safeFn = (cb, eh) => (...args) => {
|
|
11
59
|
try {
|
|
12
60
|
const result = cb(...args);
|
|
@@ -18,6 +66,21 @@ export const createNeverpanic = () => {
|
|
|
18
66
|
return eh(e);
|
|
19
67
|
}
|
|
20
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* Run an unsafe function, handle any errors and return a Result.
|
|
71
|
+
*
|
|
72
|
+
* @param cb - The async function to call.
|
|
73
|
+
* @param [eh] - Optional fallback error handler.
|
|
74
|
+
* @returns The awaited return value of cb.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* const user = await n.fromUnsafe(() => db.findUser('some-user-id'), () => 'FAILED_T0_FIND_USER')
|
|
78
|
+
* if (!user.success) {
|
|
79
|
+
* console.error(user.error)
|
|
80
|
+
* } else {
|
|
81
|
+
* console.log(user.data)
|
|
82
|
+
* }
|
|
83
|
+
*/
|
|
21
84
|
const fromUnsafe = (cb, eh) => {
|
|
22
85
|
try {
|
|
23
86
|
const result = cb();
|
|
@@ -29,6 +92,22 @@ export const createNeverpanic = () => {
|
|
|
29
92
|
return eh(e);
|
|
30
93
|
}
|
|
31
94
|
};
|
|
95
|
+
/**
|
|
96
|
+
* Convert a list of results into a single result.
|
|
97
|
+
*
|
|
98
|
+
* @param results - A list of Results.
|
|
99
|
+
* @returns A single result containing the data / errors of the input results.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const findUserResults = userIds.map((userId) =>
|
|
103
|
+
* n.fromUnsafe(
|
|
104
|
+
* () => db.findUser(userId),
|
|
105
|
+
* () => "FAILED_TO_FIND_USER" as const,
|
|
106
|
+
* ),
|
|
107
|
+
* );
|
|
108
|
+
*
|
|
109
|
+
* const result = n.resultsToResult(findUserResults)
|
|
110
|
+
*/
|
|
32
111
|
const resultsToResult = (results) => {
|
|
33
112
|
const errors = results.filter((result) => !result.success);
|
|
34
113
|
if (errors.length)
|