emessages 1.0.0 → 1.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/dist/index.d.ts +1 -1
- package/dist/index.js +11 -7
- package/package.json +3 -6
- package/src/index.ts +0 -37
- package/tsconfig.json +0 -11
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -5,19 +5,23 @@ export class ShowE extends Error {
|
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
const wrapWithHandler = (func, messages) => {
|
|
8
|
-
return
|
|
9
|
-
var _a, _b;
|
|
8
|
+
return (...args) => {
|
|
10
9
|
try {
|
|
11
|
-
|
|
10
|
+
const result = func(...args);
|
|
11
|
+
if (result instanceof Promise) {
|
|
12
|
+
return result.catch((error) => {
|
|
13
|
+
const code = error?.code ?? "UNKNOWN_ERROR";
|
|
14
|
+
console.error(messages[code] ?? "Unexpected error");
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
12
18
|
}
|
|
13
19
|
catch (error) {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
console.error(`[${func.name || "anonymous"}] ${message}`);
|
|
20
|
+
const code = error?.code ?? "UNKNOWN_ERROR";
|
|
21
|
+
console.error(messages[code] ?? "Unexpected error");
|
|
17
22
|
}
|
|
18
23
|
};
|
|
19
24
|
};
|
|
20
|
-
// 🔥 Prototype extension — runs when module is imported
|
|
21
25
|
Function.prototype.Emessage = function (map) {
|
|
22
26
|
return wrapWithHandler(this, map);
|
|
23
27
|
};
|
package/package.json
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emessages",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Now ite will work in both sync and async function",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
|
|
8
|
+
"files": ["dist"],
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
12
|
"types": "./dist/index.d.ts"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
|
|
16
15
|
"sideEffects": true,
|
|
17
|
-
|
|
18
16
|
"scripts": {
|
|
19
17
|
"build": "tsc"
|
|
20
18
|
},
|
|
21
|
-
|
|
22
19
|
"devDependencies": {
|
|
23
20
|
"typescript": "^5.9.3"
|
|
24
21
|
}
|
package/src/index.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export type ErrorMap = Record<string, string>;
|
|
2
|
-
|
|
3
|
-
export class ShowE extends Error {
|
|
4
|
-
code: string;
|
|
5
|
-
|
|
6
|
-
constructor(code: string) {
|
|
7
|
-
super(code);
|
|
8
|
-
this.code = code;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const wrapWithHandler = (func: Function, messages: ErrorMap) => {
|
|
13
|
-
return async (...args: any[]) => {
|
|
14
|
-
try {
|
|
15
|
-
return await func(...args);
|
|
16
|
-
} catch (error: any) {
|
|
17
|
-
const errorCode = error?.code ?? "UNKNOWN_ERROR";
|
|
18
|
-
const message = messages[errorCode] ?? "Unexpected error occurred";
|
|
19
|
-
console.error(`[${func.name || "anonymous"}] ${message}`);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// 🌍 Global augmentation
|
|
25
|
-
declare global {
|
|
26
|
-
interface Function {
|
|
27
|
-
Emessage(map: ErrorMap): (...args: any[]) => Promise<any>;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 🔥 Prototype extension — runs when module is imported
|
|
32
|
-
Function.prototype.Emessage = function (map: ErrorMap) {
|
|
33
|
-
return wrapWithHandler(this, map);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// Required for global augmentation
|
|
37
|
-
export {};
|