xantiagoma 0.0.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/LICENSE +21 -0
- package/README.md +41 -0
- package/dist/index.cjs +68 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +55 -0
- package/dist/index.d.mts +55 -0
- package/dist/index.mjs +66 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Santiago Montoya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# xantiagoma
|
|
2
|
+
|
|
3
|
+
Personal utilities by [Santiago Montoya](https://github.com/xantiagoma).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install xantiagoma
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## `tryCatch` / `tryCatchSync`
|
|
12
|
+
|
|
13
|
+
Convert promises or sync functions into `[data, error]` tuples — no `try/catch` needed.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { tryCatch, tryCatchSync } from "xantiagoma";
|
|
17
|
+
|
|
18
|
+
// Async
|
|
19
|
+
const [user, error] = await tryCatch(fetch("/api/me").then((r) => r.json()));
|
|
20
|
+
if (error) {
|
|
21
|
+
// handle error
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// With async function
|
|
25
|
+
const [data, err] = await tryCatch(async () => {
|
|
26
|
+
const res = await fetch("/api/data");
|
|
27
|
+
return res.json();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Sync
|
|
31
|
+
const [parsed, parseErr] = tryCatchSync(() => JSON.parse(rawString));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## See Also
|
|
35
|
+
|
|
36
|
+
- [drizzle-cursor](https://github.com/xantiagoma/drizzle-cursor) — Cursor-based pagination for Drizzle ORM
|
|
37
|
+
- [drizzle-audit](https://github.com/xantiagoma/drizzle-audit) — Configurable audit logging for Drizzle ORM
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/try-catch.ts
|
|
3
|
+
/**
|
|
4
|
+
* Convert a promise (or promise factory) into a tuple result: `[data, error]`.
|
|
5
|
+
*
|
|
6
|
+
* This is handy when you want to avoid `try/catch` at the call site.
|
|
7
|
+
*
|
|
8
|
+
* @example With a promise
|
|
9
|
+
* ```ts
|
|
10
|
+
* const [user, error] = await tryCatch(fetch("/api/me").then((r) => r.json()));
|
|
11
|
+
*
|
|
12
|
+
* if (error) {
|
|
13
|
+
* // handle error
|
|
14
|
+
* } else {
|
|
15
|
+
* // use user
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example With a function (captures sync throws too)
|
|
20
|
+
* ```ts
|
|
21
|
+
* const [data, error] = await tryCatch(async () => {
|
|
22
|
+
* if (Math.random() > 0.5) {
|
|
23
|
+
* throw new Error("boom");
|
|
24
|
+
* }
|
|
25
|
+
* return 123;
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* if (!error) {
|
|
29
|
+
* // data === 123
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
async function tryCatch(promise) {
|
|
34
|
+
try {
|
|
35
|
+
return [await (typeof promise === "function" ? promise() : promise), null];
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return [null, error];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Convert a sync function call into a tuple result: `[data, error]`.
|
|
42
|
+
*
|
|
43
|
+
* This is the sync equivalent of `tryCatch` and is handy when you want to avoid
|
|
44
|
+
* `try/catch` at the call site.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const [value, error] = tryCatchSync(() => JSON.parse('{"ok":true}'));
|
|
49
|
+
*
|
|
50
|
+
* if (error) {
|
|
51
|
+
* // handle error
|
|
52
|
+
* } else {
|
|
53
|
+
* // value.ok === true
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
function tryCatchSync(fn) {
|
|
58
|
+
try {
|
|
59
|
+
return [fn(), null];
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return [null, error];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.tryCatch = tryCatch;
|
|
66
|
+
exports.tryCatchSync = tryCatchSync;
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/try-catch.ts"],"sourcesContent":["type MaybePromise<T> = T | Promise<T>;\ntype Result<T, E = Error> = [T, null] | [null, E];\n\n/**\n * Convert a promise (or promise factory) into a tuple result: `[data, error]`.\n *\n * This is handy when you want to avoid `try/catch` at the call site.\n *\n * @example With a promise\n * ```ts\n * const [user, error] = await tryCatch(fetch(\"/api/me\").then((r) => r.json()));\n *\n * if (error) {\n * // handle error\n * } else {\n * // use user\n * }\n * ```\n *\n * @example With a function (captures sync throws too)\n * ```ts\n * const [data, error] = await tryCatch(async () => {\n * if (Math.random() > 0.5) {\n * throw new Error(\"boom\");\n * }\n * return 123;\n * });\n *\n * if (!error) {\n * // data === 123\n * }\n * ```\n */\nexport async function tryCatch<T, E = Error>(\n promise: PromiseLike<T> | (() => MaybePromise<T>),\n): Promise<Result<T, E>> {\n try {\n const data = await (typeof promise === \"function\" ? promise() : promise);\n return [data, null];\n } catch (error) {\n return [null, error as E];\n }\n}\n\n/**\n * Convert a sync function call into a tuple result: `[data, error]`.\n *\n * This is the sync equivalent of `tryCatch` and is handy when you want to avoid\n * `try/catch` at the call site.\n *\n * @example\n * ```ts\n * const [value, error] = tryCatchSync(() => JSON.parse('{\"ok\":true}'));\n *\n * if (error) {\n * // handle error\n * } else {\n * // value.ok === true\n * }\n * ```\n */\nexport function tryCatchSync<T, E = Error>(fn: () => T): Result<T, E> {\n try {\n return [fn(), null];\n } catch (error) {\n return [null, error as E];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,eAAsB,SACpB,SACuB;CACvB,IAAI;EAEF,OAAO,CAAC,OADY,OAAO,YAAY,aAAa,SAAS,GAAG,UAClD,KAAK;UACZ,OAAO;EACd,OAAO,CAAC,MAAM,MAAW;;;;;;;;;;;;;;;;;;;;AAqB7B,SAAgB,aAA2B,IAA2B;CACpE,IAAI;EACF,OAAO,CAAC,IAAI,EAAE,KAAK;UACZ,OAAO;EACd,OAAO,CAAC,MAAM,MAAW"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/try-catch.d.ts
|
|
2
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
3
|
+
type Result<T, E = Error> = [T, null] | [null, E];
|
|
4
|
+
/**
|
|
5
|
+
* Convert a promise (or promise factory) into a tuple result: `[data, error]`.
|
|
6
|
+
*
|
|
7
|
+
* This is handy when you want to avoid `try/catch` at the call site.
|
|
8
|
+
*
|
|
9
|
+
* @example With a promise
|
|
10
|
+
* ```ts
|
|
11
|
+
* const [user, error] = await tryCatch(fetch("/api/me").then((r) => r.json()));
|
|
12
|
+
*
|
|
13
|
+
* if (error) {
|
|
14
|
+
* // handle error
|
|
15
|
+
* } else {
|
|
16
|
+
* // use user
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example With a function (captures sync throws too)
|
|
21
|
+
* ```ts
|
|
22
|
+
* const [data, error] = await tryCatch(async () => {
|
|
23
|
+
* if (Math.random() > 0.5) {
|
|
24
|
+
* throw new Error("boom");
|
|
25
|
+
* }
|
|
26
|
+
* return 123;
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* if (!error) {
|
|
30
|
+
* // data === 123
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function tryCatch<T, E = Error>(promise: PromiseLike<T> | (() => MaybePromise<T>)): Promise<Result<T, E>>;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a sync function call into a tuple result: `[data, error]`.
|
|
37
|
+
*
|
|
38
|
+
* This is the sync equivalent of `tryCatch` and is handy when you want to avoid
|
|
39
|
+
* `try/catch` at the call site.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const [value, error] = tryCatchSync(() => JSON.parse('{"ok":true}'));
|
|
44
|
+
*
|
|
45
|
+
* if (error) {
|
|
46
|
+
* // handle error
|
|
47
|
+
* } else {
|
|
48
|
+
* // value.ok === true
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function tryCatchSync<T, E = Error>(fn: () => T): Result<T, E>;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { tryCatch, tryCatchSync };
|
|
55
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/try-catch.d.ts
|
|
2
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
3
|
+
type Result<T, E = Error> = [T, null] | [null, E];
|
|
4
|
+
/**
|
|
5
|
+
* Convert a promise (or promise factory) into a tuple result: `[data, error]`.
|
|
6
|
+
*
|
|
7
|
+
* This is handy when you want to avoid `try/catch` at the call site.
|
|
8
|
+
*
|
|
9
|
+
* @example With a promise
|
|
10
|
+
* ```ts
|
|
11
|
+
* const [user, error] = await tryCatch(fetch("/api/me").then((r) => r.json()));
|
|
12
|
+
*
|
|
13
|
+
* if (error) {
|
|
14
|
+
* // handle error
|
|
15
|
+
* } else {
|
|
16
|
+
* // use user
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example With a function (captures sync throws too)
|
|
21
|
+
* ```ts
|
|
22
|
+
* const [data, error] = await tryCatch(async () => {
|
|
23
|
+
* if (Math.random() > 0.5) {
|
|
24
|
+
* throw new Error("boom");
|
|
25
|
+
* }
|
|
26
|
+
* return 123;
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* if (!error) {
|
|
30
|
+
* // data === 123
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function tryCatch<T, E = Error>(promise: PromiseLike<T> | (() => MaybePromise<T>)): Promise<Result<T, E>>;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a sync function call into a tuple result: `[data, error]`.
|
|
37
|
+
*
|
|
38
|
+
* This is the sync equivalent of `tryCatch` and is handy when you want to avoid
|
|
39
|
+
* `try/catch` at the call site.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const [value, error] = tryCatchSync(() => JSON.parse('{"ok":true}'));
|
|
44
|
+
*
|
|
45
|
+
* if (error) {
|
|
46
|
+
* // handle error
|
|
47
|
+
* } else {
|
|
48
|
+
* // value.ok === true
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function tryCatchSync<T, E = Error>(fn: () => T): Result<T, E>;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { tryCatch, tryCatchSync };
|
|
55
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//#region src/try-catch.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert a promise (or promise factory) into a tuple result: `[data, error]`.
|
|
4
|
+
*
|
|
5
|
+
* This is handy when you want to avoid `try/catch` at the call site.
|
|
6
|
+
*
|
|
7
|
+
* @example With a promise
|
|
8
|
+
* ```ts
|
|
9
|
+
* const [user, error] = await tryCatch(fetch("/api/me").then((r) => r.json()));
|
|
10
|
+
*
|
|
11
|
+
* if (error) {
|
|
12
|
+
* // handle error
|
|
13
|
+
* } else {
|
|
14
|
+
* // use user
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example With a function (captures sync throws too)
|
|
19
|
+
* ```ts
|
|
20
|
+
* const [data, error] = await tryCatch(async () => {
|
|
21
|
+
* if (Math.random() > 0.5) {
|
|
22
|
+
* throw new Error("boom");
|
|
23
|
+
* }
|
|
24
|
+
* return 123;
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* if (!error) {
|
|
28
|
+
* // data === 123
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
async function tryCatch(promise) {
|
|
33
|
+
try {
|
|
34
|
+
return [await (typeof promise === "function" ? promise() : promise), null];
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return [null, error];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert a sync function call into a tuple result: `[data, error]`.
|
|
41
|
+
*
|
|
42
|
+
* This is the sync equivalent of `tryCatch` and is handy when you want to avoid
|
|
43
|
+
* `try/catch` at the call site.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const [value, error] = tryCatchSync(() => JSON.parse('{"ok":true}'));
|
|
48
|
+
*
|
|
49
|
+
* if (error) {
|
|
50
|
+
* // handle error
|
|
51
|
+
* } else {
|
|
52
|
+
* // value.ok === true
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function tryCatchSync(fn) {
|
|
57
|
+
try {
|
|
58
|
+
return [fn(), null];
|
|
59
|
+
} catch (error) {
|
|
60
|
+
return [null, error];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { tryCatch, tryCatchSync };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/try-catch.ts"],"sourcesContent":["type MaybePromise<T> = T | Promise<T>;\ntype Result<T, E = Error> = [T, null] | [null, E];\n\n/**\n * Convert a promise (or promise factory) into a tuple result: `[data, error]`.\n *\n * This is handy when you want to avoid `try/catch` at the call site.\n *\n * @example With a promise\n * ```ts\n * const [user, error] = await tryCatch(fetch(\"/api/me\").then((r) => r.json()));\n *\n * if (error) {\n * // handle error\n * } else {\n * // use user\n * }\n * ```\n *\n * @example With a function (captures sync throws too)\n * ```ts\n * const [data, error] = await tryCatch(async () => {\n * if (Math.random() > 0.5) {\n * throw new Error(\"boom\");\n * }\n * return 123;\n * });\n *\n * if (!error) {\n * // data === 123\n * }\n * ```\n */\nexport async function tryCatch<T, E = Error>(\n promise: PromiseLike<T> | (() => MaybePromise<T>),\n): Promise<Result<T, E>> {\n try {\n const data = await (typeof promise === \"function\" ? promise() : promise);\n return [data, null];\n } catch (error) {\n return [null, error as E];\n }\n}\n\n/**\n * Convert a sync function call into a tuple result: `[data, error]`.\n *\n * This is the sync equivalent of `tryCatch` and is handy when you want to avoid\n * `try/catch` at the call site.\n *\n * @example\n * ```ts\n * const [value, error] = tryCatchSync(() => JSON.parse('{\"ok\":true}'));\n *\n * if (error) {\n * // handle error\n * } else {\n * // value.ok === true\n * }\n * ```\n */\nexport function tryCatchSync<T, E = Error>(fn: () => T): Result<T, E> {\n try {\n return [fn(), null];\n } catch (error) {\n return [null, error as E];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,eAAsB,SACpB,SACuB;CACvB,IAAI;EAEF,OAAO,CAAC,OADY,OAAO,YAAY,aAAa,SAAS,GAAG,UAClD,KAAK;UACZ,OAAO;EACd,OAAO,CAAC,MAAM,MAAW;;;;;;;;;;;;;;;;;;;;AAqB7B,SAAgB,aAA2B,IAA2B;CACpE,IAAI;EACF,OAAO,CAAC,IAAI,EAAE,KAAK;UACZ,OAAO;EACd,OAAO,CAAC,MAAM,MAAW"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xantiagoma",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Santiago Montoya's utilities and tools",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"xantiagoma",
|
|
7
|
+
"utilities"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/xantiagoma/xantiagoma-lib#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/xantiagoma/xantiagoma-lib/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Santiago Montoya <xantiagoma@gmail.com>",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/xantiagoma/xantiagoma-lib.git"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.mts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"default": "./dist/index.mjs"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "vp pack",
|
|
46
|
+
"test": "vp test run",
|
|
47
|
+
"lint": "vp lint",
|
|
48
|
+
"format": "vp fmt",
|
|
49
|
+
"check": "vp lint && vp fmt --check && tsc --noEmit",
|
|
50
|
+
"release": "changelogen --release --push"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"changelogen": "^0.6.2",
|
|
54
|
+
"vite-plus": "^0.1.22"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"typescript": "^5"
|
|
58
|
+
}
|
|
59
|
+
}
|