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 CHANGED
@@ -5,7 +5,7 @@ export declare class ShowE extends Error {
5
5
  }
6
6
  declare global {
7
7
  interface Function {
8
- Emessage(map: ErrorMap): (...args: any[]) => Promise<any>;
8
+ Emessage(map: ErrorMap): (...args: any[]) => any;
9
9
  }
10
10
  }
11
11
  export {};
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 async (...args) => {
9
- var _a, _b;
8
+ return (...args) => {
10
9
  try {
11
- return await func(...args);
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 errorCode = (_a = error === null || error === void 0 ? void 0 : error.code) !== null && _a !== void 0 ? _a : "UNKNOWN_ERROR";
15
- const message = (_b = messages[errorCode]) !== null && _b !== void 0 ? _b : "Unexpected error occurred";
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.0",
4
- "description": "Function prototype based error handler with code-to-message mapping",
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 {};
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2019",
4
- "module": "ESNext",
5
- "declaration": true,
6
- "outDir": "dist",
7
- "strict": true,
8
- "esModuleInterop": true
9
- },
10
- "include": ["src"]
11
- }