@dipanshuhandoo/excelio 0.0.0-stage → 1.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.
@@ -0,0 +1,191 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * excelio — Plug-and-Play Excel ↔ JSON utility.
4
+ * TypeScript declarations for consumers; the package itself is plain JS.
5
+ */
6
+ import type { Readable, Writable } from 'node:stream';
7
+ import type { Buffer } from 'node:buffer';
8
+
9
+ export type ExcelIoErrorCode =
10
+ | 'INVALID_SPEC'
11
+ | 'IO_ERROR'
12
+ | 'ABORTED'
13
+ | 'WORKER_ERROR'
14
+ | 'ROW_VALIDATION'
15
+ | 'SOURCE_ERROR';
16
+
17
+ export class ExcelIoError extends Error {
18
+ constructor(message: string, options?: { code?: ExcelIoErrorCode; cause?: unknown; details?: unknown });
19
+ code: ExcelIoErrorCode;
20
+ cause?: unknown;
21
+ details?: unknown;
22
+ }
23
+
24
+ export type ColumnType = 'string' | 'number' | 'boolean' | 'date';
25
+
26
+ export interface ColumnSpec {
27
+ /** Property name on each data row. */
28
+ key: string;
29
+ /** Header text in row 1. Defaults to `key`. */
30
+ header?: string;
31
+ /** Type hint that informs cell formatting and write-side coercion. */
32
+ type?: ColumnType;
33
+ /** Excel number-format string (e.g. 'yyyy-mm-dd', '#,##0.00'). */
34
+ format?: string;
35
+ /** Column width in Excel units. */
36
+ width?: number;
37
+ }
38
+
39
+ export interface SheetStyling {
40
+ headerBold?: boolean;
41
+ freezeHeader?: boolean;
42
+ autoFilter?: boolean;
43
+ }
44
+
45
+ /**
46
+ * Rows for one sheet: an array, or any sync/async iterable (generator, database cursor,
47
+ * object-mode stream). Iterables are consumed once and always written inline.
48
+ */
49
+ export type RowSource<Row> = readonly Row[] | Iterable<Row> | AsyncIterable<Row>;
50
+
51
+ export interface SheetSpec<Row = Record<string, unknown>> {
52
+ sheet: string;
53
+ columns?: ColumnSpec[];
54
+ styling?: SheetStyling;
55
+ /** Omitted or null means an empty sheet. */
56
+ data?: RowSource<Row> | null;
57
+ /** Optional row-count hint used for progress `totalRows` when `data` is an iterable. */
58
+ totalRows?: number;
59
+ }
60
+
61
+ export interface WorkbookSpec<Row = Record<string, unknown>> {
62
+ workbook: string;
63
+ properties?: {
64
+ author?: string;
65
+ company?: string;
66
+ title?: string;
67
+ created?: Date | string;
68
+ modified?: Date | string;
69
+ };
70
+ sheets: SheetSpec<Row>[];
71
+ }
72
+
73
+ export type Spec<Row = Record<string, unknown>> = WorkbookSpec<Row>[];
74
+
75
+ export interface ParsedColumn {
76
+ key: string;
77
+ header: string;
78
+ }
79
+
80
+ export interface ParsedSheet<Row = Record<string, unknown>> {
81
+ sheet: string;
82
+ columns: ParsedColumn[];
83
+ data: Row[];
84
+ }
85
+
86
+ export interface ParsedWorkbook<Row = Record<string, unknown>> {
87
+ workbook: undefined;
88
+ sheets: ParsedSheet<Row>[];
89
+ }
90
+
91
+ export interface RowContext {
92
+ workbookIndex: number;
93
+ workbookName?: string;
94
+ sheetIndex: number;
95
+ sheetName: string;
96
+ /** 1 for the first worksheet of a logical sheet, 2+ for continuation sheets. Write only. */
97
+ sheetPart?: number;
98
+ rowNumber: number;
99
+ }
100
+
101
+ export interface RowError {
102
+ workbook: number;
103
+ sheet: string;
104
+ row: number;
105
+ column: string | null;
106
+ code: string;
107
+ message: string;
108
+ }
109
+
110
+ export interface ReadResult<Row = Record<string, unknown>> {
111
+ ok: boolean;
112
+ data: ParsedWorkbook<Row>[];
113
+ errors: RowError[];
114
+ stats: {
115
+ totalRows: number;
116
+ totalSheets: number;
117
+ durationMs: number;
118
+ };
119
+ }
120
+
121
+ export interface ProgressEvent {
122
+ phase: 'read' | 'write';
123
+ workbookIndex?: number;
124
+ sheetIndex?: number;
125
+ sheetName?: string;
126
+ /** Write only: worksheet part of the logical sheet (see `maxRowsPerSheet`). */
127
+ sheetPart?: number;
128
+ rowsProcessed: number;
129
+ /** Undefined when an iterable sheet has no `totalRows` hint. */
130
+ totalRows?: number;
131
+ }
132
+
133
+ export interface Logger {
134
+ info?: (msg: string, ...args: unknown[]) => void;
135
+ warn?: (msg: string, ...args: unknown[]) => void;
136
+ error?: (msg: string, ...args: unknown[]) => void;
137
+ }
138
+
139
+ export interface CommonOptions {
140
+ /** Called at batchSize row boundaries; writing also emits a final update. */
141
+ onProgress?: (e: ProgressEvent) => void;
142
+ /** Checked between rows/sheets; iterable sheets also observe it while waiting for output capacity. */
143
+ signal?: AbortSignal;
144
+ /** BYO logger. Default: no-op. */
145
+ logger?: Logger;
146
+ /** Positive integer. Progress interval; iterable sheets also yield and bound output backlog at this interval. */
147
+ batchSize?: number;
148
+ /** 'auto' (default), true (request worker), false (inline). Hooks/streams force inline. */
149
+ useWorker?: 'auto' | boolean;
150
+ /** Auto-mode threshold. Default: { rows: 50_000, bytes: 10MB }. */
151
+ workerThreshold?: { rows?: number; bytes?: number };
152
+ }
153
+
154
+ export interface WriteOptions<Row = Record<string, unknown>> extends CommonOptions {
155
+ /** File path or Writable stream. Omit to receive Buffer(s) back. */
156
+ to?: string | Writable;
157
+ /** Per-row transform applied before writing. */
158
+ beforeWrite?: (row: Row, ctx: RowContext) => Row | void;
159
+ /** Data rows per worksheet before continuing on "<name> (2)". Default and maximum 1,048,575. */
160
+ maxRowsPerSheet?: number;
161
+ /** Excel shared-strings table. 'auto' (default): on unless any sheet has iterable data. */
162
+ sharedStrings?: 'auto' | boolean;
163
+ }
164
+
165
+ export interface ReadOptions<Row = Record<string, unknown>> extends CommonOptions {
166
+ /** Row number containing headers (1-indexed). Default: 1. */
167
+ headerRow?: number;
168
+ /** Empty cells become null instead of ''. Default: true. */
169
+ nullForBlank?: boolean;
170
+ /** Trim whitespace from string cells. Default: true. */
171
+ trimStrings?: boolean;
172
+ /** Date output format. Default: 'iso'. */
173
+ dateFormat?: 'iso' | 'date' | 'epoch';
174
+ /** Per-row transform applied after parsing, before validation. */
175
+ afterRead?: (row: Row, ctx: RowContext) => Row | void;
176
+ /** Per-row validation. Return null/undefined for valid; an issue object for invalid. */
177
+ validateRow?: (row: Row, ctx: RowContext) => null | undefined | { code?: string; message: string; column?: string };
178
+ /** 'collect' (default) returns errors in the result; 'fail' throws on first. */
179
+ onRowError?: 'collect' | 'fail';
180
+ }
181
+
182
+ export interface Excelio {
183
+ write<Row = Record<string, unknown>, Target extends string | Writable = string | Writable>(spec: Spec<Row>, options: WriteOptions<Row> & { to: Target }): Promise<Target>;
184
+ write<Row = Record<string, unknown>>(spec: Spec<Row>, options?: WriteOptions<Row> & { to?: undefined }): Promise<Buffer | Buffer[]>;
185
+ write<Row = Record<string, unknown>>(spec: Spec<Row>, options?: WriteOptions<Row>): Promise<Buffer | Buffer[] | string | Writable>;
186
+ writeStream<Row = Record<string, unknown>>(spec: Spec<Row>, options?: WriteOptions<Row>): Readable;
187
+ read<Row = Record<string, unknown>>(input: Buffer | string | Readable, options?: ReadOptions<Row>): Promise<ReadResult<Row>>;
188
+ }
189
+
190
+ export const excelio: Excelio;
191
+ export default excelio;