neverpanic 0.0.3 → 0.0.5
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 +64 -0
- package/dist/index.js +89 -58
- package/package.json +4 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type Result<T = unknown, E = unknown> = {
|
|
2
|
+
success: true;
|
|
3
|
+
data: T;
|
|
4
|
+
} | {
|
|
5
|
+
success: false;
|
|
6
|
+
error: E;
|
|
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
|
+
/**
|
|
17
|
+
* Create a safe function from an unsafe one.
|
|
18
|
+
*
|
|
19
|
+
* @param cb - The async function to wrap.
|
|
20
|
+
* @param [eh] - Optional fallback error handler.
|
|
21
|
+
* @returns A new function that returns a typesafe Result.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const getUser = n.safeFn(
|
|
25
|
+
* async (id: string) => {
|
|
26
|
+
* const res = await fetch(`https://example.com/users/${id}`);
|
|
27
|
+
* if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
28
|
+
*
|
|
29
|
+
* return { success: true, data: await res.json() };
|
|
30
|
+
* },
|
|
31
|
+
* () => "FAILED_TO_GET_USER"
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* const getUserResult = await getUser("some-user-id");
|
|
35
|
+
* if (!getUserResult.success) {
|
|
36
|
+
* console.error(getUserResult.error);
|
|
37
|
+
* } else {
|
|
38
|
+
* console.log(getUserResult.data);
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
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>;
|
|
42
|
+
/**
|
|
43
|
+
* Run an unsafe function, handle any errors and return a Result.
|
|
44
|
+
*
|
|
45
|
+
* @param cb - The async function to call.
|
|
46
|
+
* @param [eh] - Optional fallback error handler.
|
|
47
|
+
* @returns The awaited return value of cb.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const user = await n.fromUnsafe(() => db.findUser('some-user-id'), () => 'FAILED_T0_FIND_USER')
|
|
51
|
+
* if (!user.success) {
|
|
52
|
+
* console.error(user.error)
|
|
53
|
+
* } else {
|
|
54
|
+
* console.log(user.data)
|
|
55
|
+
* }
|
|
56
|
+
*/
|
|
57
|
+
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
|
+
export declare const n: {
|
|
60
|
+
safeFn: typeof safeFn;
|
|
61
|
+
fromUnsafe: typeof fromUnsafe;
|
|
62
|
+
resultsToResult: typeof resultsToResult;
|
|
63
|
+
};
|
|
64
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,61 +1,92 @@
|
|
|
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
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
-
var e_1;
|
|
43
|
-
var _a;
|
|
44
|
-
return __generator(this, function (_b) {
|
|
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,
|
|
59
62
|
});
|
|
63
|
+
const createSuccessResult = (data) => ({
|
|
64
|
+
success: true,
|
|
65
|
+
data,
|
|
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
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function resultsToResult(results) {
|
|
78
|
+
let success = true;
|
|
79
|
+
const error = [];
|
|
80
|
+
const data = [];
|
|
81
|
+
for (const result of results) {
|
|
82
|
+
if (!result.success) {
|
|
83
|
+
success = false;
|
|
84
|
+
error.push(result.error);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
data.push(result.data);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return success ? { success: true, data } : { success: false, error };
|
|
60
91
|
}
|
|
61
|
-
|
|
92
|
+
export const n = { safeFn, fromUnsafe, resultsToResult };
|
package/package.json
CHANGED