emessages 2.0.0 → 2.1.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/dist/index.cjs +50 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -2
- package/dist/index.d.ts +19 -2
- package/dist/index.mjs +40 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -33,12 +43,12 @@ var globalMessageStore = /* @__PURE__ */ new Map();
|
|
|
33
43
|
function isBrowser() {
|
|
34
44
|
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
35
45
|
}
|
|
36
|
-
function showToast(message,
|
|
46
|
+
function showToast(message, toastConfig) {
|
|
37
47
|
if (!isBrowser()) {
|
|
38
48
|
return;
|
|
39
49
|
}
|
|
40
50
|
const toast = document.createElement("div");
|
|
41
|
-
const config = typeof
|
|
51
|
+
const config = typeof toastConfig === "object" ? toastConfig : {};
|
|
42
52
|
toast.textContent = config.message || message;
|
|
43
53
|
toast.className = "emessage-toast";
|
|
44
54
|
if (config.style) {
|
|
@@ -61,11 +71,12 @@ function showToast(message, tostConfig) {
|
|
|
61
71
|
}
|
|
62
72
|
|
|
63
73
|
// src/index.ts
|
|
74
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
64
75
|
function parseConfig(config) {
|
|
65
76
|
const options = {};
|
|
66
77
|
let message = null;
|
|
67
78
|
let name = null;
|
|
68
|
-
const optionKeys = ["type", "break", "
|
|
79
|
+
const optionKeys = ["type", "break", "toast", "returnEM", "callBack"];
|
|
69
80
|
for (const key in config) {
|
|
70
81
|
if (Object.prototype.hasOwnProperty.call(config, key)) {
|
|
71
82
|
if (optionKeys.includes(key)) {
|
|
@@ -93,20 +104,43 @@ function parseConfig(config) {
|
|
|
93
104
|
}
|
|
94
105
|
function processEmessage(errorName, config) {
|
|
95
106
|
const message = config.message;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
let consoleType;
|
|
108
|
+
if (config.type === false) {
|
|
109
|
+
consoleType = false;
|
|
110
|
+
} else if (config.type === true || config.type === void 0) {
|
|
111
|
+
consoleType = "err";
|
|
112
|
+
} else {
|
|
113
|
+
consoleType = config.type;
|
|
114
|
+
}
|
|
115
|
+
if (consoleType) {
|
|
116
|
+
if (isBrowser()) {
|
|
117
|
+
switch (consoleType) {
|
|
118
|
+
case "log":
|
|
119
|
+
console.log(message);
|
|
120
|
+
break;
|
|
121
|
+
case "war":
|
|
122
|
+
console.warn(message);
|
|
123
|
+
break;
|
|
124
|
+
case "err":
|
|
125
|
+
console.error(message);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
switch (consoleType) {
|
|
130
|
+
case "log":
|
|
131
|
+
console.log(import_chalk.default.bgWhite.black(message));
|
|
132
|
+
break;
|
|
133
|
+
case "war":
|
|
134
|
+
console.warn(import_chalk.default.bgYellow.white(message));
|
|
135
|
+
break;
|
|
136
|
+
case "err":
|
|
137
|
+
console.error(import_chalk.default.bgRed.white(message));
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
107
141
|
}
|
|
108
|
-
if (config.
|
|
109
|
-
showToast(message, config.
|
|
142
|
+
if (config.toast && isBrowser()) {
|
|
143
|
+
showToast(message, config.toast);
|
|
110
144
|
}
|
|
111
145
|
if (config.callBack) {
|
|
112
146
|
try {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/store.ts","../src/utils.ts"],"sourcesContent":["import { individualMessageStore, globalMessageStore } from \"./store\";\nimport type {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/store.ts","../src/utils.ts"],"sourcesContent":["import { individualMessageStore, globalMessageStore } from \"./store\";\r\nimport type {\r\n EmessageConfig,\r\n EmessageOptions,\r\n StoredEmessage,\r\n ToastConfig,\r\n MessageType,\r\n} from \"./types\";\r\nimport { isBrowser, showToast } from \"./utils\";\r\nimport chalk from \"chalk\";\r\n\r\nfunction parseConfig(\r\n config: Record<string, any>\r\n): { name: string; options: StoredEmessage } | null {\r\n const options: EmessageOptions = {};\r\n let message: string | null = null;\r\n let name: string | null = null;\r\n const optionKeys = [\"type\", \"break\", \"toast\", \"returnEM\", \"callBack\"];\r\n\r\n for (const key in config) {\r\n if (Object.prototype.hasOwnProperty.call(config, key)) {\r\n if (optionKeys.includes(key)) {\r\n (options as any)[key] = config[key];\r\n } else {\r\n if (name !== null) {\r\n console.warn(\r\n `emessages: Found multiple potential error names in one config object. Using first one found: \"${name}\".`\r\n );\r\n continue;\r\n }\r\n name = key;\r\n message = String(config[key]);\r\n }\r\n }\r\n }\r\n\r\n if (name === null || message === null) {\r\n console.error(\r\n \"emessages: Invalid config object. Could not find error name and message.\",\r\n config\r\n );\r\n return null;\r\n }\r\n\r\n return { name, options: { ...options, message } };\r\n}\r\n\r\nfunction processEmessage(\r\n errorName: string,\r\n config: StoredEmessage\r\n): string | void {\r\n const message = config.message;\r\n\r\n let consoleType: MessageType | false;\r\n if (config.type === false) {\r\n consoleType = false;\r\n } else if (config.type === true || config.type === undefined) {\r\n consoleType = \"err\";\r\n } else {\r\n consoleType = config.type;\r\n }\r\n\r\n // 1. Console log\r\n if (consoleType) {\r\n if (isBrowser()) {\r\n switch (consoleType) {\r\n case \"log\":\r\n console.log(message);\r\n break;\r\n case \"war\":\r\n console.warn(message);\r\n break;\r\n case \"err\":\r\n console.error(message);\r\n break;\r\n }\r\n } else {\r\n switch (consoleType) {\r\n case \"log\":\r\n console.log(chalk.bgWhite.black(message));\r\n break;\r\n case \"war\":\r\n console.warn(chalk.bgYellow.white(message));\r\n break;\r\n case \"err\":\r\n console.error(chalk.bgRed.white(message));\r\n break;\r\n }\r\n }\r\n }\r\n\r\n // 2. Toast notification\r\n if (config.toast && isBrowser()) {\r\n showToast(message, config.toast);\r\n }\r\n\r\n // 3. Callback\r\n if (config.callBack) {\r\n try {\r\n config.callBack();\r\n } catch (e: any) {\r\n console.error(\r\n `emessages: Error in callBack for \"${errorName}\":`,\r\n e.message\r\n );\r\n }\r\n }\r\n\r\n // 4. Return error message\r\n if (config.returnEM) {\r\n return message;\r\n }\r\n\r\n // 5. Break execution\r\n if (config.break ?? true) {\r\n if (isBrowser()) {\r\n throw new Error(message);\r\n } else {\r\n process.exit(1);\r\n }\r\n }\r\n}\r\n\r\nexport function Emessage(...configs: EmessageConfig[]) {\r\n for (const config of configs) {\r\n const parsed = parseConfig(config);\r\n if (parsed) {\r\n individualMessageStore.set(parsed.name, parsed.options);\r\n }\r\n }\r\n}\r\n\r\nEmessage.global = function (...configs: EmessageConfig[]) {\r\n for (const config of configs) {\r\n const parsed = parseConfig(config);\r\n if (parsed) {\r\n globalMessageStore.set(parsed.name, parsed.options);\r\n }\r\n }\r\n};\r\n\r\nexport function showE(error: string | Record<string, any>): string | void {\r\n let config: StoredEmessage | null = null;\r\n let errorName: string | null = null;\r\n\r\n if (typeof error === \"string\") {\r\n errorName = error;\r\n config =\r\n individualMessageStore.get(error) ?? globalMessageStore.get(error) ?? null;\r\n if (!config) {\r\n console.error(`emessages: Error \"${error}\" not found.`);\r\n return;\r\n }\r\n } else if (typeof error === \"object\" && error !== null) {\r\n const parsed = parseConfig(error);\r\n if (parsed) {\r\n errorName = parsed.name;\r\n config = parsed.options;\r\n } else {\r\n console.error(\"emessages: Invalid object passed to showE.\");\r\n return;\r\n }\r\n } else {\r\n console.error(\"emessages: Invalid argument passed to showE.\");\r\n return;\r\n }\r\n\r\n if (config && errorName) {\r\n return processEmessage(errorName, config);\r\n }\r\n}\r\n","import type { StoredEmessage } from \"./types\";\r\n\r\nexport const individualMessageStore = new Map<string, StoredEmessage>();\r\nexport const globalMessageStore = new Map<string, StoredEmessage>();\r\n","import { ToastConfig } from \"./types\";\r\n\r\nexport function isBrowser(): boolean {\r\n return typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\r\n}\r\n\r\nexport function showToast(\r\n message: string,\r\n toastConfig: boolean | ToastConfig\r\n): void {\r\n if (!isBrowser()) {\r\n return;\r\n }\r\n\r\n const toast = document.createElement(\"div\");\r\n\r\n const config: ToastConfig = typeof toastConfig === \"object\" ? toastConfig : {};\r\n\r\n toast.textContent = config.message || message;\r\n\r\n // Base styles are applied via CSS, but some can be defaults.\r\n toast.className = \"emessage-toast\";\r\n if (config.style) {\r\n toast.classList.add(...config.style.split(\" \"));\r\n }\r\n\r\n // Positioning\r\n const position = config.position || \"top-right\";\r\n toast.setAttribute(\"data-position\", position);\r\n\r\n document.body.appendChild(toast);\r\n\r\n // Animate in\r\n setTimeout(() => {\r\n toast.classList.add(\"visible\");\r\n }, 10);\r\n\r\n // Animate out and remove\r\n setTimeout(() => {\r\n toast.classList.remove(\"visible\");\r\n toast.addEventListener(\"transitionend\", () => {\r\n if (toast.parentElement) {\r\n toast.parentElement.removeChild(toast);\r\n }\r\n });\r\n }, 3000);\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,yBAAyB,oBAAI,IAA4B;AAC/D,IAAM,qBAAqB,oBAAI,IAA4B;;;ACD3D,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW,eAAe,OAAO,OAAO,aAAa;AACrE;AAEO,SAAS,UACd,SACA,aACM;AACN,MAAI,CAAC,UAAU,GAAG;AAChB;AAAA,EACF;AAEA,QAAM,QAAQ,SAAS,cAAc,KAAK;AAE1C,QAAM,SAAsB,OAAO,gBAAgB,WAAW,cAAc,CAAC;AAE7E,QAAM,cAAc,OAAO,WAAW;AAGtC,QAAM,YAAY;AAClB,MAAI,OAAO,OAAO;AAChB,UAAM,UAAU,IAAI,GAAG,OAAO,MAAM,MAAM,GAAG,CAAC;AAAA,EAChD;AAGA,QAAM,WAAW,OAAO,YAAY;AACpC,QAAM,aAAa,iBAAiB,QAAQ;AAE5C,WAAS,KAAK,YAAY,KAAK;AAG/B,aAAW,MAAM;AACf,UAAM,UAAU,IAAI,SAAS;AAAA,EAC/B,GAAG,EAAE;AAGL,aAAW,MAAM;AACf,UAAM,UAAU,OAAO,SAAS;AAChC,UAAM,iBAAiB,iBAAiB,MAAM;AAC5C,UAAI,MAAM,eAAe;AACvB,cAAM,cAAc,YAAY,KAAK;AAAA,MACvC;AAAA,IACF,CAAC;AAAA,EACH,GAAG,GAAI;AACT;;;AFrCA,mBAAkB;A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IACF;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,UAAU,MAAM;AACtD,UAAM,SAAS,YAAY,KAAK;AAChC,QAAI,QAAQ;AACV,kBAAY,OAAO;AACnB,eAAS,OAAO;AAAA,IAClB,OAAO;AACL,cAAQ,MAAM,4CAA4C;AAC1D;AAAA,IACF;AAAA,EACF,OAAO;AACL,YAAQ,MAAM,8CAA8C;AAC5D;AAAA,EACF;AAEA,MAAI,UAAU,WAAW;AACvB,WAAO,gBAAgB,WAAW,MAAM;AAAA,EAC1C;AACF;","names":["chalk"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
type MessageType = "log" | "war" | "err";
|
|
2
|
+
interface ToastConfig {
|
|
3
|
+
style?: string;
|
|
4
|
+
message?: string;
|
|
5
|
+
position?: "top" | "right" | "left" | "bottom" | "center" | "top right" | "top left" | "bottom right" | "bottom left";
|
|
6
|
+
}
|
|
7
|
+
interface EmessageOptions {
|
|
8
|
+
type?: MessageType | boolean;
|
|
9
|
+
break?: boolean;
|
|
10
|
+
toast?: boolean | ToastConfig;
|
|
11
|
+
returnEM?: boolean;
|
|
12
|
+
callBack?: (...args: any[]) => any;
|
|
13
|
+
}
|
|
14
|
+
type EmessageConfig = EmessageOptions & {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare function Emessage(...configs: EmessageConfig[]): void;
|
|
2
19
|
declare namespace Emessage {
|
|
3
|
-
var global: (...configs:
|
|
20
|
+
var global: (...configs: EmessageConfig[]) => void;
|
|
4
21
|
}
|
|
5
22
|
declare function showE(error: string | Record<string, any>): string | void;
|
|
6
23
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
type MessageType = "log" | "war" | "err";
|
|
2
|
+
interface ToastConfig {
|
|
3
|
+
style?: string;
|
|
4
|
+
message?: string;
|
|
5
|
+
position?: "top" | "right" | "left" | "bottom" | "center" | "top right" | "top left" | "bottom right" | "bottom left";
|
|
6
|
+
}
|
|
7
|
+
interface EmessageOptions {
|
|
8
|
+
type?: MessageType | boolean;
|
|
9
|
+
break?: boolean;
|
|
10
|
+
toast?: boolean | ToastConfig;
|
|
11
|
+
returnEM?: boolean;
|
|
12
|
+
callBack?: (...args: any[]) => any;
|
|
13
|
+
}
|
|
14
|
+
type EmessageConfig = EmessageOptions & {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare function Emessage(...configs: EmessageConfig[]): void;
|
|
2
19
|
declare namespace Emessage {
|
|
3
|
-
var global: (...configs:
|
|
20
|
+
var global: (...configs: EmessageConfig[]) => void;
|
|
4
21
|
}
|
|
5
22
|
declare function showE(error: string | Record<string, any>): string | void;
|
|
6
23
|
|
package/dist/index.mjs
CHANGED
|
@@ -6,12 +6,12 @@ var globalMessageStore = /* @__PURE__ */ new Map();
|
|
|
6
6
|
function isBrowser() {
|
|
7
7
|
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
8
8
|
}
|
|
9
|
-
function showToast(message,
|
|
9
|
+
function showToast(message, toastConfig) {
|
|
10
10
|
if (!isBrowser()) {
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
const toast = document.createElement("div");
|
|
14
|
-
const config = typeof
|
|
14
|
+
const config = typeof toastConfig === "object" ? toastConfig : {};
|
|
15
15
|
toast.textContent = config.message || message;
|
|
16
16
|
toast.className = "emessage-toast";
|
|
17
17
|
if (config.style) {
|
|
@@ -34,11 +34,12 @@ function showToast(message, tostConfig) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// src/index.ts
|
|
37
|
+
import chalk from "chalk";
|
|
37
38
|
function parseConfig(config) {
|
|
38
39
|
const options = {};
|
|
39
40
|
let message = null;
|
|
40
41
|
let name = null;
|
|
41
|
-
const optionKeys = ["type", "break", "
|
|
42
|
+
const optionKeys = ["type", "break", "toast", "returnEM", "callBack"];
|
|
42
43
|
for (const key in config) {
|
|
43
44
|
if (Object.prototype.hasOwnProperty.call(config, key)) {
|
|
44
45
|
if (optionKeys.includes(key)) {
|
|
@@ -66,20 +67,43 @@ function parseConfig(config) {
|
|
|
66
67
|
}
|
|
67
68
|
function processEmessage(errorName, config) {
|
|
68
69
|
const message = config.message;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
70
|
+
let consoleType;
|
|
71
|
+
if (config.type === false) {
|
|
72
|
+
consoleType = false;
|
|
73
|
+
} else if (config.type === true || config.type === void 0) {
|
|
74
|
+
consoleType = "err";
|
|
75
|
+
} else {
|
|
76
|
+
consoleType = config.type;
|
|
77
|
+
}
|
|
78
|
+
if (consoleType) {
|
|
79
|
+
if (isBrowser()) {
|
|
80
|
+
switch (consoleType) {
|
|
81
|
+
case "log":
|
|
82
|
+
console.log(message);
|
|
83
|
+
break;
|
|
84
|
+
case "war":
|
|
85
|
+
console.warn(message);
|
|
86
|
+
break;
|
|
87
|
+
case "err":
|
|
88
|
+
console.error(message);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
switch (consoleType) {
|
|
93
|
+
case "log":
|
|
94
|
+
console.log(chalk.bgWhite.black(message));
|
|
95
|
+
break;
|
|
96
|
+
case "war":
|
|
97
|
+
console.warn(chalk.bgYellow.white(message));
|
|
98
|
+
break;
|
|
99
|
+
case "err":
|
|
100
|
+
console.error(chalk.bgRed.white(message));
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
80
104
|
}
|
|
81
|
-
if (config.
|
|
82
|
-
showToast(message, config.
|
|
105
|
+
if (config.toast && isBrowser()) {
|
|
106
|
+
showToast(message, config.toast);
|
|
83
107
|
}
|
|
84
108
|
if (config.callBack) {
|
|
85
109
|
try {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/store.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { StoredEmessage } from \"./types\";\n\nexport const individualMessageStore = new Map<string, StoredEmessage>();\nexport const globalMessageStore = new Map<string, StoredEmessage>();\n","import {
|
|
1
|
+
{"version":3,"sources":["../src/store.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { StoredEmessage } from \"./types\";\r\n\r\nexport const individualMessageStore = new Map<string, StoredEmessage>();\r\nexport const globalMessageStore = new Map<string, StoredEmessage>();\r\n","import { ToastConfig } from \"./types\";\r\n\r\nexport function isBrowser(): boolean {\r\n return typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\r\n}\r\n\r\nexport function showToast(\r\n message: string,\r\n toastConfig: boolean | ToastConfig\r\n): void {\r\n if (!isBrowser()) {\r\n return;\r\n }\r\n\r\n const toast = document.createElement(\"div\");\r\n\r\n const config: ToastConfig = typeof toastConfig === \"object\" ? toastConfig : {};\r\n\r\n toast.textContent = config.message || message;\r\n\r\n // Base styles are applied via CSS, but some can be defaults.\r\n toast.className = \"emessage-toast\";\r\n if (config.style) {\r\n toast.classList.add(...config.style.split(\" \"));\r\n }\r\n\r\n // Positioning\r\n const position = config.position || \"top-right\";\r\n toast.setAttribute(\"data-position\", position);\r\n\r\n document.body.appendChild(toast);\r\n\r\n // Animate in\r\n setTimeout(() => {\r\n toast.classList.add(\"visible\");\r\n }, 10);\r\n\r\n // Animate out and remove\r\n setTimeout(() => {\r\n toast.classList.remove(\"visible\");\r\n toast.addEventListener(\"transitionend\", () => {\r\n if (toast.parentElement) {\r\n toast.parentElement.removeChild(toast);\r\n }\r\n });\r\n }, 3000);\r\n}\r\n","import { individualMessageStore, globalMessageStore } from \"./store\";\r\nimport type {\r\n EmessageConfig,\r\n EmessageOptions,\r\n StoredEmessage,\r\n ToastConfig,\r\n MessageType,\r\n} from \"./types\";\r\nimport { isBrowser, showToast } from \"./utils\";\r\nimport chalk from \"chalk\";\r\n\r\nfunction parseConfig(\r\n config: Record<string, any>\r\n): { name: string; options: StoredEmessage } | null {\r\n const options: EmessageOptions = {};\r\n let message: string | null = null;\r\n let name: string | null = null;\r\n const optionKeys = [\"type\", \"break\", \"toast\", \"returnEM\", \"callBack\"];\r\n\r\n for (const key in config) {\r\n if (Object.prototype.hasOwnProperty.call(config, key)) {\r\n if (optionKeys.includes(key)) {\r\n (options as any)[key] = config[key];\r\n } else {\r\n if (name !== null) {\r\n console.warn(\r\n `emessages: Found multiple potential error names in one config object. Using first one found: \"${name}\".`\r\n );\r\n continue;\r\n }\r\n name = key;\r\n message = String(config[key]);\r\n }\r\n }\r\n }\r\n\r\n if (name === null || message === null) {\r\n console.error(\r\n \"emessages: Invalid config object. Could not find error name and message.\",\r\n config\r\n );\r\n return null;\r\n }\r\n\r\n return { name, options: { ...options, message } };\r\n}\r\n\r\nfunction processEmessage(\r\n errorName: string,\r\n config: StoredEmessage\r\n): string | void {\r\n const message = config.message;\r\n\r\n let consoleType: MessageType | false;\r\n if (config.type === false) {\r\n consoleType = false;\r\n } else if (config.type === true || config.type === undefined) {\r\n consoleType = \"err\";\r\n } else {\r\n consoleType = config.type;\r\n }\r\n\r\n // 1. Console log\r\n if (consoleType) {\r\n if (isBrowser()) {\r\n switch (consoleType) {\r\n case \"log\":\r\n console.log(message);\r\n break;\r\n case \"war\":\r\n console.warn(message);\r\n break;\r\n case \"err\":\r\n console.error(message);\r\n break;\r\n }\r\n } else {\r\n switch (consoleType) {\r\n case \"log\":\r\n console.log(chalk.bgWhite.black(message));\r\n break;\r\n case \"war\":\r\n console.warn(chalk.bgYellow.white(message));\r\n break;\r\n case \"err\":\r\n console.error(chalk.bgRed.white(message));\r\n break;\r\n }\r\n }\r\n }\r\n\r\n // 2. Toast notification\r\n if (config.toast && isBrowser()) {\r\n showToast(message, config.toast);\r\n }\r\n\r\n // 3. Callback\r\n if (config.callBack) {\r\n try {\r\n config.callBack();\r\n } catch (e: any) {\r\n console.error(\r\n `emessages: Error in callBack for \"${errorName}\":`,\r\n e.message\r\n );\r\n }\r\n }\r\n\r\n // 4. Return error message\r\n if (config.returnEM) {\r\n return message;\r\n }\r\n\r\n // 5. Break execution\r\n if (config.break ?? true) {\r\n if (isBrowser()) {\r\n throw new Error(message);\r\n } else {\r\n process.exit(1);\r\n }\r\n }\r\n}\r\n\r\nexport function Emessage(...configs: EmessageConfig[]) {\r\n for (const config of configs) {\r\n const parsed = parseConfig(config);\r\n if (parsed) {\r\n individualMessageStore.set(parsed.name, parsed.options);\r\n }\r\n }\r\n}\r\n\r\nEmessage.global = function (...configs: EmessageConfig[]) {\r\n for (const config of configs) {\r\n const parsed = parseConfig(config);\r\n if (parsed) {\r\n globalMessageStore.set(parsed.name, parsed.options);\r\n }\r\n }\r\n};\r\n\r\nexport function showE(error: string | Record<string, any>): string | void {\r\n let config: StoredEmessage | null = null;\r\n let errorName: string | null = null;\r\n\r\n if (typeof error === \"string\") {\r\n errorName = error;\r\n config =\r\n individualMessageStore.get(error) ?? globalMessageStore.get(error) ?? null;\r\n if (!config) {\r\n console.error(`emessages: Error \"${error}\" not found.`);\r\n return;\r\n }\r\n } else if (typeof error === \"object\" && error !== null) {\r\n const parsed = parseConfig(error);\r\n if (parsed) {\r\n errorName = parsed.name;\r\n config = parsed.options;\r\n } else {\r\n console.error(\"emessages: Invalid object passed to showE.\");\r\n return;\r\n }\r\n } else {\r\n console.error(\"emessages: Invalid argument passed to showE.\");\r\n return;\r\n }\r\n\r\n if (config && errorName) {\r\n return processEmessage(errorName, config);\r\n }\r\n}\r\n"],"mappings":";AAEO,IAAM,yBAAyB,oBAAI,IAA4B;AAC/D,IAAM,qBAAqB,oBAAI,IAA4B;;;ACD3D,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW,eAAe,OAAO,OAAO,aAAa;AACrE;AAEO,SAAS,UACd,SACA,aACM;AACN,MAAI,CAAC,UAAU,GAAG;AAChB;AAAA,EACF;AAEA,QAAM,QAAQ,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Y,SAA2B;AACrD,aAAW,UAAU,SAAS;AAC5B,UAAM,SAAS,YAAY,MAAM;AACjC,QAAI,QAAQ;AACV,6BAAuB,IAAI,OAAO,MAAM,OAAO,OAAO;AAAA,IACxD;AAAA,EACF;AACF;AAEA,SAAS,SAAS,YAAa,SAA2B;AACxD,aAAW,UAAU,SAAS;AAC5B,UAAM,SAAS,YAAY,MAAM;AACjC,QAAI,QAAQ;AACV,yBAAmB,IAAI,OAAO,MAAM,OAAO,OAAO;AAAA,IACpD;AAAA,EACF;AACF;AAEO,SAAS,MAAM,OAAoD;AACxE,MAAI,SAAgC;AACpC,MAAI,YAA2B;AAE/B,MAAI,OAAO,UAAU,UAAU;AAC7B,gBAAY;AACZ,aACE,uBAAuB,IAAI,KAAK,KAAK,mBAAmB,IAAI,KAAK,KAAK;AACxE,QAAI,CAAC,QAAQ;AACX,cAAQ,MAAM,qBAAqB,KAAK,cAAc;AACtD;AAAA,IACF;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,UAAU,MAAM;AACtD,UAAM,SAAS,YAAY,KAAK;AAChC,QAAI,QAAQ;AACV,kBAAY,OAAO;AACnB,eAAS,OAAO;AAAA,IAClB,OAAO;AACL,cAAQ,MAAM,4CAA4C;AAC1D;AAAA,IACF;AAAA,EACF,OAAO;AACL,YAAQ,MAAM,8CAA8C;AAC5D;AAAA,EACF;AAEA,MAAI,UAAU,WAAW;AACvB,WAAO,gBAAgB,WAAW,MAAM;AAAA,EAC1C;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emessages",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A flexible error handling library for JS/TS. Define custom errors with console, toast, and callbacks, supporting global/local scopes and all environments.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -32,5 +32,8 @@
|
|
|
32
32
|
"@types/node": "^20.11.24",
|
|
33
33
|
"tsup": "^8.0.2",
|
|
34
34
|
"typescript": "^5.3.3"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"chalk": "^5.6.2"
|
|
35
38
|
}
|
|
36
39
|
}
|