runtime-reporter 0.2.0 → 0.3.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 +9 -8
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -11
- package/dist/index.d.ts +11 -11
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# Runtime Reporter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Structured runtime events for applications and frameworks. A composable foundation for logging, messaging, and runtime reporting.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Security
|
|
7
|
+
- **Security focused**: Pass an empty message set in production to avoid exposing internal messaging.
|
|
8
8
|
- **Centralized messages**: Define message text once; reference by a unique code everywhere else.
|
|
9
9
|
- **Tokenized templates**: Apply runtime data to messages via templated strings and tokenized variables.
|
|
10
10
|
- **Type-safe**: Autocomplete and compile-time validation for message codes and token names.
|
|
11
11
|
- **Tree-shakeable**: Pass an empty message set in production to reduce your bundle size
|
|
12
12
|
- **Test friendly**: Use `message()` to assert on final output without duplicating message text.
|
|
13
|
-
- **
|
|
14
|
-
- **Code-based messaging**: Coded messages make it easy to identify and find errors to perform debugging tasks.
|
|
13
|
+
- **Code-based messaging**: Coded messages make it easy to identify errors to perform debugging tasks.
|
|
15
14
|
- **Small footprint**: Minimal bundle size (~2 KB minified) so it adds negligible weight to your app.
|
|
16
15
|
- **Zero dependencies**: No runtime dependencies; the published package is fully self-contained.
|
|
16
|
+
- **Scalable pattern**: Can scale to fit your specific needs regardless of your project's size.
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
|
@@ -30,7 +30,7 @@ Define the message “schema” (code + template + tokens), then create a `messa
|
|
|
30
30
|
```ts
|
|
31
31
|
import type { RuntimeReporterMessages } from "runtime-reporter";
|
|
32
32
|
|
|
33
|
-
type MyMessages =
|
|
33
|
+
type MyMessages =
|
|
34
34
|
| {
|
|
35
35
|
code: "ERR01";
|
|
36
36
|
template: "{{ componentName }} failed to mount";
|
|
@@ -40,8 +40,7 @@ type MyMessages = Array<
|
|
|
40
40
|
code: "INFO01";
|
|
41
41
|
template: "Ready";
|
|
42
42
|
tokens?: undefined;
|
|
43
|
-
}
|
|
44
|
-
>;
|
|
43
|
+
};
|
|
45
44
|
|
|
46
45
|
const messages: RuntimeReporterMessages<MyMessages> = {
|
|
47
46
|
ERR01: "{{ componentName }} failed to mount",
|
|
@@ -107,6 +106,8 @@ Takes a list of messages, an optional set of configuration options, and returns
|
|
|
107
106
|
|
|
108
107
|
### Custom formatting
|
|
109
108
|
|
|
109
|
+
You can customize the format of the message by providing a custom `formatMessage` function.
|
|
110
|
+
|
|
110
111
|
```ts
|
|
111
112
|
const reporter = createReporter(messages, {
|
|
112
113
|
formatMessage: (msg, code) => `[${code}] ${msg}`,
|
|
@@ -129,7 +130,7 @@ describe("reporter messages", () => {
|
|
|
129
130
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
130
131
|
reporter.error("ERR01", { componentName: "Widget" });
|
|
131
132
|
|
|
132
|
-
expect(console.error).
|
|
133
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
133
134
|
reporter.message("ERR01", { componentName: "Widget" })
|
|
134
135
|
);
|
|
135
136
|
});
|
package/dist/index.cjs
CHANGED
|
@@ -29,8 +29,8 @@ var resolveTemplate = function resolveTemplate2(template, tokens) {
|
|
|
29
29
|
Object.entries(tokens || {}).forEach((entry) => {
|
|
30
30
|
const [token, value] = entry;
|
|
31
31
|
const replace = value instanceof Error ? value.message : String(value ?? "");
|
|
32
|
-
const
|
|
33
|
-
message = message.replace(new RegExp(`\\{\\{\\s*${
|
|
32
|
+
const sanitized = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
33
|
+
message = message.replace(new RegExp(`\\{\\{\\s*${sanitized}\\s*\\}\\}`, "g"), replace);
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
return message;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * The type information for a single runtime reporter message\n * @since v0.1.0\n */\nexport type RuntimeReporterMessage = {\n code: string;\n template: string;\n tokens?: string[];\n};\n\n/**\n * The type for a full list of messages with their associated code and template\n * @since v0.1.0\n */\nexport type RuntimeReporterMessages<T extends RuntimeReporterMessage[]> = {\n [K in T[number][\"code\"]]: Extract<T[number], { code: K }>[\"template\"];\n};\n\n/**\n * The type for the supported values of a placeholder token\n * @since v0.1.0\n */\nexport type RuntimeReporterToken = string | number | boolean | Error | null | undefined;\n\n/**\n * The type for a record of placeholder token names and their values\n * @since v0.2.0\n */\nexport type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;\n\n/**\n * A utility type used to determine the second argument of the runtime reporter methods\n * @private\n */\ntype RuntimeReporterTokensArgs<\n T extends RuntimeReporterMessages<RuntimeReporterMessage[]>,\n U extends keyof T,\n> =\n T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage[]>\n ? Extract<V[number], { code: U }>[\"tokens\"] extends infer Tokens\n ? Tokens extends readonly string[]\n ? [tokens: Record<Tokens[number], RuntimeReporterToken>]\n : []\n : []\n : never;\n\n/**\n * The runtime report object with all of it's associated methods; the result\n * of the primary export: `createReporter`\n * @private\n */\ninterface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate an of the raw message text._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n message<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): string;\n\n /**\n * Logs a warning to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n warn<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs an error to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n error<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs a message to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n log<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Throws an error with the full text of the targeted message in all environments\n *\n * _Note: When the `createReporter` function is called in production with an empty\n * array, this method will use the \"defaultTemplate\" option in this format: \"<defaultTemplate> (<code>)\"_\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n fail<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n}\n\nexport interface RuntimeReporterOptions {\n /**\n * A hook to format the message text univerally. By default, it\n * outputs the message in the following format: \"<message> (<code>)\"\n * @param message The resolved message text; the placeholders have been replaced by their token values\n * @param code The unique code associated with the message\n * @returns The final, fully formatted message\n */\n formatMessage?: (message: string, code: string) => string;\n\n /**\n * The default template to fallback on when a provided code does not\n * have an associated message. Defaults to \"An error occurred\"\n *\n * _Note: This is only used when the `fail` method is called in production\n * environments when the `createReporter` function is provided an empty array._\n */\n defaultTemplate?: string;\n}\n\n/**\n * Resolves the message text via the message template and the associated tokens\n * @param template The template string for the reported message\n * @param tokens The token names and values for the instance\n * @returns The resolved message text; returns an empty string if the template is falsy\n */\nconst resolveTemplate = function resolveTemplate(\n template: string,\n tokens?: Record<string, RuntimeReporterToken>\n): string {\n let message = template;\n\n if (message) {\n Object.entries(tokens || {}).forEach((entry) => {\n const [token, value] = entry;\n const replace = value instanceof Error ? value.message : String(value ?? \"\");\n const santized = token.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n message = message.replace(new RegExp(`\\\\{\\\\{\\\\s*${santized}\\\\s*\\\\}\\\\}`, \"g\"), replace);\n });\n }\n\n return message;\n};\n\n/**\n * Creates a reporter object with various helpful runtime methods\n * @param messages The messages record organized by code and template\n * @param options Optional configuration options\n * @returns A runtime report object\n */\nexport function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>>(\n messages: T,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n\n /**\n * Retrieves the final message for a give code and its associated tokens\n * @param code The unique code associated with the message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n * @returns The fully resolve message or an empty string if there was no template\n */\n const getMessage = function getMessage(\n code: string,\n ...args: Array<Record<string, RuntimeReporterToken>>\n ): string {\n const template = messages[code] || defaultTemplate;\n const tokens = args[0];\n const text = resolveTemplate(template, tokens);\n return formatMessage(text, code);\n };\n\n return {\n message: (code, ...args) => {\n return getMessage(code, ...args);\n },\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.log(message);\n },\n fail: (code, ...args) => {\n const message = getMessage(code, ...args);\n throw new Error(message);\n },\n };\n}\n"],"mappings":";;;;;;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AW,MAAM,GAAG,IAAI;AACxC,UAAI,SAAS,IAAI,EAAG,SAAQ,IAAI,OAAO;AAAA,IAC3C;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,YAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACJ;AACJ;","names":["resolveTemplate","getMessage"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * The type information for a single runtime reporter message\n * @since v0.1.0\n */\nexport type RuntimeReporterMessage = {\n code: string;\n template: string;\n tokens?: string[];\n};\n\n/**\n * The type for a full list of messages with their associated code and template\n * @since v0.1.0\n */\nexport type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {\n [K in T[\"code\"]]: Extract<T, { code: K }>[\"template\"];\n};\n\n/**\n * The type for the supported values of a placeholder token\n * @since v0.1.0\n */\nexport type RuntimeReporterToken = string | number | boolean | Error | null | undefined;\n\n/**\n * The type for a record of placeholder token names and their values\n * @since v0.2.0\n */\nexport type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;\n\n/**\n * A utility type used to determine the second argument of the runtime reporter methods\n * @private\n */\ntype RuntimeReporterTokensArgs<\n T extends RuntimeReporterMessages<RuntimeReporterMessage>,\n U extends keyof T,\n> =\n T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage>\n ? Extract<V, { code: U }>[\"tokens\"] extends infer Tokens\n ? Tokens extends readonly string[]\n ? [tokens: Record<Tokens[number], RuntimeReporterToken>]\n : []\n : []\n : never;\n\n/**\n * The runtime report object with all of it's associated methods; the result\n * of the primary export: `createReporter`\n * @private\n */\ninterface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate an of the raw message text._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n message<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): string;\n\n /**\n * Logs a warning to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n warn<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs an error to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n error<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs a message to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n log<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Throws an error with the full text of the targeted message in all environments\n *\n * _Note: When the `createReporter` function is called in production with an empty\n * message set, this method will use the \"defaultTemplate\" option in this format: \"<defaultTemplate> (<code>)\"_\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n fail<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n}\n\nexport interface RuntimeReporterOptions {\n /**\n * A hook to format the message text universally. By default, it\n * outputs the message in the following format: \"<message> (<code>)\"\n * @param message The resolved message text; the placeholders have been replaced by their token values\n * @param code The unique code associated with the message\n * @returns The final, fully formatted message\n */\n formatMessage?: (message: string, code: string) => string;\n\n /**\n * The default template to fallback on when a provided code does not\n * have an associated message. Defaults to \"An error occurred\"\n *\n * _Note: This is only used when the `fail` method is called in production\n * environments when the `createReporter` function is provided an empty message set._\n */\n defaultTemplate?: string;\n}\n\n/**\n * Resolves the message text via the message template and the associated tokens\n * @param template The template string for the reported message\n * @param tokens The token names and values for the instance\n * @returns The resolved message text; returns an empty string if the template is falsy\n */\nconst resolveTemplate = function resolveTemplate(\n template: string,\n tokens?: Record<string, RuntimeReporterToken>\n): string {\n let message = template;\n\n if (message) {\n Object.entries(tokens || {}).forEach((entry) => {\n const [token, value] = entry;\n const replace = value instanceof Error ? value.message : String(value ?? \"\");\n const sanitized = token.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n message = message.replace(new RegExp(`\\\\{\\\\{\\\\s*${sanitized}\\\\s*\\\\}\\\\}`, \"g\"), replace);\n });\n }\n\n return message;\n};\n\n/**\n * Creates a reporter object with various helpful runtime methods\n * @param messages The messages record organized by code and template\n * @param options Optional configuration options\n * @returns A runtime report object\n */\nexport function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>>(\n messages: T,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n\n /**\n * Retrieves the final message for a give code and its associated tokens\n * @param code The unique code associated with the message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n * @returns The fully resolve message or an empty string if there was no template\n */\n const getMessage = function getMessage(\n code: string,\n ...args: Array<Record<string, RuntimeReporterToken>>\n ): string {\n const template = messages[code] || defaultTemplate;\n const tokens = args[0];\n const text = resolveTemplate(template, tokens);\n return formatMessage(text, code);\n };\n\n return {\n message: (code, ...args) => {\n return getMessage(code, ...args);\n },\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.log(message);\n },\n fail: (code, ...args) => {\n const message = getMessage(code, ...args);\n throw new Error(message);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmJA,IAAM,kBAAkB,SAASA,iBAC7B,UACA,QACM;AACN,MAAI,UAAU;AAEd,MAAI,SAAS;AACT,WAAO,QAAQ,UAAU,CAAC,CAAC,EAAE,QAAQ,CAAC,UAAU;AAC5C,YAAM,CAAC,OAAO,KAAK,IAAI;AACvB,YAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,SAAS,EAAE;AAC3E,YAAM,YAAY,MAAM,QAAQ,uBAAuB,MAAM;AAC7D,gBAAU,QAAQ,QAAQ,IAAI,OAAO,aAAa,SAAS,cAAc,GAAG,GAAG,OAAO;AAAA,IAC1F,CAAC;AAAA,EACL;AAEA,SAAO;AACX;AAQO,SAAS,eACZ,UACA,UAAkC,CAAC,GACjB;AAClB,QAAM;AAAA,IACF,gBAAgB,CAAC,SAAS,SAAS,GAAG,OAAO,KAAK,IAAI;AAAA,IACtD,kBAAkB;AAAA,EACtB,IAAI;AAQJ,QAAM,aAAa,SAASC,YACxB,SACG,MACG;AACN,UAAM,WAAW,SAAS,IAAI,KAAK;AACnC,UAAM,SAAS,KAAK,CAAC;AACrB,UAAM,OAAO,gBAAgB,UAAU,MAAM;AAC7C,WAAO,cAAc,MAAM,IAAI;AAAA,EACnC;AAEA,SAAO;AAAA,IACH,SAAS,CAAC,SAAS,SAAS;AACxB,aAAO,WAAW,MAAM,GAAG,IAAI;AAAA,IACnC;AAAA,IACA,OAAO,CAAC,SAAS,SAAS;AACtB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,SAAS,IAAI,EAAG,SAAQ,MAAM,OAAO;AAAA,IAC7C;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,SAAS,IAAI,EAAG,SAAQ,KAAK,OAAO;AAAA,IAC5C;AAAA,IACA,KAAK,CAAC,SAAS,SAAS;AACpB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,SAAS,IAAI,EAAG,SAAQ,IAAI,OAAO;AAAA,IAC3C;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,YAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACJ;AACJ;","names":["resolveTemplate","getMessage"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -11,8 +11,8 @@ type RuntimeReporterMessage = {
|
|
|
11
11
|
* The type for a full list of messages with their associated code and template
|
|
12
12
|
* @since v0.1.0
|
|
13
13
|
*/
|
|
14
|
-
type RuntimeReporterMessages<T extends RuntimeReporterMessage
|
|
15
|
-
[K in T[
|
|
14
|
+
type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {
|
|
15
|
+
[K in T["code"]]: Extract<T, {
|
|
16
16
|
code: K;
|
|
17
17
|
}>["template"];
|
|
18
18
|
};
|
|
@@ -30,7 +30,7 @@ type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;
|
|
|
30
30
|
* A utility type used to determine the second argument of the runtime reporter methods
|
|
31
31
|
* @private
|
|
32
32
|
*/
|
|
33
|
-
type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
33
|
+
type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporterMessage>, U extends keyof T> = T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage> ? Extract<V, {
|
|
34
34
|
code: U;
|
|
35
35
|
}>["tokens"] extends infer Tokens ? Tokens extends readonly string[] ? [tokens: Record<Tokens[number], RuntimeReporterToken>] : [] : [] : never;
|
|
36
36
|
/**
|
|
@@ -38,7 +38,7 @@ type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporter
|
|
|
38
38
|
* of the primary export: `createReporter`
|
|
39
39
|
* @private
|
|
40
40
|
*/
|
|
41
|
-
interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
41
|
+
interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>> {
|
|
42
42
|
/**
|
|
43
43
|
* Retrieves the full text of the targeted message
|
|
44
44
|
*
|
|
@@ -53,7 +53,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
53
53
|
*
|
|
54
54
|
* _Note: This method will only log when the message associated with the code is found;
|
|
55
55
|
* meaning it will not be called in production if the `createReporter` function
|
|
56
|
-
* is provided an empty
|
|
56
|
+
* is provided an empty message set._
|
|
57
57
|
* @param code A direct reference to the unique code for the targeted message
|
|
58
58
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
59
59
|
*/
|
|
@@ -63,7 +63,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
63
63
|
*
|
|
64
64
|
* _Note: This method will only log when the message associated with the code is found;
|
|
65
65
|
* meaning it will not be called in production if the `createReporter` function
|
|
66
|
-
* is provided an empty
|
|
66
|
+
* is provided an empty message set._
|
|
67
67
|
* @param code A direct reference to the unique code for the targeted message
|
|
68
68
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
69
69
|
*/
|
|
@@ -73,7 +73,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
73
73
|
*
|
|
74
74
|
* _Note: This method will only log when the message associated with the code is found;
|
|
75
75
|
* meaning it will not be called in production if the `createReporter` function
|
|
76
|
-
* is provided an empty
|
|
76
|
+
* is provided an empty message set._
|
|
77
77
|
* @param code A direct reference to the unique code for the targeted message
|
|
78
78
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
79
79
|
*/
|
|
@@ -82,7 +82,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
82
82
|
* Throws an error with the full text of the targeted message in all environments
|
|
83
83
|
*
|
|
84
84
|
* _Note: When the `createReporter` function is called in production with an empty
|
|
85
|
-
*
|
|
85
|
+
* message set, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
|
|
86
86
|
* @param code A direct reference to the unique code for the targeted message
|
|
87
87
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
88
88
|
*/
|
|
@@ -90,7 +90,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
90
90
|
}
|
|
91
91
|
interface RuntimeReporterOptions {
|
|
92
92
|
/**
|
|
93
|
-
* A hook to format the message text
|
|
93
|
+
* A hook to format the message text universally. By default, it
|
|
94
94
|
* outputs the message in the following format: "<message> (<code>)"
|
|
95
95
|
* @param message The resolved message text; the placeholders have been replaced by their token values
|
|
96
96
|
* @param code The unique code associated with the message
|
|
@@ -102,7 +102,7 @@ interface RuntimeReporterOptions {
|
|
|
102
102
|
* have an associated message. Defaults to "An error occurred"
|
|
103
103
|
*
|
|
104
104
|
* _Note: This is only used when the `fail` method is called in production
|
|
105
|
-
* environments when the `createReporter` function is provided an empty
|
|
105
|
+
* environments when the `createReporter` function is provided an empty message set._
|
|
106
106
|
*/
|
|
107
107
|
defaultTemplate?: string;
|
|
108
108
|
}
|
|
@@ -112,6 +112,6 @@ interface RuntimeReporterOptions {
|
|
|
112
112
|
* @param options Optional configuration options
|
|
113
113
|
* @returns A runtime report object
|
|
114
114
|
*/
|
|
115
|
-
declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
115
|
+
declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>>(messages: T, options?: RuntimeReporterOptions): RuntimeReporter<T>;
|
|
116
116
|
|
|
117
117
|
export { type RuntimeReporterMessage, type RuntimeReporterMessages, type RuntimeReporterOptions, type RuntimeReporterToken, type RuntimeReporterTokens, createReporter };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,8 +11,8 @@ type RuntimeReporterMessage = {
|
|
|
11
11
|
* The type for a full list of messages with their associated code and template
|
|
12
12
|
* @since v0.1.0
|
|
13
13
|
*/
|
|
14
|
-
type RuntimeReporterMessages<T extends RuntimeReporterMessage
|
|
15
|
-
[K in T[
|
|
14
|
+
type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {
|
|
15
|
+
[K in T["code"]]: Extract<T, {
|
|
16
16
|
code: K;
|
|
17
17
|
}>["template"];
|
|
18
18
|
};
|
|
@@ -30,7 +30,7 @@ type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;
|
|
|
30
30
|
* A utility type used to determine the second argument of the runtime reporter methods
|
|
31
31
|
* @private
|
|
32
32
|
*/
|
|
33
|
-
type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
33
|
+
type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporterMessage>, U extends keyof T> = T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage> ? Extract<V, {
|
|
34
34
|
code: U;
|
|
35
35
|
}>["tokens"] extends infer Tokens ? Tokens extends readonly string[] ? [tokens: Record<Tokens[number], RuntimeReporterToken>] : [] : [] : never;
|
|
36
36
|
/**
|
|
@@ -38,7 +38,7 @@ type RuntimeReporterTokensArgs<T extends RuntimeReporterMessages<RuntimeReporter
|
|
|
38
38
|
* of the primary export: `createReporter`
|
|
39
39
|
* @private
|
|
40
40
|
*/
|
|
41
|
-
interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
41
|
+
interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>> {
|
|
42
42
|
/**
|
|
43
43
|
* Retrieves the full text of the targeted message
|
|
44
44
|
*
|
|
@@ -53,7 +53,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
53
53
|
*
|
|
54
54
|
* _Note: This method will only log when the message associated with the code is found;
|
|
55
55
|
* meaning it will not be called in production if the `createReporter` function
|
|
56
|
-
* is provided an empty
|
|
56
|
+
* is provided an empty message set._
|
|
57
57
|
* @param code A direct reference to the unique code for the targeted message
|
|
58
58
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
59
59
|
*/
|
|
@@ -63,7 +63,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
63
63
|
*
|
|
64
64
|
* _Note: This method will only log when the message associated with the code is found;
|
|
65
65
|
* meaning it will not be called in production if the `createReporter` function
|
|
66
|
-
* is provided an empty
|
|
66
|
+
* is provided an empty message set._
|
|
67
67
|
* @param code A direct reference to the unique code for the targeted message
|
|
68
68
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
69
69
|
*/
|
|
@@ -73,7 +73,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
73
73
|
*
|
|
74
74
|
* _Note: This method will only log when the message associated with the code is found;
|
|
75
75
|
* meaning it will not be called in production if the `createReporter` function
|
|
76
|
-
* is provided an empty
|
|
76
|
+
* is provided an empty message set._
|
|
77
77
|
* @param code A direct reference to the unique code for the targeted message
|
|
78
78
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
79
79
|
*/
|
|
@@ -82,7 +82,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
82
82
|
* Throws an error with the full text of the targeted message in all environments
|
|
83
83
|
*
|
|
84
84
|
* _Note: When the `createReporter` function is called in production with an empty
|
|
85
|
-
*
|
|
85
|
+
* message set, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
|
|
86
86
|
* @param code A direct reference to the unique code for the targeted message
|
|
87
87
|
* @param args The remaining optional argument for the function; a record containing the placeholder token values
|
|
88
88
|
*/
|
|
@@ -90,7 +90,7 @@ interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessa
|
|
|
90
90
|
}
|
|
91
91
|
interface RuntimeReporterOptions {
|
|
92
92
|
/**
|
|
93
|
-
* A hook to format the message text
|
|
93
|
+
* A hook to format the message text universally. By default, it
|
|
94
94
|
* outputs the message in the following format: "<message> (<code>)"
|
|
95
95
|
* @param message The resolved message text; the placeholders have been replaced by their token values
|
|
96
96
|
* @param code The unique code associated with the message
|
|
@@ -102,7 +102,7 @@ interface RuntimeReporterOptions {
|
|
|
102
102
|
* have an associated message. Defaults to "An error occurred"
|
|
103
103
|
*
|
|
104
104
|
* _Note: This is only used when the `fail` method is called in production
|
|
105
|
-
* environments when the `createReporter` function is provided an empty
|
|
105
|
+
* environments when the `createReporter` function is provided an empty message set._
|
|
106
106
|
*/
|
|
107
107
|
defaultTemplate?: string;
|
|
108
108
|
}
|
|
@@ -112,6 +112,6 @@ interface RuntimeReporterOptions {
|
|
|
112
112
|
* @param options Optional configuration options
|
|
113
113
|
* @returns A runtime report object
|
|
114
114
|
*/
|
|
115
|
-
declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage
|
|
115
|
+
declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>>(messages: T, options?: RuntimeReporterOptions): RuntimeReporter<T>;
|
|
116
116
|
|
|
117
117
|
export { type RuntimeReporterMessage, type RuntimeReporterMessages, type RuntimeReporterOptions, type RuntimeReporterToken, type RuntimeReporterTokens, createReporter };
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,8 @@ var resolveTemplate = function resolveTemplate2(template, tokens) {
|
|
|
5
5
|
Object.entries(tokens || {}).forEach((entry) => {
|
|
6
6
|
const [token, value] = entry;
|
|
7
7
|
const replace = value instanceof Error ? value.message : String(value ?? "");
|
|
8
|
-
const
|
|
9
|
-
message = message.replace(new RegExp(`\\{\\{\\s*${
|
|
8
|
+
const sanitized = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
9
|
+
message = message.replace(new RegExp(`\\{\\{\\s*${sanitized}\\s*\\}\\}`, "g"), replace);
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
12
|
return message;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * The type information for a single runtime reporter message\n * @since v0.1.0\n */\nexport type RuntimeReporterMessage = {\n code: string;\n template: string;\n tokens?: string[];\n};\n\n/**\n * The type for a full list of messages with their associated code and template\n * @since v0.1.0\n */\nexport type RuntimeReporterMessages<T extends RuntimeReporterMessage[]> = {\n [K in T[number][\"code\"]]: Extract<T[number], { code: K }>[\"template\"];\n};\n\n/**\n * The type for the supported values of a placeholder token\n * @since v0.1.0\n */\nexport type RuntimeReporterToken = string | number | boolean | Error | null | undefined;\n\n/**\n * The type for a record of placeholder token names and their values\n * @since v0.2.0\n */\nexport type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;\n\n/**\n * A utility type used to determine the second argument of the runtime reporter methods\n * @private\n */\ntype RuntimeReporterTokensArgs<\n T extends RuntimeReporterMessages<RuntimeReporterMessage[]>,\n U extends keyof T,\n> =\n T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage[]>\n ? Extract<V[number], { code: U }>[\"tokens\"] extends infer Tokens\n ? Tokens extends readonly string[]\n ? [tokens: Record<Tokens[number], RuntimeReporterToken>]\n : []\n : []\n : never;\n\n/**\n * The runtime report object with all of it's associated methods; the result\n * of the primary export: `createReporter`\n * @private\n */\ninterface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate an of the raw message text._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n message<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): string;\n\n /**\n * Logs a warning to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n warn<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs an error to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n error<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs a message to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty array._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n log<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Throws an error with the full text of the targeted message in all environments\n *\n * _Note: When the `createReporter` function is called in production with an empty\n * array, this method will use the \"defaultTemplate\" option in this format: \"<defaultTemplate> (<code>)\"_\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n fail<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n}\n\nexport interface RuntimeReporterOptions {\n /**\n * A hook to format the message text univerally. By default, it\n * outputs the message in the following format: \"<message> (<code>)\"\n * @param message The resolved message text; the placeholders have been replaced by their token values\n * @param code The unique code associated with the message\n * @returns The final, fully formatted message\n */\n formatMessage?: (message: string, code: string) => string;\n\n /**\n * The default template to fallback on when a provided code does not\n * have an associated message. Defaults to \"An error occurred\"\n *\n * _Note: This is only used when the `fail` method is called in production\n * environments when the `createReporter` function is provided an empty array._\n */\n defaultTemplate?: string;\n}\n\n/**\n * Resolves the message text via the message template and the associated tokens\n * @param template The template string for the reported message\n * @param tokens The token names and values for the instance\n * @returns The resolved message text; returns an empty string if the template is falsy\n */\nconst resolveTemplate = function resolveTemplate(\n template: string,\n tokens?: Record<string, RuntimeReporterToken>\n): string {\n let message = template;\n\n if (message) {\n Object.entries(tokens || {}).forEach((entry) => {\n const [token, value] = entry;\n const replace = value instanceof Error ? value.message : String(value ?? \"\");\n const santized = token.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n message = message.replace(new RegExp(`\\\\{\\\\{\\\\s*${santized}\\\\s*\\\\}\\\\}`, \"g\"), replace);\n });\n }\n\n return message;\n};\n\n/**\n * Creates a reporter object with various helpful runtime methods\n * @param messages The messages record organized by code and template\n * @param options Optional configuration options\n * @returns A runtime report object\n */\nexport function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>>(\n messages: T,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n\n /**\n * Retrieves the final message for a give code and its associated tokens\n * @param code The unique code associated with the message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n * @returns The fully resolve message or an empty string if there was no template\n */\n const getMessage = function getMessage(\n code: string,\n ...args: Array<Record<string, RuntimeReporterToken>>\n ): string {\n const template = messages[code] || defaultTemplate;\n const tokens = args[0];\n const text = resolveTemplate(template, tokens);\n return formatMessage(text, code);\n };\n\n return {\n message: (code, ...args) => {\n return getMessage(code, ...args);\n },\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.log(message);\n },\n fail: (code, ...args) => {\n const message = getMessage(code, ...args);\n throw new Error(message);\n },\n };\n}\n"],"mappings":";AAmJA,IAAM,kBAAkB,SAASA,iBAC7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3C;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,YAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACJ;AACJ;","names":["resolveTemplate","getMessage"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * The type information for a single runtime reporter message\n * @since v0.1.0\n */\nexport type RuntimeReporterMessage = {\n code: string;\n template: string;\n tokens?: string[];\n};\n\n/**\n * The type for a full list of messages with their associated code and template\n * @since v0.1.0\n */\nexport type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {\n [K in T[\"code\"]]: Extract<T, { code: K }>[\"template\"];\n};\n\n/**\n * The type for the supported values of a placeholder token\n * @since v0.1.0\n */\nexport type RuntimeReporterToken = string | number | boolean | Error | null | undefined;\n\n/**\n * The type for a record of placeholder token names and their values\n * @since v0.2.0\n */\nexport type RuntimeReporterTokens = Record<string, RuntimeReporterToken>;\n\n/**\n * A utility type used to determine the second argument of the runtime reporter methods\n * @private\n */\ntype RuntimeReporterTokensArgs<\n T extends RuntimeReporterMessages<RuntimeReporterMessage>,\n U extends keyof T,\n> =\n T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage>\n ? Extract<V, { code: U }>[\"tokens\"] extends infer Tokens\n ? Tokens extends readonly string[]\n ? [tokens: Record<Tokens[number], RuntimeReporterToken>]\n : []\n : []\n : never;\n\n/**\n * The runtime report object with all of it's associated methods; the result\n * of the primary export: `createReporter`\n * @private\n */\ninterface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate an of the raw message text._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n message<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): string;\n\n /**\n * Logs a warning to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n warn<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs an error to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n error<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Logs a message to the console with the full text of the targeted message in non-production environments\n *\n * _Note: This method will only log when the message associated with the code is found;\n * meaning it will not be called in production if the `createReporter` function\n * is provided an empty message set._\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n log<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n\n /**\n * Throws an error with the full text of the targeted message in all environments\n *\n * _Note: When the `createReporter` function is called in production with an empty\n * message set, this method will use the \"defaultTemplate\" option in this format: \"<defaultTemplate> (<code>)\"_\n * @param code A direct reference to the unique code for the targeted message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n */\n fail<U extends Extract<keyof T, string>>(\n code: U,\n ...args: RuntimeReporterTokensArgs<T, U>\n ): void;\n}\n\nexport interface RuntimeReporterOptions {\n /**\n * A hook to format the message text universally. By default, it\n * outputs the message in the following format: \"<message> (<code>)\"\n * @param message The resolved message text; the placeholders have been replaced by their token values\n * @param code The unique code associated with the message\n * @returns The final, fully formatted message\n */\n formatMessage?: (message: string, code: string) => string;\n\n /**\n * The default template to fallback on when a provided code does not\n * have an associated message. Defaults to \"An error occurred\"\n *\n * _Note: This is only used when the `fail` method is called in production\n * environments when the `createReporter` function is provided an empty message set._\n */\n defaultTemplate?: string;\n}\n\n/**\n * Resolves the message text via the message template and the associated tokens\n * @param template The template string for the reported message\n * @param tokens The token names and values for the instance\n * @returns The resolved message text; returns an empty string if the template is falsy\n */\nconst resolveTemplate = function resolveTemplate(\n template: string,\n tokens?: Record<string, RuntimeReporterToken>\n): string {\n let message = template;\n\n if (message) {\n Object.entries(tokens || {}).forEach((entry) => {\n const [token, value] = entry;\n const replace = value instanceof Error ? value.message : String(value ?? \"\");\n const sanitized = token.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n message = message.replace(new RegExp(`\\\\{\\\\{\\\\s*${sanitized}\\\\s*\\\\}\\\\}`, \"g\"), replace);\n });\n }\n\n return message;\n};\n\n/**\n * Creates a reporter object with various helpful runtime methods\n * @param messages The messages record organized by code and template\n * @param options Optional configuration options\n * @returns A runtime report object\n */\nexport function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage>>(\n messages: T,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n\n /**\n * Retrieves the final message for a give code and its associated tokens\n * @param code The unique code associated with the message\n * @param args The remaining optional argument for the function; a record containing the placeholder token values\n * @returns The fully resolve message or an empty string if there was no template\n */\n const getMessage = function getMessage(\n code: string,\n ...args: Array<Record<string, RuntimeReporterToken>>\n ): string {\n const template = messages[code] || defaultTemplate;\n const tokens = args[0];\n const text = resolveTemplate(template, tokens);\n return formatMessage(text, code);\n };\n\n return {\n message: (code, ...args) => {\n return getMessage(code, ...args);\n },\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messages[code]) console.log(message);\n },\n fail: (code, ...args) => {\n const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,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,SAAS,IAAI,EAAG,SAAQ,IAAI,OAAO;AAAA,IAC3C;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,YAAM,IAAI,MAAM,OAAO;AAAA,IAC3B;AAAA,EACJ;AACJ;","names":["resolveTemplate","getMessage"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "runtime-reporter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Structured runtime events for applications and frameworks. A composable foundation for logging, messaging, and runtime reporting.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|