go-go-try 6.0.1 → 6.0.2
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/package.json +50 -50
- package/src/index.ts +63 -63
- package/tsconfig.json +5 -1
package/package.json
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
2
|
+
"name": "go-go-try",
|
|
3
|
+
"version": "6.0.2",
|
|
4
|
+
"description": "Tries to execute a sync/async function, returns a result tuple",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "thelinuxlich/go-go-try",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Alisson Cavalcante Agiani",
|
|
9
|
+
"email": "thelinuxlich@gmail.com"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.cts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"default": "./dist/index.mjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=16"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "pkgroll && cp src/types.d.ts dist/",
|
|
30
|
+
"lint": "biome lint --write src/*.ts",
|
|
31
|
+
"test": "npm run build && npm run lint && vitest run --typecheck"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"errors",
|
|
35
|
+
"try",
|
|
36
|
+
"catch",
|
|
37
|
+
"error handling",
|
|
38
|
+
"nice-try",
|
|
39
|
+
"good-try",
|
|
40
|
+
"go-go-try",
|
|
41
|
+
"gotry",
|
|
42
|
+
"go-try"
|
|
43
|
+
],
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@ark/attest": "^0.46.0",
|
|
46
|
+
"@biomejs/biome": "^1.9.4",
|
|
47
|
+
"pkgroll": "^2.12.1",
|
|
48
|
+
"tsx": "^4.19.3",
|
|
49
|
+
"typescript": "^5.8.3",
|
|
50
|
+
"vitest": "^3.1.2"
|
|
51
|
+
}
|
|
52
52
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
export type Success<T> = readonly [undefined, T]
|
|
2
|
-
export type Failure<E> = readonly [E, undefined]
|
|
3
|
-
export type Result<E, T> = Success<T> | Failure<E
|
|
1
|
+
export type Success<T> = readonly [undefined, T]
|
|
2
|
+
export type Failure<E> = readonly [E, undefined]
|
|
3
|
+
export type Result<E, T> = Success<T> | Failure<E>
|
|
4
4
|
|
|
5
|
-
export type MaybePromise<T> = T | Promise<T
|
|
5
|
+
export type MaybePromise<T> = T | Promise<T>
|
|
6
6
|
|
|
7
7
|
export function isSuccess<E, T>(result: Result<E, T>): result is Success<T> {
|
|
8
|
-
|
|
8
|
+
return result[0] === undefined
|
|
9
9
|
}
|
|
10
10
|
export function isFailure<E, T>(result: Result<E, T>): result is Failure<E> {
|
|
11
|
-
|
|
11
|
+
return result[0] !== undefined
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export function success<T>(value: T): Success<T> {
|
|
15
|
-
|
|
15
|
+
return [undefined, value] as const
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function failure<E>(error: E): Failure<E> {
|
|
19
|
-
|
|
19
|
+
return [error, undefined] as const
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
function getErrorMessage(error: unknown): string {
|
|
23
|
-
|
|
23
|
+
if (typeof error === 'string') return error
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
if (
|
|
26
|
+
typeof error === 'object' &&
|
|
27
|
+
error !== null &&
|
|
28
|
+
'message' in error &&
|
|
29
|
+
typeof (error as Record<string, unknown>).message === 'string'
|
|
30
|
+
) {
|
|
31
|
+
return (error as { message: string }).message
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
try {
|
|
35
|
+
return JSON.stringify(error)
|
|
36
|
+
} catch {
|
|
37
|
+
// fallback in case there's an error stringifying the error
|
|
38
|
+
// with circular references for example.
|
|
39
|
+
return String(error)
|
|
40
|
+
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
function isPromise<T>(value: unknown): value is Promise<T> {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
return (
|
|
45
|
+
typeof value === 'object' &&
|
|
46
|
+
value !== null &&
|
|
47
|
+
'then' in value &&
|
|
48
|
+
typeof (value as { then: unknown }).then === 'function'
|
|
49
|
+
)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export function goTry<T>(fn: () => T): Result<string, T
|
|
53
|
-
export function goTry<T>(value: T): Result<string, T
|
|
54
|
-
export function goTry<T>(promise: Promise<T>): Promise<Result<string, T
|
|
52
|
+
export function goTry<T>(fn: () => T): Result<string, T>
|
|
53
|
+
export function goTry<T>(value: T): Result<string, T>
|
|
54
|
+
export function goTry<T>(promise: Promise<T>): Promise<Result<string, T>>
|
|
55
55
|
export function goTry<T>(
|
|
56
|
-
|
|
56
|
+
value: T | Promise<T> | (() => T | Promise<T>),
|
|
57
57
|
): Result<string, T> | Promise<Result<string, T>> {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
try {
|
|
59
|
+
const result =
|
|
60
|
+
typeof value === 'function' ? (value as () => T | Promise<T>)() : value
|
|
61
|
+
if (isPromise<T>(result)) {
|
|
62
|
+
return result
|
|
63
|
+
.then((resolvedValue) => success<T>(resolvedValue))
|
|
64
|
+
.catch((err) => failure<string>(getErrorMessage(err)))
|
|
65
|
+
}
|
|
66
|
+
return success<T>(result)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return failure<string>(getErrorMessage(err))
|
|
69
|
+
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
export function goTryRaw<T, E = Error>(fn: () => T): Result<E, T
|
|
73
|
-
export function goTryRaw<T, E = Error>(value: T): Result<E, T
|
|
72
|
+
export function goTryRaw<T, E = Error>(fn: () => T): Result<E, T>
|
|
73
|
+
export function goTryRaw<T, E = Error>(value: T): Result<E, T>
|
|
74
74
|
export function goTryRaw<T, E = Error>(
|
|
75
|
-
|
|
76
|
-
): Promise<Result<E, T
|
|
75
|
+
promise: Promise<T>,
|
|
76
|
+
): Promise<Result<E, T>>
|
|
77
77
|
export function goTryRaw<T, E = Error>(
|
|
78
|
-
|
|
78
|
+
value: T | Promise<T> | (() => T | Promise<T>),
|
|
79
79
|
): Result<E, T> | Promise<Result<E, T>> {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
80
|
+
try {
|
|
81
|
+
const result =
|
|
82
|
+
typeof value === 'function' ? (value as () => T | Promise<T>)() : value
|
|
83
|
+
if (isPromise<T>(result)) {
|
|
84
|
+
return result
|
|
85
|
+
.then((resolvedValue) => success<T>(resolvedValue))
|
|
86
|
+
.catch((err) => failure<E>(err as E))
|
|
87
|
+
}
|
|
88
|
+
return success<T>(result)
|
|
89
|
+
} catch (err) {
|
|
90
|
+
return failure<E>(err as E)
|
|
91
|
+
}
|
|
92
92
|
}
|