@lovelaces-io/storyteller 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- type StoryLevel = "tell" | "warn" | "oops";
1
+ /** Human-readable level labels stored in story records */
2
+ type StoryLevel = "Information" | "Warning" | "Error";
2
3
  type StoryContextValue = Record<string, unknown> | string;
3
4
  type StoryError = {
4
5
  name?: string;
@@ -24,22 +25,27 @@ type StoryEventBase = {
24
25
  where?: StoryContextValue;
25
26
  };
26
27
  notes: StoryNote[];
28
+ durationMs?: number;
27
29
  error?: StoryError;
28
30
  };
29
- type StorySummaryOptions = {
31
+ type ReportOptions = {
30
32
  timezone?: string;
31
33
  locale?: string;
32
- verbosity?: "brief" | "normal" | "full";
33
- maxNotes?: number;
34
+ detail?: "brief" | "normal" | "full";
35
+ noteLimit?: number;
34
36
  showData?: boolean;
35
- colorize?: boolean;
37
+ colors?: boolean;
36
38
  };
37
- type StoryPreviewOptions = StorySummaryOptions & {
39
+ /** @deprecated Use ReportOptions instead */
40
+ type StorySummaryOptions = ReportOptions;
41
+ type PreviewOptions = ReportOptions & {
38
42
  title?: string;
39
43
  level?: StoryLevel;
40
44
  error?: unknown;
41
45
  };
42
- type StorySummaryNote = {
46
+ /** @deprecated Use PreviewOptions instead */
47
+ type StoryPreviewOptions = PreviewOptions;
48
+ type ReportNote = {
43
49
  timestamp: string;
44
50
  when: string;
45
51
  note: string;
@@ -49,22 +55,28 @@ type StorySummaryNote = {
49
55
  where?: StoryContextValue;
50
56
  error?: StoryError;
51
57
  };
52
- type StorySummaryData = {
58
+ /** @deprecated Use ReportNote instead */
59
+ type StorySummaryNote = ReportNote;
60
+ type StoryReport = {
53
61
  title: string;
54
62
  level: StoryLevel;
55
63
  when: string;
56
64
  durationMs?: number;
57
65
  duration?: string;
58
66
  origin?: StoryEventBase["origin"];
59
- notes: StorySummaryNote[];
67
+ notes: ReportNote[];
60
68
  error?: StoryError;
61
69
  };
62
- type StorySummary = {
70
+ /** @deprecated Use StoryReport instead */
71
+ type StorySummaryData = StoryReport;
72
+ type FormattedReport = {
63
73
  text: string;
64
- data: StorySummaryData;
74
+ data: StoryReport;
65
75
  };
76
+ /** @deprecated Use FormattedReport instead */
77
+ type StorySummary = FormattedReport;
66
78
  type StoryEvent = StoryEventBase & {
67
- summarize: (options?: StorySummaryOptions) => StorySummary;
79
+ summarize: (options?: ReportOptions) => FormattedReport;
68
80
  };
69
81
  type AudienceMember = {
70
82
  name: string;
@@ -88,8 +100,22 @@ declare class AudienceRegistry {
88
100
  getAll(): AudienceMember[];
89
101
  /** Return only the audience members matching the given names */
90
102
  getOnly(names: string[]): AudienceMember[];
103
+ /** Check if an audience member is registered by name */
104
+ has(name: string): boolean;
105
+ /** List the names of all registered audience members */
106
+ names(): string[];
91
107
  }
92
- /** Core logging class that collects timestamped notes and emits them as structured story events */
108
+ /**
109
+ * Collects timestamped notes and emits them as a single structured story event.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * const story = new Storyteller({ origin: { who: "api-server" } });
114
+ * story.note("Request received", { what: { path: "/checkout" } });
115
+ * story.note("Validated cart");
116
+ * story.tell("Checkout started");
117
+ * ```
118
+ */
93
119
  declare class Storyteller {
94
120
  readonly audience: AudienceRegistry;
95
121
  private readonly origin?;
@@ -98,21 +124,31 @@ declare class Storyteller {
98
124
  origin?: StoryEventBase["origin"];
99
125
  audiences?: AudienceMember[];
100
126
  });
101
- /** Add a timestamped note with optional context (who, what, where, error) */
127
+ /**
128
+ * Add a timestamped note to the current story.
129
+ * @param text - What happened
130
+ * @param data - Optional context: who did it, what was involved, where it happened, any error
131
+ * @returns `this` for chaining
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * story.note("Card charged", { what: { amount: "$42" }, where: "stripe" });
136
+ * ```
137
+ */
102
138
  note(text: string, data?: NoteData): this;
103
139
  /** Clear all accumulated notes without emitting a story */
104
140
  reset(): this;
105
- /** Generate a formatted summary of current notes without emitting or clearing them */
106
- summarize(options?: StoryPreviewOptions): StorySummary;
107
- /** Emit a story at the "tell" level (success / informational) */
141
+ /** Preview the current notes as a formatted report without emitting or clearing them */
142
+ summarize(options?: PreviewOptions): FormattedReport;
143
+ /** Tell a success story — everything went well */
108
144
  tell(title: string): {
109
145
  to: (...names: string[]) => void;
110
146
  };
111
- /** Emit a story at the "warn" level (something was off) */
147
+ /** Tell a cautionary story — something was off but it's handled */
112
148
  warn(title: string): {
113
149
  to: (...names: string[]) => void;
114
150
  };
115
- /** Emit a story at the "oops" level (something broke) with an optional error */
151
+ /** Tell an error story — something broke. Pass the error for automatic normalization. */
116
152
  oops(title: string, error?: unknown): {
117
153
  to: (...names: string[]) => void;
118
154
  };
@@ -123,29 +159,84 @@ declare class Storyteller {
123
159
  /** Deliver a story event to matching audience members */
124
160
  private deliver;
125
161
  }
126
- /** Generate a formatted, human-readable summary from a story event */
127
- declare function summarizeStory(story: StoryEventBase, options?: StorySummaryOptions): StorySummary;
162
+
163
+ /**
164
+ * Format a story event into a human-readable report with optional colors.
165
+ *
166
+ * @param story - The story event to format
167
+ * @param options - Formatting options (timezone, locale, detail level, colors)
168
+ * @returns A FormattedReport with both text (for display) and data (structured)
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * const report = formatStory(event, { colors: false, detail: "brief" });
173
+ * console.log(report.text);
174
+ * ```
175
+ */
176
+ declare function formatStory(story: StoryEventBase, options?: ReportOptions): FormattedReport;
177
+ /** Convert milliseconds into a human-readable duration string */
178
+ declare function formatDuration(milliseconds: number): string;
179
+ /** @deprecated Use formatStory instead */
180
+ declare const summarizeStory: typeof formatStory;
128
181
 
129
182
  type StorytellerSharedOptions = {
130
183
  origin?: StoryEventBase["origin"];
131
184
  reset?: boolean;
132
185
  };
133
- /** Return a shared singleton Storyteller instance for cross-component or cross-service logging */
186
+ /**
187
+ * Get or create a shared Storyteller instance for cross-component logging.
188
+ * First call creates the instance; subsequent calls return the same one.
189
+ *
190
+ * @param options.origin - Origin context for the shared instance
191
+ * @param options.reset - Create a fresh instance (useful in tests)
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * // Same instance everywhere in your app
196
+ * const story = useStoryteller({ origin: { who: "worker" } });
197
+ * ```
198
+ */
134
199
  declare function useStoryteller(options?: StorytellerSharedOptions): Storyteller;
135
200
 
136
- /** Create an audience that logs stories to the browser console with color-coded grouped output */
201
+ /**
202
+ * Create an audience that prints stories to the console with color-coded grouped output.
203
+ * Registered by default on every Storyteller instance.
204
+ *
205
+ * @example
206
+ * ```ts
207
+ * // Already included — but you can re-add after removing:
208
+ * story.audience.add(consoleAudience());
209
+ * ```
210
+ */
137
211
  declare function consoleAudience(): AudienceMember;
138
212
 
139
- /** Create an audience that persists warn and oops stories to a database via the provided insert function */
213
+ /**
214
+ * Create an audience that stores warn and oops stories via your insert function.
215
+ * Tell-level events are filtered out to reduce noise — only warnings and errors are persisted.
216
+ *
217
+ * Note: if the insert function throws, the error is silently caught by the delivery
218
+ * pipeline (Promise.allSettled). Wrap your insert with try/catch to handle failures.
219
+ *
220
+ * @param insert - Function that receives the story event and stores it
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * story.audience.add(
225
+ * dbAudience(async (event) => {
226
+ * await db.insert("story_logs", event);
227
+ * })
228
+ * );
229
+ * ```
230
+ */
140
231
  declare function dbAudience(insert: (event: StoryEvent) => Promise<void> | void): AudienceMember;
141
232
 
142
233
  type StoryReportOptions = {
143
234
  timezone?: string;
144
235
  locale?: string;
145
- verbosity?: "brief" | "normal" | "full";
146
- maxNotesPerStory?: number;
236
+ detail?: "brief" | "normal" | "full";
237
+ noteLimit?: number;
147
238
  showData?: boolean;
148
- colorize?: boolean;
239
+ colors?: boolean;
149
240
  };
150
241
  /** Generate a formatted report from an array of story events, grouped by day */
151
242
  declare function writeStoryReport(stories: StoryEventBase[], options?: StoryReportOptions): string;
@@ -163,13 +254,5 @@ declare const ANSI: {
163
254
  declare function getLevelColor(level: StoryLevel): string;
164
255
  /** Format an origin context into a human-readable path like "app / page / component" */
165
256
  declare function formatOrigin(origin?: StoryEventBase["origin"]): string | undefined;
166
- /** Colorize JSON output, dimming the notes section for visual hierarchy */
167
- declare function colorizeJsonSections(json: string, colors: {
168
- base: string;
169
- notes: string;
170
- reset: string;
171
- }): string[];
172
- /** Count the net bracket depth change in a line (opening brackets minus closing brackets) */
173
- declare function countBrackets(line: string): number;
174
257
 
175
- export { ANSI, type AudienceMember, type StoryContextValue, type StoryError, type StoryEvent, type StoryEventBase, type StoryLevel, type StoryNote, type StoryPreviewOptions, type StoryReportOptions, type StorySummary, type StorySummaryData, type StorySummaryNote, type StorySummaryOptions, Storyteller, colorizeJsonSections, consoleAudience, countBrackets, dbAudience, formatOrigin, getLevelColor, summarizeStory, useStoryteller, writeStoryReport };
258
+ export { ANSI, type AudienceMember, type FormattedReport, type PreviewOptions, type ReportNote, type ReportOptions, type StoryContextValue, type StoryError, type StoryEvent, type StoryEventBase, type StoryLevel, type StoryNote, type StoryPreviewOptions, type StoryReport, type StoryReportOptions, type StorySummary, type StorySummaryData, type StorySummaryNote, type StorySummaryOptions, Storyteller, consoleAudience, dbAudience, formatDuration, formatOrigin, formatStory, getLevelColor, summarizeStory, useStoryteller, writeStoryReport };