@final-commerce/command-frame 0.1.16 → 0.1.17

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.
Files changed (42) hide show
  1. package/README.md +42 -449
  2. package/dist/actions/add-cart-discount/mock.js +5 -1
  3. package/dist/actions/add-product-to-cart/mock.js +3 -1
  4. package/dist/actions/assign-customer/mock.js +3 -1
  5. package/dist/actions/cash-payment/mock.js +3 -1
  6. package/dist/actions/clear-cart/mock.js +3 -1
  7. package/dist/client.d.ts +4 -1
  8. package/dist/client.js +24 -4
  9. package/dist/demo/database.d.ts +4 -0
  10. package/dist/demo/database.js +26 -0
  11. package/dist/index.d.ts +4 -0
  12. package/dist/index.js +6 -0
  13. package/dist/projects/manage/client.d.ts +13 -0
  14. package/dist/projects/manage/client.js +13 -0
  15. package/dist/projects/manage/index.d.ts +4 -0
  16. package/dist/projects/manage/index.js +4 -0
  17. package/dist/projects/manage/mocks.d.ts +2 -0
  18. package/dist/projects/manage/mocks.js +6 -0
  19. package/dist/projects/manage/provider.d.ts +8 -0
  20. package/dist/projects/manage/provider.js +6 -0
  21. package/dist/projects/manage/types.d.ts +5 -0
  22. package/dist/projects/manage/types.js +1 -0
  23. package/dist/projects/render/client.d.ts +13 -0
  24. package/dist/projects/render/client.js +13 -0
  25. package/dist/projects/render/index.d.ts +4 -0
  26. package/dist/projects/render/index.js +4 -0
  27. package/dist/projects/render/mocks.d.ts +2 -0
  28. package/dist/projects/render/mocks.js +94 -0
  29. package/dist/projects/render/provider.d.ts +8 -0
  30. package/dist/projects/render/provider.js +6 -0
  31. package/dist/projects/render/types.d.ts +49 -0
  32. package/dist/projects/render/types.js +1 -0
  33. package/dist/provider.d.ts +19 -0
  34. package/dist/provider.js +178 -0
  35. package/dist/pubsub/subscriber.d.ts +4 -0
  36. package/dist/pubsub/subscriber.js +53 -5
  37. package/dist/pubsub/topics/types.d.ts +14 -0
  38. package/dist/pubsub/topics/types.js +1 -0
  39. package/dist/pubsub/types.d.ts +9 -0
  40. package/package.json +1 -1
  41. package/dist/demo/registry.d.ts +0 -5
  42. package/dist/demo/registry.js +0 -94
package/dist/client.js CHANGED
@@ -2,7 +2,6 @@
2
2
  * Command Frame Client for iframe communication
3
3
  * Allows the iframe to call functions on the parent window via postMessage
4
4
  */
5
- import { MOCK_REGISTRY } from "./demo/registry";
6
5
  export class CommandFrameClient {
7
6
  constructor(options = {}) {
8
7
  this.pendingRequests = new Map();
@@ -12,11 +11,20 @@ export class CommandFrameClient {
12
11
  this.useGlobalDebug = options.debug === undefined;
13
12
  // Default to provided mockMode or false. Detection happens via getFinalContext.
14
13
  this.mockMode = options.mockMode ?? false;
14
+ this.mockRegistry = options.mockRegistry || {};
15
+ // If running standalone (no parent window), force Mock Mode immediately
16
+ // This prevents the 2s delay and ensures immediate response in dev/standalone mode
17
+ if (typeof window !== 'undefined' && (!window.parent || window.parent === window)) {
18
+ if (this.isDebugEnabled()) {
19
+ console.log("[ActionsClient] Standalone mode detected. Enabling Mock Mode immediately.");
20
+ }
21
+ this.mockMode = true;
22
+ }
15
23
  if (typeof window !== 'undefined') {
16
24
  window.addEventListener("message", this.handleMessage.bind(this));
17
25
  }
18
26
  // Auto-detect mock mode on initialization
19
- this.getFinalContext().then((context) => {
27
+ this.detectContext().then((context) => {
20
28
  if (!context) {
21
29
  if (this.isDebugEnabled()) {
22
30
  console.warn("[ActionsClient] Environment detection failed (timeout or error). Switching to Mock Mode.");
@@ -31,6 +39,18 @@ export class CommandFrameClient {
31
39
  mockMode: this.mockMode
32
40
  });
33
41
  }
42
+ // Return a Proxy to enable dynamic method calls
43
+ // This allows client.getProducts() to map to client.call('getProducts')
44
+ return new Proxy(this, {
45
+ get: (target, prop) => {
46
+ // If the property exists on the instance, return it
47
+ if (prop in target) {
48
+ return target[prop];
49
+ }
50
+ // Otherwise, assume it's an action name and return a wrapper function
51
+ return (params) => target.call(prop, params);
52
+ }
53
+ });
34
54
  }
35
55
  isDebugEnabled() {
36
56
  if (!this.useGlobalDebug) {
@@ -44,7 +64,7 @@ export class CommandFrameClient {
44
64
  if (this.isDebugEnabled()) {
45
65
  console.log("[ActionsClient] Mock Call", { action, params });
46
66
  }
47
- const mockHandler = MOCK_REGISTRY[action];
67
+ const mockHandler = this.mockRegistry[action];
48
68
  if (mockHandler) {
49
69
  // Simulate async delay
50
70
  await new Promise(resolve => setTimeout(resolve, 100));
@@ -111,7 +131,7 @@ export class CommandFrameClient {
111
131
  });
112
132
  }
113
133
  // Private check to determine environment
114
- getFinalContext() {
134
+ detectContext() {
115
135
  return new Promise((resolve) => {
116
136
  if (typeof window === 'undefined' || !window.parent || window.parent === window)
117
137
  return resolve(null);
@@ -49,4 +49,8 @@ export declare const MOCK_OUTLET: CFActiveOutlet;
49
49
  export declare let MOCK_CART: CFActiveCart;
50
50
  export declare const resetMockCart: () => void;
51
51
  export declare const safeSerialize: <T>(data: T) => T;
52
+ type MockEventCallback = (event: any) => void;
53
+ export declare const mockPublishEvent: (topic: string, eventType: string, data: any) => void;
54
+ export declare const mockSubscribeToTopic: (topic: string, callback: MockEventCallback) => void;
52
55
  export declare const createOrderFromCart: (paymentType: string, amount: number | string, processor?: string) => CFActiveOrder;
56
+ export {};
@@ -467,6 +467,30 @@ export const resetMockCart = () => {
467
467
  export const safeSerialize = (data) => {
468
468
  return JSON.parse(JSON.stringify(data));
469
469
  };
470
+ const mockTopicSubscribers = {};
471
+ export const mockPublishEvent = (topic, eventType, data) => {
472
+ const subscribers = mockTopicSubscribers[topic] || [];
473
+ const event = {
474
+ topic,
475
+ type: eventType,
476
+ data,
477
+ timestamp: new Date().toISOString()
478
+ };
479
+ subscribers.forEach(callback => {
480
+ try {
481
+ callback(event);
482
+ }
483
+ catch (error) {
484
+ console.error(`[Mock] Error in topic callback for ${topic}:`, error);
485
+ }
486
+ });
487
+ };
488
+ export const mockSubscribeToTopic = (topic, callback) => {
489
+ if (!mockTopicSubscribers[topic]) {
490
+ mockTopicSubscribers[topic] = [];
491
+ }
492
+ mockTopicSubscribers[topic].push(callback);
493
+ };
470
494
  // Helper to create order from cart
471
495
  export const createOrderFromCart = (paymentType, amount, processor = "cash") => {
472
496
  // Generate new Order ID
@@ -545,5 +569,7 @@ export const createOrderFromCart = (paymentType, amount, processor = "cash") =>
545
569
  };
546
570
  MOCK_ORDERS.push(newOrder);
547
571
  resetMockCart();
572
+ // Publish cart-created event after cart is reset (simulates new empty cart)
573
+ mockPublishEvent('cart', 'cart-created', {});
548
574
  return newOrder;
549
575
  };
package/dist/index.d.ts CHANGED
@@ -93,6 +93,10 @@ export type { SwitchUser, SwitchUserParams, SwitchUserResponse } from "./actions
93
93
  export type { TriggerWebhook, TriggerWebhookPresetType, TriggerWebhookParams, TriggerWebhookResponse } from "./actions/trigger-webhook/types";
94
94
  export type { TriggerZapierWebhook, TriggerZapierWebhookParams, TriggerZapierWebhookResponse } from "./actions/trigger-zapier-webhook/types";
95
95
  export * from "./CommonTypes";
96
+ export { CommandFrameProvider } from "./provider";
97
+ export type { ActionHandler, ActionHandlers } from "./provider";
98
+ export * from "./projects/render";
99
+ export * from "./projects/manage";
96
100
  export { commandFrameClient, CommandFrameClient } from "./client";
97
101
  export type { PostMessageRequest, PostMessageResponse } from "./client";
98
102
  export { topics } from "./pubsub/topic";
package/dist/index.js CHANGED
@@ -107,6 +107,12 @@ export const command = {
107
107
  };
108
108
  // Export Common Types
109
109
  export * from "./CommonTypes";
110
+ // Export Provider
111
+ export { CommandFrameProvider } from "./provider";
112
+ // Export Render Project
113
+ export * from "./projects/render";
114
+ // Export Manage Project
115
+ export * from "./projects/manage";
110
116
  // Export client
111
117
  export { commandFrameClient, CommandFrameClient } from "./client";
112
118
  // Export Pub/Sub
@@ -0,0 +1,13 @@
1
+ import { CommandFrameClient } from "../../client";
2
+ import { ManageProviderActions } from "./types";
3
+ export interface ManageClient extends ManageProviderActions {
4
+ }
5
+ export declare class ManageClient extends CommandFrameClient {
6
+ constructor(options?: {
7
+ timeout?: number;
8
+ origin?: string;
9
+ debug?: boolean;
10
+ mockMode?: boolean;
11
+ });
12
+ }
13
+ export declare const manageClient: ManageClient;
@@ -0,0 +1,13 @@
1
+ import { CommandFrameClient } from "../../client";
2
+ import { MANAGE_MOCKS } from "./mocks";
3
+ export class ManageClient extends CommandFrameClient {
4
+ constructor(options = {}) {
5
+ super({
6
+ ...options,
7
+ mockRegistry: MANAGE_MOCKS
8
+ });
9
+ }
10
+ }
11
+ export const manageClient = new ManageClient({
12
+ debug: typeof window !== "undefined" && window.__POSTMESSAGE_DEBUG__ === true
13
+ });
@@ -0,0 +1,4 @@
1
+ export * from "./types";
2
+ export * from "./mocks";
3
+ export * from "./provider";
4
+ export * from "./client";
@@ -0,0 +1,4 @@
1
+ export * from "./types";
2
+ export * from "./mocks";
3
+ export * from "./provider";
4
+ export * from "./client";
@@ -0,0 +1,2 @@
1
+ import { ManageProviderActions } from "./types";
2
+ export declare const MANAGE_MOCKS: ManageProviderActions;
@@ -0,0 +1,6 @@
1
+ import { mockGetContext } from "../../actions/get-context/mock";
2
+ import { mockGetFinalContext } from "../../actions/get-final-context/mock";
3
+ export const MANAGE_MOCKS = {
4
+ getContext: mockGetContext,
5
+ getFinalContext: mockGetFinalContext
6
+ };
@@ -0,0 +1,8 @@
1
+ import { CommandFrameProvider } from "../../provider";
2
+ import { ManageProviderActions } from "./types";
3
+ export declare class ManageCommandFrameProvider extends CommandFrameProvider<ManageProviderActions> {
4
+ constructor(actions: ManageProviderActions, options?: {
5
+ origin?: string;
6
+ debug?: boolean;
7
+ });
8
+ }
@@ -0,0 +1,6 @@
1
+ import { CommandFrameProvider } from "../../provider";
2
+ export class ManageCommandFrameProvider extends CommandFrameProvider {
3
+ constructor(actions, options) {
4
+ super(actions, options);
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ import type { GetContext, GetFinalContext } from "../../index";
2
+ export interface ManageProviderActions {
3
+ getContext: GetContext;
4
+ getFinalContext: GetFinalContext;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { CommandFrameClient } from "../../client";
2
+ import { RenderProviderActions } from "./types";
3
+ export interface RenderClient extends RenderProviderActions {
4
+ }
5
+ export declare class RenderClient extends CommandFrameClient {
6
+ constructor(options?: {
7
+ timeout?: number;
8
+ origin?: string;
9
+ debug?: boolean;
10
+ mockMode?: boolean;
11
+ });
12
+ }
13
+ export declare const renderClient: RenderClient;
@@ -0,0 +1,13 @@
1
+ import { CommandFrameClient } from "../../client";
2
+ import { RENDER_MOCKS } from "./mocks";
3
+ export class RenderClient extends CommandFrameClient {
4
+ constructor(options = {}) {
5
+ super({
6
+ ...options,
7
+ mockRegistry: RENDER_MOCKS
8
+ });
9
+ }
10
+ }
11
+ export const renderClient = new RenderClient({
12
+ debug: typeof window !== "undefined" && window.__POSTMESSAGE_DEBUG__ === true
13
+ });
@@ -0,0 +1,4 @@
1
+ export * from "./types";
2
+ export * from "./mocks";
3
+ export * from "./provider";
4
+ export * from "./client";
@@ -0,0 +1,4 @@
1
+ export * from "./types";
2
+ export * from "./mocks";
3
+ export * from "./provider";
4
+ export * from "./client";
@@ -0,0 +1,2 @@
1
+ import { RenderProviderActions } from "./types";
2
+ export declare const RENDER_MOCKS: RenderProviderActions;
@@ -0,0 +1,94 @@
1
+ import { mockAddCartDiscount } from "../../actions/add-cart-discount/mock";
2
+ import { mockAddCartFee } from "../../actions/add-cart-fee/mock";
3
+ import { mockAddCustomSale } from "../../actions/add-custom-sale/mock";
4
+ import { mockAddCustomer } from "../../actions/add-customer/mock";
5
+ import { mockAddCustomerNote } from "../../actions/add-customer-note/mock";
6
+ import { mockAddOrderNote } from "../../actions/add-order-note/mock";
7
+ import { mockAddProductDiscount } from "../../actions/add-product-discount/mock";
8
+ import { mockAddProductFee } from "../../actions/add-product-fee/mock";
9
+ import { mockAddProductNote } from "../../actions/add-product-note/mock";
10
+ import { mockAddProductToCart } from "../../actions/add-product-to-cart/mock";
11
+ import { mockAdjustInventory } from "../../actions/adjust-inventory/mock";
12
+ import { mockAssignCustomer } from "../../actions/assign-customer/mock";
13
+ import { mockAuthenticateUser } from "../../actions/authenticate-user/mock";
14
+ import { mockCalculateRefundTotal } from "../../actions/calculate-refund-total/mock";
15
+ import { mockCashPayment } from "../../actions/cash-payment/mock";
16
+ import { mockClearCart } from "../../actions/clear-cart/mock";
17
+ import { mockDeleteParkedOrder } from "../../actions/delete-parked-order/mock";
18
+ import { mockExampleFunction } from "../../actions/example-function/mock";
19
+ import { mockGetCategories } from "../../actions/get-categories/mock";
20
+ import { mockGetContext } from "../../actions/get-context/mock";
21
+ import { mockGetCurrentCart } from "../../actions/get-current-cart/mock";
22
+ import { mockGetCustomers } from "../../actions/get-customers/mock";
23
+ import { mockGetOrders } from "../../actions/get-orders/mock";
24
+ import { mockGetProducts } from "../../actions/get-products/mock";
25
+ import { mockGetRefunds } from "../../actions/get-refunds/mock";
26
+ import { mockGetRemainingRefundableQuantities } from "../../actions/get-remaining-refundable-quantities/mock";
27
+ import { mockGoToStationHome } from "../../actions/go-to-station-home/mock";
28
+ import { mockInitiateRefund } from "../../actions/initiate-refund/mock";
29
+ import { mockOpenCashDrawer } from "../../actions/open-cash-drawer/mock";
30
+ import { mockParkOrder } from "../../actions/park-order/mock";
31
+ import { mockPartialPayment } from "../../actions/partial-payment/mock";
32
+ import { mockProcessPartialRefund } from "../../actions/process-partial-refund/mock";
33
+ import { mockRemoveCustomerFromCart } from "../../actions/remove-customer-from-cart/mock";
34
+ import { mockResetRefundDetails } from "../../actions/reset-refund-details/mock";
35
+ import { mockResumeParkedOrder } from "../../actions/resume-parked-order/mock";
36
+ import { mockSelectAllRefundItems } from "../../actions/select-all-refund-items/mock";
37
+ import { mockSetRefundStockAction } from "../../actions/set-refund-stock-action/mock";
38
+ import { mockShowConfirmation } from "../../actions/show-confirmation/mock";
39
+ import { mockShowNotification } from "../../actions/show-notification/mock";
40
+ import { mockSwitchUser } from "../../actions/switch-user/mock";
41
+ import { mockTapToPayPayment } from "../../actions/tap-to-pay-payment/mock";
42
+ import { mockTerminalPayment } from "../../actions/terminal-payment/mock";
43
+ import { mockTriggerWebhook } from "../../actions/trigger-webhook/mock";
44
+ import { mockTriggerZapierWebhook } from "../../actions/trigger-zapier-webhook/mock";
45
+ import { mockVendaraPayment } from "../../actions/vendara-payment/mock";
46
+ import { mockGetFinalContext } from "../../actions/get-final-context/mock";
47
+ export const RENDER_MOCKS = {
48
+ addCartDiscount: mockAddCartDiscount,
49
+ addCartFee: mockAddCartFee,
50
+ addCustomSale: mockAddCustomSale,
51
+ addCustomer: mockAddCustomer,
52
+ addCustomerNote: mockAddCustomerNote,
53
+ addOrderNote: mockAddOrderNote,
54
+ addProductDiscount: mockAddProductDiscount,
55
+ addProductFee: mockAddProductFee,
56
+ addProductNote: mockAddProductNote,
57
+ addProductToCart: mockAddProductToCart,
58
+ adjustInventory: mockAdjustInventory,
59
+ assignCustomer: mockAssignCustomer,
60
+ authenticateUser: mockAuthenticateUser,
61
+ calculateRefundTotal: mockCalculateRefundTotal,
62
+ cashPayment: mockCashPayment,
63
+ clearCart: mockClearCart,
64
+ deleteParkedOrder: mockDeleteParkedOrder,
65
+ exampleFunction: mockExampleFunction,
66
+ getCategories: mockGetCategories,
67
+ getContext: mockGetContext,
68
+ getCurrentCart: mockGetCurrentCart,
69
+ getCustomers: mockGetCustomers,
70
+ getOrders: mockGetOrders,
71
+ getProducts: mockGetProducts,
72
+ getRefunds: mockGetRefunds,
73
+ getRemainingRefundableQuantities: mockGetRemainingRefundableQuantities,
74
+ goToStationHome: mockGoToStationHome,
75
+ initiateRefund: mockInitiateRefund,
76
+ openCashDrawer: mockOpenCashDrawer,
77
+ parkOrder: mockParkOrder,
78
+ partialPayment: mockPartialPayment,
79
+ processPartialRefund: mockProcessPartialRefund,
80
+ removeCustomerFromCart: mockRemoveCustomerFromCart,
81
+ resetRefundDetails: mockResetRefundDetails,
82
+ resumeParkedOrder: mockResumeParkedOrder,
83
+ selectAllRefundItems: mockSelectAllRefundItems,
84
+ setRefundStockAction: mockSetRefundStockAction,
85
+ showConfirmation: mockShowConfirmation,
86
+ showNotification: mockShowNotification,
87
+ switchUser: mockSwitchUser,
88
+ tapToPayPayment: mockTapToPayPayment,
89
+ terminalPayment: mockTerminalPayment,
90
+ triggerWebhook: mockTriggerWebhook,
91
+ triggerZapierWebhook: mockTriggerZapierWebhook,
92
+ vendaraPayment: mockVendaraPayment,
93
+ getFinalContext: mockGetFinalContext,
94
+ };
@@ -0,0 +1,8 @@
1
+ import { CommandFrameProvider } from "../../provider";
2
+ import { RenderProviderActions } from "./types";
3
+ export declare class RenderCommandFrameProvider extends CommandFrameProvider<RenderProviderActions> {
4
+ constructor(actions: RenderProviderActions, options?: {
5
+ origin?: string;
6
+ debug?: boolean;
7
+ });
8
+ }
@@ -0,0 +1,6 @@
1
+ import { CommandFrameProvider } from "../../provider";
2
+ export class RenderCommandFrameProvider extends CommandFrameProvider {
3
+ constructor(actions, options) {
4
+ super(actions, options);
5
+ }
6
+ }
@@ -0,0 +1,49 @@
1
+ import type { ExampleFunction, GetProducts, AddCustomSale, GetCustomers, AssignCustomer, AddCustomer, GetCategories, GetOrders, GetRefunds, AddProductDiscount, AddProductToCart, AddCartDiscount, GetContext, GetFinalContext, AddProductNote, AddProductFee, AdjustInventory, AddOrderNote, AddCartFee, ClearCart, ParkOrder, ResumeParkedOrder, DeleteParkedOrder, InitiateRefund, CashPayment, TapToPayPayment, TerminalPayment, VendaraPayment, AddCustomerNote, RemoveCustomerFromCart, GoToStationHome, OpenCashDrawer, ShowNotification, ShowConfirmation, AuthenticateUser, PartialPayment, SwitchUser, TriggerWebhook, TriggerZapierWebhook, SetRefundStockAction, SelectAllRefundItems, ResetRefundDetails, CalculateRefundTotal, GetRemainingRefundableQuantities, ProcessPartialRefund, GetCurrentCart } from "../../index";
2
+ export interface RenderProviderActions {
3
+ exampleFunction: ExampleFunction;
4
+ getProducts: GetProducts;
5
+ addCustomSale: AddCustomSale;
6
+ getCustomers: GetCustomers;
7
+ assignCustomer: AssignCustomer;
8
+ addCustomer: AddCustomer;
9
+ getCategories: GetCategories;
10
+ getOrders: GetOrders;
11
+ getRefunds: GetRefunds;
12
+ addProductDiscount: AddProductDiscount;
13
+ addProductToCart: AddProductToCart;
14
+ addCartDiscount: AddCartDiscount;
15
+ getContext: GetContext;
16
+ getFinalContext: GetFinalContext;
17
+ addProductNote: AddProductNote;
18
+ addProductFee: AddProductFee;
19
+ adjustInventory: AdjustInventory;
20
+ addOrderNote: AddOrderNote;
21
+ addCartFee: AddCartFee;
22
+ clearCart: ClearCart;
23
+ parkOrder: ParkOrder;
24
+ resumeParkedOrder: ResumeParkedOrder;
25
+ deleteParkedOrder: DeleteParkedOrder;
26
+ initiateRefund: InitiateRefund;
27
+ cashPayment: CashPayment;
28
+ tapToPayPayment: TapToPayPayment;
29
+ terminalPayment: TerminalPayment;
30
+ vendaraPayment: VendaraPayment;
31
+ addCustomerNote: AddCustomerNote;
32
+ removeCustomerFromCart: RemoveCustomerFromCart;
33
+ goToStationHome: GoToStationHome;
34
+ openCashDrawer: OpenCashDrawer;
35
+ showNotification: ShowNotification;
36
+ showConfirmation: ShowConfirmation;
37
+ authenticateUser: AuthenticateUser;
38
+ partialPayment: PartialPayment;
39
+ switchUser: SwitchUser;
40
+ triggerWebhook: TriggerWebhook;
41
+ triggerZapierWebhook: TriggerZapierWebhook;
42
+ setRefundStockAction: SetRefundStockAction;
43
+ selectAllRefundItems: SelectAllRefundItems;
44
+ resetRefundDetails: ResetRefundDetails;
45
+ calculateRefundTotal: CalculateRefundTotal;
46
+ getRemainingRefundableQuantities: GetRemainingRefundableQuantities;
47
+ processPartialRefund: ProcessPartialRefund;
48
+ getCurrentCart: GetCurrentCart;
49
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ export type ActionHandler<TParams = any, TResponse = any> = (params: TParams) => Promise<TResponse> | TResponse;
2
+ export type ActionHandlers = Map<string, ActionHandler>;
3
+ export declare class CommandFrameProvider<TActions extends object = any> {
4
+ private handlers;
5
+ private origin;
6
+ private debug;
7
+ private destroyed;
8
+ private boundHandleMessage;
9
+ constructor(actions?: TActions, options?: {
10
+ origin?: string;
11
+ debug?: boolean;
12
+ });
13
+ register<TParams = any, TResponse = any>(action: string, handler: ActionHandler<TParams, TResponse>): void;
14
+ unregister(action: string): void;
15
+ private handleMessage;
16
+ private sendResponse;
17
+ destroy(): void;
18
+ isDestroyed(): boolean;
19
+ }
@@ -0,0 +1,178 @@
1
+ export class CommandFrameProvider {
2
+ constructor(actions, options = {}) {
3
+ this.handlers = new Map();
4
+ this.destroyed = false;
5
+ this.origin = options.origin || "*";
6
+ this.debug = options.debug || false;
7
+ this.boundHandleMessage = this.handleMessage.bind(this);
8
+ if (typeof window !== "undefined") {
9
+ window.addEventListener("message", this.boundHandleMessage);
10
+ }
11
+ if (actions) {
12
+ Object.keys(actions).forEach((actionName) => {
13
+ const handler = actions[actionName];
14
+ if (typeof handler === "function") {
15
+ this.register(actionName, handler);
16
+ }
17
+ });
18
+ }
19
+ if (this.debug) {
20
+ console.log("[CommandFrameProvider] Initialized", {
21
+ origin: this.origin,
22
+ debug: this.debug
23
+ });
24
+ }
25
+ }
26
+ register(action, handler) {
27
+ if (this.destroyed) {
28
+ if (this.debug) {
29
+ console.warn("[CommandFrameProvider] Cannot register action after destruction", { action });
30
+ }
31
+ return;
32
+ }
33
+ this.handlers.set(action, handler);
34
+ if (this.debug) {
35
+ console.log("[CommandFrameProvider] Registered action", { action });
36
+ }
37
+ }
38
+ unregister(action) {
39
+ if (this.destroyed) {
40
+ if (this.debug) {
41
+ console.warn("[CommandFrameProvider] Cannot unregister action after destruction", { action });
42
+ }
43
+ return;
44
+ }
45
+ this.handlers.delete(action);
46
+ if (this.debug) {
47
+ console.log("[CommandFrameProvider] Unregistered action", { action });
48
+ }
49
+ }
50
+ async handleMessage(event) {
51
+ // Ignore messages after destruction
52
+ if (this.destroyed) {
53
+ return;
54
+ }
55
+ const request = event.data;
56
+ if (!request || typeof request !== "object" || !("action" in request)) {
57
+ return;
58
+ }
59
+ if (this.origin !== "*" && event.origin !== this.origin) {
60
+ if (this.debug) {
61
+ console.warn("[CommandFrameProvider] Origin mismatch", {
62
+ expected: this.origin,
63
+ received: event.origin
64
+ });
65
+ }
66
+ return;
67
+ }
68
+ if (!request.action || !request.requestId) {
69
+ if (this.debug) {
70
+ console.warn("[CommandFrameProvider] Invalid request format", {
71
+ data: event.data,
72
+ origin: event.origin
73
+ });
74
+ }
75
+ return;
76
+ }
77
+ if (this.debug) {
78
+ console.log("[CommandFrameProvider] Received request", {
79
+ origin: event.origin,
80
+ action: request.action,
81
+ requestId: request.requestId,
82
+ timestamp: new Date().toISOString()
83
+ });
84
+ }
85
+ const handler = this.handlers.get(request.action);
86
+ if (!handler) {
87
+ if (this.debug) {
88
+ console.warn("[CommandFrameProvider] Unknown action", { action: request.action });
89
+ }
90
+ // Only send response if source window is still available
91
+ if (event.source && event.source !== window) {
92
+ this.sendResponse(event.source, request.requestId, {
93
+ success: false,
94
+ error: `Unknown action: ${request.action}`
95
+ });
96
+ }
97
+ return;
98
+ }
99
+ try {
100
+ const result = await handler(request.params);
101
+ // Only send response if source window is still available
102
+ if (event.source && event.source !== window) {
103
+ this.sendResponse(event.source, request.requestId, {
104
+ success: true,
105
+ data: result
106
+ });
107
+ if (this.debug) {
108
+ console.log("[CommandFrameProvider] Action executed", {
109
+ action: request.action,
110
+ requestId: request.requestId
111
+ });
112
+ }
113
+ }
114
+ else if (this.debug) {
115
+ console.warn("[CommandFrameProvider] Cannot send response - source window unavailable", {
116
+ action: request.action,
117
+ requestId: request.requestId
118
+ });
119
+ }
120
+ }
121
+ catch (error) {
122
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
123
+ // Only send response if source window is still available
124
+ if (event.source && event.source !== window) {
125
+ this.sendResponse(event.source, request.requestId, {
126
+ success: false,
127
+ error: errorMessage
128
+ });
129
+ if (this.debug) {
130
+ console.error("[CommandFrameProvider] Action failed", {
131
+ action: request.action,
132
+ requestId: request.requestId,
133
+ error: errorMessage
134
+ });
135
+ }
136
+ }
137
+ else if (this.debug) {
138
+ console.warn("[CommandFrameProvider] Cannot send error response - source window unavailable", {
139
+ action: request.action,
140
+ requestId: request.requestId,
141
+ error: errorMessage
142
+ });
143
+ }
144
+ }
145
+ }
146
+ sendResponse(target, requestId, response) {
147
+ const message = {
148
+ requestId,
149
+ ...response
150
+ };
151
+ if (this.debug) {
152
+ console.log("[CommandFrameProvider] Sending response", {
153
+ requestId,
154
+ success: response.success
155
+ });
156
+ }
157
+ target.postMessage(message, this.origin);
158
+ }
159
+ destroy() {
160
+ if (this.destroyed) {
161
+ if (this.debug) {
162
+ console.warn("[CommandFrameProvider] Already destroyed");
163
+ }
164
+ return;
165
+ }
166
+ this.destroyed = true;
167
+ this.handlers.clear();
168
+ if (typeof window !== "undefined") {
169
+ window.removeEventListener("message", this.boundHandleMessage);
170
+ }
171
+ if (this.debug) {
172
+ console.log("[CommandFrameProvider] Destroyed");
173
+ }
174
+ }
175
+ isDestroyed() {
176
+ return this.destroyed;
177
+ }
178
+ }
@@ -11,10 +11,14 @@ export declare class TopicSubscriber {
11
11
  private useGlobalDebug;
12
12
  private boundHandleMessage;
13
13
  private subscriptionIdCounter;
14
+ private mockMode;
15
+ private detectionTimeout;
14
16
  constructor(options?: {
15
17
  origin?: string;
16
18
  debug?: boolean;
17
19
  });
20
+ private initDetection;
21
+ private switchToMockMode;
18
22
  private isDebugEnabled;
19
23
  /**
20
24
  * Request the list of available topics from the host