@createlex/onetapforms-sdk 1.2.0 → 1.2.1

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,18 @@
1
+ import type { AutofillPreviewOptions, AutofillPreviewResult } from './types';
2
+ /**
3
+ * Helper to attach the canonical mapping attribute to a DOM element.
4
+ */
5
+ export declare function otfFieldAttr(canonicalName: string): Record<string, string>;
6
+ /**
7
+ * Collects all elements with a data-otf-field attribute.
8
+ * Returns the canonical name and the element reference.
9
+ */
10
+ export declare function collectMappedFields(root?: ParentNode): Array<{
11
+ name: string;
12
+ element: Element;
13
+ }>;
14
+ /**
15
+ * Preview autofill values from the backend without mutating the DOM.
16
+ * The caller is responsible for applying values to inputs after user consent.
17
+ */
18
+ export declare function previewAutofill(options: AutofillPreviewOptions): Promise<AutofillPreviewResult>;
package/dist/index.cjs CHANGED
@@ -2403,6 +2403,58 @@ var sdk = /*#__PURE__*/Object.freeze({
2403
2403
  default: OneTapForms
2404
2404
  });
2405
2405
 
2406
+ /**
2407
+ * Helper to attach the canonical mapping attribute to a DOM element.
2408
+ */
2409
+ function otfFieldAttr(canonicalName) {
2410
+ return { 'data-otf-field': canonicalName };
2411
+ }
2412
+ /**
2413
+ * Collects all elements with a data-otf-field attribute.
2414
+ * Returns the canonical name and the element reference.
2415
+ */
2416
+ function collectMappedFields(root = document) {
2417
+ if (typeof document === 'undefined')
2418
+ return [];
2419
+ const nodes = Array.from(root.querySelectorAll('[data-otf-field]'));
2420
+ return nodes
2421
+ .map((el) => {
2422
+ const name = el.getAttribute('data-otf-field');
2423
+ return name ? { name, element: el } : null;
2424
+ })
2425
+ .filter((item) => Boolean(item));
2426
+ }
2427
+ /**
2428
+ * Preview autofill values from the backend without mutating the DOM.
2429
+ * The caller is responsible for applying values to inputs after user consent.
2430
+ */
2431
+ async function previewAutofill(options) {
2432
+ const apiUrl = options.apiUrl || 'https://api.createlex.com/api/onetapforms/autofill/preview';
2433
+ const payload = {
2434
+ fields: options.fields || null,
2435
+ formTemplate: options.formTemplate,
2436
+ };
2437
+ const headers = {
2438
+ 'Content-Type': 'application/json',
2439
+ };
2440
+ if (options.token) {
2441
+ headers['Authorization'] = `Bearer ${options.token}`;
2442
+ }
2443
+ if (options.userId) {
2444
+ headers['x-user-id'] = options.userId;
2445
+ }
2446
+ const response = await fetch(apiUrl, {
2447
+ method: 'POST',
2448
+ headers,
2449
+ body: JSON.stringify(payload),
2450
+ });
2451
+ if (!response.ok) {
2452
+ const text = await response.text();
2453
+ throw new Error(`Autofill preview failed: ${response.status} ${text}`);
2454
+ }
2455
+ return response.json();
2456
+ }
2457
+
2406
2458
  // Export the OneTapForms class and types
2407
2459
  // Widget auto-initialization for CDN/script tag usage
2408
2460
  // This runs automatically when loaded via <script> tag
@@ -2680,7 +2732,10 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {
2680
2732
  }
2681
2733
 
2682
2734
  exports.OneTapForms = OneTapForms;
2735
+ exports.collectMappedFields = collectMappedFields;
2683
2736
  exports.default = OneTapForms;
2684
2737
  exports.initWidgets = initWidgets;
2738
+ exports.otfFieldAttr = otfFieldAttr;
2739
+ exports.previewAutofill = previewAutofill;
2685
2740
  exports.showQRModal = showQRModal;
2686
2741
  //# sourceMappingURL=index.cjs.map