safe-try-with-ai 1.3.0 → 1.3.2

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 CHANGED
@@ -1,3 +1,5 @@
1
+ ---
2
+
1
3
  # safe-try-with-ai
2
4
 
3
5
  A lightweight JavaScript utility for **clean error handling** with optional **AI-style runtime suggestions**, without repetitive try/catch blocks.
@@ -10,6 +12,8 @@ A lightweight JavaScript utility for **clean error handling** with optional **AI
10
12
  npm install safe-try-with-ai
11
13
  ```
12
14
 
15
+ ---
16
+
13
17
  ## Usage
14
18
 
15
19
  ### Synchronous example
@@ -20,51 +24,167 @@ const { safeTry } = require("safe-try-with-ai");
20
24
  const [err, result] = safeTry(() => JSON.parse('{"x":1}'));
21
25
 
22
26
  if (err) {
23
- console.error(err); // Original error
27
+ console.error(err);
24
28
  } else {
25
29
  console.log(result); // { x: 1 }
26
30
  }
27
31
  ```
28
32
 
33
+ ---
34
+
35
+ ### Asynchronous example
36
+
37
+ ```js
38
+ const { safeTryAsync } = require("safe-try-with-ai");
39
+
40
+ const [err, data] = await safeTryAsync(async () => {
41
+ return await fetchData();
42
+ });
43
+
44
+ if (err) {
45
+ console.error(err);
46
+ }
47
+ ```
48
+
49
+ ---
50
+
29
51
  ## Optional AI Runtime Suggestions
30
52
 
31
53
  Enable AI-style runtime suggestions by passing `{ analyze: true }` as the second argument.
32
54
 
33
- ### Synchronous example
55
+ ```js
56
+ const { safeTry } = require("safe-try-with-ai");
57
+
58
+ const [err] = safeTry(() => JSON.parse("invalid"), { analyze: true });
59
+
60
+ if (err) {
61
+ console.error("Error:", err.message);
62
+ console.log("Suggestion:", err.suggestion);
63
+ console.log("Fix:", err.fix);
64
+ }
65
+ ```
66
+
67
+ > AI-style suggestions are **rule-based and local**.
68
+ > No real AI model, no network calls, no data collection.
69
+
70
+ ---
71
+
72
+ ## Default Fallback Value
73
+
74
+ Use `safeTryDefault` to return a fallback value when an error occurs.
34
75
 
35
76
  ```js
36
- const [err, result] = safeTry(() => JSON.parse("invalid"), { analyze: true });
77
+ const { safeTryDefault } = require("safe-try-with-ai");
78
+
79
+ const result = safeTryDefault(
80
+ () => JSON.parse("invalid"),
81
+ {},
82
+ { analyze: true }
83
+ );
84
+
85
+ console.log(result); // {}
86
+ ```
87
+
88
+ ---
89
+
90
+ ## Safe JSON Parsing
91
+
92
+ ```js
93
+ const { safeTryJson } = require("safe-try-with-ai");
94
+
95
+ const [err, data] = safeTryJson('{"x":1}', { analyze: true });
37
96
 
38
97
  if (err) {
39
- console.error("Error:", err.message); // Original error
40
- console.log("Suggestion:", err.suggestion); // AI suggestion
41
- console.log("Fix:", err.fix); // Suggested fix
98
+ console.error(err);
42
99
  }
43
100
  ```
44
101
 
45
- ## Features
102
+ ---
46
103
 
47
- - Works with synchronous and asynchronous functions
48
- - Eliminates repetitive try/catch blocks
49
- - Optional AI-style runtime error suggestions
50
- - Zero dependencies
51
- - Lightweight and fast
104
+ ## CLI Usage
52
105
 
106
+ ## CLI Usage
53
107
 
54
- ## License
108
+ Validate JSON files from the terminal:
55
109
 
56
- MIT
110
+ npx safe-try-with-ai example.json
111
+ npx safe-try-with-ai example.json --analyze
112
+
113
+ ✔ = JSON valid (green)
114
+ ✖ = JSON invalid (red)
115
+ Suggestions (blue)
116
+ Fix (green)
117
+
118
+ Exit codes:
119
+ 0 = valid JSON
120
+ 1 = invalid JSON or runtime error
121
+
122
+
123
+
124
+ ```bash
125
+ npx safe-try-with-ai example.json
126
+ ```
127
+
128
+ Example output:
129
+
130
+ ```
131
+ Error: Unexpected token
132
+ Suggestion: Check JSON formatting
133
+ Fix: Ensure commas and brackets are correct
134
+ ```
135
+
136
+ ---
137
+
138
+ ## TypeScript Support
139
+
140
+ Built-in TypeScript definitions included.
141
+
142
+ ```ts
143
+ import { safeTry } from "safe-try-with-ai";
57
144
 
145
+ const [err, result] = safeTry(() => JSON.parse(data));
146
+ ```
147
+
148
+ No configuration required.
149
+
150
+ ---
151
+
152
+ ## Features
153
+
154
+ * Works with synchronous and asynchronous functions
155
+ * Eliminates repetitive try/catch blocks
156
+ * Optional AI-style runtime suggestions
157
+ * Default fallback handling
158
+ * Safe JSON parsing helper
159
+ * CLI support via `npx`
160
+ * Built-in TypeScript definitions
161
+ * Zero dependencies
162
+ * Lightweight and fast
163
+
164
+ ---
58
165
 
59
166
  ## Changelog
60
167
 
168
+ ### v1.3.0
169
+
170
+ * Added TypeScript definitions
171
+ * Added CLI support
172
+ * Documentation improvements
173
+
61
174
  ### v1.2.0
62
- - Added `safeTryDefault` for fallback values
63
- - Added `safeTryJson` for safe JSON parsing
64
- - Improved AI-style runtime suggestions
65
- - No breaking changes
175
+
176
+ * Added `safeTryDefault`
177
+ * Added `safeTryJson`
178
+ * Improved AI-style suggestions
66
179
 
67
180
  ### v1.1.1
68
- - Improved JSON error suggestions
181
+
182
+ * Improved JSON error suggestions
183
+
184
+ ---
185
+
186
+ ## License
187
+
188
+ MIT
69
189
 
70
190
  ---
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { safeTryJson } = require("../src/index.js");
6
+
7
+ // ANSI color codes
8
+ const COLORS = {
9
+ reset: "\x1b[0m",
10
+ green: "\x1b[32m",
11
+ red: "\x1b[31m",
12
+ blue: "\x1b[34m",
13
+ white: "\x1b[37m",
14
+ };
15
+
16
+ const green = (text) => COLORS.green + text + COLORS.reset;
17
+ const red = (text) => COLORS.red + text + COLORS.reset;
18
+ const blue = (text) => COLORS.blue + text + COLORS.reset;
19
+ const white = (text) => COLORS.white + text + COLORS.reset;
20
+
21
+ // CLI Args
22
+ const args = process.argv.slice(2);
23
+ if (args.length === 0) {
24
+ console.log(red("✖ No file specified"));
25
+ console.log("Usage: npx safe-try-with-ai <file.json> [--analyze]");
26
+ process.exit(1);
27
+ }
28
+
29
+ const filePath = path.resolve(args[0]);
30
+ const analyze = args.includes("--analyze");
31
+
32
+ // Read and validate JSON
33
+ const [err] = safeTryJson(() => fs.readFileSync(filePath, "utf8"), { analyze });
34
+
35
+ if (err) {
36
+ console.log(red("✖ JSON is invalid"));
37
+ console.log(white(` └─ Error: ${err.message}`));
38
+ if (analyze && err.suggestion) {
39
+ console.log(blue(` ├─ Suggestion: ${err.suggestion}`));
40
+ if (err.fix) console.log(green(` └─ Fix: ${err.fix}`));
41
+ }
42
+ process.exit(1);
43
+ } else {
44
+ console.log(green("✔ JSON is valid"));
45
+ process.exit(0);
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "safe-try-with-ai",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Clean error handling for JavaScript",
5
5
  "keywords": [
6
6
  "error-handling",
@@ -8,6 +8,9 @@
8
8
  "functional",
9
9
  "AI"
10
10
  ],
11
+ "bin": {
12
+ "safe-try-with-ai": "./bin/safe-try.js"
13
+ },
11
14
  "license": "MIT",
12
15
  "author": "madhuka2002",
13
16
  "type": "commonjs",
package/index.d.ts DELETED
@@ -1,39 +0,0 @@
1
- export interface AISuggestion {
2
- name: string;
3
- message: string;
4
- suggestion?: string;
5
- fix?: string;
6
- }
7
-
8
- export interface SafeTryOptions {
9
- analyze?: boolean;
10
- }
11
-
12
- export type SafeTryResult<T> = [null, T] | [Error | AISuggestion, null];
13
-
14
- export function safeTry<T>(
15
- fn: () => T,
16
- options?: SafeTryOptions
17
- ): SafeTryResult<T>;
18
-
19
- export function safeTryAsync<T>(
20
- fn: () => Promise<T>,
21
- options?: SafeTryOptions
22
- ): Promise<SafeTryResult<T>>;
23
-
24
- /**
25
- * Safely parse JSON with AI-style error suggestions
26
- */
27
- export function safeTryJson<T = unknown>(
28
- json: string,
29
- options?: SafeTryOptions
30
- ): SafeTryResult<T>;
31
-
32
- /**
33
- * Execute with a default fallback value on error
34
- */
35
- export function safeTryDefault<T>(
36
- fn: () => T,
37
- defaultValue: T,
38
- options?: SafeTryOptions
39
- ): T;