@expresscsv/sdk 0.1.1 → 0.1.3

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,6 @@
1
+ /**
2
+ * Error thrown when the user cancels the import operation
3
+ */
4
+ export declare class ImportCancelledError extends Error {
5
+ constructor(message?: string);
6
+ }
@@ -0,0 +1,207 @@
1
+ import { type ExRow, type ExRowShape, type ExType, type Infer, x } from '@expresscsv/fields';
2
+ import { WidgetMode, WidgetState } from '@expresscsv/iframe-comm';
3
+ import type { ExBaseDef } from '@expresscsv/fields';
4
+ import type { ColorModeConfig, ECSVFontSource, ECSVTheme } from '@expresscsv/theme';
5
+ export { ImportCancelledError } from './errors';
6
+ /**
7
+ * A chunk of records passed to the onData callback
8
+ */
9
+ export interface RecordsChunk<T> {
10
+ /** The records in this chunk */
11
+ records: T[];
12
+ /** Total number of chunks */
13
+ totalChunks: number;
14
+ /** Current chunk index (0-based) */
15
+ currentChunkIndex: number;
16
+ /** Total number of records across all chunks */
17
+ totalRecords: number;
18
+ }
19
+ /**
20
+ * Webhook configuration for remote delivery of results
21
+ */
22
+ export interface WebhookConfig {
23
+ /** The URL to send webhook requests to */
24
+ url: string;
25
+ /** Optional HTTP headers to include in the request */
26
+ headers?: Record<string, string>;
27
+ /** HTTP method to use (default: 'POST') */
28
+ method?: 'POST' | 'PUT' | 'PATCH';
29
+ /** Request timeout in milliseconds (default: 30000) */
30
+ timeout?: number;
31
+ /** Number of retry attempts on failure (default: 0) */
32
+ retries?: number;
33
+ /** Arbitrary developer-provided metadata */
34
+ metadata?: Record<string, unknown>;
35
+ }
36
+ /**
37
+ * Type helper that requires at least one of the specified keys to be present
38
+ */
39
+ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
40
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
41
+ }[Keys];
42
+ /**
43
+ * Base delivery options - at least one must be provided
44
+ */
45
+ interface DeliveryOptionsBase<T> {
46
+ /** Local callback for processing chunks */
47
+ onData?: (chunk: RecordsChunk<T>, next: () => void) => void | Promise<void>;
48
+ /** Webhook configuration for remote delivery */
49
+ webhook?: WebhookConfig;
50
+ }
51
+ /**
52
+ * Delivery options - requires at least one of onData or webhook
53
+ */
54
+ export type DeliveryOptions<T> = RequireAtLeastOne<DeliveryOptionsBase<T>, 'onData' | 'webhook'>;
55
+ /**
56
+ * Options for the open() method
57
+ * Requires at least one of onData or webhook for delivery
58
+ */
59
+ export type OpenOptions<T> = RequireAtLeastOne<DeliveryOptionsBase<T>, 'onData' | 'webhook'> & {
60
+ /** Number of records per chunk (default: 1000) */
61
+ chunkSize?: number;
62
+ /** Called when all chunks have been processed */
63
+ onComplete?: () => void;
64
+ /** Called when the user cancels the import */
65
+ onCancel?: () => void;
66
+ /** Called when an error occurs */
67
+ onError?: (error: Error) => void;
68
+ /** Called when the widget opens */
69
+ onWidgetOpen?: () => void;
70
+ /** Called when the step changes in the wizard */
71
+ onStepChange?: (stepId: ExpressCSVStep, previousStepId?: ExpressCSVStep) => void;
72
+ /** Called when the widget closes */
73
+ onWidgetClose?: (reason: 'user_close' | 'cancel' | 'complete' | 'error') => void;
74
+ };
75
+ export type InferCSVImporter<TSchema extends ExType<unknown, ExBaseDef, unknown>> = CSVImporter<TSchema>;
76
+ export type { ColorModeConfig, ColorModePref, ECSVFontSource, ECSVTheme, TailwindThemeVars, } from '@expresscsv/theme';
77
+ export interface SDKOptions<TSchema extends ExType<unknown, ExBaseDef, unknown>> {
78
+ schema: TSchema;
79
+ publishableKey: string;
80
+ importIdentifier: string;
81
+ title?: string;
82
+ debug?: boolean;
83
+ developerMode?: boolean;
84
+ preload?: boolean;
85
+ theme?: ECSVTheme;
86
+ colorMode?: ColorModeConfig;
87
+ customCSS?: string;
88
+ fonts?: Record<string, ECSVFontSource>;
89
+ stepDisplay?: 'progressBar' | 'segmented' | 'numbered';
90
+ previewSchemaBeforeUpload?: boolean;
91
+ templateDownload?: TemplateDownloadConfig;
92
+ saveSession?: boolean;
93
+ locale?: DeepPartial<ExpressCSVLocaleInput>;
94
+ }
95
+ declare class CSVImporter<TSchema extends ExType<unknown, ExBaseDef, unknown> = ExRow<ExRowShape>> {
96
+ private options;
97
+ private iframe;
98
+ private container;
99
+ private connection;
100
+ private connectionState;
101
+ private debug;
102
+ private developerMode;
103
+ private importIdentifier;
104
+ private sessionId;
105
+ private _destroyTimer;
106
+ private _beforeUnloadHandler;
107
+ private widgetUrl;
108
+ private widgetState;
109
+ private widgetMode;
110
+ private canRestart;
111
+ private lastError;
112
+ private openOptions;
113
+ private cachedSchemaJson;
114
+ private initCompletePromise;
115
+ private resolveInitComplete;
116
+ constructor(options: SDKOptions<TSchema>);
117
+ /**
118
+ * Enhanced state management
119
+ */
120
+ private setState;
121
+ private updateDerivedState;
122
+ private handleError;
123
+ private waitForEvent;
124
+ /**
125
+ * Open the import flow with chunk-based data processing.
126
+ * Automatically opens the widget if not already open.
127
+ *
128
+ * @param options Configuration including onData callback for processing chunks
129
+ */
130
+ open(options: OpenOptions<Infer<TSchema>>): void;
131
+ /**
132
+ * Create import session on backend with configuration
133
+ * Idempotent: safe to call multiple times with same sessionId
134
+ */
135
+ private createImportSession;
136
+ /**
137
+ * Deliver results to webhook endpoint via backend API
138
+ * Chunks data using SDK-determined chunk size (independent of customer's chunkSize)
139
+ */
140
+ private deliverToWebhook;
141
+ /**
142
+ * Send a single chunk to backend webhook API with retry logic
143
+ */
144
+ private sendChunkToBackend;
145
+ /**
146
+ * Process results in chunks, calling onData and/or webhook for each chunk with backpressure control
147
+ */
148
+ private processResultsInChunks;
149
+ /**
150
+ * Initialize the iframe and connection.
151
+ * @param hidden Whether to create the iframe hidden (for preload) or visible (for normal open)
152
+ */
153
+ private initializeIframe;
154
+ private log;
155
+ private error;
156
+ /**
157
+ * Add beforeunload event listener to warn users about losing progress
158
+ */
159
+ private addBeforeUnloadListener;
160
+ /**
161
+ * Remove beforeunload event listener
162
+ */
163
+ private removeBeforeUnloadListener;
164
+ private waitForIframeLoad;
165
+ private setupConnectionAndInit;
166
+ openWidget(options?: {
167
+ reset?: boolean;
168
+ }): Promise<void>;
169
+ /**
170
+ * Makes a hidden container visible for instant display of preloaded iframe
171
+ */
172
+ private makeContainerVisible;
173
+ private setupMessageHandlers;
174
+ private createAndAppendIframe;
175
+ private destroy;
176
+ close(reason?: 'user_close' | 'cancel' | 'complete' | 'error'): Promise<void>;
177
+ resetWidget(): Promise<void>;
178
+ restart(newOptions?: Partial<SDKOptions<TSchema>>): Promise<void>;
179
+ private handleWidgetClosed;
180
+ private hideContainer;
181
+ getConnectionStatus(): boolean;
182
+ getState(): WidgetState;
183
+ getMode(): WidgetMode;
184
+ getIsReady(): boolean;
185
+ getIsOpen(): boolean;
186
+ getCanRestart(): boolean;
187
+ getLastError(): Error | null;
188
+ getStatus(): {
189
+ state: WidgetState;
190
+ mode: WidgetMode;
191
+ isReady: boolean;
192
+ isOpen: boolean;
193
+ canRestart: boolean;
194
+ hasError: boolean;
195
+ lastError: Error | null;
196
+ connectionStatus: boolean;
197
+ };
198
+ getVersion(): string;
199
+ }
200
+ export { x };
201
+ export type { Infer, ExType } from '@expresscsv/fields';
202
+ export type { ExBaseDef } from '@expresscsv/fields';
203
+ export type { ExpressCSVStep } from '@expresscsv/core';
204
+ export type { ExpressCSVLocaleInput, DeepPartial } from '@expresscsv/core';
205
+ export { WidgetState, WidgetMode } from '@expresscsv/iframe-comm';
206
+ import type { DeepPartial, ExpressCSVLocaleInput, ExpressCSVStep, TemplateDownloadConfig } from '@expresscsv/core';
207
+ export { CSVImporter };