@demokit-ai/core 0.3.0 → 0.5.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 +70 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +97 -6
- package/dist/index.d.ts +97 -6
- package/dist/index.js +70 -8
- package/dist/index.js.map +1 -1
- package/package.json +15 -16
- package/LICENSE +0 -190
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,15 @@ 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,
|
|
366
|
+
pathAliases,
|
|
367
|
+
warnOnCatchAll = typeof process !== "undefined" && process.env?.NODE_ENV !== "production"
|
|
343
368
|
} = config;
|
|
344
|
-
|
|
369
|
+
const detectionResult = detectDemoMode(detection);
|
|
370
|
+
let enabled = detectionResult.detected || (initialEnabled ?? loadDemoState(storageKey));
|
|
345
371
|
let currentFixtures = { ...initialFixtures };
|
|
346
372
|
let sessionState = createSessionState();
|
|
347
373
|
let originalFetch = null;
|
|
@@ -357,11 +383,25 @@ function createDemoInterceptor(config) {
|
|
|
357
383
|
}
|
|
358
384
|
const method = init?.method?.toUpperCase() || "GET";
|
|
359
385
|
const pathname = extractPathname(input, baseUrl);
|
|
360
|
-
|
|
386
|
+
let match = findMatchingPattern(currentFixtures, method, pathname);
|
|
387
|
+
if (!match && pathAliases) {
|
|
388
|
+
for (const [from, to] of Object.entries(pathAliases)) {
|
|
389
|
+
if (pathname.startsWith(from)) {
|
|
390
|
+
const aliasedPath = to + pathname.slice(from.length);
|
|
391
|
+
match = findMatchingPattern(currentFixtures, method, aliasedPath);
|
|
392
|
+
if (match) break;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
361
396
|
if (!match) {
|
|
362
397
|
return originalFetch(input, init);
|
|
363
398
|
}
|
|
364
399
|
const [pattern, matchResult] = match;
|
|
400
|
+
if (warnOnCatchAll && pattern.includes("*")) {
|
|
401
|
+
console.warn(
|
|
402
|
+
`[DemoKit] Catch-all fixture matched: ${method} ${pathname} \u2192 "${pattern}". Consider adding a specific fixture.`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
365
405
|
const handler = currentFixtures[pattern];
|
|
366
406
|
const url = extractUrl(input, baseUrl);
|
|
367
407
|
const headers = new Headers(init?.headers);
|
|
@@ -381,6 +421,14 @@ function createDemoInterceptor(config) {
|
|
|
381
421
|
headers,
|
|
382
422
|
session: sessionState
|
|
383
423
|
};
|
|
424
|
+
if (method !== "GET" && onMutationIntercepted) {
|
|
425
|
+
onMutationIntercepted({
|
|
426
|
+
url,
|
|
427
|
+
method,
|
|
428
|
+
params: matchResult.params,
|
|
429
|
+
pattern
|
|
430
|
+
});
|
|
431
|
+
}
|
|
384
432
|
let result;
|
|
385
433
|
try {
|
|
386
434
|
if (typeof handler === "function") {
|
|
@@ -416,14 +464,24 @@ function createDemoInterceptor(config) {
|
|
|
416
464
|
onEnable?.();
|
|
417
465
|
},
|
|
418
466
|
disable() {
|
|
419
|
-
if (!enabled) return;
|
|
467
|
+
if (!enabled) return true;
|
|
468
|
+
if (canDisable) {
|
|
469
|
+
const result = canDisable();
|
|
470
|
+
if (result !== true) {
|
|
471
|
+
return result;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
420
474
|
enabled = false;
|
|
421
475
|
saveDemoState(storageKey, false);
|
|
422
476
|
onDisable?.();
|
|
477
|
+
return true;
|
|
423
478
|
},
|
|
424
479
|
isEnabled() {
|
|
425
480
|
return enabled;
|
|
426
481
|
},
|
|
482
|
+
isPublicDemo() {
|
|
483
|
+
return detectionResult.isPublicDemo;
|
|
484
|
+
},
|
|
427
485
|
toggle() {
|
|
428
486
|
if (enabled) {
|
|
429
487
|
this.disable();
|
|
@@ -526,7 +584,8 @@ function createDemoStateStore(options = {}) {
|
|
|
526
584
|
}
|
|
527
585
|
|
|
528
586
|
// src/remote.ts
|
|
529
|
-
var
|
|
587
|
+
var DEFAULT_API_URL = "https://api.demokit.cloud/api";
|
|
588
|
+
var DEFAULT_CLOUD_URL = DEFAULT_API_URL;
|
|
530
589
|
var DEFAULT_TIMEOUT = 1e4;
|
|
531
590
|
var DEFAULT_MAX_RETRIES = 3;
|
|
532
591
|
function isValidApiKey(apiKey) {
|
|
@@ -549,13 +608,16 @@ function getBackoffDelay(attempt, baseDelay = 1e3) {
|
|
|
549
608
|
async function fetchCloudFixtures(config) {
|
|
550
609
|
const {
|
|
551
610
|
apiKey,
|
|
552
|
-
|
|
611
|
+
apiUrl,
|
|
612
|
+
cloudUrl,
|
|
613
|
+
// deprecated, for backwards compatibility
|
|
553
614
|
onError,
|
|
554
615
|
onLoad,
|
|
555
616
|
timeout = DEFAULT_TIMEOUT,
|
|
556
617
|
retry = true,
|
|
557
618
|
maxRetries = DEFAULT_MAX_RETRIES
|
|
558
619
|
} = config;
|
|
620
|
+
const baseUrl = apiUrl || cloudUrl || DEFAULT_API_URL;
|
|
559
621
|
if (!isValidApiKey(apiKey)) {
|
|
560
622
|
const error = new RemoteFetchError(
|
|
561
623
|
"Invalid API key format. Expected format: dk_live_xxx",
|
|
@@ -565,7 +627,7 @@ async function fetchCloudFixtures(config) {
|
|
|
565
627
|
onError?.(error);
|
|
566
628
|
throw error;
|
|
567
629
|
}
|
|
568
|
-
const url = `${
|
|
630
|
+
const url = `${baseUrl.replace(/\/$/, "")}/fixtures`;
|
|
569
631
|
let lastError = null;
|
|
570
632
|
const maxAttempts = retry ? maxRetries : 1;
|
|
571
633
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
@@ -6849,6 +6911,7 @@ function getRequiredFieldRules(rules) {
|
|
|
6849
6911
|
return rules.filter((rule) => rule.required);
|
|
6850
6912
|
}
|
|
6851
6913
|
|
|
6914
|
+
exports.DEFAULT_API_URL = DEFAULT_API_URL;
|
|
6852
6915
|
exports.DEFAULT_CLOUD_URL = DEFAULT_CLOUD_URL;
|
|
6853
6916
|
exports.DEFAULT_MAX_RETRIES = DEFAULT_MAX_RETRIES;
|
|
6854
6917
|
exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;
|