go-go-try 6.0.5 → 6.0.6

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 CHANGED
@@ -46,7 +46,7 @@ const [err, todos = []] = await goTry(fetchTodos()) // err is string | undefined
46
46
  if (err) sendToErrorTrackingService(err)
47
47
 
48
48
  // goTry extracts the error message from the error object, if you want the raw error object, use goTryRaw
49
- const [err, value] = goTryRaw(() => JSON.parse('{/}')) // err will be Error, value will be always T
49
+ const [err, value] = goTryRaw(() => JSON.parse('{/}')) // err is Error | undefined, value is T | undefined
50
50
 
51
51
  // fetch todos, fallback to empty array, send error to your error tracking service
52
52
  const [err, todos = []] = await goTryRaw(fetchTodos()) // err is Error | undefined
@@ -59,11 +59,7 @@ if (err) sendToErrorTrackingService(err)
59
59
 
60
60
  - synchronous/asynchronous function / Promise
61
61
 
62
- **Returns** a tuple with the possible error and result (Golang style)
62
+ **Returns** a tuple with the possible error and result as `[Err | undefined, T | undefined]` (Golang style)
63
63
 
64
64
 
65
65
  If you use TypeScript, the types are well defined and won't let you make a mistake.
66
-
67
- ## Inspiration
68
-
69
- - This library started as a fork of [good-try](https://github.com/astoilkov/good-try) but diverged a lot so I decided to rename it
package/dist/index.d.cts CHANGED
@@ -6,9 +6,50 @@ declare function isSuccess<E, T>(result: Result<E, T>): result is Success<T>;
6
6
  declare function isFailure<E, T>(result: Result<E, T>): result is Failure<E>;
7
7
  declare function success<T>(value: T): Success<T>;
8
8
  declare function failure<E>(error: E): Failure<E>;
9
+ /**
10
+ * Executes a function, promise, or value and returns a Result type.
11
+ * If an error occurs, it returns a Failure with the error message as a string.
12
+ *
13
+ * @template T The type of the successful result
14
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
15
+ * @returns {Result<string, T> | Promise<Result<string, T>>} A Result type or a Promise of a Result type
16
+ *
17
+ * @example
18
+ * // With a value
19
+ * const [err, result] = goTry(42);
20
+ *
21
+ * @example
22
+ * // With a function
23
+ * const [err, result] = goTry(() => JSON.parse('{"key": "value"}'));
24
+ *
25
+ * @example
26
+ * // With a promise
27
+ * const [err, result] = await goTry(fetch('https://api.example.com/data'));
28
+ */
9
29
  declare function goTry<T>(promise: Promise<T>): Promise<Result<string, T>>;
10
30
  declare function goTry<T>(fn: () => T): Result<string, T>;
11
31
  declare function goTry<T>(value: T): Result<string, T>;
32
+ /**
33
+ * Executes a function, promise, or value and returns a Result type.
34
+ * If an error occurs, it returns a Failure with the raw error object.
35
+ *
36
+ * @template T The type of the successful result
37
+ * @template E The type of the error, defaults to Error
38
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
39
+ * @returns {Result<E, T> | Promise<Result<E, T>>} A Result type or a Promise of a Result type
40
+ *
41
+ * @example
42
+ * // With a value
43
+ * const [err, result] = goTryRaw(42);
44
+ *
45
+ * @example
46
+ * // With a function
47
+ * const [err, result] = goTryRaw(() => JSON.parse('{"key": "value"}'));
48
+ *
49
+ * @example
50
+ * // With a promise
51
+ * const [err, result] = await goTryRaw(fetch('https://api.example.com/data'));
52
+ */
12
53
  declare function goTryRaw<T, E = Error>(promise: Promise<T>): Promise<Result<E, T>>;
13
54
  declare function goTryRaw<T, E = Error>(fn: () => T): Result<E, T>;
14
55
  declare function goTryRaw<T, E = Error>(value: T): Result<E, T>;
package/dist/index.d.mts CHANGED
@@ -6,9 +6,50 @@ declare function isSuccess<E, T>(result: Result<E, T>): result is Success<T>;
6
6
  declare function isFailure<E, T>(result: Result<E, T>): result is Failure<E>;
7
7
  declare function success<T>(value: T): Success<T>;
8
8
  declare function failure<E>(error: E): Failure<E>;
9
+ /**
10
+ * Executes a function, promise, or value and returns a Result type.
11
+ * If an error occurs, it returns a Failure with the error message as a string.
12
+ *
13
+ * @template T The type of the successful result
14
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
15
+ * @returns {Result<string, T> | Promise<Result<string, T>>} A Result type or a Promise of a Result type
16
+ *
17
+ * @example
18
+ * // With a value
19
+ * const [err, result] = goTry(42);
20
+ *
21
+ * @example
22
+ * // With a function
23
+ * const [err, result] = goTry(() => JSON.parse('{"key": "value"}'));
24
+ *
25
+ * @example
26
+ * // With a promise
27
+ * const [err, result] = await goTry(fetch('https://api.example.com/data'));
28
+ */
9
29
  declare function goTry<T>(promise: Promise<T>): Promise<Result<string, T>>;
10
30
  declare function goTry<T>(fn: () => T): Result<string, T>;
11
31
  declare function goTry<T>(value: T): Result<string, T>;
32
+ /**
33
+ * Executes a function, promise, or value and returns a Result type.
34
+ * If an error occurs, it returns a Failure with the raw error object.
35
+ *
36
+ * @template T The type of the successful result
37
+ * @template E The type of the error, defaults to Error
38
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
39
+ * @returns {Result<E, T> | Promise<Result<E, T>>} A Result type or a Promise of a Result type
40
+ *
41
+ * @example
42
+ * // With a value
43
+ * const [err, result] = goTryRaw(42);
44
+ *
45
+ * @example
46
+ * // With a function
47
+ * const [err, result] = goTryRaw(() => JSON.parse('{"key": "value"}'));
48
+ *
49
+ * @example
50
+ * // With a promise
51
+ * const [err, result] = await goTryRaw(fetch('https://api.example.com/data'));
52
+ */
12
53
  declare function goTryRaw<T, E = Error>(promise: Promise<T>): Promise<Result<E, T>>;
13
54
  declare function goTryRaw<T, E = Error>(fn: () => T): Result<E, T>;
14
55
  declare function goTryRaw<T, E = Error>(value: T): Result<E, T>;
package/package.json CHANGED
@@ -1,52 +1,55 @@
1
1
  {
2
- "name": "go-go-try",
3
- "version": "6.0.5",
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",
30
- "lint": "biome lint --write src/*.ts",
31
- "test": "npm run build && npm run lint && vitest run"
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
- }
2
+ "name": "go-go-try",
3
+ "version": "6.0.6",
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",
30
+ "lint": "biome lint --write src/*.ts",
31
+ "test": "npm run build && npm run lint && vitest run"
32
+ },
33
+ "keywords": [
34
+ "errors",
35
+ "try",
36
+ "catch",
37
+ "error handling",
38
+ "nice-try",
39
+ "good-try",
40
+ "neverthrow",
41
+ "ts-result",
42
+ "effect",
43
+ "go-go-try",
44
+ "gotry",
45
+ "go-try"
46
+ ],
47
+ "devDependencies": {
48
+ "@ark/attest": "^0.46.0",
49
+ "@biomejs/biome": "^1.9.4",
50
+ "pkgroll": "^2.12.1",
51
+ "tsx": "^4.19.3",
52
+ "typescript": "^5.8.3",
53
+ "vitest": "^3.1.2"
54
+ }
52
55
  }
package/src/index.ts CHANGED
@@ -1,92 +1,133 @@
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
- return result[0] === undefined;
8
+ return result[0] === undefined
9
9
  }
10
10
  export function isFailure<E, T>(result: Result<E, T>): result is Failure<E> {
11
- return result[0] !== undefined;
11
+ return result[0] !== undefined
12
12
  }
13
13
 
14
14
  export function success<T>(value: T): Success<T> {
15
- return [undefined, value] as const;
15
+ return [undefined, value] as const
16
16
  }
17
17
 
18
18
  export function failure<E>(error: E): Failure<E> {
19
- return [error, undefined] as const;
19
+ return [error, undefined] as const
20
20
  }
21
21
 
22
22
  function getErrorMessage(error: unknown): string {
23
- if (typeof error === "string") return error;
23
+ if (typeof error === 'string') return error
24
24
 
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
- }
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
- 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
- }
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
- return (
45
- typeof value === "object" &&
46
- value !== null &&
47
- "then" in value &&
48
- typeof (value as { then: unknown }).then === "function"
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>(promise: Promise<T>): Promise<Result<string, T>>;
53
- export function goTry<T>(fn: () => T): Result<string, T>;
54
- export function goTry<T>(value: T): Result<string, T>;
52
+ /**
53
+ * Executes a function, promise, or value and returns a Result type.
54
+ * If an error occurs, it returns a Failure with the error message as a string.
55
+ *
56
+ * @template T The type of the successful result
57
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
58
+ * @returns {Result<string, T> | Promise<Result<string, T>>} A Result type or a Promise of a Result type
59
+ *
60
+ * @example
61
+ * // With a value
62
+ * const [err, result] = goTry(42);
63
+ *
64
+ * @example
65
+ * // With a function
66
+ * const [err, result] = goTry(() => JSON.parse('{"key": "value"}'));
67
+ *
68
+ * @example
69
+ * // With a promise
70
+ * const [err, result] = await goTry(fetch('https://api.example.com/data'));
71
+ */
72
+ export function goTry<T>(promise: Promise<T>): Promise<Result<string, T>>
73
+ export function goTry<T>(fn: () => T): Result<string, T>
74
+ export function goTry<T>(value: T): Result<string, T>
55
75
  export function goTry<T>(
56
- value: T | Promise<T> | (() => T | Promise<T>),
76
+ value: T | Promise<T> | (() => T | Promise<T>),
57
77
  ): Result<string, T> | Promise<Result<string, T>> {
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
- }
78
+ try {
79
+ const result =
80
+ typeof value === 'function' ? (value as () => T | Promise<T>)() : value
81
+ if (isPromise<T>(result)) {
82
+ return result
83
+ .then((resolvedValue) => success<T>(resolvedValue))
84
+ .catch((err) => failure<string>(getErrorMessage(err)))
85
+ }
86
+ return success<T>(result)
87
+ } catch (err) {
88
+ return failure<string>(getErrorMessage(err))
89
+ }
70
90
  }
71
91
 
92
+ /**
93
+ * Executes a function, promise, or value and returns a Result type.
94
+ * If an error occurs, it returns a Failure with the raw error object.
95
+ *
96
+ * @template T The type of the successful result
97
+ * @template E The type of the error, defaults to Error
98
+ * @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
99
+ * @returns {Result<E, T> | Promise<Result<E, T>>} A Result type or a Promise of a Result type
100
+ *
101
+ * @example
102
+ * // With a value
103
+ * const [err, result] = goTryRaw(42);
104
+ *
105
+ * @example
106
+ * // With a function
107
+ * const [err, result] = goTryRaw(() => JSON.parse('{"key": "value"}'));
108
+ *
109
+ * @example
110
+ * // With a promise
111
+ * const [err, result] = await goTryRaw(fetch('https://api.example.com/data'));
112
+ */
72
113
  export function goTryRaw<T, E = Error>(
73
- promise: Promise<T>,
74
- ): Promise<Result<E, T>>;
75
- export function goTryRaw<T, E = Error>(fn: () => T): Result<E, T>;
76
- export function goTryRaw<T, E = Error>(value: T): Result<E, T>;
114
+ promise: Promise<T>,
115
+ ): Promise<Result<E, T>>
116
+ export function goTryRaw<T, E = Error>(fn: () => T): Result<E, T>
117
+ export function goTryRaw<T, E = Error>(value: T): Result<E, T>
77
118
  export function goTryRaw<T, E = Error>(
78
- value: T | Promise<T> | (() => T | Promise<T>),
119
+ value: T | Promise<T> | (() => T | Promise<T>),
79
120
  ): Result<E, T> | Promise<Result<E, T>> {
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
- }
121
+ try {
122
+ const result =
123
+ typeof value === 'function' ? (value as () => T | Promise<T>)() : value
124
+ if (isPromise<T>(result)) {
125
+ return result
126
+ .then((resolvedValue) => success<T>(resolvedValue))
127
+ .catch((err) => failure<E>(err as E))
128
+ }
129
+ return success<T>(result)
130
+ } catch (err) {
131
+ return failure<E>(err as E)
132
+ }
92
133
  }