@garagem-ai/site-sdk 0.4.3 → 0.4.5

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."
@@ -318,6 +319,7 @@ async function getCmsEntry(options) {
318
319
  var VISIT_TIME_ZONE = "America/Sao_Paulo";
319
320
  var HHMM_RE = /^([01]\d|2[0-3]):([0-5]\d)$/;
320
321
  var YMD_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
322
+ var ISO_INSTANT_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
321
323
  var WEEKDAY_SHORT = {
322
324
  Sun: 0,
323
325
  Mon: 1,
@@ -419,9 +421,12 @@ function parseYmd(value) {
419
421
  }
420
422
  return { year, month, day };
421
423
  }
422
- function toCalendarYmd(date, timeZone) {
424
+ function normalizeVisitDate(date, timeZone = VISIT_TIME_ZONE) {
423
425
  if (typeof date === "string") {
424
- return parseYmd(date) ? date : null;
426
+ if (parseYmd(date)) return date;
427
+ if (!ISO_INSTANT_RE.test(date)) return null;
428
+ const instant = new Date(date);
429
+ return Number.isFinite(instant.getTime()) ? calendarYmd(instant, timeZone) : null;
425
430
  }
426
431
  if (date instanceof Date && Number.isFinite(date.getTime())) {
427
432
  return calendarYmd(date, timeZone);
@@ -438,10 +443,11 @@ function weekdayOfYmd(ymd, timeZone) {
438
443
  }
439
444
  function slotsForDate(schedule, date, now = /* @__PURE__ */ new Date()) {
440
445
  const timeZone = schedule.timeZone;
441
- const ymd = toCalendarYmd(date, timeZone);
446
+ const ymd = normalizeVisitDate(date, timeZone);
442
447
  if (!ymd) return [];
443
448
  const weekday = weekdayOfYmd(ymd, timeZone);
444
449
  if (weekday == null) return [];
450
+ if (schedule.disabledDays.includes(weekday)) return [];
445
451
  if (weekday === 0 && !schedule.sunday) return [];
446
452
  if (weekday === 6 && !schedule.saturday) return [];
447
453
  const base = weekday === 0 || weekday === 6 ? schedule.weekendTimeSlots : schedule.weekdayTimeSlots;
@@ -459,7 +465,56 @@ function isVisitSlotAllowed(schedule, visitDate, visitTime, now = /* @__PURE__ *
459
465
  return slotsForDate(schedule, visitDate, now).includes(visitTime);
460
466
  }
461
467
 
468
+ // src/resolve-site-id.ts
469
+ var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
470
+ function isStudioPreviewHost(host) {
471
+ const normalized = stripPort(host);
472
+ if (normalized === "localhost" || normalized === "127.0.0.1") return true;
473
+ return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
474
+ }
475
+ function stripPort(host) {
476
+ return host.trim().toLowerCase().split(":")[0] ?? "";
477
+ }
478
+ async function resolvePublicSiteId(options) {
479
+ const fallback = options.fallback?.trim() ?? "";
480
+ const host = options.host ? stripPort(options.host) : "";
481
+ if (host && !isStudioPreviewHost(host)) {
482
+ const site = await getSite({
483
+ domain: host,
484
+ baseUrl: options.baseUrl,
485
+ fetch: options.fetch
486
+ });
487
+ if (site?.id) return site.id;
488
+ throw new GaragemSdkError(
489
+ `Garagem SDK: no published site found for domain "${host}"`,
490
+ { status: 404, url: host }
491
+ );
492
+ }
493
+ if (fallback) return fallback;
494
+ throw new GaragemSdkError(
495
+ "Garagem SDK: could not resolve a site id (no host match and no fallback)",
496
+ { status: 404, url: host || "fallback" }
497
+ );
498
+ }
499
+
462
500
  // src/leads.ts
501
+ function browserHost() {
502
+ if (typeof window === "undefined") return "";
503
+ return window.location.host;
504
+ }
505
+ async function resolveLeadSiteId(input) {
506
+ try {
507
+ return await coerceSiteId(input.siteId);
508
+ } catch (error) {
509
+ const host = browserHost();
510
+ if (!host) throw error;
511
+ return resolvePublicSiteId({
512
+ host,
513
+ baseUrl: input.baseUrl,
514
+ fetch: input.fetch
515
+ });
516
+ }
517
+ }
463
518
  var LeadSubmitError = class extends Error {
464
519
  code;
465
520
  status;
@@ -504,7 +559,7 @@ function studioDefaultLeadUi() {
504
559
  };
505
560
  }
506
561
  async function getLeadUi(options) {
507
- const siteId = await coerceSiteId(options.siteId);
562
+ const siteId = await resolveLeadSiteId(options);
508
563
  const url = `${normalizeBaseUrl(options.baseUrl)}/api/sites/${encodeURIComponent(siteId)}/leads/ui`;
509
564
  return callJson(
510
565
  url,
@@ -562,7 +617,7 @@ function normalizeContact(input) {
562
617
  };
563
618
  }
564
619
  async function postSiteLead(input) {
565
- const siteId = await coerceSiteId(input.siteId);
620
+ const siteId = await resolveLeadSiteId(input);
566
621
  const base = normalizeBaseUrl(input.baseUrl);
567
622
  const url = `${base}/api/webhooks/sites/${encodeURIComponent(siteId)}/leads`;
568
623
  const f = input.fetch ?? fetch;
@@ -621,7 +676,8 @@ function leadPostContext(input) {
621
676
  async function submitVisitLead(input) {
622
677
  const prepared = leadPostContext(input);
623
678
  const schedule = input.schedule ?? STUDIO_DEFAULT_VISIT_SCHEDULE;
624
- if (!isVisitSlotAllowed(schedule, input.visitDate, input.visitTime)) {
679
+ const visitDate = normalizeVisitDate(input.visitDate, schedule.timeZone);
680
+ if (!visitDate || !isVisitSlotAllowed(schedule, visitDate, input.visitTime)) {
625
681
  throw new LeadSubmitError(
626
682
  "Garagem SDK: visitDate/visitTime is outside the published schedule.",
627
683
  { code: "invalid_slot" }
@@ -637,7 +693,7 @@ async function submitVisitLead(input) {
637
693
  ...prepared.contact,
638
694
  propertyId: prepared.listingId,
639
695
  listingId: prepared.listingId,
640
- visitDate: input.visitDate,
696
+ visitDate,
641
697
  visitTime: input.visitTime,
642
698
  ...prepared.extras
643
699
  }
@@ -1513,38 +1569,6 @@ function paginationPages(currentPage, totalPages, padding) {
1513
1569
  return pages;
1514
1570
  }
1515
1571
 
1516
- // src/resolve-site-id.ts
1517
- var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
1518
- function isStudioPreviewHost(host) {
1519
- const normalized = stripPort(host);
1520
- if (normalized === "localhost" || normalized === "127.0.0.1") return true;
1521
- return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
1522
- }
1523
- function stripPort(host) {
1524
- return host.trim().toLowerCase().split(":")[0] ?? "";
1525
- }
1526
- async function resolvePublicSiteId(options) {
1527
- const fallback = options.fallback?.trim() ?? "";
1528
- const host = options.host ? stripPort(options.host) : "";
1529
- if (host && !isStudioPreviewHost(host)) {
1530
- const site = await getSite({
1531
- domain: host,
1532
- baseUrl: options.baseUrl,
1533
- fetch: options.fetch
1534
- });
1535
- if (site?.id) return site.id;
1536
- throw new GaragemSdkError(
1537
- `Garagem SDK: no published site found for domain "${host}"`,
1538
- { status: 404, url: host }
1539
- );
1540
- }
1541
- if (fallback) return fallback;
1542
- throw new GaragemSdkError(
1543
- "Garagem SDK: could not resolve a site id (no host match and no fallback)",
1544
- { status: 404, url: host || "fallback" }
1545
- );
1546
- }
1547
-
1548
1572
  // src/studio-contract.ts
1549
1573
  var STUDIO_SDK_PACKAGE = "@garagem-ai/site-sdk";
1550
1574
  var STUDIO_SDK_CONTRACT = {
@@ -1886,6 +1910,7 @@ export {
1886
1910
  listingLookupKey,
1887
1911
  listingPublicPath,
1888
1912
  mergeAppliedSearchQueryIntoSearch2 as mergeAppliedSearchQueryIntoSearch,
1913
+ normalizeVisitDate,
1889
1914
  paginationPages,
1890
1915
  parseAppliedSearchQuery2 as parseAppliedSearchQuery,
1891
1916
  parseGroupedInteger,