@garagem-ai/site-sdk 0.1.1 → 0.3.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 +347 -8
- package/dist/index.d.ts +347 -8
- package/dist/index.js +783 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +762 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +19 -6
- package/src/applied-query.ts +42 -0
- package/src/client.ts +174 -29
- package/src/fetchers.ts +259 -16
- package/src/index.ts +61 -0
- package/src/internal/url.ts +4 -2
- package/src/listing-path.ts +19 -0
- package/src/loc-params.ts +36 -0
- package/src/path.ts +73 -0
- package/src/resolve-site-id.ts +55 -0
- package/src/types.ts +187 -4
package/src/fetchers.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
CmsCollection,
|
|
3
|
+
CmsEntriesResult,
|
|
4
|
+
CmsEntry,
|
|
2
5
|
Listing,
|
|
3
6
|
ListingDetail,
|
|
4
7
|
ListingFilters,
|
|
5
8
|
ListingsResult,
|
|
9
|
+
QueryListingsParams,
|
|
10
|
+
QueryListingsResult,
|
|
6
11
|
RequestNextOptions,
|
|
7
12
|
SearchListingsParams,
|
|
8
13
|
SearchListingsResult,
|
|
14
|
+
SearchUi,
|
|
15
|
+
SimilarListingsResult,
|
|
9
16
|
Site,
|
|
10
17
|
} from "./types";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
normalizeBaseUrl,
|
|
14
|
-
} from "./internal/url";
|
|
18
|
+
import { listingLookupKey } from "./listing-path";
|
|
19
|
+
import { buildListingFiltersQuery, normalizeBaseUrl } from "./internal/url";
|
|
15
20
|
|
|
16
21
|
/** Custom fetch implementation. Defaults to the global `fetch`. */
|
|
17
22
|
export type FetchLike = typeof fetch;
|
|
@@ -50,6 +55,44 @@ export interface SearchListingsOptions extends BaseOptions {
|
|
|
50
55
|
params: SearchListingsParams;
|
|
51
56
|
}
|
|
52
57
|
|
|
58
|
+
export interface GetSearchUiOptions extends BaseOptions {
|
|
59
|
+
siteId: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface QueryListingsOptions extends BaseOptions {
|
|
63
|
+
siteId: string;
|
|
64
|
+
params?: QueryListingsParams;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface GetSimilarListingsOptions extends BaseOptions {
|
|
68
|
+
siteId: string;
|
|
69
|
+
listingId: string;
|
|
70
|
+
/** Max similar cards to return (API default 6, cap 12). */
|
|
71
|
+
limit?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface GetCmsCollectionsOptions extends BaseOptions {
|
|
75
|
+
siteId: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface GetCmsCollectionOptions extends BaseOptions {
|
|
79
|
+
siteId: string;
|
|
80
|
+
slug: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface GetCmsEntriesOptions extends BaseOptions {
|
|
84
|
+
siteId: string;
|
|
85
|
+
collection: string;
|
|
86
|
+
limit?: number;
|
|
87
|
+
offset?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface GetCmsEntryOptions extends BaseOptions {
|
|
91
|
+
siteId: string;
|
|
92
|
+
collection: string;
|
|
93
|
+
slug: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
53
96
|
interface FetchInit extends RequestInit {
|
|
54
97
|
/**
|
|
55
98
|
* Next.js attaches this on `fetch` (App Router). Adding it here keeps
|
|
@@ -109,7 +152,7 @@ export async function getListings(
|
|
|
109
152
|
|
|
110
153
|
const data = await callJson<{ listings?: Listing[]; total?: number }>(
|
|
111
154
|
url,
|
|
112
|
-
{ method: "GET", headers: {
|
|
155
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
113
156
|
fetchImpl ?? fetch
|
|
114
157
|
);
|
|
115
158
|
return {
|
|
@@ -120,11 +163,22 @@ export async function getListings(
|
|
|
120
163
|
|
|
121
164
|
/**
|
|
122
165
|
* Fetch a single listing's full detail (amenities, gallery, building).
|
|
166
|
+
* `listingId` accepts a code, UUID, or `id-{code}` URL segment.
|
|
123
167
|
*/
|
|
124
168
|
export async function getListing(
|
|
125
169
|
options: GetListingOptions
|
|
126
170
|
): Promise<ListingDetail> {
|
|
127
|
-
const {
|
|
171
|
+
const {
|
|
172
|
+
siteId,
|
|
173
|
+
listingId: rawListingId,
|
|
174
|
+
baseUrl,
|
|
175
|
+
fetch: fetchImpl,
|
|
176
|
+
next,
|
|
177
|
+
} = options;
|
|
178
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
179
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
180
|
+
throw new TypeError("getListing: `listingId` is required");
|
|
181
|
+
}
|
|
128
182
|
const base = normalizeBaseUrl(baseUrl);
|
|
129
183
|
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
130
184
|
listingId
|
|
@@ -132,7 +186,7 @@ export async function getListing(
|
|
|
132
186
|
|
|
133
187
|
return callJson<ListingDetail>(
|
|
134
188
|
url,
|
|
135
|
-
{ method: "GET", headers: {
|
|
189
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
136
190
|
fetchImpl ?? fetch
|
|
137
191
|
);
|
|
138
192
|
}
|
|
@@ -145,9 +199,7 @@ export async function getListing(
|
|
|
145
199
|
* const site = await getSite({ domain: "imobiliariax.com.br" });
|
|
146
200
|
* if (!site) return notFound();
|
|
147
201
|
*/
|
|
148
|
-
export async function getSite(
|
|
149
|
-
options: GetSiteOptions
|
|
150
|
-
): Promise<Site | null> {
|
|
202
|
+
export async function getSite(options: GetSiteOptions): Promise<Site | null> {
|
|
151
203
|
const { siteId, domain, baseUrl, fetch: fetchImpl, next } = options;
|
|
152
204
|
if (!siteId && !domain) {
|
|
153
205
|
throw new TypeError(
|
|
@@ -169,7 +221,7 @@ export async function getSite(
|
|
|
169
221
|
const f = fetchImpl ?? fetch;
|
|
170
222
|
const response = await f(url, {
|
|
171
223
|
method: "GET",
|
|
172
|
-
headers: {
|
|
224
|
+
headers: { Accept: "application/json" },
|
|
173
225
|
next,
|
|
174
226
|
} as FetchInit);
|
|
175
227
|
if (response.status === 404) return null;
|
|
@@ -198,8 +250,8 @@ export async function getSite(
|
|
|
198
250
|
*
|
|
199
251
|
* Calls `POST /api/sites/{siteId}/search` directly and returns the raw
|
|
200
252
|
* Typesense response (`{ found, page, hits: [{ document, ... }], ... }`).
|
|
201
|
-
* Use when
|
|
202
|
-
*
|
|
253
|
+
* Use when `queryListings` isn't expressive enough — faceted search,
|
|
254
|
+
* multi-field sort, custom grouping, etc.
|
|
203
255
|
*
|
|
204
256
|
* @example
|
|
205
257
|
* const result = await searchListings({
|
|
@@ -218,9 +270,7 @@ export async function getSite(
|
|
|
218
270
|
* console.log(hit.document, hit.highlight);
|
|
219
271
|
* }
|
|
220
272
|
*/
|
|
221
|
-
export async function searchListings<
|
|
222
|
-
TDocument = Record<string, unknown>,
|
|
223
|
-
>(
|
|
273
|
+
export async function searchListings<TDocument = Record<string, unknown>>(
|
|
224
274
|
options: SearchListingsOptions
|
|
225
275
|
): Promise<SearchListingsResult<TDocument>> {
|
|
226
276
|
const { siteId, params, baseUrl, fetch: fetchImpl, next } = options;
|
|
@@ -242,3 +292,196 @@ export async function searchListings<
|
|
|
242
292
|
fetchImpl ?? fetch
|
|
243
293
|
);
|
|
244
294
|
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Tenant search schema: filters, slugs, sort tokens, defaultLimit 12.
|
|
298
|
+
* Use this to render the search UI — do not hardcode property types.
|
|
299
|
+
*/
|
|
300
|
+
export async function getSearchUi(
|
|
301
|
+
options: GetSearchUiOptions
|
|
302
|
+
): Promise<SearchUi> {
|
|
303
|
+
const { siteId, baseUrl, fetch: fetchImpl, next } = options;
|
|
304
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
305
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/search-ui`;
|
|
306
|
+
return callJson<SearchUi>(
|
|
307
|
+
url,
|
|
308
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
309
|
+
fetchImpl ?? fetch
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Friendly catalog search (editor combinators). Prefer this over
|
|
315
|
+
* `searchListings` — that helper is a raw Typesense escape hatch.
|
|
316
|
+
*/
|
|
317
|
+
export async function queryListings(
|
|
318
|
+
options: QueryListingsOptions
|
|
319
|
+
): Promise<QueryListingsResult> {
|
|
320
|
+
const { siteId, params, baseUrl, fetch: fetchImpl, next } = options;
|
|
321
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
322
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/search/listings`;
|
|
323
|
+
return callJson<QueryListingsResult>(
|
|
324
|
+
url,
|
|
325
|
+
{
|
|
326
|
+
method: "POST",
|
|
327
|
+
headers: {
|
|
328
|
+
Accept: "application/json",
|
|
329
|
+
"Content-Type": "application/json",
|
|
330
|
+
},
|
|
331
|
+
body: JSON.stringify(params ?? {}),
|
|
332
|
+
next,
|
|
333
|
+
},
|
|
334
|
+
fetchImpl ?? fetch
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Fetch similar listings for a detail page carousel.
|
|
340
|
+
* Returns an empty list when the reference listing is missing.
|
|
341
|
+
*/
|
|
342
|
+
export async function getSimilarListings(
|
|
343
|
+
options: GetSimilarListingsOptions
|
|
344
|
+
): Promise<SimilarListingsResult> {
|
|
345
|
+
const {
|
|
346
|
+
siteId,
|
|
347
|
+
listingId: rawListingId,
|
|
348
|
+
limit,
|
|
349
|
+
baseUrl,
|
|
350
|
+
fetch: fetchImpl,
|
|
351
|
+
next,
|
|
352
|
+
} = options;
|
|
353
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
354
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
355
|
+
throw new TypeError("getSimilarListings: `listingId` is required");
|
|
356
|
+
}
|
|
357
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
358
|
+
const params = new URLSearchParams();
|
|
359
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
360
|
+
params.set("limit", String(Math.floor(limit)));
|
|
361
|
+
}
|
|
362
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
363
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
364
|
+
listingId
|
|
365
|
+
)}/similar${qs}`;
|
|
366
|
+
|
|
367
|
+
const data = await callJson<{ listings?: Listing[]; total?: number }>(
|
|
368
|
+
url,
|
|
369
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
370
|
+
fetchImpl ?? fetch
|
|
371
|
+
);
|
|
372
|
+
return {
|
|
373
|
+
listings: data.listings ?? [],
|
|
374
|
+
total: data.total ?? 0,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** List CMS collections for a site. */
|
|
379
|
+
export async function getCmsCollections(
|
|
380
|
+
options: GetCmsCollectionsOptions
|
|
381
|
+
): Promise<CmsCollection[]> {
|
|
382
|
+
const { siteId, baseUrl, fetch: fetchImpl, next } = options;
|
|
383
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
384
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections`;
|
|
385
|
+
const data = await callJson<{ collections?: CmsCollection[] }>(
|
|
386
|
+
url,
|
|
387
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
388
|
+
fetchImpl ?? fetch
|
|
389
|
+
);
|
|
390
|
+
return data.collections ?? [];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Fetch one CMS collection schema by slug. */
|
|
394
|
+
export async function getCmsCollection(
|
|
395
|
+
options: GetCmsCollectionOptions
|
|
396
|
+
): Promise<CmsCollection> {
|
|
397
|
+
const { siteId, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
398
|
+
if (!slug) {
|
|
399
|
+
throw new TypeError("getCmsCollection: `slug` is required");
|
|
400
|
+
}
|
|
401
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
402
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections/${encodeURIComponent(
|
|
403
|
+
slug
|
|
404
|
+
)}`;
|
|
405
|
+
const data = await callJson<{ collection?: CmsCollection } | CmsCollection>(
|
|
406
|
+
url,
|
|
407
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
408
|
+
fetchImpl ?? fetch
|
|
409
|
+
);
|
|
410
|
+
if (
|
|
411
|
+
data &&
|
|
412
|
+
typeof data === "object" &&
|
|
413
|
+
"collection" in data &&
|
|
414
|
+
data.collection
|
|
415
|
+
) {
|
|
416
|
+
return data.collection;
|
|
417
|
+
}
|
|
418
|
+
return data as CmsCollection;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** List published CMS entries for a collection. */
|
|
422
|
+
export async function getCmsEntries(
|
|
423
|
+
options: GetCmsEntriesOptions
|
|
424
|
+
): Promise<CmsEntriesResult> {
|
|
425
|
+
const {
|
|
426
|
+
siteId,
|
|
427
|
+
collection,
|
|
428
|
+
limit,
|
|
429
|
+
offset,
|
|
430
|
+
baseUrl,
|
|
431
|
+
fetch: fetchImpl,
|
|
432
|
+
next,
|
|
433
|
+
} = options;
|
|
434
|
+
if (!collection) {
|
|
435
|
+
throw new TypeError("getCmsEntries: `collection` is required");
|
|
436
|
+
}
|
|
437
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
438
|
+
const params = new URLSearchParams();
|
|
439
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
440
|
+
params.set("limit", String(Math.floor(limit)));
|
|
441
|
+
}
|
|
442
|
+
if (typeof offset === "number" && Number.isFinite(offset) && offset >= 0) {
|
|
443
|
+
params.set("offset", String(Math.floor(offset)));
|
|
444
|
+
}
|
|
445
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
446
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
447
|
+
collection
|
|
448
|
+
)}/entries${qs}`;
|
|
449
|
+
|
|
450
|
+
const data = await callJson<Partial<CmsEntriesResult>>(
|
|
451
|
+
url,
|
|
452
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
453
|
+
fetchImpl ?? fetch
|
|
454
|
+
);
|
|
455
|
+
return {
|
|
456
|
+
entries: data.entries ?? [],
|
|
457
|
+
total: data.total ?? 0,
|
|
458
|
+
limit: data.limit ?? 0,
|
|
459
|
+
offset: data.offset ?? 0,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Fetch one published CMS entry by slug. */
|
|
464
|
+
export async function getCmsEntry(
|
|
465
|
+
options: GetCmsEntryOptions
|
|
466
|
+
): Promise<CmsEntry> {
|
|
467
|
+
const { siteId, collection, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
468
|
+
if (!collection) {
|
|
469
|
+
throw new TypeError("getCmsEntry: `collection` is required");
|
|
470
|
+
}
|
|
471
|
+
if (!slug) {
|
|
472
|
+
throw new TypeError("getCmsEntry: `slug` is required");
|
|
473
|
+
}
|
|
474
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
475
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
476
|
+
collection
|
|
477
|
+
)}/entries/${encodeURIComponent(slug)}`;
|
|
478
|
+
const data = await callJson<{ entry?: CmsEntry } | CmsEntry>(
|
|
479
|
+
url,
|
|
480
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
481
|
+
fetchImpl ?? fetch
|
|
482
|
+
);
|
|
483
|
+
if (data && typeof data === "object" && "entry" in data && data.entry) {
|
|
484
|
+
return data.entry;
|
|
485
|
+
}
|
|
486
|
+
return data as CmsEntry;
|
|
487
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,15 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
export type {
|
|
23
|
+
AppliedSearchChange,
|
|
24
|
+
AppliedSearchQuery,
|
|
25
|
+
CmsCollection,
|
|
26
|
+
CmsEntriesResult,
|
|
27
|
+
CmsEntry,
|
|
28
|
+
CmsEntryStatus,
|
|
29
|
+
CmsFieldDefinition,
|
|
30
|
+
CmsFieldType,
|
|
31
|
+
CmsSeoData,
|
|
23
32
|
Listing,
|
|
24
33
|
ListingBusinessType,
|
|
25
34
|
ListingDetail,
|
|
@@ -29,25 +38,77 @@ export type {
|
|
|
29
38
|
ListingPriceVisibility,
|
|
30
39
|
ListingPropertySummary,
|
|
31
40
|
ListingsResult,
|
|
41
|
+
QueryListingsFilters,
|
|
42
|
+
QueryListingsLocation,
|
|
43
|
+
QueryListingsParams,
|
|
44
|
+
QueryListingsResult,
|
|
32
45
|
RequestNextOptions,
|
|
33
46
|
SearchListingsParams,
|
|
34
47
|
SearchListingsResult,
|
|
48
|
+
SearchPathFilters,
|
|
49
|
+
SearchPathLocation,
|
|
50
|
+
SearchUi,
|
|
51
|
+
SearchUiDictionary,
|
|
52
|
+
SearchUiFilter,
|
|
53
|
+
SearchUiOption,
|
|
54
|
+
SimilarListingsResult,
|
|
35
55
|
Site,
|
|
36
56
|
} from "./types";
|
|
37
57
|
|
|
38
58
|
export {
|
|
59
|
+
getCmsCollection,
|
|
60
|
+
getCmsCollections,
|
|
61
|
+
getCmsEntries,
|
|
62
|
+
getCmsEntry,
|
|
39
63
|
getListing,
|
|
40
64
|
getListings,
|
|
65
|
+
getSearchUi,
|
|
66
|
+
getSimilarListings,
|
|
41
67
|
getSite,
|
|
68
|
+
queryListings,
|
|
42
69
|
searchListings,
|
|
43
70
|
GaragemSdkError,
|
|
44
71
|
type FetchLike,
|
|
72
|
+
type GetCmsCollectionOptions,
|
|
73
|
+
type GetCmsCollectionsOptions,
|
|
74
|
+
type GetCmsEntriesOptions,
|
|
75
|
+
type GetCmsEntryOptions,
|
|
45
76
|
type GetListingOptions,
|
|
46
77
|
type GetListingsOptions,
|
|
78
|
+
type GetSearchUiOptions,
|
|
79
|
+
type GetSimilarListingsOptions,
|
|
47
80
|
type GetSiteOptions,
|
|
81
|
+
type QueryListingsOptions,
|
|
48
82
|
type SearchListingsOptions,
|
|
49
83
|
} from "./fetchers";
|
|
50
84
|
|
|
85
|
+
export { listingLookupKey, listingPublicPath } from "./listing-path";
|
|
86
|
+
|
|
87
|
+
export {
|
|
88
|
+
composeSearchPublicPath,
|
|
89
|
+
parseSearchPublicPath,
|
|
90
|
+
pathFiltersToQueryFilters,
|
|
91
|
+
} from "./path";
|
|
92
|
+
|
|
93
|
+
export {
|
|
94
|
+
composeSearchPublicHref,
|
|
95
|
+
mergeAppliedSearchQueryIntoSearch,
|
|
96
|
+
parseAppliedSearchQuery,
|
|
97
|
+
reduceAppliedSearchQuery,
|
|
98
|
+
serializeAppliedSearchQuery,
|
|
99
|
+
} from "./applied-query";
|
|
100
|
+
|
|
101
|
+
export {
|
|
102
|
+
pathLocationToFilterInput,
|
|
103
|
+
type LocationFilterInput,
|
|
104
|
+
} from "./loc-params";
|
|
105
|
+
|
|
106
|
+
export {
|
|
107
|
+
resolvePublicSiteId,
|
|
108
|
+
isStudioPreviewHost,
|
|
109
|
+
type ResolvePublicSiteIdOptions,
|
|
110
|
+
} from "./resolve-site-id";
|
|
111
|
+
|
|
51
112
|
export {
|
|
52
113
|
createGaragemClient,
|
|
53
114
|
type CreateGaragemClientOptions,
|
package/src/internal/url.ts
CHANGED
|
@@ -25,8 +25,10 @@ export function buildListingFiltersQuery(
|
|
|
25
25
|
if (filters.propertyType) params.set("propertyType", filters.propertyType);
|
|
26
26
|
if (filters.city) params.set("city", filters.city);
|
|
27
27
|
if (filters.neighborhood) params.set("neighborhood", filters.neighborhood);
|
|
28
|
-
if (filters.minPrice != null)
|
|
29
|
-
|
|
28
|
+
if (filters.minPrice != null)
|
|
29
|
+
params.set("minPrice", String(filters.minPrice));
|
|
30
|
+
if (filters.maxPrice != null)
|
|
31
|
+
params.set("maxPrice", String(filters.maxPrice));
|
|
30
32
|
|
|
31
33
|
return params;
|
|
32
34
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Public listing URLs. Prefer `listing.path` from the API; fallback is `/imoveis/id-{listingCode}`. */
|
|
2
|
+
|
|
3
|
+
export function listingLookupKey(raw: string): string {
|
|
4
|
+
return raw.trim().replace(/^id-/, "").trim();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function listingPublicPath(listing: {
|
|
8
|
+
path?: string | null;
|
|
9
|
+
listingCode?: string | null;
|
|
10
|
+
id?: string | null;
|
|
11
|
+
}): string {
|
|
12
|
+
const composed = listing.path?.trim() ?? "";
|
|
13
|
+
if (composed.startsWith("/") && !composed.startsWith("//")) return composed;
|
|
14
|
+
const identity = listing.listingCode?.trim() || listing.id?.trim() || "";
|
|
15
|
+
if (!identity || identity === "undefined" || identity === "null") {
|
|
16
|
+
throw new TypeError("listingPublicPath: listing has no listingCode or id");
|
|
17
|
+
}
|
|
18
|
+
return `/imoveis/id-${identity}`;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Re-exported from @garagem/site-search to avoid publishing the private package
|
|
2
|
+
export type LocationFilterInput = {
|
|
3
|
+
type: string;
|
|
4
|
+
value: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Convert a SearchPathLocation (from parseSearchPublicPath) to the correct
|
|
9
|
+
* LocationFilterInput format for the friendly search API. This ensures that
|
|
10
|
+
* hierarchical location filters are ANDed correctly instead of ORed.
|
|
11
|
+
*/
|
|
12
|
+
export function pathLocationToFilterInput(location: {
|
|
13
|
+
state?: string;
|
|
14
|
+
city?: string;
|
|
15
|
+
neighborhood?: string;
|
|
16
|
+
street?: string;
|
|
17
|
+
}): LocationFilterInput | null {
|
|
18
|
+
if (!location || typeof location !== "object") return null;
|
|
19
|
+
|
|
20
|
+
const { state, city, neighborhood, street } = location;
|
|
21
|
+
const levels: string[] = [];
|
|
22
|
+
if (state) levels.push(state);
|
|
23
|
+
if (city) levels.push(city);
|
|
24
|
+
if (neighborhood) levels.push(neighborhood);
|
|
25
|
+
if (street) levels.push(street);
|
|
26
|
+
|
|
27
|
+
if (levels.length === 0) return null;
|
|
28
|
+
if (levels.length === 1) {
|
|
29
|
+
if (state) return { type: "state", value: state };
|
|
30
|
+
if (city) return { type: "city", value: city };
|
|
31
|
+
if (neighborhood) return { type: "neighborhood", value: neighborhood };
|
|
32
|
+
if (street) return { type: "street", value: street };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return { type: "path", value: levels.join("/") };
|
|
36
|
+
}
|
package/src/path.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
composeSearchPublicPath as composePath,
|
|
3
|
+
parseSearchPublicPath as parsePath,
|
|
4
|
+
} from "@garagem/search-url";
|
|
5
|
+
import type {
|
|
6
|
+
QueryListingsFilters,
|
|
7
|
+
SearchPathFilters,
|
|
8
|
+
SearchUiDictionary,
|
|
9
|
+
} from "./types";
|
|
10
|
+
|
|
11
|
+
export function composeSearchPublicPath(
|
|
12
|
+
filters: SearchPathFilters,
|
|
13
|
+
dictionary?: SearchUiDictionary
|
|
14
|
+
): string {
|
|
15
|
+
return dictionary ? composePath(filters, dictionary) : composePath(filters);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function parseSearchPublicPath(
|
|
19
|
+
pathname: string,
|
|
20
|
+
dictionary?: SearchUiDictionary
|
|
21
|
+
): SearchPathFilters | null {
|
|
22
|
+
return dictionary ? parsePath(pathname, dictionary) : parsePath(pathname);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert `SearchPathFilters` (returned by `parseSearchPublicPath`) to
|
|
27
|
+
* `QueryListingsFilters` (accepted by `queryListings`).
|
|
28
|
+
*
|
|
29
|
+
* This maps path-based filter keys (`bedrooms`, `parkingSpots`, `area`)
|
|
30
|
+
* to the POST API keys (`property_bedrooms`, `property_parking_spots`,
|
|
31
|
+
* `property_area_total`).
|
|
32
|
+
*
|
|
33
|
+
* Location is **not** included — use `pathLocationToFilterInput` separately
|
|
34
|
+
* and pass the result as the `location` field in `QueryListingsParams`.
|
|
35
|
+
*/
|
|
36
|
+
export function pathFiltersToQueryFilters(
|
|
37
|
+
pathFilters: SearchPathFilters
|
|
38
|
+
): QueryListingsFilters {
|
|
39
|
+
const result: QueryListingsFilters = {};
|
|
40
|
+
|
|
41
|
+
if (pathFilters.listingType !== undefined) {
|
|
42
|
+
result.listing_type = pathFilters.listingType;
|
|
43
|
+
}
|
|
44
|
+
if (pathFilters.propertyType !== undefined) {
|
|
45
|
+
result.property_type = pathFilters.propertyType;
|
|
46
|
+
}
|
|
47
|
+
if (pathFilters.bedrooms !== undefined) {
|
|
48
|
+
result.property_bedrooms = pathFilters.bedrooms;
|
|
49
|
+
}
|
|
50
|
+
if (pathFilters.bathrooms !== undefined) {
|
|
51
|
+
result.property_bathrooms = pathFilters.bathrooms;
|
|
52
|
+
}
|
|
53
|
+
if (pathFilters.suites !== undefined) {
|
|
54
|
+
result.property_suites = pathFilters.suites;
|
|
55
|
+
}
|
|
56
|
+
if (pathFilters.parkingSpots !== undefined) {
|
|
57
|
+
result.property_parking_spots = pathFilters.parkingSpots;
|
|
58
|
+
}
|
|
59
|
+
if (pathFilters.price !== undefined) {
|
|
60
|
+
result.price = pathFilters.price;
|
|
61
|
+
}
|
|
62
|
+
if (pathFilters.area !== undefined) {
|
|
63
|
+
result.property_area_total = pathFilters.area;
|
|
64
|
+
}
|
|
65
|
+
if (pathFilters.propertyFeatures !== undefined) {
|
|
66
|
+
result.propertyFeatures = pathFilters.propertyFeatures;
|
|
67
|
+
}
|
|
68
|
+
if (pathFilters.buildingAmenities !== undefined) {
|
|
69
|
+
result.buildingAmenities = pathFilters.buildingAmenities;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { GaragemSdkError, getSite, type FetchLike } from "./fetchers";
|
|
2
|
+
|
|
3
|
+
export interface ResolvePublicSiteIdOptions {
|
|
4
|
+
/** Request host (`headers().get("host")`). Port is stripped. */
|
|
5
|
+
host?: string | null;
|
|
6
|
+
/** Preview hosts only (`localhost`, `.vusercontent.net`). Live misses throw. */
|
|
7
|
+
fallback?: string | null;
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
fetch?: FetchLike;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** v0 in-chat preview only. Published Studio sites often live on `*.vercel.app`. */
|
|
13
|
+
const PREVIEW_HOST_SUFFIXES = [".vusercontent.net"] as const;
|
|
14
|
+
|
|
15
|
+
export function isStudioPreviewHost(host: string): boolean {
|
|
16
|
+
const normalized = stripPort(host);
|
|
17
|
+
if (normalized === "localhost" || normalized === "127.0.0.1") return true;
|
|
18
|
+
return PREVIEW_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function stripPort(host: string): string {
|
|
22
|
+
return host.trim().toLowerCase().split(":")[0] ?? "";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Live Studio sites share one v0 project env (`GARAGEM_SITE_ID` = showcase).
|
|
27
|
+
* After claim, resolve the published site by hostname; preview keeps the
|
|
28
|
+
* fallback catalog id.
|
|
29
|
+
*/
|
|
30
|
+
export async function resolvePublicSiteId(
|
|
31
|
+
options: ResolvePublicSiteIdOptions
|
|
32
|
+
): Promise<string> {
|
|
33
|
+
const fallback = options.fallback?.trim() ?? "";
|
|
34
|
+
const host = options.host ? stripPort(options.host) : "";
|
|
35
|
+
|
|
36
|
+
if (host && !isStudioPreviewHost(host)) {
|
|
37
|
+
const site = await getSite({
|
|
38
|
+
domain: host,
|
|
39
|
+
baseUrl: options.baseUrl,
|
|
40
|
+
fetch: options.fetch,
|
|
41
|
+
});
|
|
42
|
+
if (site?.id) return site.id;
|
|
43
|
+
throw new GaragemSdkError(
|
|
44
|
+
`Garagem SDK: no published site found for domain "${host}"`,
|
|
45
|
+
{ status: 404, url: host }
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (fallback) return fallback;
|
|
50
|
+
|
|
51
|
+
throw new GaragemSdkError(
|
|
52
|
+
"Garagem SDK: could not resolve a site id (no host match and no fallback)",
|
|
53
|
+
{ status: 404, url: host || "fallback" }
|
|
54
|
+
);
|
|
55
|
+
}
|