@garagem-ai/site-sdk 0.4.4 → 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.d.mts CHANGED
@@ -209,7 +209,7 @@ type LeadUi = {
209
209
  };
210
210
  };
211
211
  type GetLeadUiOptions = {
212
- siteId: string;
212
+ siteId?: string | Promise<string>;
213
213
  baseUrl?: string;
214
214
  fetch?: FetchLike;
215
215
  next?: RequestNextOptions;
@@ -221,7 +221,7 @@ type LeadContact = {
221
221
  message?: string;
222
222
  };
223
223
  type LeadSubmitBase = LeadContact & {
224
- siteId: string;
224
+ siteId?: string | Promise<string>;
225
225
  baseUrl?: string;
226
226
  fetch?: FetchLike;
227
227
  listingId: string;
package/dist/index.d.ts CHANGED
@@ -209,7 +209,7 @@ type LeadUi = {
209
209
  };
210
210
  };
211
211
  type GetLeadUiOptions = {
212
- siteId: string;
212
+ siteId?: string | Promise<string>;
213
213
  baseUrl?: string;
214
214
  fetch?: FetchLike;
215
215
  next?: RequestNextOptions;
@@ -221,7 +221,7 @@ type LeadContact = {
221
221
  message?: string;
222
222
  };
223
223
  type LeadSubmitBase = LeadContact & {
224
- siteId: string;
224
+ siteId?: string | Promise<string>;
225
225
  baseUrl?: string;
226
226
  fetch?: FetchLike;
227
227
  listingId: string;
package/dist/index.js CHANGED
@@ -126,7 +126,8 @@ function normalizeBaseUrl(baseUrl) {
126
126
  return url.endsWith("/") ? url.slice(0, -1) : url;
127
127
  }
128
128
  async function coerceSiteId(siteId) {
129
- const value = await Promise.resolve(siteId);
129
+ const raw = typeof siteId === "function" ? siteId() : siteId;
130
+ const value = await Promise.resolve(raw);
130
131
  if (typeof value !== "string" || !value.trim()) {
131
132
  throw new TypeError(
132
133
  "Garagem SDK: siteId must be a string. Await resolveSiteId(...) before getListings / getSearchUi / queryListings."
@@ -553,6 +554,7 @@ function slotsForDate(schedule, date, now = /* @__PURE__ */ new Date()) {
553
554
  if (!ymd) return [];
554
555
  const weekday = weekdayOfYmd(ymd, timeZone);
555
556
  if (weekday == null) return [];
557
+ if (schedule.disabledDays.includes(weekday)) return [];
556
558
  if (weekday === 0 && !schedule.sunday) return [];
557
559
  if (weekday === 6 && !schedule.saturday) return [];
558
560
  const base = weekday === 0 || weekday === 6 ? schedule.weekendTimeSlots : schedule.weekdayTimeSlots;
@@ -570,7 +572,56 @@ function isVisitSlotAllowed(schedule, visitDate, visitTime, now = /* @__PURE__ *
570
572
  return slotsForDate(schedule, visitDate, now).includes(visitTime);
571
573
  }
572
574
 
575
+ // src/resolve-site-id.ts
576
+ var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
577
+ function isStudioPreviewHost(host) {
578
+ const normalized = stripPort(host);
579
+ if (normalized === "localhost" || normalized === "127.0.0.1") return true;
580
+ return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
581
+ }
582
+ function stripPort(host) {
583
+ return host.trim().toLowerCase().split(":")[0] ?? "";
584
+ }
585
+ async function resolvePublicSiteId(options) {
586
+ const fallback = options.fallback?.trim() ?? "";
587
+ const host = options.host ? stripPort(options.host) : "";
588
+ if (host && !isStudioPreviewHost(host)) {
589
+ const site = await getSite({
590
+ domain: host,
591
+ baseUrl: options.baseUrl,
592
+ fetch: options.fetch
593
+ });
594
+ if (site?.id) return site.id;
595
+ throw new GaragemSdkError(
596
+ `Garagem SDK: no published site found for domain "${host}"`,
597
+ { status: 404, url: host }
598
+ );
599
+ }
600
+ if (fallback) return fallback;
601
+ throw new GaragemSdkError(
602
+ "Garagem SDK: could not resolve a site id (no host match and no fallback)",
603
+ { status: 404, url: host || "fallback" }
604
+ );
605
+ }
606
+
573
607
  // src/leads.ts
608
+ function browserHost() {
609
+ if (typeof window === "undefined") return "";
610
+ return window.location.host;
611
+ }
612
+ async function resolveLeadSiteId(input) {
613
+ try {
614
+ return await coerceSiteId(input.siteId);
615
+ } catch (error) {
616
+ const host = browserHost();
617
+ if (!host) throw error;
618
+ return resolvePublicSiteId({
619
+ host,
620
+ baseUrl: input.baseUrl,
621
+ fetch: input.fetch
622
+ });
623
+ }
624
+ }
574
625
  var LeadSubmitError = class extends Error {
575
626
  code;
576
627
  status;
@@ -615,7 +666,7 @@ function studioDefaultLeadUi() {
615
666
  };
616
667
  }
617
668
  async function getLeadUi(options) {
618
- const siteId = await coerceSiteId(options.siteId);
669
+ const siteId = await resolveLeadSiteId(options);
619
670
  const url = `${normalizeBaseUrl(options.baseUrl)}/api/sites/${encodeURIComponent(siteId)}/leads/ui`;
620
671
  return callJson(
621
672
  url,
@@ -673,7 +724,7 @@ function normalizeContact(input) {
673
724
  };
674
725
  }
675
726
  async function postSiteLead(input) {
676
- const siteId = await coerceSiteId(input.siteId);
727
+ const siteId = await resolveLeadSiteId(input);
677
728
  const base = normalizeBaseUrl(input.baseUrl);
678
729
  const url = `${base}/api/webhooks/sites/${encodeURIComponent(siteId)}/leads`;
679
730
  const f = input.fetch ?? fetch;
@@ -1625,38 +1676,6 @@ function paginationPages(currentPage, totalPages, padding) {
1625
1676
  return pages;
1626
1677
  }
1627
1678
 
1628
- // src/resolve-site-id.ts
1629
- var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
1630
- function isStudioPreviewHost(host) {
1631
- const normalized = stripPort(host);
1632
- if (normalized === "localhost" || normalized === "127.0.0.1") return true;
1633
- return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
1634
- }
1635
- function stripPort(host) {
1636
- return host.trim().toLowerCase().split(":")[0] ?? "";
1637
- }
1638
- async function resolvePublicSiteId(options) {
1639
- const fallback = options.fallback?.trim() ?? "";
1640
- const host = options.host ? stripPort(options.host) : "";
1641
- if (host && !isStudioPreviewHost(host)) {
1642
- const site = await getSite({
1643
- domain: host,
1644
- baseUrl: options.baseUrl,
1645
- fetch: options.fetch
1646
- });
1647
- if (site?.id) return site.id;
1648
- throw new GaragemSdkError(
1649
- `Garagem SDK: no published site found for domain "${host}"`,
1650
- { status: 404, url: host }
1651
- );
1652
- }
1653
- if (fallback) return fallback;
1654
- throw new GaragemSdkError(
1655
- "Garagem SDK: could not resolve a site id (no host match and no fallback)",
1656
- { status: 404, url: host || "fallback" }
1657
- );
1658
- }
1659
-
1660
1679
  // src/studio-contract.ts
1661
1680
  var STUDIO_SDK_PACKAGE = "@garagem-ai/site-sdk";
1662
1681
  var STUDIO_SDK_CONTRACT = {