safe-clone-async 1.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/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # safe-clone-async
2
+
3
+ A clean, safe alternative to `try/catch` for async code.
4
+ Inspired by `safe-await`, `safe-clone` wraps your promises and returns a tuple `[data, error]`, ensuring your code never throws unhandled errors.
5
+
6
+ ## Features
7
+
8
+ - ✅ **Never throws**: Errors are caught and returned as values.
9
+ - ✅ **Type-safe**: Generic support for return types and error types.
10
+ - ✅ **Works with any Promise**: Compatible with all async functions and libraries.
11
+ - ✅ **Dual Support**: Works with both **ES Modules (ESM)** and **CommonJS (CJS)**.
12
+ - ✅ **Zero Dependencies**: Lightweight and fast.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install safe-clone-async
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Basic Usage
23
+
24
+ Use `safeClone` to wrap any promise. It returns an array where the first element is the data (or null) and the second is the error (or null).
25
+
26
+ ```typescript
27
+ import { safeClone } from 'safe-clone-async';
28
+
29
+ async function getUser() {
30
+ const [user, error] = await safeClone(fetchUser(1));
31
+
32
+ if (error) {
33
+ console.error('Failed to fetch user:', error);
34
+ return;
35
+ }
36
+
37
+ console.log('User data:', user);
38
+ }
39
+ ```
40
+
41
+ ### With Custom Error Types
42
+
43
+ You can specify the error type for better type safety.
44
+
45
+ ```typescript
46
+ import { safeClone } from 'safe-clone-async';
47
+
48
+ interface ApiError {
49
+ message: string;
50
+ code: number;
51
+ }
52
+
53
+ // ... inside async function
54
+ const [data, error] = await safeClone<User, ApiError>(fetchUser(1));
55
+
56
+ if (error) {
57
+ // error is typed as ApiError
58
+ console.log(error.code);
59
+ }
60
+ ```
61
+
62
+ ### CommonJS (require)
63
+
64
+ ```javascript
65
+ const { safeClone } = require('safe-clone-async');
66
+
67
+ safeClone(somePromise).then(([data, error]) => {
68
+ // ...
69
+ });
70
+ ```
71
+
72
+ ## API
73
+
74
+ ### `safeClone<T, E = Error>(promise: Promise<T>): Promise<[T | null, E | null]>`
75
+
76
+ - **promise**: The promise to execute.
77
+ - **Returns**: A promise that resolves to:
78
+ - `[data, null]` if the promise resolves.
79
+ - `[null, error]` if the promise rejects.
80
+
81
+ ## License
82
+
83
+ MIT
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A safe wrapper for promises that returns a tuple [data, error]
3
+ * @param promise The promise to wrap
4
+ * @returns A tuple containing [data, null] if successful, or [null, error] if failed
5
+ */
6
+ declare function safeClone<T, E = Error>(promise: Promise<T>): Promise<[T, null] | [null, E]>;
7
+
8
+ export { safeClone as default, safeClone };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A safe wrapper for promises that returns a tuple [data, error]
3
+ * @param promise The promise to wrap
4
+ * @returns A tuple containing [data, null] if successful, or [null, error] if failed
5
+ */
6
+ declare function safeClone<T, E = Error>(promise: Promise<T>): Promise<[T, null] | [null, E]>;
7
+
8
+ export { safeClone as default, safeClone };
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => index_default,
24
+ safeClone: () => safeClone
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ async function safeClone(promise) {
28
+ try {
29
+ const data = await promise;
30
+ return [data, null];
31
+ } catch (error) {
32
+ return [null, error];
33
+ }
34
+ }
35
+ var index_default = safeClone;
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ safeClone
39
+ });
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * A safe wrapper for promises that returns a tuple [data, error]\n * @param promise The promise to wrap\n * @returns A tuple containing [data, null] if successful, or [null, error] if failed\n */\nexport async function safeClone<T, E = Error>(\n promise: Promise<T>\n): Promise<[T, null] | [null, E]> {\n try {\n const data = await promise;\n return [data, null];\n } catch (error) {\n return [null, error as E];\n }\n}\n\nexport default safeClone;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,eAAsB,UACpB,SACgC;AAChC,MAAI;AACF,UAAM,OAAO,MAAM;AACnB,WAAO,CAAC,MAAM,IAAI;AAAA,EACpB,SAAS,OAAO;AACd,WAAO,CAAC,MAAM,KAAU;AAAA,EAC1B;AACF;AAEA,IAAO,gBAAQ;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,15 @@
1
+ // src/index.ts
2
+ async function safeClone(promise) {
3
+ try {
4
+ const data = await promise;
5
+ return [data, null];
6
+ } catch (error) {
7
+ return [null, error];
8
+ }
9
+ }
10
+ var index_default = safeClone;
11
+ export {
12
+ index_default as default,
13
+ safeClone
14
+ };
15
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * A safe wrapper for promises that returns a tuple [data, error]\n * @param promise The promise to wrap\n * @returns A tuple containing [data, null] if successful, or [null, error] if failed\n */\nexport async function safeClone<T, E = Error>(\n promise: Promise<T>\n): Promise<[T, null] | [null, E]> {\n try {\n const data = await promise;\n return [data, null];\n } catch (error) {\n return [null, error as E];\n }\n}\n\nexport default safeClone;\n"],"mappings":";AAKA,eAAsB,UACpB,SACgC;AAChC,MAAI;AACF,UAAM,OAAO,MAAM;AACnB,WAAO,CAAC,MAAM,IAAI;AAAA,EACpB,SAAS,OAAO;AACd,WAAO,CAAC,MAAM,KAAU;AAAA,EAC1B;AACF;AAEA,IAAO,gBAAQ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "safe-clone-async",
3
+ "version": "1.0.0",
4
+ "description": "A clean alternative to try/catch for async code, returning a tuple [data, error].",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "require": "./dist/index.js",
12
+ "import": "./dist/index.mjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "dev": "tsup --watch",
21
+ "test": "vitest",
22
+ "lint": "tsc --noEmit",
23
+ "prepublishOnly": "npm run build"
24
+ },
25
+ "keywords": [
26
+ "async",
27
+ "await",
28
+ "try-catch",
29
+ "error-handling",
30
+ "safe",
31
+ "promise",
32
+ "tuple"
33
+ ],
34
+ "author": "Ahmer Arain",
35
+ "license": "MIT",
36
+ "devDependencies": {
37
+ "tsup": "^8.0.0",
38
+ "typescript": "^5.0.0",
39
+ "vitest": "^1.0.0",
40
+ "@types/node": "^20.0.0"
41
+ }
42
+ }