neverpanic 0.0.3 → 0.0.4
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 +54 -0
- package/dist/index.js +74 -58
- package/package.json +4 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type Result<T = unknown, E = unknown> = {
|
|
2
|
+
success: true;
|
|
3
|
+
data: T;
|
|
4
|
+
} | {
|
|
5
|
+
success: false;
|
|
6
|
+
error: E;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Create a safe function from an unsafe one.
|
|
10
|
+
*
|
|
11
|
+
* @param cb - The async function to wrap.
|
|
12
|
+
* @param [eh] - Optional fallback error handler.
|
|
13
|
+
* @returns A new function that returns a typesafe Result.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const getUser = n.safeFn(
|
|
17
|
+
* async (id: string) => {
|
|
18
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
19
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
20
|
+
*
|
|
21
|
+
* return { success: true, data: await res.json() };
|
|
22
|
+
* },
|
|
23
|
+
* () => "FAILED_TO_GET_USER"
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* const getUserResult = await getUser("some-user-id");
|
|
27
|
+
* if (!getUserResult.success) {
|
|
28
|
+
* console.error(getUserResult.error);
|
|
29
|
+
* } else {
|
|
30
|
+
* console.log(getUserResult.data);
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
33
|
+
declare function safeFn<T extends Result | Promise<Result>, A extends unknown[], E = null>(cb: (...args: A) => T, eh?: (e: unknown) => E): (...args: A) => T | Result<never, E>;
|
|
34
|
+
/**
|
|
35
|
+
* Run an unsafe function, handle any errors and return a Result.
|
|
36
|
+
*
|
|
37
|
+
* @param cb - The async function to call.
|
|
38
|
+
* @param [eh] - Optional fallback error handler.
|
|
39
|
+
* @returns The awaited return value of cb.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const user = await n.fromUnsafe(() => db.findUser('some-user-id'), () => 'FAILED_T0_FIND_USER')
|
|
43
|
+
* if (!user.success) {
|
|
44
|
+
* console.error(user.error)
|
|
45
|
+
* } else {
|
|
46
|
+
* console.log(user.data)
|
|
47
|
+
* }
|
|
48
|
+
*/
|
|
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;
|
|
50
|
+
export declare const n: {
|
|
51
|
+
safeFn: typeof safeFn;
|
|
52
|
+
fromUnsafe: typeof fromUnsafe;
|
|
53
|
+
};
|
|
54
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,61 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Create a safe function from an unsafe one.
|
|
3
|
+
*
|
|
4
|
+
* @param cb - The async function to wrap.
|
|
5
|
+
* @param [eh] - Optional fallback error handler.
|
|
6
|
+
* @returns A new function that returns a typesafe Result.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const getUser = n.safeFn(
|
|
10
|
+
* async (id: string) => {
|
|
11
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
12
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
13
|
+
*
|
|
14
|
+
* return { success: true, data: await res.json() };
|
|
15
|
+
* },
|
|
16
|
+
* () => "FAILED_TO_GET_USER"
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* const getUserResult = await getUser("some-user-id");
|
|
20
|
+
* if (!getUserResult.success) {
|
|
21
|
+
* console.error(getUserResult.error);
|
|
22
|
+
* } else {
|
|
23
|
+
* console.log(getUserResult.data);
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
function safeFn(cb, eh) {
|
|
27
|
+
const createErrorResult = (e) => ({
|
|
28
|
+
success: false,
|
|
29
|
+
error: eh?.(e) ?? null,
|
|
9
30
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
switch (_b.label) {
|
|
46
|
-
case 0:
|
|
47
|
-
_b.trys.push([0, 2, , 3]);
|
|
48
|
-
return [4 /*yield*/, cb()];
|
|
49
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
50
|
-
case 2:
|
|
51
|
-
e_1 = _b.sent();
|
|
52
|
-
return [2 /*return*/, {
|
|
53
|
-
success: false,
|
|
54
|
-
error: (_a = eh === null || eh === void 0 ? void 0 : eh(e_1)) !== null && _a !== void 0 ? _a : null,
|
|
55
|
-
}];
|
|
56
|
-
case 3: return [2 /*return*/];
|
|
57
|
-
}
|
|
58
|
-
});
|
|
31
|
+
return (...args) => {
|
|
32
|
+
try {
|
|
33
|
+
const result = cb(...args);
|
|
34
|
+
if (result instanceof Promise)
|
|
35
|
+
return result.catch(createErrorResult);
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
return createErrorResult(e);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Run an unsafe function, handle any errors and return a Result.
|
|
45
|
+
*
|
|
46
|
+
* @param cb - The async function to call.
|
|
47
|
+
* @param [eh] - Optional fallback error handler.
|
|
48
|
+
* @returns The awaited return value of cb.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const user = await n.fromUnsafe(() => db.findUser('some-user-id'), () => 'FAILED_T0_FIND_USER')
|
|
52
|
+
* if (!user.success) {
|
|
53
|
+
* console.error(user.error)
|
|
54
|
+
* } else {
|
|
55
|
+
* console.log(user.data)
|
|
56
|
+
* }
|
|
57
|
+
*/
|
|
58
|
+
function fromUnsafe(cb, eh) {
|
|
59
|
+
const createErrorResult = (e) => ({
|
|
60
|
+
success: false,
|
|
61
|
+
error: eh?.(e) ?? null,
|
|
62
|
+
});
|
|
63
|
+
const createSuccessResult = (data) => ({
|
|
64
|
+
success: true,
|
|
65
|
+
data,
|
|
59
66
|
});
|
|
67
|
+
try {
|
|
68
|
+
const result = cb();
|
|
69
|
+
if (result instanceof Promise)
|
|
70
|
+
return result.then(createSuccessResult).catch(createErrorResult);
|
|
71
|
+
return createSuccessResult(result);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
return createErrorResult(e);
|
|
75
|
+
}
|
|
60
76
|
}
|
|
61
|
-
|
|
77
|
+
export const n = { safeFn, fromUnsafe };
|
package/package.json
CHANGED