@flonkid/kyc 1.9.2 → 1.9.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.
package/dist/core.d.ts ADDED
@@ -0,0 +1,294 @@
1
+ type DocumentType = 'passport' | 'id_card' | 'driver_license';
2
+ type WidgetLanguage = 'en' | 'de' | 'uk';
3
+ type FlonkDiagnosticLevel = 'info' | 'warn' | 'error';
4
+ /**
5
+ * Known diagnostic codes emitted when the SDK degrades or branches. The union
6
+ * is open (`| string`) so new codes don't break consumers' exhaustiveness.
7
+ */
8
+ type FlonkDiagnosticCode = 'LOADER_FALLBACK_BUNDLED' | 'LOADER_SCRIPT_BLOCKED' | 'PREWARM_SKIPPED' | 'IFRAME_REUSE' | 'IFRAME_FRESH' | 'PROTOCOL_VERSION_MISMATCH' | 'READY_TIMEOUT_REVEAL' | (string & {});
9
+ /**
10
+ * A structured diagnostic surfaced via `onDiagnostic` (and console when
11
+ * `window.__FLONK_DEBUG__` is set). Never contains tokens or PII.
12
+ */
13
+ interface FlonkDiagnostic {
14
+ code: FlonkDiagnosticCode;
15
+ level: FlonkDiagnosticLevel;
16
+ message: string;
17
+ detail?: Record<string, unknown>;
18
+ }
19
+ interface FlonkKYCOptions {
20
+ widgetUrl?: string;
21
+ apiBase?: string;
22
+ /**
23
+ * Observe SDK degradations/branches (loader fallback, blocked script, prewarm
24
+ * skipped, ready-timeout reveal, …). Errors thrown here are swallowed — they
25
+ * can never break the SDK. Console output for the same events is gated behind
26
+ * `window.__FLONK_DEBUG__ = true` so there's no noise by default.
27
+ */
28
+ onDiagnostic?: (event: FlonkDiagnostic) => void;
29
+ }
30
+ interface WidgetInitConfig {
31
+ serverUrl?: string;
32
+ /**
33
+ * Your project's publishable key (`pk_live_*` or `pk_sandbox_*`).
34
+ * Found in Dashboard → Project Settings → API Keys.
35
+ *
36
+ * Enables an instant branded loading screen: the brand color is resolved
37
+ * directly from the key (a Redis-cached read) in parallel with session
38
+ * setup, so the loader paints your color on first frame instead of waiting
39
+ * on the session. Works with both flows:
40
+ * - `serverUrl` (your backend creates the session), and
41
+ * - `sessionId` + `embedToken` (you pass a pre-created session).
42
+ *
43
+ * Without it, branding is resolved from the session and the loader shows
44
+ * the default color until that request returns. Recommended for the best UX.
45
+ */
46
+ publishableKey?: string;
47
+ sessionId?: string;
48
+ embedToken?: string;
49
+ /**
50
+ * URL the desktop→mobile QR code points to. Needed for the mobile-transfer
51
+ * QR to render. In the `serverUrl` flow, return it from your endpoint
52
+ * alongside `sessionId`/`embedToken` (it's the `qrCodeUrl` from `createSession`);
53
+ * in the `sessionId` + `embedToken` flow, pass it here.
54
+ */
55
+ qrCodeUrl?: string;
56
+ clientMetadata?: Record<string, unknown>;
57
+ lang?: WidgetLanguage;
58
+ overlayColor?: string;
59
+ allowManualUpload?: boolean;
60
+ mount?: HTMLElement;
61
+ /**
62
+ * Extra headers sent with the `serverUrl` POST request.
63
+ * Use this to pass Authorization tokens (e.g. JWT) when your
64
+ * backend requires authentication.
65
+ *
66
+ * @example { Authorization: 'Bearer <jwt>' }
67
+ */
68
+ requestHeaders?: Record<string, string>;
69
+ onSuccess?: (result: VerificationResult) => void;
70
+ onError?: (error: string) => void;
71
+ onCancel?: () => void;
72
+ onReady?: () => void;
73
+ }
74
+ interface WidgetPreviewConfig {
75
+ colors?: PreviewColors;
76
+ documentType?: DocumentType;
77
+ lang?: WidgetLanguage;
78
+ overlayColor?: string;
79
+ onSuccess?: (result: VerificationResult) => void;
80
+ onError?: (error: string) => void;
81
+ onCancel?: () => void;
82
+ }
83
+ interface WidgetEmbedConfig {
84
+ container: string | HTMLElement;
85
+ colors?: PreviewColors;
86
+ device?: 'mobile' | 'desktop';
87
+ scale?: number;
88
+ documentType?: DocumentType;
89
+ lang?: WidgetLanguage;
90
+ }
91
+ interface WidgetInstance {
92
+ iframe: HTMLIFrameElement;
93
+ destroy: () => void;
94
+ }
95
+ interface EmbedInstance extends WidgetInstance {
96
+ setColors: (colors: Partial<PreviewColors>) => void;
97
+ setDevice: (device: 'mobile' | 'desktop') => void;
98
+ getColors: () => PreviewColors;
99
+ }
100
+ interface PreviewColors {
101
+ primaryColor?: string;
102
+ secondaryColor?: string;
103
+ }
104
+ interface VerificationResult {
105
+ sessionId?: string;
106
+ status?: string;
107
+ [key: string]: unknown;
108
+ }
109
+
110
+ /**
111
+ * Self-prewarming for the KYC widget.
112
+ *
113
+ * Warms the connection and assets to our widget origin BEFORE the user clicks
114
+ * "Start", so the loader (and ultimately the widget) appears with little or no
115
+ * wait. All work is scheduled at idle / after the host page's `load`, so it
116
+ * never competes with the host page's own critical resources.
117
+ *
118
+ * Levels (`prewarm`):
119
+ * - 'connect' (default): preconnect + idle prefetch of the loader/branding
120
+ * assets and the widget document. Cheap; warms DNS/TLS + caches.
121
+ * - 'intent': warm when the user signals intent (hover / focus / the trigger
122
+ * scrolls into view). Best for widgets on general pages — no cost for
123
+ * visitors who never engage.
124
+ * - 'eager': also pre-mount a hidden iframe at idle so the full widget bundle
125
+ * loads in the background. Best for dedicated, high-click-through KYC pages.
126
+ * - 'none': do nothing.
127
+ *
128
+ * Never throws; SSR-safe (no-ops when there's no document). Sessions are NEVER
129
+ * pre-created here — only static assets are warmed.
130
+ */
131
+ type PrewarmLevel = 'connect' | 'intent' | 'eager' | 'none';
132
+
133
+ declare class FlonkKYC {
134
+ static readonly version = "1.9.3";
135
+ private readonly widgetUrl;
136
+ private readonly apiBase;
137
+ private disposeDiagnostics;
138
+ constructor(options?: FlonkKYCOptions);
139
+ /** Unregister this instance's `onDiagnostic` handler. */
140
+ dispose(): void;
141
+ /**
142
+ * Prewarm the widget ahead of the user's click — preconnect + idle prefetch
143
+ * of branding/assets, and (with `level:'eager'`) a hidden background iframe so
144
+ * the full bundle is loaded before the click. Call on page mount / route
145
+ * enter. Returns a cleanup (removes `intent` listeners). Never pre-creates a
146
+ * session. SSR-safe.
147
+ *
148
+ * @example
149
+ * FlonkKYC.prewarm({ publishableKey: 'pk_live_…', level: 'eager' });
150
+ * // or, warm only when the user shows intent:
151
+ * FlonkKYC.prewarm({ publishableKey: 'pk_live_…', level: 'intent', trigger: btn });
152
+ */
153
+ static prewarm(opts?: {
154
+ publishableKey?: string;
155
+ sessionId?: string;
156
+ widgetUrl?: string;
157
+ apiBase?: string;
158
+ level?: PrewarmLevel;
159
+ trigger?: Element | null;
160
+ }): () => void;
161
+ /**
162
+ * Warm the project's branding (colors) ahead of time so the widget paints the
163
+ * brand color on the first frame, with no branding round-trip at click time.
164
+ *
165
+ * Call it early — on page mount, route enter, or hover of the "verify" button
166
+ * — well before `init()`. The result is cached (module-level, 5-min TTL) and
167
+ * every subsequent `init()`/`open()` for the same key reads from it. Safe to
168
+ * call repeatedly; concurrent calls dedupe. Never throws.
169
+ *
170
+ * @example
171
+ * // in a layout effect, long before the user clicks "Verify"
172
+ * FlonkKYC.preloadBranding({ publishableKey: 'pk_live_...' });
173
+ */
174
+ static preloadBranding(opts: {
175
+ publishableKey?: string;
176
+ sessionId?: string;
177
+ apiBase?: string;
178
+ }): Promise<void>;
179
+ /**
180
+ * Open the KYC verification widget.
181
+ *
182
+ * Flows (pick one; add `publishableKey` to any for an instant branded loader):
183
+ * 1. `{ serverUrl }` — SDK auto-creates the session via your backend (recommended).
184
+ * 2. `{ sessionId, embedToken }` — you created the session; pass its credentials.
185
+ * 3. `{ sessionId }` — **deprecated**: exchanges the sessionId for an embedToken
186
+ * via an extra round-trip. Prefer flow 2 by returning `embedToken` from your
187
+ * backend alongside `sessionId`.
188
+ * 4. `{ publishableKey }` — client-only; SDK mints a short-lived widget token.
189
+ */
190
+ init(config: WidgetInitConfig): Promise<WidgetInstance>;
191
+ /**
192
+ * Preview mode — no API calls, mock data.
193
+ */
194
+ preview(config?: WidgetPreviewConfig): WidgetInstance;
195
+ /**
196
+ * Embed inline preview in a container (for dashboards).
197
+ */
198
+ embed(config: WidgetEmbedConfig): EmbedInstance;
199
+ /**
200
+ * Flow 1: serverUrl — POST to client's backend, get sessionId + embedToken.
201
+ *
202
+ * When `publishableKey` is provided, design tokens are fetched in parallel
203
+ * with the session creation request. This shows the branded loader ~200-500ms
204
+ * faster because we don't have to wait for the session to be created first.
205
+ */
206
+ private initWithServerUrl;
207
+ /**
208
+ * Flow 2: sessionId + embedToken — fetch session data, open widget.
209
+ */
210
+ private initWithEmbedToken;
211
+ /**
212
+ * Flow 3: sessionId only — exchange for embedToken, then init.
213
+ *
214
+ * @deprecated Prefer flow 2 (`sessionId` + `embedToken`). Return the
215
+ * `embedToken` from your backend together with the `sessionId` to skip this
216
+ * extra token-exchange round-trip.
217
+ */
218
+ private initWithSession;
219
+ /**
220
+ * Flow 4: publishableKey — fetch widget token, open widget.
221
+ */
222
+ private initWithPublishableKey;
223
+ private buildWidget;
224
+ /**
225
+ * Core: create iframe, attach listeners, return WidgetInstance.
226
+ */
227
+ private openWidget;
228
+ }
229
+
230
+ /**
231
+ * Tiny observability sink. The SDK emits a structured diagnostic at every
232
+ * degradation/branch point (loader fallback, blocked script, prewarm skipped,
233
+ * ready-timeout reveal, …) so integrators — and we — can see WHY the SDK did
234
+ * what it did, instead of guessing from a silent fallback.
235
+ *
236
+ * Module-level by necessity: several emit points (prewarm, the server-loader
237
+ * script load) run outside any FlonkKYC instance (static / constructor). The
238
+ * FlonkKYC constructor registers its `onDiagnostic` here; `window.__FLONK_DEBUG__`
239
+ * additionally mirrors events to the console. Zero-cost when nothing is
240
+ * registered and the debug flag is unset.
241
+ */
242
+ type DiagnosticHandler = (event: FlonkDiagnostic) => void;
243
+ /** Register a diagnostic handler. Returns an unsubscribe. De-dupes by reference. */
244
+ declare function addDiagnosticHandler(handler: DiagnosticHandler): () => void;
245
+
246
+ /**
247
+ * Stable, machine-branchable error codes returned by the API in the `code`
248
+ * field of an error body. Open union (`| (string & {})`) so new codes don't
249
+ * break consumers — branch on the ones you handle, fall through on the rest.
250
+ */
251
+ type FlonkErrorCode = 'authentication_error' | 'invalid_publishable_key' | 'session_not_found' | 'session_expired' | 'session_invalid_state' | 'rate_limited' | 'validation_error' | 'api_error' | (string & {});
252
+ declare class FlonkError extends Error {
253
+ readonly code: FlonkErrorCode;
254
+ readonly statusCode?: number | undefined;
255
+ constructor(message: string, code: FlonkErrorCode, statusCode?: number | undefined);
256
+ }
257
+ declare class FlonkValidationError extends FlonkError {
258
+ constructor(message: string);
259
+ }
260
+
261
+ declare const SDK_VERSION = "1.9.3";
262
+ /**
263
+ * REST API version this SDK is built against (date-pinned, separate from the
264
+ * package version). Sent as the `Flonk-Version` header so a future breaking API
265
+ * change can be served the old shape to old SDKs and the new shape to new ones.
266
+ * The API is additive-only within a version; a breaking change mints a new date.
267
+ */
268
+ declare const API_VERSION = "2026-06-01";
269
+ declare const PROTOCOL_VERSION = 1;
270
+ /** postMessage `type` values exchanged between the SDK and the iframe. */
271
+ declare const WIDGET_EVENTS: {
272
+ readonly READY: "KYC_WIDGET_READY";
273
+ readonly COMPLETE: "KYC_COMPLETE";
274
+ readonly CANCEL: "KYC_CANCEL";
275
+ readonly ERROR: "KYC_ERROR";
276
+ readonly CONFIG: "KYC_WIDGET_CONFIG";
277
+ };
278
+ /** Canonical iframe URL param keys the SDK stamps onto the widget src. */
279
+ declare const WIDGET_PARAMS: {
280
+ readonly PROTOCOL_VERSION: "pv";
281
+ readonly SESSION_ID: "sessionId";
282
+ readonly EMBED_TOKEN: "embedToken";
283
+ readonly TOKEN: "token";
284
+ readonly PUBLISHABLE_KEY: "publishableKey";
285
+ readonly CLIENT_ID: "clientId";
286
+ readonly CLIENT_METADATA: "clientMetadata";
287
+ readonly DESIGN_TOKENS: "designTokens";
288
+ readonly ALLOW_MANUAL_UPLOAD: "allowManualUpload";
289
+ readonly LANG: "lang";
290
+ readonly OVERLAY_COLOR: "overlayColor";
291
+ readonly MODE: "mode";
292
+ };
293
+
294
+ export { API_VERSION, type DocumentType, type EmbedInstance, type FlonkDiagnostic, type FlonkDiagnosticCode, type FlonkDiagnosticLevel, FlonkError, type FlonkErrorCode, FlonkKYC, type FlonkKYCOptions, FlonkValidationError, PROTOCOL_VERSION, type PreviewColors, SDK_VERSION, type VerificationResult, WIDGET_EVENTS, WIDGET_PARAMS, type WidgetEmbedConfig, type WidgetInitConfig, type WidgetInstance, type WidgetLanguage, type WidgetPreviewConfig, addDiagnosticHandler };
package/dist/core.js ADDED
@@ -0,0 +1,3 @@
1
+ export { API_VERSION, FlonkError, FlonkKYC, FlonkValidationError, PROTOCOL_VERSION, SDK_VERSION, WIDGET_EVENTS, WIDGET_PARAMS, addDiagnosticHandler } from './chunk-EEHIUYDR.js';
2
+ //# sourceMappingURL=core.js.map
3
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"core.js"}
package/dist/index.cjs CHANGED
@@ -4,7 +4,7 @@ var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
 
6
6
  // src/shared/constants.ts
7
- var SDK_VERSION = "1.9.2";
7
+ var SDK_VERSION = "1.9.3";
8
8
  var DEFAULT_WIDGET_URL = "https://widget.flonk.id";
9
9
  var DEFAULT_API_BASE = "https://api.flonk.id/v1";
10
10
  var API_VERSION = "2026-06-01";