runtime-reporter 0.2.0 → 0.4.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 CHANGED
@@ -1,19 +1,19 @@
1
1
  # Runtime Reporter
2
2
 
3
- Runtime messaging that is convenient in development and secure in production.
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 minded**: Pass an empty message set in production to avoid exposing internal messaging.
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
- - **Dual module output**: Works with both ESM and CJS project environments.
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
 
@@ -25,26 +25,23 @@ npm install runtime-reporter
25
25
 
26
26
  ### 1) Define your messages
27
27
 
28
- Define the message schema (code + template + tokens), then create a `messages` record keyed by the codes.
28
+ Define your messages by creating an object with the message "schema" (code + template + tokens) and keyed by their codes.
29
29
 
30
30
  ```ts
31
31
  import type { RuntimeReporterMessages } from "runtime-reporter";
32
32
 
33
- type MyMessages = Array<
33
+ const messages: RuntimeReporterMessages<
34
34
  | {
35
35
  code: "ERR01";
36
- template: "{{ componentName }} failed to mount";
37
- tokens: ["componentName"];
36
+ template: "{{ componentName }} failed at {{ phase }}";
37
+ tokens: "componentName" | "phase";
38
38
  }
39
39
  | {
40
40
  code: "INFO01";
41
41
  template: "Ready";
42
- tokens?: undefined;
43
42
  }
44
- >;
45
-
46
- const messages: RuntimeReporterMessages<MyMessages> = {
47
- ERR01: "{{ componentName }} failed to mount",
43
+ > = {
44
+ ERR01: "{{ componentName }} failed at {{ phase }}",
48
45
  INFO01: "Ready",
49
46
  };
50
47
  ```
@@ -67,8 +64,8 @@ export const reporter = createReporter(
67
64
  Call the various reporter methods wherever you need them.
68
65
 
69
66
  ```ts
70
- reporter.error("ERR01", { componentName: "MyComponent" });
71
- // logs: "MyComponent failed to mount (ERR01)"
67
+ reporter.error("ERR01", { componentName: "Router", phase: "mount" });
68
+ // logs: "Router failed at mount (ERR01)"
72
69
 
73
70
  reporter.message("INFO01");
74
71
  // returns: "Ready (INFO01)"
@@ -107,6 +104,8 @@ Takes a list of messages, an optional set of configuration options, and returns
107
104
 
108
105
  ### Custom formatting
109
106
 
107
+ You can customize the format of the message by providing a custom `formatMessage` function.
108
+
110
109
  ```ts
111
110
  const reporter = createReporter(messages, {
112
111
  formatMessage: (msg, code) => `[${code}] ${msg}`,
@@ -116,6 +115,32 @@ reporter.message("INFO01");
116
115
  // "[INFO01] Ready"
117
116
  ```
118
117
 
118
+ ### Using the reporter in production
119
+
120
+ When the `createReporter` function is called in production with an empty message set, the `fail` method will use the customizable `defaultTemplate` option which defaults to "An error occurred". This message is intended to be generic so that it does not reveal sensitive information about the system, while still providing a code for debugging purposes.
121
+
122
+ ```ts
123
+ const reporter = createReporter(
124
+ // Pass an empty object in production for better security and a smaller bundle size
125
+ process.env.NODE_ENV === "production" ? ({} as typeof messages) : messages
126
+ );
127
+
128
+ reporter.fail("ERR01", { componentName: "Router", phase: "mount" });
129
+ // throws: "An error occurred (ERR01)"
130
+ ```
131
+
132
+ The remaining reporter methods will not log anything in production when the code is missing from the message set.
133
+
134
+ ```ts
135
+ const reporter = createReporter(
136
+ // Pass an empty object in production for better security and a smaller bundle size
137
+ process.env.NODE_ENV === "production" ? ({} as typeof messages) : messages
138
+ );
139
+
140
+ reporter.error("ERR01", { componentName: "Router", phase: "mount" });
141
+ // does not log anything
142
+ ```
143
+
119
144
  ### Using `message()` in tests
120
145
 
121
146
  The `message` method returns the resolved string without side effects, allowing you to validate precise messaging without duplicating text.
@@ -129,18 +154,51 @@ describe("reporter messages", () => {
129
154
  vi.spyOn(console, "error").mockImplementation(() => {});
130
155
  reporter.error("ERR01", { componentName: "Widget" });
131
156
 
132
- expect(console.error).toHaveBeenCalledWidth(
157
+ expect(console.error).toHaveBeenCalledWith(
133
158
  reporter.message("ERR01", { componentName: "Widget" })
134
159
  );
135
160
  });
136
161
  });
137
162
  ```
138
163
 
164
+ ### Using the reporter without TypeScript
165
+
166
+ You can use the reporter without TypeScript, you will just lose the type safety and autocomplete.
167
+
168
+ ```js
169
+ import { createReporter } from "runtime-reporter";
170
+
171
+ const reporter = createReporter({
172
+ ERR01: "{{ componentName }} failed at {{ phase }}",
173
+ });
174
+
175
+ reporter.error("ERR01", { componentName: "Router", phase: "mount" });
176
+ // logs: "Router failed at mount (ERR01)"
177
+ ```
178
+
179
+ However, you can still get the same benefits as TypeScript by using JSDoc-style type annotations.
180
+
181
+ ```js
182
+ /**
183
+ * @type {import("runtime-reporter").RuntimeReporterMessages<{
184
+ * code: "ERR01";
185
+ * template: "{{ componentName }} failed at {{ phase }}";
186
+ * tokens: "componentName" | "phase";
187
+ * } | {
188
+ * code: "INFO01";
189
+ * template: "Ready";
190
+ * }>}
191
+ */
192
+ const messages = {
193
+ ERR01: "{{ componentName }} failed at {{ phase }}",
194
+ INFO01: "Ready",
195
+ };
196
+ ```
197
+
139
198
  ### CommonJS support
140
199
 
141
200
  If needed, Common JS imports are also available.
142
201
 
143
202
  ```js
144
- // CJS
145
203
  const { createReporter } = require("runtime-reporter");
146
204
  ```
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 santized = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
- message = message.replace(new RegExp(`\\{\\{\\s*${santized}\\s*\\}\\}`, "g"), replace);
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;
@@ -40,27 +40,26 @@ function createReporter(messages, options = {}) {
40
40
  formatMessage = (message, code) => `${message} (${code})`,
41
41
  defaultTemplate = "An error occurred"
42
42
  } = options;
43
+ const messagesByCode = messages;
43
44
  const getMessage = function getMessage2(code, ...args) {
44
- const template = messages[code] || defaultTemplate;
45
+ const template = messagesByCode[code] || defaultTemplate;
45
46
  const tokens = args[0];
46
47
  const text = resolveTemplate(template, tokens);
47
48
  return formatMessage(text, code);
48
49
  };
49
50
  return {
50
- message: (code, ...args) => {
51
- return getMessage(code, ...args);
52
- },
51
+ message: (code, ...args) => getMessage(code, ...args),
53
52
  error: (code, ...args) => {
54
53
  const message = getMessage(code, ...args);
55
- if (messages[code]) console.error(message);
54
+ if (messagesByCode[code]) console.error(message);
56
55
  },
57
56
  warn: (code, ...args) => {
58
57
  const message = getMessage(code, ...args);
59
- if (messages[code]) console.warn(message);
58
+ if (messagesByCode[code]) console.warn(message);
60
59
  },
61
60
  log: (code, ...args) => {
62
61
  const message = getMessage(code, ...args);
63
- if (messages[code]) console.log(message);
62
+ if (messagesByCode[code]) console.log(message);
64
63
  },
65
64
  fail: (code, ...args) => {
66
65
  const message = getMessage(code, ...args);
@@ -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":";;;;;;;;;;;;;;;;;;;;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,WAAW,MAAM,QAAQ,uBAAuB,MAAM;AAC5D,gBAAU,QAAQ,QAAQ,IAAI,OAAO,aAAa,QAAQ,cAAc,GAAG,GAAG,OAAO;AAAA,IACzF,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"]}
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 ReporterTokensArgs<T extends RuntimeReporterMessage, U extends T[\"code\"]> = Extract<\n T,\n { code: U }\n>[\"tokens\"] extends infer Tokens\n ? [Tokens] extends [string]\n ? [tokens: Record<Tokens, RuntimeReporterToken>]\n : []\n : [];\n\n/**\n * Return type for message(); displays the template + code in default format on hover.\n * The runtime value is the resolved string (tokens substituted); the type is for DX only.\n * @private\n */\ntype MessageReturnType<T extends RuntimeReporterMessage, U extends T[\"code\"]> =\n Extract<T, { code: U }> extends { template: infer Template }\n ? Template extends string\n ? `${Template} (${U})`\n : string\n : string;\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 RuntimeReporterMessage> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Note: As a convenience, the return type will attempt to show the literal type of the template\n * pattern and code suffix (in the default format) for the message on hover; the actual output may\n * vary based on the tokens provided and the `formatMessage` option._\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate any 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 T[\"code\"]>(\n code: U,\n ...args: ReporterTokensArgs<T, U>\n ): MessageReturnType<T, U>;\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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 RuntimeReporterMessage>(\n messages: RuntimeReporterMessages<T>,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n const messagesByCode = messages as Record<string, string>;\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 = messagesByCode[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 getMessage(code, ...args) as MessageReturnType<T, typeof code & T[\"code\"]>,\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[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;AAoJA,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;AACJ,QAAM,iBAAiB;AAQvB,QAAM,aAAa,SAASC,YACxB,SACG,MACG;AACN,UAAM,WAAW,eAAe,IAAI,KAAK;AACzC,UAAM,SAAS,KAAK,CAAC;AACrB,UAAM,OAAO,gBAAgB,UAAU,MAAM;AAC7C,WAAO,cAAc,MAAM,IAAI;AAAA,EACnC;AAEA,SAAO;AAAA,IACH,SAAS,CAAC,SAAS,SACf,WAAW,MAAM,GAAG,IAAI;AAAA,IAC5B,OAAO,CAAC,SAAS,SAAS;AACtB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,MAAM,OAAO;AAAA,IACnD;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,KAAK,OAAO;AAAA,IAClD;AAAA,IACA,KAAK,CAAC,SAAS,SAAS;AACpB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,IAAI,OAAO;AAAA,IACjD;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
@@ -5,14 +5,14 @@
5
5
  type RuntimeReporterMessage = {
6
6
  code: string;
7
7
  template: string;
8
- tokens?: string[];
8
+ tokens?: string;
9
9
  };
10
10
  /**
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[number]["code"]]: Extract<T[number], {
14
+ type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {
15
+ [K in T["code"]]: Extract<T, {
16
16
  code: K;
17
17
  }>["template"];
18
18
  };
@@ -30,67 +30,81 @@ 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[]>, U extends keyof T> = T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage[]> ? Extract<V[number], {
33
+ type ReporterTokensArgs<T extends RuntimeReporterMessage, U extends T["code"]> = Extract<T, {
34
34
  code: U;
35
- }>["tokens"] extends infer Tokens ? Tokens extends readonly string[] ? [tokens: Record<Tokens[number], RuntimeReporterToken>] : [] : [] : never;
35
+ }>["tokens"] extends infer Tokens ? [Tokens] extends [string] ? [tokens: Record<Tokens, RuntimeReporterToken>] : [] : [];
36
+ /**
37
+ * Return type for message(); displays the template + code in default format on hover.
38
+ * The runtime value is the resolved string (tokens substituted); the type is for DX only.
39
+ * @private
40
+ */
41
+ type MessageReturnType<T extends RuntimeReporterMessage, U extends T["code"]> = Extract<T, {
42
+ code: U;
43
+ }> extends {
44
+ template: infer Template;
45
+ } ? Template extends string ? `${Template} (${U})` : string : string;
36
46
  /**
37
47
  * The runtime report object with all of it's associated methods; the result
38
48
  * of the primary export: `createReporter`
39
49
  * @private
40
50
  */
41
- interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>> {
51
+ interface RuntimeReporter<T extends RuntimeReporterMessage> {
42
52
  /**
43
53
  * Retrieves the full text of the targeted message
44
54
  *
55
+ * _Note: As a convenience, the return type will attempt to show the literal type of the template
56
+ * pattern and code suffix (in the default format) for the message on hover; the actual output may
57
+ * vary based on the tokens provided and the `formatMessage` option._
58
+ *
45
59
  * _Tip: This method is particularly useful in the test environment; allowing you
46
- * to make precise assertions without having to duplicate an of the raw message text._
60
+ * to make precise assertions without having to duplicate any of the raw message text._
47
61
  * @param code A direct reference to the unique code for the targeted message
48
62
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
49
63
  */
50
- message<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): string;
64
+ message<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): MessageReturnType<T, U>;
51
65
  /**
52
66
  * Logs a warning to the console with the full text of the targeted message in non-production environments
53
67
  *
54
68
  * _Note: This method will only log when the message associated with the code is found;
55
69
  * meaning it will not be called in production if the `createReporter` function
56
- * is provided an empty array._
70
+ * is provided an empty message set._
57
71
  * @param code A direct reference to the unique code for the targeted message
58
72
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
59
73
  */
60
- warn<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
74
+ warn<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
61
75
  /**
62
76
  * Logs an error to the console with the full text of the targeted message in non-production environments
63
77
  *
64
78
  * _Note: This method will only log when the message associated with the code is found;
65
79
  * meaning it will not be called in production if the `createReporter` function
66
- * is provided an empty array._
80
+ * is provided an empty message set._
67
81
  * @param code A direct reference to the unique code for the targeted message
68
82
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
69
83
  */
70
- error<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
84
+ error<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
71
85
  /**
72
86
  * Logs a message to the console with the full text of the targeted message in non-production environments
73
87
  *
74
88
  * _Note: This method will only log when the message associated with the code is found;
75
89
  * meaning it will not be called in production if the `createReporter` function
76
- * is provided an empty array._
90
+ * is provided an empty message set._
77
91
  * @param code A direct reference to the unique code for the targeted message
78
92
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
79
93
  */
80
- log<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
94
+ log<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
81
95
  /**
82
96
  * Throws an error with the full text of the targeted message in all environments
83
97
  *
84
98
  * _Note: When the `createReporter` function is called in production with an empty
85
- * array, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
99
+ * message set, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
86
100
  * @param code A direct reference to the unique code for the targeted message
87
101
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
88
102
  */
89
- fail<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
103
+ fail<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
90
104
  }
91
105
  interface RuntimeReporterOptions {
92
106
  /**
93
- * A hook to format the message text univerally. By default, it
107
+ * A hook to format the message text universally. By default, it
94
108
  * outputs the message in the following format: "<message> (<code>)"
95
109
  * @param message The resolved message text; the placeholders have been replaced by their token values
96
110
  * @param code The unique code associated with the message
@@ -102,7 +116,7 @@ interface RuntimeReporterOptions {
102
116
  * have an associated message. Defaults to "An error occurred"
103
117
  *
104
118
  * _Note: This is only used when the `fail` method is called in production
105
- * environments when the `createReporter` function is provided an empty array._
119
+ * environments when the `createReporter` function is provided an empty message set._
106
120
  */
107
121
  defaultTemplate?: string;
108
122
  }
@@ -112,6 +126,6 @@ interface RuntimeReporterOptions {
112
126
  * @param options Optional configuration options
113
127
  * @returns A runtime report object
114
128
  */
115
- declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>>(messages: T, options?: RuntimeReporterOptions): RuntimeReporter<T>;
129
+ declare function createReporter<T extends RuntimeReporterMessage>(messages: RuntimeReporterMessages<T>, options?: RuntimeReporterOptions): RuntimeReporter<T>;
116
130
 
117
131
  export { type RuntimeReporterMessage, type RuntimeReporterMessages, type RuntimeReporterOptions, type RuntimeReporterToken, type RuntimeReporterTokens, createReporter };
package/dist/index.d.ts CHANGED
@@ -5,14 +5,14 @@
5
5
  type RuntimeReporterMessage = {
6
6
  code: string;
7
7
  template: string;
8
- tokens?: string[];
8
+ tokens?: string;
9
9
  };
10
10
  /**
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[number]["code"]]: Extract<T[number], {
14
+ type RuntimeReporterMessages<T extends RuntimeReporterMessage> = {
15
+ [K in T["code"]]: Extract<T, {
16
16
  code: K;
17
17
  }>["template"];
18
18
  };
@@ -30,67 +30,81 @@ 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[]>, U extends keyof T> = T extends RuntimeReporterMessages<infer V extends RuntimeReporterMessage[]> ? Extract<V[number], {
33
+ type ReporterTokensArgs<T extends RuntimeReporterMessage, U extends T["code"]> = Extract<T, {
34
34
  code: U;
35
- }>["tokens"] extends infer Tokens ? Tokens extends readonly string[] ? [tokens: Record<Tokens[number], RuntimeReporterToken>] : [] : [] : never;
35
+ }>["tokens"] extends infer Tokens ? [Tokens] extends [string] ? [tokens: Record<Tokens, RuntimeReporterToken>] : [] : [];
36
+ /**
37
+ * Return type for message(); displays the template + code in default format on hover.
38
+ * The runtime value is the resolved string (tokens substituted); the type is for DX only.
39
+ * @private
40
+ */
41
+ type MessageReturnType<T extends RuntimeReporterMessage, U extends T["code"]> = Extract<T, {
42
+ code: U;
43
+ }> extends {
44
+ template: infer Template;
45
+ } ? Template extends string ? `${Template} (${U})` : string : string;
36
46
  /**
37
47
  * The runtime report object with all of it's associated methods; the result
38
48
  * of the primary export: `createReporter`
39
49
  * @private
40
50
  */
41
- interface RuntimeReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>> {
51
+ interface RuntimeReporter<T extends RuntimeReporterMessage> {
42
52
  /**
43
53
  * Retrieves the full text of the targeted message
44
54
  *
55
+ * _Note: As a convenience, the return type will attempt to show the literal type of the template
56
+ * pattern and code suffix (in the default format) for the message on hover; the actual output may
57
+ * vary based on the tokens provided and the `formatMessage` option._
58
+ *
45
59
  * _Tip: This method is particularly useful in the test environment; allowing you
46
- * to make precise assertions without having to duplicate an of the raw message text._
60
+ * to make precise assertions without having to duplicate any of the raw message text._
47
61
  * @param code A direct reference to the unique code for the targeted message
48
62
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
49
63
  */
50
- message<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): string;
64
+ message<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): MessageReturnType<T, U>;
51
65
  /**
52
66
  * Logs a warning to the console with the full text of the targeted message in non-production environments
53
67
  *
54
68
  * _Note: This method will only log when the message associated with the code is found;
55
69
  * meaning it will not be called in production if the `createReporter` function
56
- * is provided an empty array._
70
+ * is provided an empty message set._
57
71
  * @param code A direct reference to the unique code for the targeted message
58
72
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
59
73
  */
60
- warn<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
74
+ warn<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
61
75
  /**
62
76
  * Logs an error to the console with the full text of the targeted message in non-production environments
63
77
  *
64
78
  * _Note: This method will only log when the message associated with the code is found;
65
79
  * meaning it will not be called in production if the `createReporter` function
66
- * is provided an empty array._
80
+ * is provided an empty message set._
67
81
  * @param code A direct reference to the unique code for the targeted message
68
82
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
69
83
  */
70
- error<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
84
+ error<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
71
85
  /**
72
86
  * Logs a message to the console with the full text of the targeted message in non-production environments
73
87
  *
74
88
  * _Note: This method will only log when the message associated with the code is found;
75
89
  * meaning it will not be called in production if the `createReporter` function
76
- * is provided an empty array._
90
+ * is provided an empty message set._
77
91
  * @param code A direct reference to the unique code for the targeted message
78
92
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
79
93
  */
80
- log<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
94
+ log<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
81
95
  /**
82
96
  * Throws an error with the full text of the targeted message in all environments
83
97
  *
84
98
  * _Note: When the `createReporter` function is called in production with an empty
85
- * array, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
99
+ * message set, this method will use the "defaultTemplate" option in this format: "<defaultTemplate> (<code>)"_
86
100
  * @param code A direct reference to the unique code for the targeted message
87
101
  * @param args The remaining optional argument for the function; a record containing the placeholder token values
88
102
  */
89
- fail<U extends Extract<keyof T, string>>(code: U, ...args: RuntimeReporterTokensArgs<T, U>): void;
103
+ fail<U extends T["code"]>(code: U, ...args: ReporterTokensArgs<T, U>): void;
90
104
  }
91
105
  interface RuntimeReporterOptions {
92
106
  /**
93
- * A hook to format the message text univerally. By default, it
107
+ * A hook to format the message text universally. By default, it
94
108
  * outputs the message in the following format: "<message> (<code>)"
95
109
  * @param message The resolved message text; the placeholders have been replaced by their token values
96
110
  * @param code The unique code associated with the message
@@ -102,7 +116,7 @@ interface RuntimeReporterOptions {
102
116
  * have an associated message. Defaults to "An error occurred"
103
117
  *
104
118
  * _Note: This is only used when the `fail` method is called in production
105
- * environments when the `createReporter` function is provided an empty array._
119
+ * environments when the `createReporter` function is provided an empty message set._
106
120
  */
107
121
  defaultTemplate?: string;
108
122
  }
@@ -112,6 +126,6 @@ interface RuntimeReporterOptions {
112
126
  * @param options Optional configuration options
113
127
  * @returns A runtime report object
114
128
  */
115
- declare function createReporter<T extends RuntimeReporterMessages<RuntimeReporterMessage[]>>(messages: T, options?: RuntimeReporterOptions): RuntimeReporter<T>;
129
+ declare function createReporter<T extends RuntimeReporterMessage>(messages: RuntimeReporterMessages<T>, options?: RuntimeReporterOptions): RuntimeReporter<T>;
116
130
 
117
131
  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 santized = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
9
- message = message.replace(new RegExp(`\\{\\{\\s*${santized}\\s*\\}\\}`, "g"), replace);
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;
@@ -16,27 +16,26 @@ function createReporter(messages, options = {}) {
16
16
  formatMessage = (message, code) => `${message} (${code})`,
17
17
  defaultTemplate = "An error occurred"
18
18
  } = options;
19
+ const messagesByCode = messages;
19
20
  const getMessage = function getMessage2(code, ...args) {
20
- const template = messages[code] || defaultTemplate;
21
+ const template = messagesByCode[code] || defaultTemplate;
21
22
  const tokens = args[0];
22
23
  const text = resolveTemplate(template, tokens);
23
24
  return formatMessage(text, code);
24
25
  };
25
26
  return {
26
- message: (code, ...args) => {
27
- return getMessage(code, ...args);
28
- },
27
+ message: (code, ...args) => getMessage(code, ...args),
29
28
  error: (code, ...args) => {
30
29
  const message = getMessage(code, ...args);
31
- if (messages[code]) console.error(message);
30
+ if (messagesByCode[code]) console.error(message);
32
31
  },
33
32
  warn: (code, ...args) => {
34
33
  const message = getMessage(code, ...args);
35
- if (messages[code]) console.warn(message);
34
+ if (messagesByCode[code]) console.warn(message);
36
35
  },
37
36
  log: (code, ...args) => {
38
37
  const message = getMessage(code, ...args);
39
- if (messages[code]) console.log(message);
38
+ if (messagesByCode[code]) console.log(message);
40
39
  },
41
40
  fail: (code, ...args) => {
42
41
  const message = getMessage(code, ...args);
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,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,WAAW,MAAM,QAAQ,uBAAuB,MAAM;AAC5D,gBAAU,QAAQ,QAAQ,IAAI,OAAO,aAAa,QAAQ,cAAc,GAAG,GAAG,OAAO;AAAA,IACzF,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"]}
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 ReporterTokensArgs<T extends RuntimeReporterMessage, U extends T[\"code\"]> = Extract<\n T,\n { code: U }\n>[\"tokens\"] extends infer Tokens\n ? [Tokens] extends [string]\n ? [tokens: Record<Tokens, RuntimeReporterToken>]\n : []\n : [];\n\n/**\n * Return type for message(); displays the template + code in default format on hover.\n * The runtime value is the resolved string (tokens substituted); the type is for DX only.\n * @private\n */\ntype MessageReturnType<T extends RuntimeReporterMessage, U extends T[\"code\"]> =\n Extract<T, { code: U }> extends { template: infer Template }\n ? Template extends string\n ? `${Template} (${U})`\n : string\n : string;\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 RuntimeReporterMessage> {\n /**\n * Retrieves the full text of the targeted message\n *\n * _Note: As a convenience, the return type will attempt to show the literal type of the template\n * pattern and code suffix (in the default format) for the message on hover; the actual output may\n * vary based on the tokens provided and the `formatMessage` option._\n *\n * _Tip: This method is particularly useful in the test environment; allowing you\n * to make precise assertions without having to duplicate any 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 T[\"code\"]>(\n code: U,\n ...args: ReporterTokensArgs<T, U>\n ): MessageReturnType<T, U>;\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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 T[\"code\"]>(code: U, ...args: ReporterTokensArgs<T, U>): 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 RuntimeReporterMessage>(\n messages: RuntimeReporterMessages<T>,\n options: RuntimeReporterOptions = {}\n): RuntimeReporter<T> {\n const {\n formatMessage = (message, code) => `${message} (${code})`,\n defaultTemplate = \"An error occurred\",\n } = options;\n const messagesByCode = messages as Record<string, string>;\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 = messagesByCode[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 getMessage(code, ...args) as MessageReturnType<T, typeof code & T[\"code\"]>,\n error: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[code]) console.error(message);\n },\n warn: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[code]) console.warn(message);\n },\n log: (code, ...args) => {\n const message = getMessage(code, ...args);\n if (messagesByCode[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":";AAoJA,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;AACJ,QAAM,iBAAiB;AAQvB,QAAM,aAAa,SAASC,YACxB,SACG,MACG;AACN,UAAM,WAAW,eAAe,IAAI,KAAK;AACzC,UAAM,SAAS,KAAK,CAAC;AACrB,UAAM,OAAO,gBAAgB,UAAU,MAAM;AAC7C,WAAO,cAAc,MAAM,IAAI;AAAA,EACnC;AAEA,SAAO;AAAA,IACH,SAAS,CAAC,SAAS,SACf,WAAW,MAAM,GAAG,IAAI;AAAA,IAC5B,OAAO,CAAC,SAAS,SAAS;AACtB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,MAAM,OAAO;AAAA,IACnD;AAAA,IACA,MAAM,CAAC,SAAS,SAAS;AACrB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,KAAK,OAAO;AAAA,IAClD;AAAA,IACA,KAAK,CAAC,SAAS,SAAS;AACpB,YAAM,UAAU,WAAW,MAAM,GAAG,IAAI;AACxC,UAAI,eAAe,IAAI,EAAG,SAAQ,IAAI,OAAO;AAAA,IACjD;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.2.0",
4
- "description": "Runtime messaging that is convenient in development and secure in production",
3
+ "version": "0.4.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",