@marvalt/madapter 1.1.0 → 2.1.0

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/index.d.ts CHANGED
@@ -18,15 +18,21 @@ import * as _tanstack_react_query from '@tanstack/react-query';
18
18
  * See the GNU General Public License for more details.
19
19
  */
20
20
  type AuthMode = 'cloudflare_proxy' | 'direct';
21
+ /**
22
+ * Mautic client configuration (for runtime/browser use)
23
+ * Convention-based: Uses /api/mautic-submit by default
24
+ */
21
25
  interface MauticConfig {
22
- authMode: AuthMode;
26
+ /** Mautic instance URL (e.g., https://your-mautic.com) */
23
27
  apiUrl?: string;
24
- cloudflareWorkerUrl?: string;
25
- clientId?: string;
26
- clientSecret?: string;
27
- cfAccessClientId?: string;
28
- cfAccessClientSecret?: string;
28
+ /**
29
+ * Proxy endpoint for server-side authentication
30
+ * @default '/api/mautic-submit'
31
+ */
32
+ proxyEndpoint?: string;
33
+ /** Request timeout in milliseconds */
29
34
  timeout?: number;
35
+ /** Number of retry attempts */
30
36
  retries?: number;
31
37
  }
32
38
  interface MauticGeneratorConfig {
@@ -175,16 +181,9 @@ interface MauticApiResponse<T = any> {
175
181
  */
176
182
 
177
183
  declare class MauticClient {
178
- private baseUrl;
179
- private proxyUrl?;
180
- private appId?;
181
- private workerSecret?;
182
- private useProxy;
183
- private isConfigured;
184
+ private proxyEndpoint;
184
185
  private config;
185
186
  constructor(config: MauticConfig);
186
- private validateConfiguration;
187
- private getAuthHeaders;
188
187
  private makeRequest;
189
188
  /**
190
189
  * Submit a form to Mautic
@@ -684,6 +683,7 @@ declare const getDataMetadata: () => {
684
683
 
685
684
  /**
686
685
  * Create Mautic configuration from environment variables
686
+ * Convention-based: Uses /api/mautic-submit by default
687
687
  */
688
688
  declare const createMauticConfig: (overrides?: Partial<MauticConfig>) => MauticConfig;
689
689
  /**
@@ -709,9 +709,8 @@ declare const isMauticEnabled: (config: MauticConfig) => boolean;
709
709
  * Get Mautic configuration summary (for debugging)
710
710
  */
711
711
  declare const getMauticConfigSummary: (config: MauticConfig) => {
712
- authMode: AuthMode;
713
712
  hasApiUrl: boolean;
714
- hasCloudflareWorkerUrl: boolean;
713
+ proxyEndpoint: string;
715
714
  timeout: number | undefined;
716
715
  retries: number | undefined;
717
716
  isEnabled: boolean;
package/dist/index.esm.js CHANGED
@@ -21,58 +21,17 @@ import { useQueryClient, useMutation, useQuery } from '@tanstack/react-query';
21
21
  */
22
22
  class MauticClient {
23
23
  constructor(config) {
24
- this.baseUrl = '';
25
24
  this.config = config;
26
- if (config.authMode === 'cloudflare_proxy' && config.cloudflareWorkerUrl) {
27
- // Cloudflare Worker proxy (recommended - uses Secrets Store)
28
- this.proxyUrl = config.cloudflareWorkerUrl;
29
- this.useProxy = true;
30
- this.isConfigured = true;
31
- }
32
- else if (config.authMode === 'direct' && config.apiUrl) {
33
- // Direct Mautic API access (legacy - for local development only)
34
- this.baseUrl = config.apiUrl || '';
35
- this.useProxy = false;
36
- this.isConfigured = true;
37
- }
38
- else {
39
- // Fallback or error state
40
- this.useProxy = false;
41
- this.isConfigured = false;
42
- }
43
- }
44
- validateConfiguration() {
45
- if (!this.isConfigured) {
46
- throw new Error('Mautic service not properly configured. Check your configuration.');
47
- }
25
+ // Convention-based: Always use /api/mautic-submit unless explicitly overridden
26
+ this.proxyEndpoint = config.proxyEndpoint || '/api/mautic-submit';
48
27
  }
49
- getAuthHeaders() { return {}; }
50
28
  async makeRequest(endpoint, options = {}) {
51
- this.validateConfiguration();
52
- let url;
53
- if (this.useProxy) {
54
- // Use Cloudflare Worker proxy
55
- const proxyEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
56
- url = `${this.proxyUrl}?endpoint=${encodeURIComponent(proxyEndpoint)}`;
57
- }
58
- else {
59
- // Use direct Mautic API
60
- // Determine if this is a form submission endpoint (should not use /api prefix)
61
- const isFormSubmission = endpoint.startsWith('/form/submit') ||
62
- (endpoint.startsWith('/forms/') && endpoint.includes('/submit'));
63
- if (isFormSubmission) {
64
- // Form submissions go directly to the form endpoint, not the API
65
- url = `${this.baseUrl}${endpoint}`;
66
- }
67
- else {
68
- // API calls use the /api prefix
69
- url = `${this.baseUrl}/api${endpoint}`;
70
- }
71
- }
29
+ // Always use proxy endpoint (convention-based)
30
+ const proxyEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
31
+ const url = `${this.proxyEndpoint}?endpoint=${encodeURIComponent(proxyEndpoint)}`;
72
32
  const isFormSubmission = endpoint.startsWith('/form/submit') ||
73
33
  (endpoint.startsWith('/forms/') && endpoint.includes('/submit'));
74
34
  const headers = {
75
- // no app headers in proxy mode
76
35
  ...options.headers,
77
36
  };
78
37
  // Default JSON only for non-form submissions when a body exists and no explicit content-type provided
@@ -276,6 +235,7 @@ class MauticClient {
276
235
  */
277
236
  /**
278
237
  * Create Mautic configuration from environment variables
238
+ * Convention-based: Uses /api/mautic-submit by default
279
239
  */
280
240
  const createMauticConfig = (overrides = {}) => {
281
241
  // Use import.meta.env for Vite compatibility in browser
@@ -302,26 +262,12 @@ const createMauticConfig = (overrides = {}) => {
302
262
  return undefined;
303
263
  };
304
264
  const merged = {
305
- authMode: getEnvVar('VITE_AUTH_MODE') || getEnvVar('VITE_MAUTIC_AUTH_MODE') || 'cloudflare_proxy',
306
265
  apiUrl: getEnvVar('VITE_MAUTIC_URL'),
307
- cloudflareWorkerUrl: getEnvVar('VITE_MAUTIC_PROXY_URL'),
266
+ proxyEndpoint: getEnvVar('VITE_MAUTIC_PROXY_ENDPOINT') || '/api/mautic-submit',
308
267
  timeout: parseInt(getEnvVar('VITE_MAUTIC_TIMEOUT') || '30000'),
309
268
  retries: parseInt(getEnvVar('VITE_MAUTIC_RETRIES') || '3'),
310
269
  ...overrides,
311
270
  };
312
- // Enforce required fields depending on authMode
313
- const errors = [];
314
- if (merged.authMode === 'cloudflare_proxy') {
315
- if (!merged.cloudflareWorkerUrl)
316
- errors.push('cloudflareWorkerUrl is required for cloudflare_proxy mode');
317
- }
318
- if (merged.authMode === 'direct') {
319
- if (!merged.apiUrl)
320
- errors.push('apiUrl is required for direct mode');
321
- }
322
- if (errors.length) {
323
- throw new Error(`Invalid Mautic configuration: ${errors.join('; ')}`);
324
- }
325
271
  return merged;
326
272
  };
327
273
  /**
@@ -329,19 +275,6 @@ const createMauticConfig = (overrides = {}) => {
329
275
  */
330
276
  const validateMauticConfig = (config) => {
331
277
  const errors = [];
332
- if (!config.authMode) {
333
- errors.push('authMode is required');
334
- }
335
- if (config.authMode === 'cloudflare_proxy') {
336
- if (!config.cloudflareWorkerUrl) {
337
- errors.push('cloudflareWorkerUrl is required for cloudflare_proxy mode');
338
- }
339
- }
340
- else if (config.authMode === 'direct') {
341
- if (!config.apiUrl) {
342
- errors.push('apiUrl is required for direct mode');
343
- }
344
- }
345
278
  if (config.timeout && config.timeout < 1000) {
346
279
  errors.push('timeout must be at least 1000ms');
347
280
  }
@@ -358,7 +291,7 @@ const validateMauticConfig = (config) => {
358
291
  */
359
292
  const getDefaultMauticConfig = () => {
360
293
  return {
361
- authMode: 'cloudflare_proxy',
294
+ proxyEndpoint: '/api/mautic-submit',
362
295
  timeout: 30000,
363
296
  retries: 3,
364
297
  };
@@ -376,22 +309,15 @@ const mergeMauticConfig = (base, overrides) => {
376
309
  * Check if Mautic is enabled based on configuration
377
310
  */
378
311
  const isMauticEnabled = (config) => {
379
- if (config.authMode === 'cloudflare_proxy') {
380
- return !!config.cloudflareWorkerUrl;
381
- }
382
- else if (config.authMode === 'direct') {
383
- return !!config.apiUrl;
384
- }
385
- return false;
312
+ return !!config.apiUrl;
386
313
  };
387
314
  /**
388
315
  * Get Mautic configuration summary (for debugging)
389
316
  */
390
317
  const getMauticConfigSummary = (config) => {
391
318
  return {
392
- authMode: config.authMode,
393
319
  hasApiUrl: !!config.apiUrl,
394
- hasCloudflareWorkerUrl: !!config.cloudflareWorkerUrl,
320
+ proxyEndpoint: config.proxyEndpoint || '/api/mautic-submit',
395
321
  timeout: config.timeout,
396
322
  retries: config.retries,
397
323
  isEnabled: isMauticEnabled(config),
@@ -533,14 +459,11 @@ class MauticGenerator {
533
459
  constructor(config) {
534
460
  this.cachedToken = null;
535
461
  this.config = config;
462
+ // Generator uses direct OAuth2 calls, not the client
463
+ // Client is only used for proxy mode (cloudflare_proxy)
536
464
  this.client = new MauticClient({
537
- authMode: config.authMode,
538
465
  apiUrl: config.apiUrl,
539
- cloudflareWorkerUrl: config.cloudflareWorkerUrl,
540
- clientId: config.clientId,
541
- clientSecret: config.clientSecret,
542
- cfAccessClientId: config.cfAccessClientId,
543
- cfAccessClientSecret: config.cfAccessClientSecret,
466
+ proxyEndpoint: config.cloudflareWorkerUrl,
544
467
  timeout: config.timeout,
545
468
  retries: config.retries,
546
469
  });