@guillaume-docquier/tools-ts 1.4.0 → 2.0.0

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 CHANGED
@@ -48,16 +48,25 @@ export declare const Result: {
48
48
  * If the promise rejects, you will get a {@link Result.Failure}.
49
49
  * If the promise rejects with a fatal error, the error will be rethrown.
50
50
  *
51
- * @param fn The callback to execute.
51
+ * @param promise The promise.
52
52
  */
53
- declare function tryCatch<T>(fn: () => Promise<T>): Promise<Result<T, unknown>>;
53
+ declare function tryCatch<T>(promise: Promise<T>): Promise<Result<T, Error>>;
54
+ /**
55
+ * Creates a Result from the result of a function that returns a promise.
56
+ * If the returned promise resolves, you will get a {@link Result.Success}.
57
+ * If the returned promise rejects, you will get a {@link Result.Failure}.
58
+ * If the returned promise rejects with a fatal error, the error will be rethrown.
59
+ *
60
+ * @param asyncFn The asynchronous callback to execute.
61
+ */
62
+ declare function tryCatch<T>(asyncFn: () => Promise<T>): Promise<Result<T, Error>>;
54
63
  /**
55
64
  * Creates a Result from the result of a function that could throw.
56
65
  * If the function does not throw, you will get a {@link Result.Success}.
57
66
  * If the function throws, you will get a {@link Result.Failure}.
58
67
  * If the function throws a fatal error, the error will be rethrown.
59
68
  *
60
- * @param fn The callback to execute.
69
+ * @param syncFn The synchronous callback to execute.
61
70
  */
62
- declare function tryCatch<T>(fn: () => T): Result<T, unknown>;
71
+ declare function tryCatch<T>(syncFn: () => T): Result<T, Error>;
63
72
  export {};
package/dist/Result.js CHANGED
@@ -35,15 +35,15 @@ export const Result = {
35
35
  // You can't overload while defining object properties, which is why the function implementation is not inlined
36
36
  tryCatch,
37
37
  };
38
- function tryCatch(fn) {
38
+ function tryCatch(op) {
39
+ if (op instanceof Promise) {
40
+ return tryCatchPromise(op);
41
+ }
39
42
  try {
40
- const result = fn();
43
+ const result = op();
41
44
  // Async handling
42
45
  if (result instanceof Promise) {
43
- return result.then(Result.Success).catch((error) => {
44
- Rethrow.ifFatal(error);
45
- return Result.Failure(error);
46
- });
46
+ return tryCatchPromise(result);
47
47
  }
48
48
  // Sync handling
49
49
  return Result.Success(result);
@@ -53,4 +53,10 @@ function tryCatch(fn) {
53
53
  return Result.Failure(error);
54
54
  }
55
55
  }
56
+ function tryCatchPromise(promise) {
57
+ return promise.then(Result.Success).catch((error) => {
58
+ Rethrow.ifFatal(error);
59
+ return Result.Failure(error);
60
+ });
61
+ }
56
62
  //# sourceMappingURL=Result.js.map
@@ -1 +1 @@
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,+GAA+G;IAC/G,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,CAA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I,CAAC;QACH,MAAM,MAAM,GAAG,EAAE,EAAE,CAAA;QAEnB,iBAAiB;QACjB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;YAC9B,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;QAChC,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;AAED,SAAS,eAAe,CAAI,OAAmB;IAC7C,OAAO,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAClD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guillaume-docquier/tools-ts",
3
- "version": "1.4.0",
3
+ "version": "2.0.0",
4
4
  "description": "My personal collection of typescript tools",
5
5
  "homepage": "https://github.com/Guillaume-Docquier/tools-ts",
6
6
  "repository": {
@@ -21,11 +21,12 @@
21
21
  ],
22
22
  "scripts": {
23
23
  "build": "rm -rf dist && tsc --project tsconfig.build.json",
24
+ "checks": "pnpm lint:fix && pnpm format:fix && pnpm typecheck && pnpm test --run",
24
25
  "format": "prettier --check . && pnpm format:package-json --check",
25
26
  "format:fix": "prettier --write --list-different . && pnpm format:package-json",
26
27
  "format:package-json": "sort-package-json package.json",
27
28
  "lint": "eslint . --max-warnings 0",
28
- "lint:fix": "pnpm lint -- --fix",
29
+ "lint:fix": "pnpm lint --fix",
29
30
  "prepack": "pnpm build",
30
31
  "prepare": "husky",
31
32
  "prepublishOnly": "pnpm i",