@garagem-ai/site-sdk 0.4.4 → 0.4.6

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.mjs CHANGED
@@ -19,7 +19,8 @@ function normalizeBaseUrl(baseUrl) {
19
19
  return url.endsWith("/") ? url.slice(0, -1) : url;
20
20
  }
21
21
  async function coerceSiteId(siteId) {
22
- const value = await Promise.resolve(siteId);
22
+ const raw = typeof siteId === "function" ? siteId() : siteId;
23
+ const value = await Promise.resolve(raw);
23
24
  if (typeof value !== "string" || !value.trim()) {
24
25
  throw new TypeError(
25
26
  "Garagem SDK: siteId must be a string. Await resolveSiteId(...) before getListings / getSearchUi / queryListings."
@@ -317,8 +318,10 @@ async function getCmsEntry(options) {
317
318
  // src/schedule.ts
318
319
  var VISIT_TIME_ZONE = "America/Sao_Paulo";
319
320
  var HHMM_RE = /^([01]\d|2[0-3]):([0-5]\d)$/;
321
+ var VISIT_TIME_RE = /^([01]\d|2[0-3]):([0-5]\d)(?::[0-5]\d(?:\.\d+)?)?$/;
320
322
  var YMD_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
321
323
  var ISO_INSTANT_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
324
+ var DATETIME_LOCAL_RE = /^(\d{4}-\d{2}-\d{2})T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?$/;
322
325
  var WEEKDAY_SHORT = {
323
326
  Sun: 0,
324
327
  Mon: 1,
@@ -341,6 +344,10 @@ function minutesOfDay(hhmm) {
341
344
  if (!parsed) return null;
342
345
  return parsed.hour * 60 + parsed.minute;
343
346
  }
347
+ function normalizeVisitTime(value) {
348
+ const match = VISIT_TIME_RE.exec(value.trim());
349
+ return match ? `${match[1]}:${match[2]}` : null;
350
+ }
344
351
  function expandVisitTimeSlots(hours) {
345
352
  const start = minutesOfDay(hours.start);
346
353
  const end = minutesOfDay(hours.end);
@@ -422,10 +429,15 @@ function parseYmd(value) {
422
429
  }
423
430
  function normalizeVisitDate(date, timeZone = VISIT_TIME_ZONE) {
424
431
  if (typeof date === "string") {
425
- if (parseYmd(date)) return date;
426
- if (!ISO_INSTANT_RE.test(date)) return null;
427
- const instant = new Date(date);
428
- return Number.isFinite(instant.getTime()) ? calendarYmd(instant, timeZone) : null;
432
+ const trimmed = date.trim();
433
+ if (parseYmd(trimmed)) return trimmed;
434
+ if (ISO_INSTANT_RE.test(trimmed)) {
435
+ const instant = new Date(trimmed);
436
+ return Number.isFinite(instant.getTime()) ? calendarYmd(instant, timeZone) : null;
437
+ }
438
+ const local = DATETIME_LOCAL_RE.exec(trimmed);
439
+ if (local && parseYmd(local[1])) return local[1];
440
+ return null;
429
441
  }
430
442
  if (date instanceof Date && Number.isFinite(date.getTime())) {
431
443
  return calendarYmd(date, timeZone);
@@ -446,6 +458,7 @@ function slotsForDate(schedule, date, now = /* @__PURE__ */ new Date()) {
446
458
  if (!ymd) return [];
447
459
  const weekday = weekdayOfYmd(ymd, timeZone);
448
460
  if (weekday == null) return [];
461
+ if (schedule.disabledDays.includes(weekday)) return [];
449
462
  if (weekday === 0 && !schedule.sunday) return [];
450
463
  if (weekday === 6 && !schedule.saturday) return [];
451
464
  const base = weekday === 0 || weekday === 6 ? schedule.weekendTimeSlots : schedule.weekdayTimeSlots;
@@ -460,10 +473,61 @@ function slotsForDate(schedule, date, now = /* @__PURE__ */ new Date()) {
460
473
  });
461
474
  }
462
475
  function isVisitSlotAllowed(schedule, visitDate, visitTime, now = /* @__PURE__ */ new Date()) {
463
- return slotsForDate(schedule, visitDate, now).includes(visitTime);
476
+ const time = normalizeVisitTime(visitTime);
477
+ if (!time) return false;
478
+ return slotsForDate(schedule, visitDate, now).includes(time);
479
+ }
480
+
481
+ // src/resolve-site-id.ts
482
+ var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
483
+ function isStudioPreviewHost(host) {
484
+ const normalized = stripPort(host);
485
+ if (normalized === "localhost" || normalized === "127.0.0.1") return true;
486
+ return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
487
+ }
488
+ function stripPort(host) {
489
+ return host.trim().toLowerCase().split(":")[0] ?? "";
490
+ }
491
+ async function resolvePublicSiteId(options) {
492
+ const fallback = options.fallback?.trim() ?? "";
493
+ const host = options.host ? stripPort(options.host) : "";
494
+ if (host && !isStudioPreviewHost(host)) {
495
+ const site = await getSite({
496
+ domain: host,
497
+ baseUrl: options.baseUrl,
498
+ fetch: options.fetch
499
+ });
500
+ if (site?.id) return site.id;
501
+ throw new GaragemSdkError(
502
+ `Garagem SDK: no published site found for domain "${host}"`,
503
+ { status: 404, url: host }
504
+ );
505
+ }
506
+ if (fallback) return fallback;
507
+ throw new GaragemSdkError(
508
+ "Garagem SDK: could not resolve a site id (no host match and no fallback)",
509
+ { status: 404, url: host || "fallback" }
510
+ );
464
511
  }
465
512
 
466
513
  // src/leads.ts
514
+ function browserHost() {
515
+ if (typeof window === "undefined") return "";
516
+ return window.location.host;
517
+ }
518
+ async function resolveLeadSiteId(input) {
519
+ try {
520
+ return await coerceSiteId(input.siteId);
521
+ } catch (error) {
522
+ const host = browserHost();
523
+ if (!host) throw error;
524
+ return resolvePublicSiteId({
525
+ host,
526
+ baseUrl: input.baseUrl,
527
+ fetch: input.fetch
528
+ });
529
+ }
530
+ }
467
531
  var LeadSubmitError = class extends Error {
468
532
  code;
469
533
  status;
@@ -508,7 +572,7 @@ function studioDefaultLeadUi() {
508
572
  };
509
573
  }
510
574
  async function getLeadUi(options) {
511
- const siteId = await coerceSiteId(options.siteId);
575
+ const siteId = await resolveLeadSiteId(options);
512
576
  const url = `${normalizeBaseUrl(options.baseUrl)}/api/sites/${encodeURIComponent(siteId)}/leads/ui`;
513
577
  return callJson(
514
578
  url,
@@ -566,7 +630,7 @@ function normalizeContact(input) {
566
630
  };
567
631
  }
568
632
  async function postSiteLead(input) {
569
- const siteId = await coerceSiteId(input.siteId);
633
+ const siteId = await resolveLeadSiteId(input);
570
634
  const base = normalizeBaseUrl(input.baseUrl);
571
635
  const url = `${base}/api/webhooks/sites/${encodeURIComponent(siteId)}/leads`;
572
636
  const f = input.fetch ?? fetch;
@@ -626,7 +690,8 @@ async function submitVisitLead(input) {
626
690
  const prepared = leadPostContext(input);
627
691
  const schedule = input.schedule ?? STUDIO_DEFAULT_VISIT_SCHEDULE;
628
692
  const visitDate = normalizeVisitDate(input.visitDate, schedule.timeZone);
629
- if (!visitDate || !isVisitSlotAllowed(schedule, visitDate, input.visitTime)) {
693
+ const visitTime = normalizeVisitTime(input.visitTime);
694
+ if (!visitDate || !visitTime || !isVisitSlotAllowed(schedule, visitDate, visitTime)) {
630
695
  throw new LeadSubmitError(
631
696
  "Garagem SDK: visitDate/visitTime is outside the published schedule.",
632
697
  { code: "invalid_slot" }
@@ -643,7 +708,7 @@ async function submitVisitLead(input) {
643
708
  propertyId: prepared.listingId,
644
709
  listingId: prepared.listingId,
645
710
  visitDate,
646
- visitTime: input.visitTime,
711
+ visitTime,
647
712
  ...prepared.extras
648
713
  }
649
714
  });
@@ -1518,38 +1583,6 @@ function paginationPages(currentPage, totalPages, padding) {
1518
1583
  return pages;
1519
1584
  }
1520
1585
 
1521
- // src/resolve-site-id.ts
1522
- var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
1523
- function isStudioPreviewHost(host) {
1524
- const normalized = stripPort(host);
1525
- if (normalized === "localhost" || normalized === "127.0.0.1") return true;
1526
- return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
1527
- }
1528
- function stripPort(host) {
1529
- return host.trim().toLowerCase().split(":")[0] ?? "";
1530
- }
1531
- async function resolvePublicSiteId(options) {
1532
- const fallback = options.fallback?.trim() ?? "";
1533
- const host = options.host ? stripPort(options.host) : "";
1534
- if (host && !isStudioPreviewHost(host)) {
1535
- const site = await getSite({
1536
- domain: host,
1537
- baseUrl: options.baseUrl,
1538
- fetch: options.fetch
1539
- });
1540
- if (site?.id) return site.id;
1541
- throw new GaragemSdkError(
1542
- `Garagem SDK: no published site found for domain "${host}"`,
1543
- { status: 404, url: host }
1544
- );
1545
- }
1546
- if (fallback) return fallback;
1547
- throw new GaragemSdkError(
1548
- "Garagem SDK: could not resolve a site id (no host match and no fallback)",
1549
- { status: 404, url: host || "fallback" }
1550
- );
1551
- }
1552
-
1553
1586
  // src/studio-contract.ts
1554
1587
  var STUDIO_SDK_PACKAGE = "@garagem-ai/site-sdk";
1555
1588
  var STUDIO_SDK_CONTRACT = {
@@ -1557,10 +1590,9 @@ var STUDIO_SDK_CONTRACT = {
1557
1590
  /**
1558
1591
  * Exact pin for generated Studio sites. Must be ≥ `selfInjectSince` so
1559
1592
  * prompts that skip the layout chrome tag match the tarball v0 installs.
1560
- * This PR’s changeset publishes that tarball as 0.4.3 — do not generate
1561
- * against this floor until Version Packages is on npm.
1593
+ * 0.4.5 normalizes visit dates and recovers a missing browser siteId.
1562
1594
  */
1563
- minNpmVersion: "0.4.3",
1595
+ minNpmVersion: "0.4.5",
1564
1596
  /** First release where ListingCard and ResultsSkeleton inject the sheet. */
1565
1597
  selfInjectSince: "0.4.2",
1566
1598
  chromeStrategy: "self-inject",
@@ -1892,6 +1924,7 @@ export {
1892
1924
  listingPublicPath,
1893
1925
  mergeAppliedSearchQueryIntoSearch2 as mergeAppliedSearchQueryIntoSearch,
1894
1926
  normalizeVisitDate,
1927
+ normalizeVisitTime,
1895
1928
  paginationPages,
1896
1929
  parseAppliedSearchQuery2 as parseAppliedSearchQuery,
1897
1930
  parseGroupedInteger,