quario 0.0.1 → 0.1.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/lib/index.d.ts ADDED
@@ -0,0 +1,473 @@
1
+ import type { PadvinderErrorCode, QueryOptions, QueryPath } from "padvinder";
2
+ import type {
3
+ LiteralToken as SjabloonLiteralToken,
4
+ SjabloonBlock,
5
+ SjabloonErrorCode,
6
+ ValueToken as SjabloonValueToken,
7
+ } from "sjabloon";
8
+
9
+ // Re-exported so render targets type the stream's `paths` metadata from
10
+ // 'quario' alone, without their own padvinder dependency.
11
+ export type { QueryPath } from "padvinder";
12
+
13
+ export type FunctionRegistry = Record<string, (...args: any[]) => any>;
14
+ export type ExpressionValue<T = unknown> = T | `=${string}`;
15
+ export type ReducerSpec = string;
16
+
17
+ /**
18
+ * A value a JSON document can hold. `params` accepts these and nothing else:
19
+ * a Date, Map, Set or typed array is a definition error, as are `NaN` and
20
+ * `Infinity`, which this type cannot exclude but the traversal rejects.
21
+ */
22
+ export type JsonValue =
23
+ | null
24
+ | boolean
25
+ | number
26
+ | string
27
+ | JsonValue[]
28
+ | { [key: string]: JsonValue };
29
+
30
+ /**
31
+ * The closed, target-neutral style vocabulary. Literal values are validated
32
+ * strictly (unknown names and mistyped literals are definition errors);
33
+ * `=` expression results are coerced leniently by each target at render.
34
+ * Colors are `#rgb`/`#rrggbb` hex only. Each target maps the declarations to
35
+ * its own formatting model (the HTML target to inline CSS, the PDF target to
36
+ * faces, points, and rects).
37
+ */
38
+ export interface StyleDeclarations {
39
+ /** `'sans'` | `'serif'` | `'mono'`, or an embedded font family name. */
40
+ family?: ExpressionValue<string>;
41
+ /** Font size in points. */
42
+ size?: ExpressionValue<number>;
43
+ bold?: ExpressionValue<boolean>;
44
+ italic?: ExpressionValue<boolean>;
45
+ underline?: ExpressionValue<boolean>;
46
+ strikethrough?: ExpressionValue<boolean>;
47
+ /** Text color, `#rgb`/`#rrggbb`. */
48
+ color?: ExpressionValue<string>;
49
+ /** Background color, `#rgb`/`#rrggbb`. */
50
+ background?: ExpressionValue<string>;
51
+ /** Horizontal alignment within the cell. */
52
+ align?: ExpressionValue<Align>;
53
+ }
54
+
55
+ export interface SortKey {
56
+ by: `=${string}`;
57
+ dir?: "asc" | "desc";
58
+ }
59
+
60
+ /** A cell value: one template string. */
61
+ export type CellValue = string;
62
+
63
+ export type Align = "left" | "center" | "right";
64
+
65
+ export interface Cell {
66
+ value: CellValue;
67
+ visible?: ExpressionValue<boolean>;
68
+ /** Literal only — never an expression. */
69
+ style?: StyleDeclarations;
70
+ }
71
+
72
+ export interface TextItem extends Cell {
73
+ type: "text";
74
+ }
75
+
76
+ /** How an image is sized: at its intrinsic size, or scaled to the content width. */
77
+ export type ImageFit = "natural" | "width";
78
+
79
+ /**
80
+ * The declarations an image item accepts. The rest of the vocabulary describes
81
+ * text, which an image does not have, so any other name on one is a definition
82
+ * error. A narrowing of the one vocabulary rather than a second list of its
83
+ * own, so the two cannot drift.
84
+ */
85
+ export type ImageStyleDeclarations = Pick<StyleDeclarations, "background" | "align">;
86
+
87
+ /**
88
+ * A host-supplied raster graphic in the band flow. `source` is an expression
89
+ * (never a literal — JSON has no way to write bytes) yielding the complete
90
+ * image file as a `Uint8Array` of PNG or JPEG bytes; nullish skips the item
91
+ * silently, anything else invalid is a located render error.
92
+ */
93
+ export interface ImageItem {
94
+ type: "image";
95
+ source: `=${string}`;
96
+ /** Defaults to `"natural"`. */
97
+ fit?: ImageFit;
98
+ /** The image's textual stand-in, as a cell value. */
99
+ alt?: CellValue;
100
+ visible?: ExpressionValue<boolean>;
101
+ style?: ImageStyleDeclarations;
102
+ }
103
+
104
+ /** One item in a band: text, or an image. Future types may be additive. */
105
+ export type Item = TextItem | ImageItem;
106
+
107
+ export interface TableHeader {
108
+ value: CellValue;
109
+ /** Literal only — never an expression. */
110
+ style?: StyleDeclarations;
111
+ }
112
+
113
+ export interface TableColumn extends Cell {
114
+ header: string | TableHeader;
115
+ /** Column width as a percentage of the table width (0 < width <= 100). */
116
+ width?: number;
117
+ }
118
+
119
+ export interface TableRow {
120
+ visible?: ExpressionValue<boolean>;
121
+ style?: StyleDeclarations;
122
+ }
123
+
124
+ export interface TableDetail {
125
+ row?: TableRow;
126
+ columns: [TableColumn, ...TableColumn[]];
127
+ total?: Cell[];
128
+ }
129
+
130
+ export interface Group {
131
+ name: string;
132
+ by: `=${string}`;
133
+ /** Start every instance of this group on a new page (paginated targets). */
134
+ break?: "page";
135
+ /**
136
+ * Restart `page.number` / `page.total` at every instance. Each instance also
137
+ * starts on a fresh page, the same rule `break` has.
138
+ */
139
+ reset?: "page";
140
+ /**
141
+ * Flow each instance's content in this many page columns (integer >= 2).
142
+ * Columned regions cannot nest.
143
+ */
144
+ columns?: number;
145
+ sort?: SortKey[];
146
+ /**
147
+ * Keep this many rows after this group's `sort`. Integer >= 1. Rows past
148
+ * the count are dropped from aggregates and runners, unlike `visible`.
149
+ */
150
+ take?: number;
151
+ aggregates?: Record<string, ReducerSpec>;
152
+ /** Running accumulators, read as `run.<name>` on detail rows. */
153
+ run?: Record<string, ReducerSpec>;
154
+ header?: Item[];
155
+ footer?: Item[];
156
+ }
157
+
158
+ /**
159
+ * Page header/footer bands, rendered once per page by paginated targets
160
+ * (unpaginated targets ignore them). Cells read `$` and the `page` anchor
161
+ * (`page.number` / `page.total`, 1-based, the current sequence); `@` stays unbound.
162
+ */
163
+ export interface PageBands {
164
+ header?: Item[];
165
+ footer?: Item[];
166
+ }
167
+
168
+ export interface ReportSchema {
169
+ params?: Record<string, JsonValue>;
170
+ data: string;
171
+ where?: `=${string}`;
172
+ sort?: SortKey[];
173
+ /**
174
+ * Keep this many rows after the report `sort`. Integer >= 1. Rows past the
175
+ * count are dropped from aggregates and runners, unlike `visible`.
176
+ */
177
+ take?: number;
178
+ aggregates?: Record<string, ReducerSpec>;
179
+ /** Running accumulators, read as `run.<name>` on detail rows. */
180
+ run?: Record<string, ReducerSpec>;
181
+ header?: Item[];
182
+ empty?: Item[];
183
+ /**
184
+ * Flow the report body in this many page columns (integer >= 2). Columned
185
+ * regions cannot nest.
186
+ */
187
+ columns?: number;
188
+ groups?: Group[];
189
+ detail?: Item[] | TableDetail;
190
+ footer?: Item[];
191
+ page?: PageBands;
192
+ }
193
+
194
+ /**
195
+ * One render target — the rollup-plugin shape: self-naming, with `compile`
196
+ * called per `render(target, data)` call against the public event stream, so
197
+ * it must be cheap and stateless; per-render work belongs in the renderer it
198
+ * returns. Official factories (`html()`, `pdf()`, `xlsx()`, `csv()`) return these; a
199
+ * custom target is any object honoring the contract (SCHEMA.md, "Instances
200
+ * and targets"). A renderer may return its output synchronously; `render` is
201
+ * always a promise.
202
+ */
203
+ export interface Target<Name extends string = string, Out = unknown> {
204
+ /** A diagnostic label — a plain identifier naming what the target is. */
205
+ name: Name;
206
+ /** Builds this target's renderer over one compiled event stream. */
207
+ compile: (stream: ReportEventStream) => (data?: unknown) => Out;
208
+ }
209
+
210
+ export interface QuarioOptions {
211
+ query?: QueryOptions;
212
+ /**
213
+ * License key (LICENSE section 6). Verified offline once, when the instance
214
+ * is created; `license` settles with the result. Without a key, or with one
215
+ * whose validity window does not cover this version's release date, every
216
+ * report this instance compiles marks its output as unlicensed.
217
+ */
218
+ license?: string;
219
+ }
220
+
221
+ /** The settled result of an instance's license key verification. */
222
+ export interface LicenseInfo {
223
+ licensed: boolean;
224
+ licensee?: string;
225
+ id?: string;
226
+ }
227
+
228
+ export type ItemRole =
229
+ | "report-header"
230
+ | "empty"
231
+ | "group-header"
232
+ | "detail"
233
+ | "group-footer"
234
+ | "report-footer"
235
+ | "page-header"
236
+ | "page-footer";
237
+
238
+ /** One static text run of a cell's template, verbatim (sjabloon's literal token). */
239
+ export type LiteralToken = SjabloonLiteralToken;
240
+
241
+ /** One interpolation's pre-stringify value (sjabloon's value token). */
242
+ export type ValueToken = SjabloonValueToken;
243
+
244
+ export type Token = LiteralToken | ValueToken;
245
+
246
+ export interface EventCell {
247
+ tokens: Token[];
248
+ style?: Record<string, unknown>;
249
+ }
250
+
251
+ /** The page anchor a paginated target passes to page band closures. */
252
+ export interface PageInfo {
253
+ /** 1-based page number of the current sequence. */
254
+ number: number;
255
+ /** Page count of the current sequence. */
256
+ total: number;
257
+ }
258
+
259
+ /**
260
+ * Per-render page band closures on `report-start` (present only when the
261
+ * schema declares page bands). A paginated target calls them once per page;
262
+ * each call returns resolved `page-header`/`page-footer` item events.
263
+ */
264
+ export interface PageBandRenderers {
265
+ header?: (page: PageInfo) => (ItemEvent | ImageEvent)[];
266
+ footer?: (page: PageInfo) => (ItemEvent | ImageEvent)[];
267
+ }
268
+
269
+ export interface ReportStartEvent {
270
+ type: "report-start";
271
+ params: Record<string, JsonValue>;
272
+ aggregates: Record<string, unknown>;
273
+ page?: PageBandRenderers;
274
+ /** The declared page column count, present when the root declares one. */
275
+ columns?: number;
276
+ /**
277
+ * The marking wording, present when this render is not covered by a valid
278
+ * license key — including while verification is still settling (await the
279
+ * stream's `license` promise first, as the shipped targets do). Absent
280
+ * otherwise. Each target renders this text in its own idiom; the engine
281
+ * states it so no target has to know it.
282
+ */
283
+ marking?: string;
284
+ }
285
+
286
+ export interface ItemEvent {
287
+ type: "item";
288
+ role: ItemRole;
289
+ tokens: Token[];
290
+ style?: Record<string, unknown>;
291
+ run?: Record<string, unknown>;
292
+ }
293
+
294
+ /** The format the engine sniffed from an image's bytes. */
295
+ export type ImageFormat = "png" | "jpeg";
296
+
297
+ export interface ImageEvent {
298
+ type: "image";
299
+ role: ItemRole;
300
+ /** The bytes exactly as the `source` expression yielded them. */
301
+ bytes: Uint8Array;
302
+ /** Sniffed from the bytes' magic numbers, so no consumer repeats it. */
303
+ format: ImageFormat;
304
+ fit: ImageFit;
305
+ /** The rendered `alt` template, present when the item declares one. */
306
+ alt?: Token[];
307
+ style?: Record<string, unknown>;
308
+ run?: Record<string, unknown>;
309
+ }
310
+
311
+ export interface GroupStartEvent {
312
+ type: "group-start";
313
+ name: string;
314
+ depth: number;
315
+ key: unknown;
316
+ aggregates: Record<string, unknown>;
317
+ break?: "page";
318
+ /** Restart `page.number` / `page.total` at every instance (paginated targets). */
319
+ reset?: "page";
320
+ /** The declared page column count, present when the group declares one. */
321
+ columns?: number;
322
+ }
323
+
324
+ export interface GroupEndEvent {
325
+ type: "group-end";
326
+ name: string;
327
+ depth: number;
328
+ }
329
+
330
+ export interface TableStartEvent {
331
+ type: "table-start";
332
+ columns: { header: EventCell; width?: number }[];
333
+ }
334
+
335
+ export interface RowEvent {
336
+ type: "row";
337
+ cells: EventCell[];
338
+ style?: Record<string, unknown>;
339
+ run?: Record<string, unknown>;
340
+ }
341
+
342
+ export interface TotalRowEvent {
343
+ type: "total-row";
344
+ cells: EventCell[];
345
+ }
346
+
347
+ export interface TableEndEvent {
348
+ type: "table-end";
349
+ }
350
+
351
+ export interface ReportEndEvent {
352
+ type: "report-end";
353
+ }
354
+
355
+ export type ReportEvent =
356
+ | ReportStartEvent
357
+ | ItemEvent
358
+ | ImageEvent
359
+ | GroupStartEvent
360
+ | GroupEndEvent
361
+ | TableStartEvent
362
+ | RowEvent
363
+ | TotalRowEvent
364
+ | TableEndEvent
365
+ | ReportEndEvent;
366
+
367
+ export interface ReportEventStream {
368
+ (data?: unknown): Generator<ReportEvent, void, undefined>;
369
+ readonly names: readonly string[];
370
+ readonly functions: readonly string[];
371
+ readonly paths: readonly QueryPath[];
372
+ }
373
+
374
+ /**
375
+ * Per-event handlers for `walk`, keyed by event type; a missing handler
376
+ * ignores that event.
377
+ */
378
+ export type WalkHandlers = {
379
+ [K in ReportEvent["type"]]?: (event: Extract<ReportEvent, { type: K }>) => void;
380
+ };
381
+
382
+ /**
383
+ * The walk driver: dispatch one render's event stream to per-event handlers,
384
+ * in stream order and exactly once each, pulling lazily and handing the loop
385
+ * back between batches. Handlers are synchronous — a promise one returns is
386
+ * not awaited. The stream's first event reaches its handler before a second one
387
+ * is pulled, so a target settles what it needs from `report-start` there rather
388
+ * than pulling the stream itself; no opening event is required, and a stream
389
+ * that starts part-way through walks like any other.
390
+ */
391
+ export function walk(events: Iterable<ReportEvent>, handlers: WalkHandlers): Promise<void>;
392
+
393
+ /**
394
+ * Hand the event loop back to the host, resolving once it has had its turn.
395
+ * The walk driver calls it between batches; a target running a loop of its own
396
+ * calls it the same way, so a long report never blocks the host.
397
+ */
398
+ export function breathe(): Promise<void>;
399
+
400
+ export type QuarioErrorCode = SjabloonErrorCode | PadvinderErrorCode;
401
+
402
+ export interface QuarioDiagnostic extends Error {
403
+ /** Absent on padvinder path-syntax errors; present on every other diagnostic. */
404
+ readonly code?: QuarioErrorCode;
405
+ readonly start?: number;
406
+ readonly end?: number;
407
+ readonly limit?: number;
408
+ readonly actual?: number;
409
+ readonly blocks?: readonly SjabloonBlock[];
410
+ }
411
+
412
+ /** The compiled report: `stream` plus its metadata, rendered per target. */
413
+ export interface CompiledReport {
414
+ /** The raw event seam: one generator of report events per call. */
415
+ stream(data?: unknown): Generator<ReportEvent, void, undefined>;
416
+ readonly names: readonly string[];
417
+ readonly functions: readonly string[];
418
+ readonly paths: readonly QueryPath[];
419
+ /**
420
+ * Render this compiled report through one target. Resolves the target's
421
+ * output, whether the target renders synchronously or not; target shape
422
+ * problems throw synchronously as definition errors.
423
+ */
424
+ render<Out>(target: Target<string, Out>, data?: unknown): Promise<Awaited<Out>>;
425
+ }
426
+
427
+ export interface Quario {
428
+ /**
429
+ * Settles when this instance's license key verification completes;
430
+ * `{ licensed: false }` without a key. Never rejects.
431
+ */
432
+ readonly license: Promise<LicenseInfo>;
433
+ /** Compile a definition once; every target renders from it. */
434
+ report(schema: ReportSchema, functions?: FunctionRegistry): CompiledReport;
435
+ }
436
+
437
+ /**
438
+ * Create a configured instance: host-level options (license key, query
439
+ * budgets).
440
+ */
441
+ export function quario(options?: QuarioOptions): Quario;
442
+
443
+ export function validate(schema: unknown, functions?: FunctionRegistry): string[];
444
+
445
+ /**
446
+ * True when a caught value is one of the stack's located diagnostics —
447
+ * re-thrown by quario with an engine original behind it, or thrown by
448
+ * xprsn/sjabloon/padvinder directly. Authenticated by identity: an error
449
+ * merely shaped like a diagnostic does not pass.
450
+ */
451
+ export function isDiagnostic(e: unknown): e is QuarioDiagnostic;
452
+
453
+ /**
454
+ * Join a token stream to display text: literals verbatim, values as
455
+ * `String(value ?? '')`. Re-exported from sjabloon.
456
+ */
457
+ export function text(tokens: readonly Token[]): string;
458
+
459
+ /**
460
+ * The typed-cell seam: exactly one value token holding a finite number, a
461
+ * boolean, or a valid Date keeps its pre-stringify value; anything else —
462
+ * including a lone null — reports `undefined` and joins to display text.
463
+ */
464
+ export function typed(tokens: readonly Token[]): number | boolean | Date | undefined;
465
+
466
+ /**
467
+ * Whether a band item's `role` names one of the report's own bands — its
468
+ * header, its footer, the `empty` band that replaces its body, or a page band —
469
+ * rather than a group instance's. Shipped so a consumer that needs the split
470
+ * does not restate the list. Page-band roles answer true although no walk emits
471
+ * one: the question is whose band a role names, not what a walk can hand over.
472
+ */
473
+ export function isReportBand(role: string | undefined): boolean;