runtime-reporter 0.4.4 → 0.4.6

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.
Files changed (2) hide show
  1. package/README.md +102 -99
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,60 +1,108 @@
1
1
  # Runtime Reporter
2
2
 
3
- Structured runtime reporting that is type-safe, centralized, and production-ready — for frameworks and applications
3
+ Replace ad-hoc logging with structured, code-based messaging that is also type-safe, centralized, and production-ready.
4
4
 
5
- ## Why Runtime Reporter?
5
+ ```ts
6
+ // ./src/runtime-reporter.ts
7
+
8
+ import { createReporter } from "runtime-reporter";
9
+
10
+ export const reporter = createReporter({
11
+ ERR01: "Something went wrong",
12
+ });
6
13
 
7
- Most projects eventually accumulate:
14
+ // ./src/MyComponent.ts
15
+
16
+ import { reporter } from "./runtime-reporter";
17
+
18
+ export function MyComponent() {
19
+ useEffect(() => {
20
+ reporter.error("ERR01");
21
+ }, []);
22
+ }
23
+ ```
24
+
25
+ ## Why?
26
+
27
+ Runtime Reporter solves these problems:
8
28
 
9
29
  - duplicated log messages
10
30
  - inconsistent error wording
11
31
  - fragile test assertions
12
32
  - accidental exposure of sensitive data
13
33
 
14
- Runtime Reporter replaces ad-hoc logging with structured, code-based messaging.
34
+ by introducing these features:
15
35
 
16
- ## Who is Runtime Reporter for?
36
+ - centralized and standardized error messaging
37
+ - stable error codes for debugging and error tracking
38
+ - test assertions without string duplication
39
+ - safer production output (no sensitive data exposure)
17
40
 
18
- Use Runtime Reporter if:
41
+ in a lightweight, self-contained package (less than 1 KB gzipped).
19
42
 
20
- - you're building a framework or library
21
- - you want to avoid exposing sensitive information in production
22
- - you want stable error codes for debugging and tracing runtime behavior
23
- - you want to avoid bloat from verbose console messages
24
- - you want a lightweight tool (~2 KB minified) that is easy to use
25
- - you want to avoid duplicating message text in tests
43
+ ## Who is this for?
26
44
 
27
- ## Features
45
+ Runtime Reporter is ideal for front-end frameworks and libraries. However, it is ultimately useful for any project looking for standardized console messaging.
28
46
 
29
- If you are new to Runtime Reporter, take a moment to explore the its core features.
47
+ ## Installation
30
48
 
31
- ### Basic usage
49
+ ```bash
50
+ npm install runtime-reporter
51
+ ```
52
+
53
+ ## Quick start
32
54
 
33
- Getting started is easy. Create a reporter instance with your messages and start logging.
55
+ A full, copy-and-paste example of how to use Runtime Reporter in your project:
34
56
 
35
57
  ```ts
36
- import { createReporter } from "runtime-reporter";
58
+ import { createReporter, type RuntimeReporterMessages } from "runtime-reporter";
37
59
 
38
- const reporter = createReporter({
39
- ERR01: "MyComponent failed to mount",
40
- });
60
+ const messages: RuntimeReporterMessages<
61
+ | {
62
+ code: "ERR01";
63
+ template: "{{ componentName }} failed to mount";
64
+ tokens: "componentName";
65
+ }
66
+ | {
67
+ code: "ERR02";
68
+ template: "Failed to load configuration";
69
+ }
70
+ | {
71
+ code: "ERR03";
72
+ template: "Failed to fetch {{ resource }} from {{ url }}";
73
+ tokens: "resource" | "url";
74
+ }
75
+ > = {
76
+ ERR01: "{{ componentName }} failed to mount",
77
+ ERR02: "Failed to load configuration",
78
+ ERR03: "Failed to fetch {{ resource }} from {{ url }}",
79
+ };
41
80
 
42
- reporter.error("ERR01");
81
+ /** The runtime reporter for <project-name> */
82
+ const reporter = createReporter(messages);
83
+
84
+ export default reporter;
43
85
  ```
44
86
 
45
- ### Code-based messaging
87
+ ## Features
88
+
89
+ If you are new to Runtime Reporter, take a moment to explore its core features.
90
+
91
+ ### 1. Code-based messaging
46
92
 
47
93
  Replace inline strings with centralized, code-based identifiers.
48
94
 
49
95
  ```ts
50
- // Without runtime-reporter (logs "MyComponent failed to mount")
51
- console.error("MyComponent failed to mount");
96
+ // Without runtime-reporter
97
+ console.log("Something went wrong");
98
+ // ❌ Logs: "Something went wrong"
52
99
 
53
- // With runtime-reporter (logs "MyComponent failed to mount (ERR01)")
54
- reporter.error("ERR01");
100
+ // With runtime-reporter
101
+ reporter.log("ERR01");
102
+ // ✅ Logs: "Something went wrong (ERR01)"
55
103
  ```
56
104
 
57
- ### Dynamic messages
105
+ ### 2. Dynamic messages
58
106
 
59
107
  Inject runtime data into your messages via message templates and tokenized variables.
60
108
 
@@ -64,9 +112,32 @@ const reporter = createReporter({
64
112
  });
65
113
 
66
114
  reporter.error("ERR01", { componentName: "MyComponent" });
115
+ // Logs: "MyComponent failed to mount (ERR01)"
116
+ ```
117
+
118
+ ### 3. Development vs. production
119
+
120
+ Pass an empty object to the `createReporter` function in production environments for better security and a smaller bundle size.
121
+
122
+ ```ts
123
+ const reporter = createReporter(
124
+ process.env.NODE_ENV === "production" ? ({} as typeof messages) : messages
125
+ );
126
+ ```
127
+
128
+ Development environments get detailed messaging, while production environments get as little as possible.
129
+
130
+ ```ts
131
+ reporter.error("ERR01");
132
+ // In development, it logs: "Something went wrong (ERR01)"
133
+ // In production, it does not log
134
+
135
+ reporter.fail("ERR01");
136
+ // In development, it throws: "Something went wrong (ERR01)"
137
+ // In production, it throws: "An error occurred (ERR01)"
67
138
  ```
68
139
 
69
- ### Type safety
140
+ ### 4. Type safety
70
141
 
71
142
  Annotate your messages to get autocomplete and compile-time validation for message codes and token names.
72
143
 
@@ -91,17 +162,7 @@ reporter.error("ERR02", { componentName: "MyComponent" });
91
162
  reporter.error("ERR01");
92
163
  ```
93
164
 
94
- ### Production environments
95
-
96
- Pass an empty object to the `createReporter` function in production environments for better security and a smaller bundle size.
97
-
98
- ```ts
99
- const reporter = createReporter(
100
- process.env.NODE_ENV === "production" ? ({} as typeof messages) : messages
101
- );
102
- ```
103
-
104
- ### Test friendly
165
+ ### 5. Test friendly
105
166
 
106
167
  Assert against resolved messages without duplicating message text in your test environment.
107
168
 
@@ -117,69 +178,11 @@ it("should log error if component fails to mount", () => {
117
178
  });
118
179
  ```
119
180
 
120
- ## Installation
121
-
122
- ```bash
123
- npm install runtime-reporter
124
- ```
125
-
126
- ## Quick start
127
-
128
- A copy-and-paste example of how to use Runtime Reporter in your project.
129
-
130
- ```ts
131
- // src/runtime-reporter.ts
132
-
133
- import { createReporter, type RuntimeReporterMessages } from "runtime-reporter";
134
-
135
- const messages: RuntimeReporterMessages<
136
- | {
137
- code: "ERR01";
138
- template: "{{ componentName }} failed to mount";
139
- tokens: "componentName";
140
- }
141
- | {
142
- code: "ERR02";
143
- template: "Failed to load configuration";
144
- }
145
- | {
146
- code: "ERR03";
147
- template: "Failed to fetch {{ resource }} from {{ url }}";
148
- tokens: "resource" | "url";
149
- }
150
- > = {
151
- ERR01: "{{ componentName }} failed to mount",
152
- ERR02: "Failed to load configuration",
153
- ERR03: "Failed to fetch {{ resource }} from {{ url }}",
154
- };
155
-
156
- /** The runtime reporter for <project-name> */
157
- const reporter = createReporter(messages);
158
-
159
- export default reporter;
160
- ```
161
-
162
- Once your project's reporter is created, you can import and use it wherever you need it like this:
163
-
164
- ```ts
165
- // src/my-component.ts
166
-
167
- import { reporter } from "./runtime-reporter";
168
-
169
- export function MyComponent() {
170
- useEffect(() => {
171
- reporter.error("ERR01", { componentName: "MyComponent" });
172
- }, []);
173
-
174
- return <div>My Component</div>;
175
- }
176
- ```
177
-
178
181
  ## API
179
182
 
180
183
  ### `createReporter(RuntimeReporterMessages, options?: RuntimeReporterOptions): RuntimeReporter`
181
184
 
182
- Takes a list of messages, an optional set of configuration options, and returns a reporter object.
185
+ Takes a messages object, an optional set of configuration options, and returns a reporter object.
183
186
 
184
187
  ### `RuntimeReporter`
185
188
 
@@ -268,7 +271,7 @@ You can still get the same benefits as TypeScript by using JSDoc-style type anno
268
271
  * }>}
269
272
  */
270
273
  const messages = {
271
- ERR01: "{{ componentName }} failed at {{ phase }}",
274
+ ERR01: "{{ componentName }} failed to mount",
272
275
  ERR02: "Failed to load configuration",
273
276
  ERR03: "Failed to fetch {{ resource }} from {{ url }}",
274
277
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "runtime-reporter",
3
- "version": "0.4.4",
4
- "description": "Structured runtime reporting that is type-safe, centralized, and production-ready.",
3
+ "version": "0.4.6",
4
+ "description": "Replace ad-hoc logging with structured, code-based messaging that is also type-safe, centralized, and production-ready",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",