@returningai/widget-sdk 1.7.2 → 1.8.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.
@@ -1,5 +1,4 @@
1
- import type { ReturningAIStoreCallbackFieldOptionsCallback, ReturningAIStorePurchaseSuccessCallback, ReturningAIWidgetCallback, WidgetConfig } from "./types";
2
- import { STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME, STORE_PURCHASE_SUCCESS_CALLBACK_NAME } from "./types";
1
+ import type { ReturningAIWidgetCallback, ReturningAIWidgetCallbackMap, WidgetConfig } from "./types";
3
2
  export declare function readConfig(el: Element, existingId?: string): WidgetConfig;
4
3
  export declare abstract class BaseWidget extends HTMLElement {
5
4
  static get observedAttributes(): string[];
@@ -17,12 +16,12 @@ export declare abstract class BaseWidget extends HTMLElement {
17
16
  private hasAttemptedInit;
18
17
  private initGeneration;
19
18
  private lottieInstances;
19
+ private renderedLoaderAnimationKey;
20
20
  private callbackRegistry;
21
21
  private bannerNode;
22
22
  private releaseAutoThemeDisableLease?;
23
23
  constructor();
24
- registerCallback(callbackName: typeof STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME, callback: ReturningAIStoreCallbackFieldOptionsCallback): void;
25
- registerCallback(callbackName: typeof STORE_PURCHASE_SUCCESS_CALLBACK_NAME, callback: ReturningAIStorePurchaseSuccessCallback): void;
24
+ registerCallback<K extends keyof ReturningAIWidgetCallbackMap>(callbackName: K, callback: ReturningAIWidgetCallbackMap[K]): void;
26
25
  registerCallback(callbackName: string, callback: ReturningAIWidgetCallback): void;
27
26
  lockCallbacks(): void;
28
27
  protected abstract buildWidgetUrl(config: WidgetConfig): string;
@@ -53,6 +52,8 @@ export declare abstract class BaseWidget extends HTMLElement {
53
52
  private applyFirstPaintColors;
54
53
  private applyFont;
55
54
  private applyPalette;
55
+ private applyLoadingSettings;
56
+ private getLoadingAnimationRenderKey;
56
57
  private renderShell;
57
58
  /**
58
59
  * Loads the community loading animation + theme palette from cache or API
@@ -98,6 +99,7 @@ export declare abstract class BaseWidget extends HTMLElement {
98
99
  private isCurrentInit;
99
100
  private teardownMountedWidget;
100
101
  private createIframe;
102
+ private prepareBundleRuntimeEnvironment;
101
103
  private mountWidget;
102
104
  private scheduleRefresh;
103
105
  private schedulePeriodicSync;
@@ -6,6 +6,11 @@ interface BundleRuntimeLoaderOptions {
6
6
  readGlobal: (globalName: string) => unknown;
7
7
  baseUrl: () => string;
8
8
  }
9
+ interface BundleRuntimeWarmerOptions {
10
+ preloadScript: (url: string) => Promise<void>;
11
+ baseUrl: () => string;
12
+ }
13
+ export declare function createBundleRuntimeWarmer({ preloadScript, baseUrl, }: BundleRuntimeWarmerOptions): (globalName: string, bundleUrl: string) => void;
9
14
  export declare function normalizeBundleUrl(url: string, baseUrl: string): string;
10
15
  /**
11
16
  * Cache widget runtimes by both their window global and normalized bundle URL.
@@ -16,5 +21,6 @@ export declare function normalizeBundleUrl(url: string, baseUrl: string): string
16
21
  * reuse A without re-evaluating its IIFE.
17
22
  */
18
23
  export declare function createBundleRuntimeLoader({ loadScript, readGlobal, baseUrl, }: BundleRuntimeLoaderOptions): (globalName: string, bundleUrl: string) => Promise<WidgetBundleRuntime>;
24
+ export declare function warmBundleRuntime(globalName: string, bundleUrl: string): void;
19
25
  export declare function getOrLoadBundleRuntime(globalName: string, bundleUrl: string): Promise<WidgetBundleRuntime>;
20
26
  export {};
@@ -0,0 +1,7 @@
1
+ import type { WidgetConfig, WidgetState } from "../types";
2
+ export declare const BUNDLE_AUTH_TOKEN_CHANGE_EVENT = "authtoken-change";
3
+ export type BundleTokenRuntime = EventTarget & {
4
+ guest_token?: string;
5
+ };
6
+ /** Publish a refreshed access token to a mounted no-iframe widget bundle. */
7
+ export declare function syncBundleAccessToken(config: WidgetConfig, state: WidgetState, runtime?: BundleTokenRuntime): boolean;
@@ -0,0 +1,49 @@
1
+ import type { ReturningAIWidgetCallbacks, ReturningAIWidgetType, WidgetConfig, WidgetHostCallbackPayload } from '../types';
2
+ export type HostCallbackCompletionMode = 'result' | 'acknowledged' | 'detached';
3
+ export type HostCallbackTransport = 'bundle' | 'iframe';
4
+ export type HostCallbackAction = 'widget:host-callback' | 'store:host-callback';
5
+ export type HostCallbackLifecycleEventType = 'callback.received' | 'callback.handler_missing' | 'callback.handler_started' | 'callback.handler_resolved' | 'callback.handler_rejected';
6
+ export interface HostCallbackLifecycleEvent {
7
+ type: HostCallbackLifecycleEventType;
8
+ invocationId: string;
9
+ callbackName: string;
10
+ scope: string;
11
+ sourceWidgetType: ReturningAIWidgetType;
12
+ hostWidgetType: ReturningAIWidgetType;
13
+ completionMode: HostCallbackCompletionMode;
14
+ transport?: HostCallbackTransport;
15
+ error?: unknown;
16
+ }
17
+ /** Optional observation boundary. The SDK does not install an observer by default. */
18
+ export type HostCallbackObserver = (event: HostCallbackLifecycleEvent) => void;
19
+ export interface HostCallbackDefinition {
20
+ callbackName: string;
21
+ scope: string;
22
+ sourceWidgetType: ReturningAIWidgetType;
23
+ allowedHostWidgetTypes: readonly ReturningAIWidgetType[];
24
+ completionMode: HostCallbackCompletionMode;
25
+ missingHandler: 'reject' | 'ignore';
26
+ legacyActions: readonly HostCallbackAction[];
27
+ validatePayload(payload: unknown): payload is WidgetHostCallbackPayload;
28
+ }
29
+ export interface DispatchHostCallbackInput {
30
+ callbacks: ReturningAIWidgetCallbacks;
31
+ callbackName: string;
32
+ payload: unknown;
33
+ hostWidgetType: ReturningAIWidgetType;
34
+ invocationId?: string;
35
+ transport?: HostCallbackTransport;
36
+ observer?: HostCallbackObserver;
37
+ }
38
+ /** Mirrors auth identity-source precedence; token authenticity is enforced by widget authentication. */
39
+ export declare function resolveHostUserIdentifiers(config: Pick<WidgetConfig, 'embedToken' | 'userIdentifiers'>): Record<string, string>;
40
+ /** Adds host-owned identity values without making widget code depend on auth internals. */
41
+ export declare function enrichHostCallbackPayload(payload: unknown, userIdentifiers?: Record<string, string>): unknown;
42
+ export declare function getHostCallbackDefinition(callbackName: string, payload: unknown): HostCallbackDefinition | undefined;
43
+ export declare function isHostCallbackMessageSupported(definition: HostCallbackDefinition, action: string, sourceWidgetType: string, hostWidgetType: ReturningAIWidgetType): boolean;
44
+ /**
45
+ * Runs every supported host callback through one policy-driven boundary.
46
+ * Detached callbacks return immediately; acknowledged/result callbacks return
47
+ * a promise that preserves their existing request/response behavior.
48
+ */
49
+ export declare function dispatchHostCallback(input: DispatchHostCallbackInput): Promise<unknown> | void;
@@ -18,6 +18,6 @@ declare global {
18
18
  }
19
19
  }
20
20
  export { StoreWidget, ChannelWidget, MilestoneWidget, SocialWidget, CurrencyWidget, ReferralWidget, CustomWidget };
21
- export type { ReturningAIStoreCallbackFieldOptionsCallback, ReturningAIStoreHostCallbackActionMessage, ReturningAIStorePurchaseSuccessCallback, ReturningAIWidgetCallback, ReturningAIWidgetCallbackRegistration, ReturningAIWidgetActionMessage, ReturningAIWidgetActionResultMessage, ReturningAIWidgetCallbacks, StoreCallbackFieldOptionsContext, StoreCallbackFieldOptionsField, StoreCallbackFieldOptionsFieldType, StoreCallbackFieldOptionsPayload, StoreCallbackFieldOptionsResult, StoreHostCallbackPayload, StorePurchaseSuccessPayload, } from './types';
22
- export { STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME, STORE_CALLBACK_FIELD_OPTIONS_SCOPE, STORE_CALLBACK_FIELD_OPTIONS_TIMEOUT_MS, STORE_PURCHASE_SUCCESS_CALLBACK_NAME, STORE_PURCHASE_SUCCESS_SCOPE, } from './types';
21
+ export type { MilestoneCtaClickPayload, ReturningAIGenericHostCallbackActionMessage, ReturningAIMilestoneCtaClickCallback, ReturningAIStoreCallbackFieldOptionsCallback, ReturningAIStoreHostCallbackActionMessage, ReturningAIStorePurchaseSuccessCallback, ReturningAIWidgetCallback, ReturningAIWidgetCallbackMap, ReturningAIWidgetCallbackRegistration, ReturningAIWidgetActionMessage, ReturningAIWidgetActionResultMessage, ReturningAIWidgetCallbacks, ReturningAIWidgetHostCallbackActionMessage, ReturningAIWidgetType, StoreCallbackFieldOptionsContext, StoreCallbackFieldOptionsField, StoreCallbackFieldOptionsFieldType, StoreCallbackFieldOptionsPayload, StoreCallbackFieldOptionsResult, StoreHostCallbackPayload, StorePurchaseSuccessPayload, WidgetHostCallbackPayload, } from './types';
22
+ export { MILESTONE_CTA_CLICK_CALLBACK_NAME, MILESTONE_CTA_CLICK_EVENT, MILESTONE_CTA_CLICK_SCOPE, STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME, STORE_CALLBACK_FIELD_OPTIONS_SCOPE, STORE_CALLBACK_FIELD_OPTIONS_TIMEOUT_MS, STORE_PURCHASE_SUCCESS_CALLBACK_NAME, STORE_PURCHASE_SUCCESS_SCOPE, } from './types';
23
23
  export type {} from './jsx';
@@ -3,6 +3,10 @@ export declare const STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME: "callbackFieldO
3
3
  export declare const STORE_CALLBACK_FIELD_OPTIONS_TIMEOUT_MS = 15000;
4
4
  export declare const STORE_PURCHASE_SUCCESS_SCOPE: "store.purchase_success";
5
5
  export declare const STORE_PURCHASE_SUCCESS_CALLBACK_NAME: "storePurchaseSuccess";
6
+ export declare const MILESTONE_CTA_CLICK_SCOPE: "milestone.cta_click";
7
+ export declare const MILESTONE_CTA_CLICK_CALLBACK_NAME: "milestoneCtaClick";
8
+ export declare const MILESTONE_CTA_CLICK_EVENT: "milestone_cta_clicked";
9
+ export type ReturningAIWidgetType = 'store' | 'channel' | 'milestone' | 'social' | 'currency-view' | 'referral-conditions' | 'custom';
6
10
  export type StoreCallbackFieldOptionsFieldType = 'single-select';
7
11
  export interface StoreCallbackFieldOptionsField {
8
12
  id: string;
@@ -53,25 +57,55 @@ export interface StorePurchaseSuccessPayload {
53
57
  };
54
58
  }
55
59
  export type StoreHostCallbackPayload = StoreCallbackFieldOptionsPayload | StorePurchaseSuccessPayload;
60
+ export interface MilestoneCtaClickPayload {
61
+ scope: typeof MILESTONE_CTA_CLICK_SCOPE;
62
+ callbackName: typeof MILESTONE_CTA_CLICK_CALLBACK_NAME;
63
+ context: {
64
+ event: typeof MILESTONE_CTA_CLICK_EVENT;
65
+ community: {
66
+ id: string;
67
+ name?: string | null;
68
+ };
69
+ milestone: {
70
+ id: string;
71
+ name: string;
72
+ };
73
+ stage: {
74
+ id: string;
75
+ name: string;
76
+ };
77
+ button: {
78
+ name: string;
79
+ };
80
+ user: {
81
+ identifiers: Record<string, string>;
82
+ };
83
+ clickedAt: string;
84
+ };
85
+ }
86
+ export type WidgetHostCallbackPayload = StoreHostCallbackPayload | MilestoneCtaClickPayload;
56
87
  export type ReturningAIWidgetCallback<TPayload = any, TResult = any> = (payload: TPayload) => Promise<TResult> | TResult;
57
88
  export type ReturningAIStoreCallbackFieldOptionsCallback = (payload: StoreCallbackFieldOptionsPayload) => Promise<StoreCallbackFieldOptionsResult> | StoreCallbackFieldOptionsResult;
58
89
  export type ReturningAIStorePurchaseSuccessCallback = (payload: StorePurchaseSuccessPayload) => Promise<unknown> | unknown;
59
- export type ReturningAIWidgetCallbacks = {
60
- 'callbackFieldOptions'?: ReturningAIStoreCallbackFieldOptionsCallback;
61
- 'storePurchaseSuccess'?: ReturningAIStorePurchaseSuccessCallback;
90
+ export type ReturningAIMilestoneCtaClickCallback = (payload: MilestoneCtaClickPayload) => Promise<unknown> | unknown;
91
+ export interface ReturningAIWidgetCallbackMap {
92
+ callbackFieldOptions: ReturningAIStoreCallbackFieldOptionsCallback;
93
+ storePurchaseSuccess: ReturningAIStorePurchaseSuccessCallback;
94
+ milestoneCtaClick: ReturningAIMilestoneCtaClickCallback;
95
+ }
96
+ export type ReturningAIWidgetCallbacks = Partial<ReturningAIWidgetCallbackMap> & {
62
97
  [callbackName: string]: ReturningAIWidgetCallback | undefined;
63
98
  };
64
99
  export interface ReturningAIWidgetCallbackRegistration {
65
- registerCallback(callbackName: typeof STORE_CALLBACK_FIELD_OPTIONS_CALLBACK_NAME, callback: ReturningAIStoreCallbackFieldOptionsCallback): void;
66
- registerCallback(callbackName: typeof STORE_PURCHASE_SUCCESS_CALLBACK_NAME, callback: ReturningAIStorePurchaseSuccessCallback): void;
100
+ registerCallback<K extends keyof ReturningAIWidgetCallbackMap>(callbackName: K, callback: ReturningAIWidgetCallbackMap[K]): void;
67
101
  registerCallback(callbackName: string, callback: ReturningAIWidgetCallback): void;
68
102
  lockCallbacks(): void;
69
103
  }
70
- export interface ReturningAIWidgetActionMessage<TPayload extends StoreHostCallbackPayload = StoreCallbackFieldOptionsPayload> {
104
+ export interface ReturningAIWidgetActionMessage<TPayload extends WidgetHostCallbackPayload = StoreCallbackFieldOptionsPayload, TWidgetType extends ReturningAIWidgetType = 'store', TAction extends 'widget:host-callback' | 'store:host-callback' = 'store:host-callback'> {
71
105
  type: 'RETURNINGAI_WIDGET_ACTION';
72
106
  requestId: string;
73
- widgetType: 'store';
74
- action: 'store:host-callback';
107
+ widgetType: TWidgetType;
108
+ action: TAction;
75
109
  containerId?: string;
76
110
  widgetId?: string;
77
111
  callbackName: string;
@@ -79,10 +113,12 @@ export interface ReturningAIWidgetActionMessage<TPayload extends StoreHostCallba
79
113
  payload: TPayload;
80
114
  }
81
115
  export type ReturningAIStoreHostCallbackActionMessage = ReturningAIWidgetActionMessage<StoreHostCallbackPayload>;
116
+ export type ReturningAIGenericHostCallbackActionMessage = ReturningAIWidgetActionMessage<WidgetHostCallbackPayload, ReturningAIWidgetType, 'widget:host-callback'>;
117
+ export type ReturningAIWidgetHostCallbackActionMessage = ReturningAIStoreHostCallbackActionMessage | ReturningAIGenericHostCallbackActionMessage;
82
118
  export interface ReturningAIWidgetActionResultMessage {
83
119
  type: 'RETURNINGAI_WIDGET_ACTION_RESULT';
84
120
  requestId: string;
85
- action: 'store:host-callback';
121
+ action: 'store:host-callback' | 'widget:host-callback';
86
122
  ok: boolean;
87
123
  status: 'success' | 'failed';
88
124
  value?: StoreCallbackFieldOptionsResult;
@@ -101,7 +137,7 @@ export interface WidgetConfig {
101
137
  * Set to the same value as communityId by readConfig().
102
138
  */
103
139
  widgetId: string;
104
- widgetType: 'store' | 'channel' | 'milestone' | 'social' | 'currency-view' | 'referral-conditions' | 'custom';
140
+ widgetType: ReturningAIWidgetType;
105
141
  theme: 'light' | 'dark';
106
142
  themeExplicit: boolean;
107
143
  container: string;
@@ -173,12 +209,30 @@ export interface WidgetState {
173
209
  };
174
210
  };
175
211
  } | null;
212
+ loadingSettings: {
213
+ backgroundColor?: string;
214
+ backgroundColors?: {
215
+ light?: string;
216
+ dark?: string;
217
+ };
218
+ loadingText?: {
219
+ enabled: boolean;
220
+ text: string;
221
+ fontFamily: string;
222
+ fontSizePx: number;
223
+ bold: boolean;
224
+ italic: boolean;
225
+ underline: boolean;
226
+ color: string;
227
+ };
228
+ } | null;
176
229
  themePalette: {
177
230
  light?: Record<string, string>;
178
231
  dark?: Record<string, string>;
179
232
  } | null;
180
233
  font: string | null;
181
234
  defaultColorSchema: 'light' | 'dark' | null;
235
+ widgetData: Record<string, unknown> | null;
182
236
  }
183
237
  export interface TokenData {
184
238
  accessToken: string;
@@ -186,4 +240,5 @@ export interface TokenData {
186
240
  tokenFamily?: string;
187
241
  accessTokenTTL: number;
188
242
  refreshTokenTTL: number;
243
+ widgetData?: Record<string, unknown>;
189
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@returningai/widget-sdk",
3
- "version": "1.7.2",
3
+ "version": "1.8.1",
4
4
  "description": "Shadow DOM isolated widget SDK for ReturningAI",
5
5
  "main": "dist/rai-widget.iife.js",
6
6
  "module": "dist/rai-widget.js",
@@ -18,7 +18,6 @@
18
18
  "README.md"
19
19
  ],
20
20
  "scripts": {
21
- "dev": "vite",
22
21
  "build": "vite build",
23
22
  "build:min": "vite build --mode production",
24
23
  "build:banner": "vite build --config vite.banner.config.ts",
@@ -26,10 +25,12 @@
26
25
  "build:all": "npm run build && npm run build:banner",
27
26
  "build:all:min": "npm run build:min && npm run build:banner:min",
28
27
  "types": "tsc --emitDeclarationOnly --outDir dist/types && cp src/jsx.d.ts dist/types/jsx.d.ts",
29
- "test": "npm run test:host-callback && npm run test:bundle-readiness && npm run test:bundle-runtime-cache && npm run test:jsx-types",
28
+ "test": "npm run test:host-callback && npm run test:bundle-readiness && npm run test:bundle-runtime-cache && npm run test:bundle-token-sync && npm run test:auth-bootstrap && npm run test:jsx-types",
30
29
  "test:host-callback": "rm -rf test/.tmp && tsc -p test/tsconfig.json && node test/.tmp/test/postmessage-host-callback.test.js",
31
30
  "test:bundle-readiness": "rm -rf test/.tmp && tsc -p test/tsconfig.json && node test/.tmp/test/bundle-readiness.test.js",
32
31
  "test:bundle-runtime-cache": "rm -rf test/.tmp && tsc -p test/tsconfig.json && node test/.tmp/test/bundle-runtime-cache.test.js",
32
+ "test:bundle-token-sync": "rm -rf test/.tmp && tsc -p test/tsconfig.json && node test/.tmp/test/bundle-token-sync.test.js",
33
+ "test:auth-bootstrap": "rm -rf test/.tmp && tsc -p test/tsconfig.json && node test/.tmp/test/auth-bootstrap.test.js",
33
34
  "test:jsx-types": "tsc --noEmit --strict --lib ES2020,DOM --skipLibCheck false src/jsx.d.ts",
34
35
  "coverage:repowise": "rm -rf coverage && npm run coverage:host-callback && npm run coverage:bundle-readiness && npm run test:jsx-types",
35
36
  "coverage:host-callback": "rm -rf test/.tmp && tsc -p test/tsconfig.json && c8 --reporter=lcov --reports-dir=coverage/host-callback --exclude-after-remap --exclude='**/*.test.ts' node test/.tmp/test/postmessage-host-callback.test.js && node scripts/name-lcov-test.mjs coverage/host-callback/lcov.info test/postmessage-host-callback.test.ts",
@@ -43,7 +44,7 @@
43
44
  "vite": "^5.1.0"
44
45
  },
45
46
  "dependencies": {
46
- "dompurify": "^3.4.9",
47
+ "dompurify": "^3.4.12",
47
48
  "lottie-web": "^5.13.0"
48
49
  }
49
50
  }