@guillaume-docquier/tools-ts 1.2.1 → 1.3.1
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/Result.d.ts +2 -0
- package/dist/Result.js +6 -1
- package/dist/Result.js.map +1 -1
- package/dist/utility-types.d.ts +22 -0
- package/package.json +1 -1
package/dist/Result.d.ts
CHANGED
|
@@ -46,6 +46,7 @@ export declare const Result: {
|
|
|
46
46
|
* Creates a Result from the result of a promise.
|
|
47
47
|
* If the promise resolves, you will get a {@link Result.Success}.
|
|
48
48
|
* If the promise rejects, you will get a {@link Result.Failure}.
|
|
49
|
+
* If the promise rejects with a fatal error, the error will be rethrown.
|
|
49
50
|
*
|
|
50
51
|
* @param fn The callback to execute.
|
|
51
52
|
*/
|
|
@@ -54,6 +55,7 @@ declare function tryCatch<T>(fn: () => Promise<T>): Promise<Result<T, unknown>>;
|
|
|
54
55
|
* Creates a Result from the result of a function that could throw.
|
|
55
56
|
* If the function does not throw, you will get a {@link Result.Success}.
|
|
56
57
|
* If the function throws, you will get a {@link Result.Failure}.
|
|
58
|
+
* If the function throws a fatal error, the error will be rethrown.
|
|
57
59
|
*
|
|
58
60
|
* @param fn The callback to execute.
|
|
59
61
|
*/
|
package/dist/Result.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Rethrow } from "./errors/Rethrow.js";
|
|
1
2
|
/**
|
|
2
3
|
* Utility functions to work with Results.
|
|
3
4
|
* @example
|
|
@@ -39,12 +40,16 @@ function tryCatch(fn) {
|
|
|
39
40
|
const result = fn();
|
|
40
41
|
// Async handling
|
|
41
42
|
if (result instanceof Promise) {
|
|
42
|
-
return result.then(Result.Success).catch(
|
|
43
|
+
return result.then(Result.Success).catch((error) => {
|
|
44
|
+
Rethrow.ifFatal(error);
|
|
45
|
+
return Result.Failure(error);
|
|
46
|
+
});
|
|
43
47
|
}
|
|
44
48
|
// Sync handling
|
|
45
49
|
return Result.Success(result);
|
|
46
50
|
}
|
|
47
51
|
catch (error) {
|
|
52
|
+
Rethrow.ifFatal(error);
|
|
48
53
|
return Result.Failure(error);
|
|
49
54
|
}
|
|
50
55
|
}
|
package/dist/Result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAkB7C;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED,6GAA6G;IAC7G,QAAQ;CACT,CAAA;AAoBD,SAAS,QAAQ,CAAI,EAAwB;IAC3C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,EAAE,EAAE,CAAA;QAEnB,iBAAiB;QACjB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;YAC9B,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACjD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAC9B,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,gBAAgB;QAChB,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC"}
|
package/dist/utility-types.d.ts
CHANGED
|
@@ -139,3 +139,25 @@ export type PartialProperties<TObject, TKey extends keyof TObject> = Omit<TObjec
|
|
|
139
139
|
* ```
|
|
140
140
|
*/
|
|
141
141
|
export type ValueOf<T> = T[keyof T];
|
|
142
|
+
/**
|
|
143
|
+
* Creates an enum type out of a const object.
|
|
144
|
+
* Useful for projects that run native Typescript in node and can't use enums, but want the same ergonomics.
|
|
145
|
+
* This is basically an alias of {@link ValueOf}.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* type MyEnum = Enumify<MyEnum>
|
|
150
|
+
* ^? "a" | "b" | "c"
|
|
151
|
+
* const MyEnum = {
|
|
152
|
+
* A: "a",
|
|
153
|
+
* B: "b",
|
|
154
|
+
* C: "c",
|
|
155
|
+
* } as const
|
|
156
|
+
*
|
|
157
|
+
* function useEnum(value: MyEnum) {}
|
|
158
|
+
*
|
|
159
|
+
* useEnum(MyEnum.A) // works
|
|
160
|
+
* useEnum("a") // also works
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export type Enumify<TObject extends Record<string, string>> = ValueOf<TObject>;
|