@garagem-ai/site-sdk 0.1.1 → 0.2.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/README.md +149 -61
- package/dist/index.d.mts +160 -1
- package/dist/index.d.ts +160 -1
- package/dist/index.js +263 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +254 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -3
- package/src/client.ts +138 -28
- package/src/fetchers.ts +203 -14
- package/src/index.ts +26 -0
- package/src/internal/url.ts +4 -2
- package/src/listing-path.ts +19 -0
- package/src/resolve-site-id.ts +55 -0
- package/src/types.ts +80 -0
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ interface ListingPropertySummary {
|
|
|
18
18
|
parkingSpaces: number;
|
|
19
19
|
totalArea: number;
|
|
20
20
|
usableArea: number;
|
|
21
|
+
/** Monthly condo fee in BRL when the listing has one. */
|
|
22
|
+
condoFee?: number;
|
|
21
23
|
}
|
|
22
24
|
/** Address breakdown returned with every listing. */
|
|
23
25
|
interface ListingLocation {
|
|
@@ -55,7 +57,16 @@ interface ListingMedia {
|
|
|
55
57
|
* promotional flows) don't get masked as "sob consulta".
|
|
56
58
|
*/
|
|
57
59
|
interface Listing {
|
|
60
|
+
/** Internal UUID — POST lead with this. Never put it in the URL. */
|
|
58
61
|
id: string;
|
|
62
|
+
/** Public identity for display (`#ABC`) and lookup. `null` if the row has no code. */
|
|
63
|
+
listingCode: string | null;
|
|
64
|
+
/**
|
|
65
|
+
* Canonical public href from the Sites API (`/imoveis/.../id-{listingCode}`).
|
|
66
|
+
* Prefer `listingPublicPath(listing)` so a missing `path` (CDN deploy
|
|
67
|
+
* window) still falls back to `/imoveis/id-{listingCode}`.
|
|
68
|
+
*/
|
|
69
|
+
path: string;
|
|
59
70
|
title: string;
|
|
60
71
|
description: string;
|
|
61
72
|
businessType: ListingBusinessType;
|
|
@@ -168,6 +179,59 @@ interface Site {
|
|
|
168
179
|
domain: string | null;
|
|
169
180
|
tenantId: string;
|
|
170
181
|
}
|
|
182
|
+
/** Wrapped response from `getSimilarListings`. */
|
|
183
|
+
interface SimilarListingsResult {
|
|
184
|
+
listings: Listing[];
|
|
185
|
+
total: number;
|
|
186
|
+
}
|
|
187
|
+
/** CMS field type mirrored from the public CMS worker. */
|
|
188
|
+
type CmsFieldType = "text" | "richText" | "image" | "date" | "select" | "number" | "url" | "entry-reference";
|
|
189
|
+
interface CmsFieldDefinition {
|
|
190
|
+
id: string;
|
|
191
|
+
label: string;
|
|
192
|
+
type: CmsFieldType;
|
|
193
|
+
required?: boolean;
|
|
194
|
+
placeholder?: string;
|
|
195
|
+
options?: string[];
|
|
196
|
+
targetCollection?: string;
|
|
197
|
+
displayField?: string;
|
|
198
|
+
inheritFields?: string[];
|
|
199
|
+
inheritLevelField?: boolean;
|
|
200
|
+
}
|
|
201
|
+
/** CMS collection schema. Returned by `getCmsCollections` / `getCmsCollection`. */
|
|
202
|
+
interface CmsCollection {
|
|
203
|
+
id: string;
|
|
204
|
+
name: string;
|
|
205
|
+
slug: string;
|
|
206
|
+
templatePageId: string | null;
|
|
207
|
+
fields: CmsFieldDefinition[];
|
|
208
|
+
createdAt: string;
|
|
209
|
+
updatedAt: string;
|
|
210
|
+
}
|
|
211
|
+
type CmsEntryStatus = "draft" | "ready" | "published";
|
|
212
|
+
interface CmsSeoData {
|
|
213
|
+
title?: string;
|
|
214
|
+
description?: string;
|
|
215
|
+
ogImage?: string;
|
|
216
|
+
}
|
|
217
|
+
/** Published CMS entry. Returned by `getCmsEntries` / `getCmsEntry`. */
|
|
218
|
+
interface CmsEntry {
|
|
219
|
+
id: string;
|
|
220
|
+
collectionId: string;
|
|
221
|
+
slug: string;
|
|
222
|
+
status: CmsEntryStatus;
|
|
223
|
+
fields: Record<string, unknown>;
|
|
224
|
+
seo: CmsSeoData | null;
|
|
225
|
+
publishedAt: string | null;
|
|
226
|
+
createdAt: string;
|
|
227
|
+
updatedAt: string;
|
|
228
|
+
}
|
|
229
|
+
interface CmsEntriesResult {
|
|
230
|
+
entries: CmsEntry[];
|
|
231
|
+
total: number;
|
|
232
|
+
limit: number;
|
|
233
|
+
offset: number;
|
|
234
|
+
}
|
|
171
235
|
/**
|
|
172
236
|
* App Router caching hints passed straight through to the underlying
|
|
173
237
|
* `fetch` call. See https://nextjs.org/docs/app/api-reference/functions/fetch.
|
|
@@ -211,6 +275,30 @@ interface SearchListingsOptions extends BaseOptions {
|
|
|
211
275
|
siteId: string;
|
|
212
276
|
params: SearchListingsParams;
|
|
213
277
|
}
|
|
278
|
+
interface GetSimilarListingsOptions extends BaseOptions {
|
|
279
|
+
siteId: string;
|
|
280
|
+
listingId: string;
|
|
281
|
+
/** Max similar cards to return (API default 6, cap 12). */
|
|
282
|
+
limit?: number;
|
|
283
|
+
}
|
|
284
|
+
interface GetCmsCollectionsOptions extends BaseOptions {
|
|
285
|
+
siteId: string;
|
|
286
|
+
}
|
|
287
|
+
interface GetCmsCollectionOptions extends BaseOptions {
|
|
288
|
+
siteId: string;
|
|
289
|
+
slug: string;
|
|
290
|
+
}
|
|
291
|
+
interface GetCmsEntriesOptions extends BaseOptions {
|
|
292
|
+
siteId: string;
|
|
293
|
+
collection: string;
|
|
294
|
+
limit?: number;
|
|
295
|
+
offset?: number;
|
|
296
|
+
}
|
|
297
|
+
interface GetCmsEntryOptions extends BaseOptions {
|
|
298
|
+
siteId: string;
|
|
299
|
+
collection: string;
|
|
300
|
+
slug: string;
|
|
301
|
+
}
|
|
214
302
|
/** Error thrown when a fetcher hits a non-2xx response. */
|
|
215
303
|
declare class GaragemSdkError extends Error {
|
|
216
304
|
readonly status: number;
|
|
@@ -233,6 +321,7 @@ declare class GaragemSdkError extends Error {
|
|
|
233
321
|
declare function getListings(options: GetListingsOptions): Promise<ListingsResult>;
|
|
234
322
|
/**
|
|
235
323
|
* Fetch a single listing's full detail (amenities, gallery, building).
|
|
324
|
+
* `listingId` accepts a code, UUID, or `id-{code}` URL segment.
|
|
236
325
|
*/
|
|
237
326
|
declare function getListing(options: GetListingOptions): Promise<ListingDetail>;
|
|
238
327
|
/**
|
|
@@ -270,6 +359,43 @@ declare function getSite(options: GetSiteOptions): Promise<Site | null>;
|
|
|
270
359
|
* }
|
|
271
360
|
*/
|
|
272
361
|
declare function searchListings<TDocument = Record<string, unknown>>(options: SearchListingsOptions): Promise<SearchListingsResult<TDocument>>;
|
|
362
|
+
/**
|
|
363
|
+
* Fetch similar listings for a detail page carousel.
|
|
364
|
+
* Returns an empty list when the reference listing is missing.
|
|
365
|
+
*/
|
|
366
|
+
declare function getSimilarListings(options: GetSimilarListingsOptions): Promise<SimilarListingsResult>;
|
|
367
|
+
/** List CMS collections for a site. */
|
|
368
|
+
declare function getCmsCollections(options: GetCmsCollectionsOptions): Promise<CmsCollection[]>;
|
|
369
|
+
/** Fetch one CMS collection schema by slug. */
|
|
370
|
+
declare function getCmsCollection(options: GetCmsCollectionOptions): Promise<CmsCollection>;
|
|
371
|
+
/** List published CMS entries for a collection. */
|
|
372
|
+
declare function getCmsEntries(options: GetCmsEntriesOptions): Promise<CmsEntriesResult>;
|
|
373
|
+
/** Fetch one published CMS entry by slug. */
|
|
374
|
+
declare function getCmsEntry(options: GetCmsEntryOptions): Promise<CmsEntry>;
|
|
375
|
+
|
|
376
|
+
/** Public listing URLs. Prefer `listing.path` from the API; fallback is `/imoveis/id-{listingCode}`. */
|
|
377
|
+
declare function listingLookupKey(raw: string): string;
|
|
378
|
+
declare function listingPublicPath(listing: {
|
|
379
|
+
path?: string | null;
|
|
380
|
+
listingCode?: string | null;
|
|
381
|
+
id?: string | null;
|
|
382
|
+
}): string;
|
|
383
|
+
|
|
384
|
+
interface ResolvePublicSiteIdOptions {
|
|
385
|
+
/** Request host (`headers().get("host")`). Port is stripped. */
|
|
386
|
+
host?: string | null;
|
|
387
|
+
/** Preview hosts only (`localhost`, `.vusercontent.net`). Live misses throw. */
|
|
388
|
+
fallback?: string | null;
|
|
389
|
+
baseUrl?: string;
|
|
390
|
+
fetch?: FetchLike;
|
|
391
|
+
}
|
|
392
|
+
declare function isStudioPreviewHost(host: string): boolean;
|
|
393
|
+
/**
|
|
394
|
+
* Live Studio sites share one v0 project env (`GARAGEM_SITE_ID` = showcase).
|
|
395
|
+
* After claim, resolve the published site by hostname; preview keeps the
|
|
396
|
+
* fallback catalog id.
|
|
397
|
+
*/
|
|
398
|
+
declare function resolvePublicSiteId(options: ResolvePublicSiteIdOptions): Promise<string>;
|
|
273
399
|
|
|
274
400
|
interface CreateGaragemClientOptions {
|
|
275
401
|
/** Garagem API base URL. Defaults to `https://app.garagem.ai`. */
|
|
@@ -311,6 +437,39 @@ interface GaragemClient {
|
|
|
311
437
|
siteId?: string;
|
|
312
438
|
next?: RequestNextOptions;
|
|
313
439
|
}): Promise<SearchListingsResult<TDocument>>;
|
|
440
|
+
/** Similar listings carousel for a detail page. */
|
|
441
|
+
getSimilarListings(args: {
|
|
442
|
+
listingId: string;
|
|
443
|
+
limit?: number;
|
|
444
|
+
siteId?: string;
|
|
445
|
+
next?: RequestNextOptions;
|
|
446
|
+
}): Promise<SimilarListingsResult>;
|
|
447
|
+
/** List CMS collections. */
|
|
448
|
+
getCmsCollections(args?: {
|
|
449
|
+
siteId?: string;
|
|
450
|
+
next?: RequestNextOptions;
|
|
451
|
+
}): Promise<CmsCollection[]>;
|
|
452
|
+
/** Fetch one CMS collection by slug. */
|
|
453
|
+
getCmsCollection(args: {
|
|
454
|
+
slug: string;
|
|
455
|
+
siteId?: string;
|
|
456
|
+
next?: RequestNextOptions;
|
|
457
|
+
}): Promise<CmsCollection>;
|
|
458
|
+
/** List published CMS entries. */
|
|
459
|
+
getCmsEntries(args: {
|
|
460
|
+
collection: string;
|
|
461
|
+
limit?: number;
|
|
462
|
+
offset?: number;
|
|
463
|
+
siteId?: string;
|
|
464
|
+
next?: RequestNextOptions;
|
|
465
|
+
}): Promise<CmsEntriesResult>;
|
|
466
|
+
/** Fetch one published CMS entry. */
|
|
467
|
+
getCmsEntry(args: {
|
|
468
|
+
collection: string;
|
|
469
|
+
slug: string;
|
|
470
|
+
siteId?: string;
|
|
471
|
+
next?: RequestNextOptions;
|
|
472
|
+
}): Promise<CmsEntry>;
|
|
314
473
|
}
|
|
315
474
|
/**
|
|
316
475
|
* Bind base URL, fetch and site to a single client object. Useful when
|
|
@@ -322,4 +481,4 @@ interface GaragemClient {
|
|
|
322
481
|
*/
|
|
323
482
|
declare function createGaragemClient(options?: CreateGaragemClientOptions): GaragemClient;
|
|
324
483
|
|
|
325
|
-
export { type CreateGaragemClientOptions, type FetchLike, type GaragemClient, GaragemSdkError, type GetListingOptions, type GetListingsOptions, type GetSiteOptions, type Listing, type ListingBusinessType, type ListingDetail, type ListingFilters, type ListingLocation, type ListingMedia, type ListingPriceVisibility, type ListingPropertySummary, type ListingsResult, type RequestNextOptions, type SearchListingsOptions, type SearchListingsParams, type SearchListingsResult, type Site, createGaragemClient, getListing, getListings, getSite, searchListings };
|
|
484
|
+
export { type CmsCollection, type CmsEntriesResult, type CmsEntry, type CmsEntryStatus, type CmsFieldDefinition, type CmsFieldType, type CmsSeoData, type CreateGaragemClientOptions, type FetchLike, type GaragemClient, GaragemSdkError, type GetCmsCollectionOptions, type GetCmsCollectionsOptions, type GetCmsEntriesOptions, type GetCmsEntryOptions, type GetListingOptions, type GetListingsOptions, type GetSimilarListingsOptions, type GetSiteOptions, type Listing, type ListingBusinessType, type ListingDetail, type ListingFilters, type ListingLocation, type ListingMedia, type ListingPriceVisibility, type ListingPropertySummary, type ListingsResult, type RequestNextOptions, type ResolvePublicSiteIdOptions, type SearchListingsOptions, type SearchListingsParams, type SearchListingsResult, type SimilarListingsResult, type Site, createGaragemClient, getCmsCollection, getCmsCollections, getCmsEntries, getCmsEntry, getListing, getListings, getSimilarListings, getSite, isStudioPreviewHost, listingLookupKey, listingPublicPath, resolvePublicSiteId, searchListings };
|
package/dist/index.js
CHANGED
|
@@ -22,13 +22,36 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
GaragemSdkError: () => GaragemSdkError,
|
|
24
24
|
createGaragemClient: () => createGaragemClient,
|
|
25
|
+
getCmsCollection: () => getCmsCollection,
|
|
26
|
+
getCmsCollections: () => getCmsCollections,
|
|
27
|
+
getCmsEntries: () => getCmsEntries,
|
|
28
|
+
getCmsEntry: () => getCmsEntry,
|
|
25
29
|
getListing: () => getListing,
|
|
26
30
|
getListings: () => getListings,
|
|
31
|
+
getSimilarListings: () => getSimilarListings,
|
|
27
32
|
getSite: () => getSite,
|
|
33
|
+
isStudioPreviewHost: () => isStudioPreviewHost,
|
|
34
|
+
listingLookupKey: () => listingLookupKey,
|
|
35
|
+
listingPublicPath: () => listingPublicPath,
|
|
36
|
+
resolvePublicSiteId: () => resolvePublicSiteId,
|
|
28
37
|
searchListings: () => searchListings
|
|
29
38
|
});
|
|
30
39
|
module.exports = __toCommonJS(index_exports);
|
|
31
40
|
|
|
41
|
+
// src/listing-path.ts
|
|
42
|
+
function listingLookupKey(raw) {
|
|
43
|
+
return raw.trim().replace(/^id-/, "").trim();
|
|
44
|
+
}
|
|
45
|
+
function listingPublicPath(listing) {
|
|
46
|
+
const composed = listing.path?.trim() ?? "";
|
|
47
|
+
if (composed.startsWith("/") && !composed.startsWith("//")) return composed;
|
|
48
|
+
const identity = listing.listingCode?.trim() || listing.id?.trim() || "";
|
|
49
|
+
if (!identity || identity === "undefined" || identity === "null") {
|
|
50
|
+
throw new TypeError("listingPublicPath: listing has no listingCode or id");
|
|
51
|
+
}
|
|
52
|
+
return `/imoveis/id-${identity}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
32
55
|
// src/internal/url.ts
|
|
33
56
|
var DEFAULT_BASE_URL = "https://app.garagem.ai";
|
|
34
57
|
function normalizeBaseUrl(baseUrl) {
|
|
@@ -44,8 +67,10 @@ function buildListingFiltersQuery(filters) {
|
|
|
44
67
|
if (filters.propertyType) params.set("propertyType", filters.propertyType);
|
|
45
68
|
if (filters.city) params.set("city", filters.city);
|
|
46
69
|
if (filters.neighborhood) params.set("neighborhood", filters.neighborhood);
|
|
47
|
-
if (filters.minPrice != null)
|
|
48
|
-
|
|
70
|
+
if (filters.minPrice != null)
|
|
71
|
+
params.set("minPrice", String(filters.minPrice));
|
|
72
|
+
if (filters.maxPrice != null)
|
|
73
|
+
params.set("maxPrice", String(filters.maxPrice));
|
|
49
74
|
return params;
|
|
50
75
|
}
|
|
51
76
|
|
|
@@ -79,7 +104,7 @@ async function getListings(options) {
|
|
|
79
104
|
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings${qs}`;
|
|
80
105
|
const data = await callJson(
|
|
81
106
|
url,
|
|
82
|
-
{ method: "GET", headers: {
|
|
107
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
83
108
|
fetchImpl ?? fetch
|
|
84
109
|
);
|
|
85
110
|
return {
|
|
@@ -88,14 +113,24 @@ async function getListings(options) {
|
|
|
88
113
|
};
|
|
89
114
|
}
|
|
90
115
|
async function getListing(options) {
|
|
91
|
-
const {
|
|
116
|
+
const {
|
|
117
|
+
siteId,
|
|
118
|
+
listingId: rawListingId,
|
|
119
|
+
baseUrl,
|
|
120
|
+
fetch: fetchImpl,
|
|
121
|
+
next
|
|
122
|
+
} = options;
|
|
123
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
124
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
125
|
+
throw new TypeError("getListing: `listingId` is required");
|
|
126
|
+
}
|
|
92
127
|
const base = normalizeBaseUrl(baseUrl);
|
|
93
128
|
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
94
129
|
listingId
|
|
95
130
|
)}`;
|
|
96
131
|
return callJson(
|
|
97
132
|
url,
|
|
98
|
-
{ method: "GET", headers: {
|
|
133
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
99
134
|
fetchImpl ?? fetch
|
|
100
135
|
);
|
|
101
136
|
}
|
|
@@ -119,7 +154,7 @@ async function getSite(options) {
|
|
|
119
154
|
const f = fetchImpl ?? fetch;
|
|
120
155
|
const response = await f(url, {
|
|
121
156
|
method: "GET",
|
|
122
|
-
headers: {
|
|
157
|
+
headers: { Accept: "application/json" },
|
|
123
158
|
next
|
|
124
159
|
});
|
|
125
160
|
if (response.status === 404) return null;
|
|
@@ -157,6 +192,159 @@ async function searchListings(options) {
|
|
|
157
192
|
fetchImpl ?? fetch
|
|
158
193
|
);
|
|
159
194
|
}
|
|
195
|
+
async function getSimilarListings(options) {
|
|
196
|
+
const {
|
|
197
|
+
siteId,
|
|
198
|
+
listingId: rawListingId,
|
|
199
|
+
limit,
|
|
200
|
+
baseUrl,
|
|
201
|
+
fetch: fetchImpl,
|
|
202
|
+
next
|
|
203
|
+
} = options;
|
|
204
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
205
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
206
|
+
throw new TypeError("getSimilarListings: `listingId` is required");
|
|
207
|
+
}
|
|
208
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
209
|
+
const params = new URLSearchParams();
|
|
210
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
211
|
+
params.set("limit", String(Math.floor(limit)));
|
|
212
|
+
}
|
|
213
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
214
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
215
|
+
listingId
|
|
216
|
+
)}/similar${qs}`;
|
|
217
|
+
const data = await callJson(
|
|
218
|
+
url,
|
|
219
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
220
|
+
fetchImpl ?? fetch
|
|
221
|
+
);
|
|
222
|
+
return {
|
|
223
|
+
listings: data.listings ?? [],
|
|
224
|
+
total: data.total ?? 0
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
async function getCmsCollections(options) {
|
|
228
|
+
const { siteId, baseUrl, fetch: fetchImpl, next } = options;
|
|
229
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
230
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections`;
|
|
231
|
+
const data = await callJson(
|
|
232
|
+
url,
|
|
233
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
234
|
+
fetchImpl ?? fetch
|
|
235
|
+
);
|
|
236
|
+
return data.collections ?? [];
|
|
237
|
+
}
|
|
238
|
+
async function getCmsCollection(options) {
|
|
239
|
+
const { siteId, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
240
|
+
if (!slug) {
|
|
241
|
+
throw new TypeError("getCmsCollection: `slug` is required");
|
|
242
|
+
}
|
|
243
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
244
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections/${encodeURIComponent(
|
|
245
|
+
slug
|
|
246
|
+
)}`;
|
|
247
|
+
const data = await callJson(
|
|
248
|
+
url,
|
|
249
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
250
|
+
fetchImpl ?? fetch
|
|
251
|
+
);
|
|
252
|
+
if (data && typeof data === "object" && "collection" in data && data.collection) {
|
|
253
|
+
return data.collection;
|
|
254
|
+
}
|
|
255
|
+
return data;
|
|
256
|
+
}
|
|
257
|
+
async function getCmsEntries(options) {
|
|
258
|
+
const {
|
|
259
|
+
siteId,
|
|
260
|
+
collection,
|
|
261
|
+
limit,
|
|
262
|
+
offset,
|
|
263
|
+
baseUrl,
|
|
264
|
+
fetch: fetchImpl,
|
|
265
|
+
next
|
|
266
|
+
} = options;
|
|
267
|
+
if (!collection) {
|
|
268
|
+
throw new TypeError("getCmsEntries: `collection` is required");
|
|
269
|
+
}
|
|
270
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
271
|
+
const params = new URLSearchParams();
|
|
272
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
273
|
+
params.set("limit", String(Math.floor(limit)));
|
|
274
|
+
}
|
|
275
|
+
if (typeof offset === "number" && Number.isFinite(offset) && offset >= 0) {
|
|
276
|
+
params.set("offset", String(Math.floor(offset)));
|
|
277
|
+
}
|
|
278
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
279
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
280
|
+
collection
|
|
281
|
+
)}/entries${qs}`;
|
|
282
|
+
const data = await callJson(
|
|
283
|
+
url,
|
|
284
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
285
|
+
fetchImpl ?? fetch
|
|
286
|
+
);
|
|
287
|
+
return {
|
|
288
|
+
entries: data.entries ?? [],
|
|
289
|
+
total: data.total ?? 0,
|
|
290
|
+
limit: data.limit ?? 0,
|
|
291
|
+
offset: data.offset ?? 0
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
async function getCmsEntry(options) {
|
|
295
|
+
const { siteId, collection, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
296
|
+
if (!collection) {
|
|
297
|
+
throw new TypeError("getCmsEntry: `collection` is required");
|
|
298
|
+
}
|
|
299
|
+
if (!slug) {
|
|
300
|
+
throw new TypeError("getCmsEntry: `slug` is required");
|
|
301
|
+
}
|
|
302
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
303
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
304
|
+
collection
|
|
305
|
+
)}/entries/${encodeURIComponent(slug)}`;
|
|
306
|
+
const data = await callJson(
|
|
307
|
+
url,
|
|
308
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
309
|
+
fetchImpl ?? fetch
|
|
310
|
+
);
|
|
311
|
+
if (data && typeof data === "object" && "entry" in data && data.entry) {
|
|
312
|
+
return data.entry;
|
|
313
|
+
}
|
|
314
|
+
return data;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// src/resolve-site-id.ts
|
|
318
|
+
var PREVIEW_HOST_SUFFIXES = [".vusercontent.net"];
|
|
319
|
+
function isStudioPreviewHost(host) {
|
|
320
|
+
const normalized = stripPort(host);
|
|
321
|
+
if (normalized === "localhost" || normalized === "127.0.0.1") return true;
|
|
322
|
+
return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
|
|
323
|
+
}
|
|
324
|
+
function stripPort(host) {
|
|
325
|
+
return host.trim().toLowerCase().split(":")[0] ?? "";
|
|
326
|
+
}
|
|
327
|
+
async function resolvePublicSiteId(options) {
|
|
328
|
+
const fallback = options.fallback?.trim() ?? "";
|
|
329
|
+
const host = options.host ? stripPort(options.host) : "";
|
|
330
|
+
if (host && !isStudioPreviewHost(host)) {
|
|
331
|
+
const site = await getSite({
|
|
332
|
+
domain: host,
|
|
333
|
+
baseUrl: options.baseUrl,
|
|
334
|
+
fetch: options.fetch
|
|
335
|
+
});
|
|
336
|
+
if (site?.id) return site.id;
|
|
337
|
+
throw new GaragemSdkError(
|
|
338
|
+
`Garagem SDK: no published site found for domain "${host}"`,
|
|
339
|
+
{ status: 404, url: host }
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (fallback) return fallback;
|
|
343
|
+
throw new GaragemSdkError(
|
|
344
|
+
"Garagem SDK: could not resolve a site id (no host match and no fallback)",
|
|
345
|
+
{ status: 404, url: host || "fallback" }
|
|
346
|
+
);
|
|
347
|
+
}
|
|
160
348
|
|
|
161
349
|
// src/client.ts
|
|
162
350
|
function createGaragemClient(options = {}) {
|
|
@@ -173,26 +361,28 @@ function createGaragemClient(options = {}) {
|
|
|
173
361
|
if (!options.next && !callNext) return void 0;
|
|
174
362
|
return { ...options.next, ...callNext };
|
|
175
363
|
};
|
|
364
|
+
const withDefaults = (opts) => {
|
|
365
|
+
const out = { ...opts };
|
|
366
|
+
if (options.baseUrl !== void 0) out.baseUrl = options.baseUrl;
|
|
367
|
+
if (options.fetch !== void 0) out.fetch = options.fetch;
|
|
368
|
+
return out;
|
|
369
|
+
};
|
|
176
370
|
return {
|
|
177
371
|
options: Object.freeze({ ...options }),
|
|
178
372
|
async getListings({ filters, siteId, next } = {}) {
|
|
179
|
-
const opts = {
|
|
373
|
+
const opts = withDefaults({
|
|
180
374
|
siteId: resolveSiteId(siteId)
|
|
181
|
-
};
|
|
375
|
+
});
|
|
182
376
|
if (filters !== void 0) opts.filters = filters;
|
|
183
|
-
if (options.baseUrl !== void 0) opts.baseUrl = options.baseUrl;
|
|
184
|
-
if (options.fetch !== void 0) opts.fetch = options.fetch;
|
|
185
377
|
const merged = mergeNext(next);
|
|
186
378
|
if (merged !== void 0) opts.next = merged;
|
|
187
379
|
return getListings(opts);
|
|
188
380
|
},
|
|
189
381
|
async getListing({ listingId, siteId, next }) {
|
|
190
|
-
const opts = {
|
|
382
|
+
const opts = withDefaults({
|
|
191
383
|
siteId: resolveSiteId(siteId),
|
|
192
384
|
listingId
|
|
193
|
-
};
|
|
194
|
-
if (options.baseUrl !== void 0) opts.baseUrl = options.baseUrl;
|
|
195
|
-
if (options.fetch !== void 0) opts.fetch = options.fetch;
|
|
385
|
+
});
|
|
196
386
|
const merged = mergeNext(next);
|
|
197
387
|
if (merged !== void 0) opts.next = merged;
|
|
198
388
|
return getListing(opts);
|
|
@@ -212,15 +402,61 @@ function createGaragemClient(options = {}) {
|
|
|
212
402
|
siteId,
|
|
213
403
|
next
|
|
214
404
|
}) {
|
|
215
|
-
const opts = {
|
|
405
|
+
const opts = withDefaults({
|
|
216
406
|
siteId: resolveSiteId(siteId),
|
|
217
407
|
params
|
|
218
|
-
};
|
|
219
|
-
if (options.baseUrl !== void 0) opts.baseUrl = options.baseUrl;
|
|
220
|
-
if (options.fetch !== void 0) opts.fetch = options.fetch;
|
|
408
|
+
});
|
|
221
409
|
const merged = mergeNext(next);
|
|
222
410
|
if (merged !== void 0) opts.next = merged;
|
|
223
411
|
return searchListings(opts);
|
|
412
|
+
},
|
|
413
|
+
async getSimilarListings({ listingId, limit, siteId, next }) {
|
|
414
|
+
const opts = withDefaults({
|
|
415
|
+
siteId: resolveSiteId(siteId),
|
|
416
|
+
listingId
|
|
417
|
+
});
|
|
418
|
+
if (limit !== void 0) opts.limit = limit;
|
|
419
|
+
const merged = mergeNext(next);
|
|
420
|
+
if (merged !== void 0) opts.next = merged;
|
|
421
|
+
return getSimilarListings(opts);
|
|
422
|
+
},
|
|
423
|
+
async getCmsCollections({ siteId, next } = {}) {
|
|
424
|
+
const opts = withDefaults({
|
|
425
|
+
siteId: resolveSiteId(siteId)
|
|
426
|
+
});
|
|
427
|
+
const merged = mergeNext(next);
|
|
428
|
+
if (merged !== void 0) opts.next = merged;
|
|
429
|
+
return getCmsCollections(opts);
|
|
430
|
+
},
|
|
431
|
+
async getCmsCollection({ slug, siteId, next }) {
|
|
432
|
+
const opts = withDefaults({
|
|
433
|
+
siteId: resolveSiteId(siteId),
|
|
434
|
+
slug
|
|
435
|
+
});
|
|
436
|
+
const merged = mergeNext(next);
|
|
437
|
+
if (merged !== void 0) opts.next = merged;
|
|
438
|
+
return getCmsCollection(opts);
|
|
439
|
+
},
|
|
440
|
+
async getCmsEntries({ collection, limit, offset, siteId, next }) {
|
|
441
|
+
const opts = withDefaults({
|
|
442
|
+
siteId: resolveSiteId(siteId),
|
|
443
|
+
collection
|
|
444
|
+
});
|
|
445
|
+
if (limit !== void 0) opts.limit = limit;
|
|
446
|
+
if (offset !== void 0) opts.offset = offset;
|
|
447
|
+
const merged = mergeNext(next);
|
|
448
|
+
if (merged !== void 0) opts.next = merged;
|
|
449
|
+
return getCmsEntries(opts);
|
|
450
|
+
},
|
|
451
|
+
async getCmsEntry({ collection, slug, siteId, next }) {
|
|
452
|
+
const opts = withDefaults({
|
|
453
|
+
siteId: resolveSiteId(siteId),
|
|
454
|
+
collection,
|
|
455
|
+
slug
|
|
456
|
+
});
|
|
457
|
+
const merged = mergeNext(next);
|
|
458
|
+
if (merged !== void 0) opts.next = merged;
|
|
459
|
+
return getCmsEntry(opts);
|
|
224
460
|
}
|
|
225
461
|
};
|
|
226
462
|
}
|
|
@@ -228,9 +464,18 @@ function createGaragemClient(options = {}) {
|
|
|
228
464
|
0 && (module.exports = {
|
|
229
465
|
GaragemSdkError,
|
|
230
466
|
createGaragemClient,
|
|
467
|
+
getCmsCollection,
|
|
468
|
+
getCmsCollections,
|
|
469
|
+
getCmsEntries,
|
|
470
|
+
getCmsEntry,
|
|
231
471
|
getListing,
|
|
232
472
|
getListings,
|
|
473
|
+
getSimilarListings,
|
|
233
474
|
getSite,
|
|
475
|
+
isStudioPreviewHost,
|
|
476
|
+
listingLookupKey,
|
|
477
|
+
listingPublicPath,
|
|
478
|
+
resolvePublicSiteId,
|
|
234
479
|
searchListings
|
|
235
480
|
});
|
|
236
481
|
//# sourceMappingURL=index.js.map
|