@tagadapay/plugin-sdk 3.0.1 → 3.0.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.
@@ -4,6 +4,7 @@ import { Environment, EnvironmentConfig, Session, AuthState, Customer, Store, Lo
4
4
  import { FunnelClient } from './funnelClient';
5
5
  export interface TagadaClientConfig {
6
6
  environment?: Environment;
7
+ customApiConfig?: Partial<EnvironmentConfig>;
7
8
  debugMode?: boolean;
8
9
  localConfig?: string;
9
10
  rawPluginConfig?: RawPluginConfig;
@@ -24,7 +24,21 @@ export class TagadaClient {
24
24
  }
25
25
  // Initialize default state
26
26
  const env = this.resolveEnvironment();
27
- const envConfig = getEnvironmentConfig(env);
27
+ let envConfig = getEnvironmentConfig(env);
28
+ // Apply custom API config if provided
29
+ if (config.customApiConfig) {
30
+ envConfig = {
31
+ ...envConfig,
32
+ ...config.customApiConfig,
33
+ apiConfig: {
34
+ ...envConfig.apiConfig,
35
+ ...config.customApiConfig.apiConfig,
36
+ },
37
+ };
38
+ if (this.config.debugMode) {
39
+ console.log(`[TagadaClient ${this.instanceId}] Applied custom API config:`, envConfig.apiConfig.baseUrl);
40
+ }
41
+ }
28
42
  this.state = {
29
43
  auth: {
30
44
  isAuthenticated: false,
@@ -24,6 +24,7 @@ export type { StoreConfig } from './core/resources/storeConfig';
24
24
  export { FunnelActionType } from './core/resources/funnel';
25
25
  export type { BackNavigationActionData, CartUpdatedActionData, DirectNavigationActionData, FormSubmitActionData, FunnelContextUpdateRequest, FunnelContextUpdateResponse, FunnelAction as FunnelEvent, FunnelInitializeRequest, FunnelInitializeResponse, FunnelNavigateRequest, FunnelNavigateResponse, FunnelNavigationAction, FunnelNavigationResult, NextAction, OfferAcceptedActionData, OfferDeclinedActionData, PaymentFailedActionData, PaymentSuccessActionData, SimpleFunnelContext } from './core/resources/funnel';
26
26
  export { ApplePayButton, ExpressPaymentMethodsProvider, formatMoney, getAvailableLanguages, GooglePayButton, queryKeys, TagadaProvider, useApiMutation, useApiQuery, useAuth, useCheckout, useCheckoutToken, useClubOffers, useCountryOptions, useCredits, useCurrency, useCustomer, useCustomerInfos, useCustomerOrders, useCustomerSubscriptions, useDiscounts, useExpressPaymentMethods, useFunnel, useFunnelLegacy, useGeoLocation, useGoogleAutocomplete, useInvalidateQuery, useISOData, useLanguageImport, useLogin, useOffer, useOrder, useOrderBump, usePayment, usePluginConfig, usePostPurchases, usePreloadQuery, useProducts, usePromotions, useRegionOptions, useRemappableParams, useShippingRates, useSimpleFunnel, useStoreConfig, useTagadaContext, useThreeds, useThreedsModal, useTranslation, useVipOffers } from './react';
27
+ export type { DebugScript } from './react';
27
28
  export type { TranslateFunction, TranslationText, UseTranslationOptions, UseTranslationResult } from './react/hooks/useTranslation';
28
29
  export type { FunnelContextValue } from './react/hooks/useFunnel';
29
30
  export type { ClubOffer, ClubOfferItem, ClubOfferLineItem, ClubOfferSummary, UseClubOffersOptions, UseClubOffersResult } from './react/hooks/useClubOffers';
@@ -77,6 +77,8 @@ export const DebugDrawer = ({ isOpen, onClose }) => {
77
77
  const context = useTagadaContext();
78
78
  const pluginConfig = usePluginConfig();
79
79
  const [activeTab, setActiveTab] = useState('overview');
80
+ const [runningScripts, setRunningScripts] = useState(new Set());
81
+ const [scriptResults, setScriptResults] = useState({});
80
82
  // Handler to jump to a specific step using direct_navigation
81
83
  const handleJumpToStep = async (stepId, stepName) => {
82
84
  // Try to get sessionId from debug data or context
@@ -168,7 +170,49 @@ export const DebugDrawer = ({ isOpen, onClose }) => {
168
170
  const itemsTab = context.debugCheckout.isActive && context.debugCheckout.data?.checkout?.summary
169
171
  ? [{ id: 'items', label: 'Items' }]
170
172
  : [];
171
- const tabs = [...baseTabs, ...funnelTab, ...checkoutTab, ...itemsTab, { id: 'raw', label: 'Raw Data' }];
173
+ // Add scripts tab if debug scripts are available
174
+ const scriptsTab = context.debugScripts && context.debugScripts.length > 0
175
+ ? [{ id: 'scripts', label: `Scripts (${context.debugScripts.length})` }]
176
+ : [];
177
+ const tabs = [
178
+ ...baseTabs,
179
+ ...funnelTab,
180
+ ...checkoutTab,
181
+ ...itemsTab,
182
+ ...scriptsTab,
183
+ { id: 'raw', label: 'Raw Data' },
184
+ ];
185
+ // Handler to run a debug script
186
+ const handleRunScript = async (scriptId) => {
187
+ const script = context.debugScripts?.find((s) => s.id === scriptId);
188
+ if (!script)
189
+ return;
190
+ setRunningScripts((prev) => new Set(prev).add(scriptId));
191
+ setScriptResults((prev) => ({ ...prev, [scriptId]: undefined }));
192
+ try {
193
+ await script.run(context);
194
+ setScriptResults((prev) => ({
195
+ ...prev,
196
+ [scriptId]: { success: true, message: 'Script executed successfully' },
197
+ }));
198
+ }
199
+ catch (error) {
200
+ setScriptResults((prev) => ({
201
+ ...prev,
202
+ [scriptId]: {
203
+ success: false,
204
+ message: error instanceof Error ? error.message : 'Script failed',
205
+ },
206
+ }));
207
+ }
208
+ finally {
209
+ setRunningScripts((prev) => {
210
+ const next = new Set(prev);
211
+ next.delete(scriptId);
212
+ return next;
213
+ });
214
+ }
215
+ };
172
216
  return (_jsxs(_Fragment, { children: [_jsx("div", { style: {
173
217
  position: 'fixed',
174
218
  top: 0,
@@ -681,6 +725,59 @@ export const DebugDrawer = ({ isOpen, onClose }) => {
681
725
  fontSize: '11px',
682
726
  margin: 0,
683
727
  whiteSpace: 'pre-wrap',
684
- }, children: context.debugCheckout.error.stack }))] })] })), _jsxs("div", { children: [_jsx("h4", { style: { margin: '0 0 12px 0', color: '#60a5fa' }, children: "Full Checkout Data" }), _jsx(TreeView, { data: context.debugCheckout.data, name: "checkoutData" })] })] })) : (_jsx("p", { style: { color: '#6b7280' }, children: "No checkout hook active" }))] })), activeTab === 'raw' && (_jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 16px 0', color: '#60a5fa' }, children: "Raw Context Data" }), _jsx("div", { style: { fontSize: '12px' }, children: _jsx(TreeView, { data: context, name: "tagadaContext", maxLevel: 4 }) })] }))] })] })] }));
728
+ }, children: context.debugCheckout.error.stack }))] })] })), _jsxs("div", { children: [_jsx("h4", { style: { margin: '0 0 12px 0', color: '#60a5fa' }, children: "Full Checkout Data" }), _jsx(TreeView, { data: context.debugCheckout.data, name: "checkoutData" })] })] })) : (_jsx("p", { style: { color: '#6b7280' }, children: "No checkout hook active" }))] })), activeTab === 'scripts' && (_jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 12px 0', color: '#60a5fa', fontSize: '14px' }, children: "\uD83D\uDEE0\uFE0F Debug Scripts" }), _jsx("p", { style: { margin: '0 0 16px 0', fontSize: '11px', color: '#9ca3af' }, children: "Template-provided scripts for debugging and testing. Click a script to run it." }), context.debugScripts && context.debugScripts.length > 0 ? (_jsx("div", { style: { display: 'grid', gap: '10px' }, children: (() => {
729
+ const grouped = context.debugScripts.reduce((acc, script) => {
730
+ const category = script.category || 'General';
731
+ if (!acc[category])
732
+ acc[category] = [];
733
+ acc[category].push(script);
734
+ return acc;
735
+ }, {});
736
+ return Object.entries(grouped).map(([category, scripts]) => (_jsxs("div", { children: [_jsx("div", { style: {
737
+ fontSize: '11px',
738
+ color: '#9ca3af',
739
+ fontWeight: 'bold',
740
+ marginBottom: '8px',
741
+ textTransform: 'uppercase',
742
+ letterSpacing: '0.5px',
743
+ }, children: category }), _jsx("div", { style: { display: 'grid', gap: '8px' }, children: scripts.map((script) => {
744
+ const isRunning = runningScripts.has(script.id);
745
+ const result = scriptResults[script.id];
746
+ return (_jsxs("div", { style: {
747
+ border: '1px solid #374151',
748
+ borderRadius: '6px',
749
+ padding: '10px 12px',
750
+ backgroundColor: '#111827',
751
+ }, children: [_jsxs("div", { style: {
752
+ display: 'flex',
753
+ justifyContent: 'space-between',
754
+ alignItems: 'flex-start',
755
+ gap: '12px',
756
+ }, children: [_jsxs("div", { style: { flex: 1 }, children: [_jsx("div", { style: {
757
+ color: '#f9fafb',
758
+ fontWeight: 'bold',
759
+ fontSize: '12px',
760
+ marginBottom: '4px',
761
+ }, children: script.name }), script.description && (_jsx("div", { style: { fontSize: '11px', color: '#9ca3af' }, children: script.description }))] }), _jsx("button", { onClick: () => handleRunScript(script.id), disabled: isRunning, style: {
762
+ backgroundColor: isRunning ? '#374151' : '#3b82f6',
763
+ color: '#fff',
764
+ border: 'none',
765
+ borderRadius: '4px',
766
+ padding: '6px 12px',
767
+ fontSize: '11px',
768
+ fontWeight: 'bold',
769
+ cursor: isRunning ? 'not-allowed' : 'pointer',
770
+ transition: 'background-color 0.2s',
771
+ minWidth: '60px',
772
+ }, children: isRunning ? '⏳' : '▶ Run' })] }), result && (_jsxs("div", { style: {
773
+ marginTop: '8px',
774
+ padding: '6px 8px',
775
+ borderRadius: '4px',
776
+ fontSize: '10px',
777
+ backgroundColor: result.success ? '#065f46' : '#7f1d1d',
778
+ color: result.success ? '#6ee7b7' : '#fca5a5',
779
+ }, children: [result.success ? '✅' : '❌', " ", result.message] }))] }, script.id));
780
+ }) })] }, category)));
781
+ })() })) : (_jsx("p", { style: { color: '#6b7280' }, children: "No debug scripts available" }))] })), activeTab === 'raw' && (_jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 16px 0', color: '#60a5fa' }, children: "Raw Context Data" }), _jsx("div", { style: { fontSize: '12px' }, children: _jsx(TreeView, { data: context, name: "tagadaContext", maxLevel: 4 }) })] }))] })] })] }));
685
782
  };
686
783
  export default DebugDrawer;
@@ -4,6 +4,7 @@
4
4
  */
5
5
  export { ExpressPaymentMethodsProvider } from './providers/ExpressPaymentMethodsProvider';
6
6
  export { TagadaProvider, useTagadaContext } from './providers/TagadaProvider';
7
+ export type { DebugScript } from './providers/TagadaProvider';
7
8
  export { ApplePayButton } from './components/ApplePayButton';
8
9
  export { GooglePayButton } from './components/GooglePayButton';
9
10
  export { useAuth } from './hooks/useAuth';
@@ -1,11 +1,27 @@
1
1
  import { ReactNode } from 'react';
2
- import { TagadaClient, TagadaState, TagadaClientConfig } from '../../core/client';
3
- import { RawPluginConfig } from '../../core/utils/pluginConfig';
4
- import { Environment, EnvironmentConfig } from '../../core/types';
5
2
  import { ApiService } from '../../../react/services/apiService';
6
- import { formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, convertCurrency } from '../../../react/utils/money';
7
- import { FunnelAction, FunnelNavigationResult, SimpleFunnelContext } from '../../core/resources/funnel';
3
+ import { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits } from '../../../react/utils/money';
4
+ import { TagadaClient, TagadaClientConfig, TagadaState } from '../../core/client';
8
5
  import { FunnelState } from '../../core/funnelClient';
6
+ import { FunnelAction, FunnelNavigationResult, SimpleFunnelContext } from '../../core/resources/funnel';
7
+ import { Environment, EnvironmentConfig } from '../../core/types';
8
+ import { RawPluginConfig } from '../../core/utils/pluginConfig';
9
+ /**
10
+ * Debug Script Definition
11
+ * Templates can provide debug scripts that will appear in the Debug Drawer "Scripts" tab
12
+ */
13
+ export interface DebugScript {
14
+ /** Unique identifier for the script */
15
+ id: string;
16
+ /** Display name shown in the Scripts tab */
17
+ name: string;
18
+ /** Optional description of what the script does */
19
+ description?: string;
20
+ /** The function to execute when the script is run */
21
+ run: (context: TagadaContextValue) => void | Promise<void>;
22
+ /** Optional category to group scripts */
23
+ category?: string;
24
+ }
9
25
  interface TagadaContextValue extends TagadaState {
10
26
  client: TagadaClient;
11
27
  apiService: ApiService;
@@ -38,6 +54,7 @@ interface TagadaContextValue extends TagadaState {
38
54
  lastUpdated: Date | null;
39
55
  };
40
56
  updateFunnelDebugData: (data: any, error?: Error | null, isLoading?: boolean) => void;
57
+ debugScripts: DebugScript[];
41
58
  refreshCoordinator: {
42
59
  registerCheckoutRefresh: (refreshFn: () => Promise<void>, name?: string) => void;
43
60
  registerOrderBumpRefresh: (refreshFn: () => Promise<void>) => void;
@@ -91,8 +108,13 @@ interface TagadaProviderProps {
91
108
  * Callback fired on funnel errors
92
109
  */
93
110
  onFunnelError?: (error: Error) => void;
111
+ /**
112
+ * Debug scripts provided by the template.
113
+ * These will appear in the Debug Drawer "Scripts" tab when debugMode is enabled.
114
+ * Only shown if the array is not empty.
115
+ */
116
+ debugScripts?: DebugScript[];
94
117
  }
95
- export declare function TagadaProvider({ children, environment, customApiConfig, // Ignored for now in TagadaClient, or need to add support
96
- debugMode, localConfig, blockUntilSessionReady, rawPluginConfig, features, funnelId, autoInitializeFunnel, onNavigate, onFunnelError, }: TagadaProviderProps): import("react/jsx-runtime").JSX.Element;
118
+ export declare function TagadaProvider({ children, environment, customApiConfig, debugMode, localConfig, blockUntilSessionReady, rawPluginConfig, features, funnelId, autoInitializeFunnel, onNavigate, onFunnelError, debugScripts, }: TagadaProviderProps): import("react/jsx-runtime").JSX.Element;
97
119
  export declare function useTagadaContext(): TagadaContextValue;
98
120
  export {};
@@ -4,10 +4,10 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
4
4
  * TagadaProvider - Main provider component for the Tagada Pay React SDK
5
5
  */
6
6
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
7
- import { createContext, useCallback, useContext, useEffect, useMemo, useState, useRef, } from 'react';
8
- import { TagadaClient } from '../../core/client';
7
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, } from 'react';
9
8
  import { ApiService } from '../../../react/services/apiService';
10
- import { formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, convertCurrency, } from '../../../react/utils/money';
9
+ import { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, } from '../../../react/utils/money';
10
+ import { TagadaClient } from '../../core/client';
11
11
  import { default as DebugDrawer } from '../components/DebugDrawer';
12
12
  import { setGlobalApiClient } from '../hooks/useApiQuery';
13
13
  // Professional, subtle loading component for initialization
@@ -43,8 +43,7 @@ const InitializationLoader = () => (_jsxs("div", { style: {
43
43
  }
44
44
  ` })] }));
45
45
  const TagadaContext = createContext(null);
46
- export function TagadaProvider({ children, environment, customApiConfig, // Ignored for now in TagadaClient, or need to add support
47
- debugMode, localConfig, blockUntilSessionReady = false, rawPluginConfig, features, funnelId, autoInitializeFunnel = true, onNavigate, onFunnelError, }) {
46
+ export function TagadaProvider({ children, environment, customApiConfig, debugMode, localConfig, blockUntilSessionReady = false, rawPluginConfig, features, funnelId, autoInitializeFunnel = true, onNavigate, onFunnelError, debugScripts = [], }) {
48
47
  // Auto-detect debug mode from environment if not explicitly set
49
48
  const effectiveDebugMode = debugMode !== undefined
50
49
  ? debugMode
@@ -54,6 +53,7 @@ debugMode, localConfig, blockUntilSessionReady = false, rawPluginConfig, feature
54
53
  const client = useMemo(() => {
55
54
  const config = {
56
55
  environment,
56
+ customApiConfig,
57
57
  debugMode: effectiveDebugMode,
58
58
  localConfig,
59
59
  rawPluginConfig,
@@ -280,6 +280,7 @@ debugMode, localConfig, blockUntilSessionReady = false, rawPluginConfig, feature
280
280
  updateCheckoutDebugData,
281
281
  debugFunnel,
282
282
  updateFunnelDebugData,
283
+ debugScripts,
283
284
  refreshCoordinator,
284
285
  money: moneyUtils,
285
286
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",