runtime-reporter 0.4.3 → 0.4.5
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 +92 -89
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,36 +1,92 @@
|
|
|
1
1
|
# Runtime Reporter
|
|
2
2
|
|
|
3
|
-
Structured runtime reporting that is type-safe, centralized, and production-ready — for frameworks and applications
|
|
3
|
+
Structured runtime reporting that is type-safe, centralized, and production-ready — for front-end frameworks and applications.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Runtime Reporter replaces ad-hoc logging with structured, code-based messaging.
|
|
6
|
+
|
|
7
|
+
## Why?
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
Runtime Reporter solves these problems:
|
|
8
10
|
|
|
9
11
|
- duplicated log messages
|
|
10
12
|
- inconsistent error wording
|
|
11
13
|
- fragile test assertions
|
|
12
14
|
- accidental exposure of sensitive data
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
by introducing these features:
|
|
17
|
+
|
|
18
|
+
- centralized and standardized error messaging
|
|
19
|
+
- stable error codes for debugging and error tracking
|
|
20
|
+
- test assertions without string duplication
|
|
21
|
+
- safer production output (no sensitive data exposure)
|
|
22
|
+
|
|
23
|
+
in a lightweight, self-contained package (less than 1 KB).
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install runtime-reporter
|
|
29
|
+
```
|
|
15
30
|
|
|
16
|
-
##
|
|
31
|
+
## Quick start
|
|
17
32
|
|
|
18
|
-
|
|
33
|
+
A copy-and-paste example of how to use Runtime Reporter in your project.
|
|
19
34
|
|
|
20
|
-
|
|
21
|
-
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
```ts
|
|
36
|
+
// src/runtime-reporter.ts
|
|
37
|
+
|
|
38
|
+
import { createReporter, type RuntimeReporterMessages } from "runtime-reporter";
|
|
39
|
+
|
|
40
|
+
const messages: RuntimeReporterMessages<
|
|
41
|
+
| {
|
|
42
|
+
code: "ERR01";
|
|
43
|
+
template: "{{ componentName }} failed to mount";
|
|
44
|
+
tokens: "componentName";
|
|
45
|
+
}
|
|
46
|
+
| {
|
|
47
|
+
code: "ERR02";
|
|
48
|
+
template: "Failed to load configuration";
|
|
49
|
+
}
|
|
50
|
+
| {
|
|
51
|
+
code: "ERR03";
|
|
52
|
+
template: "Failed to fetch {{ resource }} from {{ url }}";
|
|
53
|
+
tokens: "resource" | "url";
|
|
54
|
+
}
|
|
55
|
+
> = {
|
|
56
|
+
ERR01: "{{ componentName }} failed to mount",
|
|
57
|
+
ERR02: "Failed to load configuration",
|
|
58
|
+
ERR03: "Failed to fetch {{ resource }} from {{ url }}",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** The runtime reporter for <project-name> */
|
|
62
|
+
const reporter = createReporter(messages);
|
|
63
|
+
|
|
64
|
+
export default reporter;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Once your reporter is created, import and use it wherever you want:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
// src/my-component.ts
|
|
71
|
+
|
|
72
|
+
import { reporter } from "./runtime-reporter";
|
|
73
|
+
|
|
74
|
+
export function MyComponent() {
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
reporter.error("ERR01", { componentName: "MyComponent" });
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
return <div>My Component</div>;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
26
82
|
|
|
27
83
|
## Features
|
|
28
84
|
|
|
29
|
-
If you are new to Runtime Reporter, take a moment to explore
|
|
85
|
+
If you are new to Runtime Reporter, take a moment to explore its core features.
|
|
30
86
|
|
|
31
|
-
### Basic usage
|
|
87
|
+
### 1. Basic usage
|
|
32
88
|
|
|
33
|
-
|
|
89
|
+
Create a reporter instance with your messages and start logging.
|
|
34
90
|
|
|
35
91
|
```ts
|
|
36
92
|
import { createReporter } from "runtime-reporter";
|
|
@@ -42,7 +98,7 @@ const reporter = createReporter({
|
|
|
42
98
|
reporter.error("ERR01");
|
|
43
99
|
```
|
|
44
100
|
|
|
45
|
-
### Code-based messaging
|
|
101
|
+
### 2. Code-based messaging
|
|
46
102
|
|
|
47
103
|
Replace inline strings with centralized, code-based identifiers.
|
|
48
104
|
|
|
@@ -54,19 +110,19 @@ console.error("MyComponent failed to mount");
|
|
|
54
110
|
reporter.error("ERR01");
|
|
55
111
|
```
|
|
56
112
|
|
|
57
|
-
### Dynamic messages
|
|
113
|
+
### 3. Dynamic messages
|
|
58
114
|
|
|
59
115
|
Inject runtime data into your messages via message templates and tokenized variables.
|
|
60
116
|
|
|
61
117
|
```ts
|
|
62
118
|
const reporter = createReporter({
|
|
63
|
-
ERR01: "{{ componentName }} failed
|
|
119
|
+
ERR01: "{{ componentName }} failed to mount",
|
|
64
120
|
});
|
|
65
121
|
|
|
66
|
-
reporter.error("ERR01", { componentName: "MyComponent"
|
|
122
|
+
reporter.error("ERR01", { componentName: "MyComponent" });
|
|
67
123
|
```
|
|
68
124
|
|
|
69
|
-
### Type safety
|
|
125
|
+
### 4. Type safety
|
|
70
126
|
|
|
71
127
|
Annotate your messages to get autocomplete and compile-time validation for message codes and token names.
|
|
72
128
|
|
|
@@ -91,7 +147,7 @@ reporter.error("ERR02", { componentName: "MyComponent" });
|
|
|
91
147
|
reporter.error("ERR01");
|
|
92
148
|
```
|
|
93
149
|
|
|
94
|
-
### Production environments
|
|
150
|
+
### 5. Production environments
|
|
95
151
|
|
|
96
152
|
Pass an empty object to the `createReporter` function in production environments for better security and a smaller bundle size.
|
|
97
153
|
|
|
@@ -101,7 +157,7 @@ const reporter = createReporter(
|
|
|
101
157
|
);
|
|
102
158
|
```
|
|
103
159
|
|
|
104
|
-
### Test friendly
|
|
160
|
+
### 6. Test friendly
|
|
105
161
|
|
|
106
162
|
Assert against resolved messages without duplicating message text in your test environment.
|
|
107
163
|
|
|
@@ -117,69 +173,11 @@ it("should log error if component fails to mount", () => {
|
|
|
117
173
|
});
|
|
118
174
|
```
|
|
119
175
|
|
|
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", phase: "mount" });
|
|
172
|
-
}, []);
|
|
173
|
-
|
|
174
|
-
return <div>My Component</div>;
|
|
175
|
-
}
|
|
176
|
-
```
|
|
177
|
-
|
|
178
176
|
## API
|
|
179
177
|
|
|
180
178
|
### `createReporter(RuntimeReporterMessages, options?: RuntimeReporterOptions): RuntimeReporter`
|
|
181
179
|
|
|
182
|
-
Takes a
|
|
180
|
+
Takes a messages object, an optional set of configuration options, and returns a reporter object.
|
|
183
181
|
|
|
184
182
|
### `RuntimeReporter`
|
|
185
183
|
|
|
@@ -228,7 +226,7 @@ const reporter = createReporter(
|
|
|
228
226
|
process.env.NODE_ENV === "production" ? ({} as typeof messages) : messages
|
|
229
227
|
);
|
|
230
228
|
|
|
231
|
-
reporter.fail("ERR01", { componentName: "Router"
|
|
229
|
+
reporter.fail("ERR01", { componentName: "Router" });
|
|
232
230
|
// throws: "An error occurred (ERR01)"
|
|
233
231
|
```
|
|
234
232
|
|
|
@@ -243,7 +241,7 @@ it("should log error if component fails to mount", () => {
|
|
|
243
241
|
render(<MyComponent />);
|
|
244
242
|
|
|
245
243
|
expect(console.error).toHaveBeenCalledWith(
|
|
246
|
-
reporter.message("ERR01", { componentName: "MyComponent"
|
|
244
|
+
reporter.message("ERR01", { componentName: "MyComponent" })
|
|
247
245
|
);
|
|
248
246
|
});
|
|
249
247
|
```
|
|
@@ -256,16 +254,21 @@ You can still get the same benefits as TypeScript by using JSDoc-style type anno
|
|
|
256
254
|
/**
|
|
257
255
|
* @type {import("runtime-reporter").RuntimeReporterMessages<{
|
|
258
256
|
* code: "ERR01";
|
|
259
|
-
* template: "{{ componentName }} failed
|
|
260
|
-
* tokens: "componentName"
|
|
257
|
+
* template: "{{ componentName }} failed to mount";
|
|
258
|
+
* tokens: "componentName";
|
|
259
|
+
* } | {
|
|
260
|
+
* code: "ERR02";
|
|
261
|
+
* template: "Failed to load configuration";
|
|
261
262
|
* } | {
|
|
262
|
-
* code: "
|
|
263
|
-
* template: "
|
|
263
|
+
* code: "ERR03";
|
|
264
|
+
* template: "Failed to fetch {{ resource }} from {{ url }}";
|
|
265
|
+
* tokens: "resource" | "url";
|
|
264
266
|
* }>}
|
|
265
267
|
*/
|
|
266
268
|
const messages = {
|
|
267
|
-
ERR01: "{{ componentName }} failed
|
|
268
|
-
|
|
269
|
+
ERR01: "{{ componentName }} failed to mount",
|
|
270
|
+
ERR02: "Failed to load configuration",
|
|
271
|
+
ERR03: "Failed to fetch {{ resource }} from {{ url }}",
|
|
269
272
|
};
|
|
270
273
|
```
|
|
271
274
|
|