@demokit-ai/core 0.3.0 → 0.4.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.cjs CHANGED
@@ -332,6 +332,26 @@ function extractUrl(input, baseUrl) {
332
332
  }
333
333
  return baseUrl;
334
334
  }
335
+ function detectDemoMode(detection) {
336
+ if (!detection || typeof window === "undefined") {
337
+ return { detected: false, isPublicDemo: false };
338
+ }
339
+ if (detection.subdomains?.length) {
340
+ const hostname = window.location.hostname;
341
+ if (detection.subdomains.some((sub) => hostname === sub)) {
342
+ return { detected: true, isPublicDemo: true };
343
+ }
344
+ }
345
+ const queryParams = detection.queryParams ?? ["demo"];
346
+ const searchParams = new URLSearchParams(window.location.search);
347
+ for (const param of queryParams) {
348
+ const value = searchParams.get(param);
349
+ if (value !== null && value !== "false") {
350
+ return { detected: true, isPublicDemo: false };
351
+ }
352
+ }
353
+ return { detected: false, isPublicDemo: false };
354
+ }
335
355
  function createDemoInterceptor(config) {
336
356
  const {
337
357
  fixtures: initialFixtures,
@@ -339,9 +359,13 @@ function createDemoInterceptor(config) {
339
359
  onEnable,
340
360
  onDisable,
341
361
  initialEnabled,
342
- baseUrl = "http://localhost"
362
+ baseUrl = "http://localhost",
363
+ detection,
364
+ canDisable,
365
+ onMutationIntercepted
343
366
  } = config;
344
- let enabled = initialEnabled ?? loadDemoState(storageKey);
367
+ const detectionResult = detectDemoMode(detection);
368
+ let enabled = detectionResult.detected || (initialEnabled ?? loadDemoState(storageKey));
345
369
  let currentFixtures = { ...initialFixtures };
346
370
  let sessionState = createSessionState();
347
371
  let originalFetch = null;
@@ -353,14 +377,19 @@ function createDemoInterceptor(config) {
353
377
  originalFetch = globalThis.fetch;
354
378
  globalThis.fetch = async function interceptedFetch(input, init) {
355
379
  if (!enabled) {
380
+ console.log("[DemoKit] Demo mode disabled, passing through");
356
381
  return originalFetch(input, init);
357
382
  }
358
383
  const method = init?.method?.toUpperCase() || "GET";
359
384
  const pathname = extractPathname(input, baseUrl);
385
+ console.log("[DemoKit] Intercepting request:", { method, pathname, enabled });
386
+ console.log("[DemoKit] Available fixtures:", Object.keys(currentFixtures));
360
387
  const match = findMatchingPattern(currentFixtures, method, pathname);
361
388
  if (!match) {
389
+ console.log("[DemoKit] No matching fixture for:", `${method} ${pathname}`);
362
390
  return originalFetch(input, init);
363
391
  }
392
+ console.log("[DemoKit] Found matching fixture:", match[0]);
364
393
  const [pattern, matchResult] = match;
365
394
  const handler = currentFixtures[pattern];
366
395
  const url = extractUrl(input, baseUrl);
@@ -381,6 +410,14 @@ function createDemoInterceptor(config) {
381
410
  headers,
382
411
  session: sessionState
383
412
  };
413
+ if (method !== "GET" && onMutationIntercepted) {
414
+ onMutationIntercepted({
415
+ url,
416
+ method,
417
+ params: matchResult.params,
418
+ pattern
419
+ });
420
+ }
384
421
  let result;
385
422
  try {
386
423
  if (typeof handler === "function") {
@@ -416,14 +453,24 @@ function createDemoInterceptor(config) {
416
453
  onEnable?.();
417
454
  },
418
455
  disable() {
419
- if (!enabled) return;
456
+ if (!enabled) return true;
457
+ if (canDisable) {
458
+ const result = canDisable();
459
+ if (result !== true) {
460
+ return result;
461
+ }
462
+ }
420
463
  enabled = false;
421
464
  saveDemoState(storageKey, false);
422
465
  onDisable?.();
466
+ return true;
423
467
  },
424
468
  isEnabled() {
425
469
  return enabled;
426
470
  },
471
+ isPublicDemo() {
472
+ return detectionResult.isPublicDemo;
473
+ },
427
474
  toggle() {
428
475
  if (enabled) {
429
476
  this.disable();
@@ -526,7 +573,8 @@ function createDemoStateStore(options = {}) {
526
573
  }
527
574
 
528
575
  // src/remote.ts
529
- var DEFAULT_CLOUD_URL = "https://api.demokit.cloud";
576
+ var DEFAULT_API_URL = "https://api.demokit.cloud/api";
577
+ var DEFAULT_CLOUD_URL = DEFAULT_API_URL;
530
578
  var DEFAULT_TIMEOUT = 1e4;
531
579
  var DEFAULT_MAX_RETRIES = 3;
532
580
  function isValidApiKey(apiKey) {
@@ -549,13 +597,16 @@ function getBackoffDelay(attempt, baseDelay = 1e3) {
549
597
  async function fetchCloudFixtures(config) {
550
598
  const {
551
599
  apiKey,
552
- cloudUrl = DEFAULT_CLOUD_URL,
600
+ apiUrl,
601
+ cloudUrl,
602
+ // deprecated, for backwards compatibility
553
603
  onError,
554
604
  onLoad,
555
605
  timeout = DEFAULT_TIMEOUT,
556
606
  retry = true,
557
607
  maxRetries = DEFAULT_MAX_RETRIES
558
608
  } = config;
609
+ const baseUrl = apiUrl || cloudUrl || DEFAULT_API_URL;
559
610
  if (!isValidApiKey(apiKey)) {
560
611
  const error = new RemoteFetchError(
561
612
  "Invalid API key format. Expected format: dk_live_xxx",
@@ -565,7 +616,7 @@ async function fetchCloudFixtures(config) {
565
616
  onError?.(error);
566
617
  throw error;
567
618
  }
568
- const url = `${cloudUrl.replace(/\/$/, "")}/api/v1/fixtures`;
619
+ const url = `${baseUrl.replace(/\/$/, "")}/fixtures`;
569
620
  let lastError = null;
570
621
  const maxAttempts = retry ? maxRetries : 1;
571
622
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
@@ -6849,6 +6900,7 @@ function getRequiredFieldRules(rules) {
6849
6900
  return rules.filter((rule) => rule.required);
6850
6901
  }
6851
6902
 
6903
+ exports.DEFAULT_API_URL = DEFAULT_API_URL;
6852
6904
  exports.DEFAULT_CLOUD_URL = DEFAULT_CLOUD_URL;
6853
6905
  exports.DEFAULT_MAX_RETRIES = DEFAULT_MAX_RETRIES;
6854
6906
  exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;