@zokugun/xtry 0.1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2025-present Baptiste Augrain
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ [@zokugun/xtry](https://github.com/zokugun/node-xtry)
2
+ ==========================================================
3
+
4
+ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
5
+ [![NPM Version](https://img.shields.io/npm/v/@zokugun/xtry.svg?colorB=green)](https://www.npmjs.com/package/@zokugun/xtry)
6
+ [![Donation](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam)
7
+ [![Donation](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate)
8
+ [![Donation](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)
9
+
10
+ Simple `try/catch` wrapper returning Result
11
+
12
+ Getting Started
13
+ ---------------
14
+
15
+ With [node](http://nodejs.org) previously installed:
16
+
17
+ npm install @zokugun/xtry
18
+
19
+ Basic Example
20
+ -------------
21
+
22
+ ```typescript
23
+ import { xatry } from '@zokugun/xtry'
24
+
25
+ const { fails, value, error } = xatry(somePromise);
26
+ ```
27
+
28
+ Advanced Example
29
+ ----------------
30
+
31
+ ```typescript
32
+ import { err, ok, type Result, xatry, xtry } from '@zokugun/xtry'
33
+
34
+ export type FoobarError = { type: 'FOOBAR'; message: string };
35
+
36
+ function foobar(): Result<number, FoobarError> {
37
+ const { fails, value, error } = xatry(somePromise);
38
+
39
+ if (fails) {
40
+ return err({ type: 'FOOBAR', message: 'The promise has failed...' });
41
+ }
42
+
43
+ return ok(value);
44
+ }
45
+
46
+ function main() {
47
+ const result = foobar();
48
+
49
+ if (result.fails) {
50
+ console.error(result.error.message);
51
+ exit(1);
52
+ }
53
+
54
+ console.log(value);
55
+ }
56
+ ```
57
+
58
+ API
59
+ ---
60
+
61
+ ```typescript
62
+ type Success<T> = {
63
+ fails: false;
64
+ value: T;
65
+ error: null;
66
+ };
67
+ type Failure<E> = {
68
+ fails: true;
69
+ value: null;
70
+ error: E;
71
+ };
72
+ export type Result<T, E> = Success<T> | Failure<E>;
73
+ export function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: ((error: E) => void)): Result<T, E>;
74
+ export async function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: ((error: E) => void)): Promise<Result<T, E>>;
75
+ export function ok<T>(value: T): Success<T>;
76
+ export function err<E>(error: E): Failure<E>;
77
+ ```
78
+
79
+ Donations
80
+ ---------
81
+
82
+ Support this project by becoming a financial contributor.
83
+
84
+ <table>
85
+ <tr>
86
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_kofi.png" alt="Ko-fi" width="80px" height="80px"></td>
87
+ <td><a href="https://ko-fi.com/daiyam" target="_blank">ko-fi.com/daiyam</a></td>
88
+ </tr>
89
+ <tr>
90
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_liberapay.png" alt="Liberapay" width="80px" height="80px"></td>
91
+ <td><a href="https://liberapay.com/daiyam/donate" target="_blank">liberapay.com/daiyam/donate</a></td>
92
+ </tr>
93
+ <tr>
94
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_paypal.png" alt="PayPal" width="80px" height="80px"></td>
95
+ <td><a href="https://paypal.me/daiyam99" target="_blank">paypal.me/daiyam99</a></td>
96
+ </tr>
97
+ </table>
98
+
99
+ License
100
+ -------
101
+
102
+ Copyright &copy; 2025-present Baptiste Augrain
103
+
104
+ Licensed under the [MIT license](https://opensource.org/licenses/MIT).
package/lib/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ type Success<T> = {
2
+ fails: false;
3
+ value: T;
4
+ error: null;
5
+ };
6
+ type Failure<E> = {
7
+ fails: true;
8
+ value: null;
9
+ error: E;
10
+ };
11
+ export type Result<T, E> = Success<T> | Failure<E>;
12
+ export declare function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: ((error: E) => void)): Result<T, E>;
13
+ export declare function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: ((error: E) => void)): Promise<Result<T, E>>;
14
+ export declare function ok<T>(value: T): Success<T>;
15
+ export declare function err<E>(error: E): Failure<E>;
16
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,38 @@
1
+ export function xtry(func, handler) {
2
+ try {
3
+ const value = func();
4
+ return ok(value);
5
+ }
6
+ catch (error) {
7
+ if (handler) {
8
+ handler(error);
9
+ }
10
+ return err(error);
11
+ }
12
+ }
13
+ export async function xatry(func, handler) {
14
+ try {
15
+ const value = await (func instanceof Promise ? func : Promise.resolve().then(func));
16
+ return ok(value);
17
+ }
18
+ catch (error) {
19
+ if (handler) {
20
+ handler(error);
21
+ }
22
+ return err(error);
23
+ }
24
+ }
25
+ export function ok(value) {
26
+ return {
27
+ fails: false,
28
+ value,
29
+ error: null,
30
+ };
31
+ }
32
+ export function err(error) {
33
+ return {
34
+ fails: true,
35
+ value: null,
36
+ error,
37
+ };
38
+ }
package/lib/test.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export type FoobarError = {
2
+ type: 'FOOBAR';
3
+ message: string;
4
+ };
package/lib/test.js ADDED
@@ -0,0 +1,21 @@
1
+ import { err, ok, xatry } from './index.js';
2
+ import { exit } from 'node:process';
3
+ import { readFile } from "fs/promises";
4
+ async function readConfigFile() {
5
+ return readFile("./config.json", "utf-8").then((data) => JSON.parse(data).PORT);
6
+ }
7
+ async function foobar() {
8
+ const { fails, value } = await xatry(readConfigFile());
9
+ if (fails) {
10
+ return err({ type: 'FOOBAR', message: 'The promise has failed...' });
11
+ }
12
+ return ok(value);
13
+ }
14
+ async function main() {
15
+ const { fails, value } = await xatry(foobar);
16
+ if (fails) {
17
+ exit(1);
18
+ }
19
+ console.log(value);
20
+ }
21
+ main();
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@zokugun/xtry",
3
+ "description": "simple try/catch wrapper returning Result",
4
+ "version": "0.1.0",
5
+ "author": {
6
+ "name": "Baptiste Augrain",
7
+ "email": "daiyam@zokugun.org"
8
+ },
9
+ "license": "MIT",
10
+ "homepage": "https://github.com/zokugun/node-xtry",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/zokugun/node-xtry.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/zokugun/node-xtry/issues"
17
+ },
18
+ "type": "module",
19
+ "main": "lib/index.js",
20
+ "scripts": {
21
+ "commit": "cz",
22
+ "compile": "tsc -p src",
23
+ "lint": "xo",
24
+ "prepare": "husky; fixpack || true",
25
+ "prepublishOnly": "npm run compile",
26
+ "release": "release-it",
27
+ "test": "tsc -p test && mocha",
28
+ "test:dev": "mocha",
29
+ "test:watch": "tsc-watch -p test --onSuccess 'mocha'",
30
+ "watch:src": "tsc -watch -p src",
31
+ "watch:test": "tsc-watch -p test"
32
+ },
33
+ "dependencies": {},
34
+ "devDependencies": {
35
+ "@commitlint/cli": "^19.7.1",
36
+ "@commitlint/config-conventional": "^19.7.1",
37
+ "@types/chai": "^5.0.1",
38
+ "@types/fs-extra": "^11.0.4",
39
+ "@types/mocha": "^10.0.10",
40
+ "@types/node": "^20.14.8",
41
+ "chai": "^5.2.0",
42
+ "commitizen": "^4.3.1",
43
+ "eslint-plugin-chai-friendly": "^1.0.1",
44
+ "fixpack": "^4.0.0",
45
+ "fs-extra": "^11.3.0",
46
+ "globby": "^14.1.0",
47
+ "husky": "^9.1.7",
48
+ "lint-staged": "^15.4.3",
49
+ "mocha": "^11.1.0",
50
+ "release-it": "^18.1.2",
51
+ "source-map-support": "^0.5.21",
52
+ "tsc-watch": "^6.3.0",
53
+ "typescript": "^5.7.3",
54
+ "xo": "0.60.0"
55
+ },
56
+ "keywords": [
57
+ "catch",
58
+ "error",
59
+ "result",
60
+ "try"
61
+ ]
62
+ }