@garagem-ai/site-sdk 0.1.0 → 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@garagem-ai/site-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Typed, runtime-agnostic SDK for building custom real estate sites against the Garagem platform. Drop into any Next.js (or other JS) project to fetch listings, sites and CMS data.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -27,12 +27,15 @@
|
|
|
27
27
|
"dev": "tsup --watch",
|
|
28
28
|
"test": "vitest run",
|
|
29
29
|
"test:watch": "vitest",
|
|
30
|
-
"typecheck": "
|
|
30
|
+
"typecheck": "tsgo --noEmit",
|
|
31
|
+
"lint": "eslint ."
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"tsup": "^8.0.1",
|
|
34
35
|
"typescript": "^5.3.3",
|
|
35
|
-
"vitest": "^4.0.0"
|
|
36
|
+
"vitest": "^4.0.0",
|
|
37
|
+
"@garagem/eslint-config": "workspace:*",
|
|
38
|
+
"eslint": "^9.0.0"
|
|
36
39
|
},
|
|
37
40
|
"sideEffects": false,
|
|
38
41
|
"keywords": [
|
package/src/client.ts
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
1
|
import {
|
|
2
|
+
getCmsCollection,
|
|
3
|
+
getCmsCollections,
|
|
4
|
+
getCmsEntries,
|
|
5
|
+
getCmsEntry,
|
|
2
6
|
getListing,
|
|
3
7
|
getListings,
|
|
8
|
+
getSimilarListings,
|
|
4
9
|
getSite,
|
|
5
10
|
searchListings,
|
|
6
11
|
type FetchLike,
|
|
12
|
+
type GetCmsCollectionOptions,
|
|
13
|
+
type GetCmsCollectionsOptions,
|
|
14
|
+
type GetCmsEntriesOptions,
|
|
15
|
+
type GetCmsEntryOptions,
|
|
7
16
|
type GetListingOptions,
|
|
8
17
|
type GetListingsOptions,
|
|
18
|
+
type GetSimilarListingsOptions,
|
|
9
19
|
type GetSiteOptions,
|
|
10
20
|
type SearchListingsOptions,
|
|
11
21
|
} from "./fetchers";
|
|
12
22
|
import type {
|
|
23
|
+
CmsCollection,
|
|
24
|
+
CmsEntriesResult,
|
|
25
|
+
CmsEntry,
|
|
13
26
|
ListingDetail,
|
|
14
27
|
ListingFilters,
|
|
15
28
|
ListingsResult,
|
|
16
29
|
RequestNextOptions,
|
|
17
30
|
SearchListingsParams,
|
|
18
31
|
SearchListingsResult,
|
|
32
|
+
SimilarListingsResult,
|
|
19
33
|
Site,
|
|
20
34
|
} from "./types";
|
|
21
35
|
|
|
@@ -34,28 +48,65 @@ export interface GaragemClient {
|
|
|
34
48
|
/** Configuration the client was created with. */
|
|
35
49
|
readonly options: Readonly<CreateGaragemClientOptions>;
|
|
36
50
|
/** List listings for the bound (or explicitly passed) site. */
|
|
37
|
-
getListings(
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
getListings(args?: {
|
|
52
|
+
filters?: ListingFilters;
|
|
53
|
+
siteId?: string;
|
|
54
|
+
next?: RequestNextOptions;
|
|
55
|
+
}): Promise<ListingsResult>;
|
|
40
56
|
/** Fetch a single listing detail. */
|
|
41
|
-
getListing(
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
getListing(args: {
|
|
58
|
+
listingId: string;
|
|
59
|
+
siteId?: string;
|
|
60
|
+
next?: RequestNextOptions;
|
|
61
|
+
}): Promise<ListingDetail>;
|
|
44
62
|
/** Resolve a site by ID or domain. */
|
|
45
|
-
getSite(
|
|
46
|
-
|
|
47
|
-
|
|
63
|
+
getSite(args: {
|
|
64
|
+
siteId?: string;
|
|
65
|
+
domain?: string;
|
|
66
|
+
next?: RequestNextOptions;
|
|
67
|
+
}): Promise<Site | null>;
|
|
48
68
|
/**
|
|
49
69
|
* Advanced search — raw Typesense parameters, raw response. Use when
|
|
50
70
|
* the friendly `getListings({ filters })` API isn't expressive enough.
|
|
51
71
|
*/
|
|
52
|
-
searchListings<TDocument = Record<string, unknown>>(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
searchListings<TDocument = Record<string, unknown>>(args: {
|
|
73
|
+
params: SearchListingsParams;
|
|
74
|
+
siteId?: string;
|
|
75
|
+
next?: RequestNextOptions;
|
|
76
|
+
}): Promise<SearchListingsResult<TDocument>>;
|
|
77
|
+
/** Similar listings carousel for a detail page. */
|
|
78
|
+
getSimilarListings(args: {
|
|
79
|
+
listingId: string;
|
|
80
|
+
limit?: number;
|
|
81
|
+
siteId?: string;
|
|
82
|
+
next?: RequestNextOptions;
|
|
83
|
+
}): Promise<SimilarListingsResult>;
|
|
84
|
+
/** List CMS collections. */
|
|
85
|
+
getCmsCollections(args?: {
|
|
86
|
+
siteId?: string;
|
|
87
|
+
next?: RequestNextOptions;
|
|
88
|
+
}): Promise<CmsCollection[]>;
|
|
89
|
+
/** Fetch one CMS collection by slug. */
|
|
90
|
+
getCmsCollection(args: {
|
|
91
|
+
slug: string;
|
|
92
|
+
siteId?: string;
|
|
93
|
+
next?: RequestNextOptions;
|
|
94
|
+
}): Promise<CmsCollection>;
|
|
95
|
+
/** List published CMS entries. */
|
|
96
|
+
getCmsEntries(args: {
|
|
97
|
+
collection: string;
|
|
98
|
+
limit?: number;
|
|
99
|
+
offset?: number;
|
|
100
|
+
siteId?: string;
|
|
101
|
+
next?: RequestNextOptions;
|
|
102
|
+
}): Promise<CmsEntriesResult>;
|
|
103
|
+
/** Fetch one published CMS entry. */
|
|
104
|
+
getCmsEntry(args: {
|
|
105
|
+
collection: string;
|
|
106
|
+
slug: string;
|
|
107
|
+
siteId?: string;
|
|
108
|
+
next?: RequestNextOptions;
|
|
109
|
+
}): Promise<CmsEntry>;
|
|
59
110
|
}
|
|
60
111
|
|
|
61
112
|
/**
|
|
@@ -85,29 +136,42 @@ export function createGaragemClient(
|
|
|
85
136
|
return { ...options.next, ...callNext };
|
|
86
137
|
};
|
|
87
138
|
|
|
139
|
+
const withDefaults = <T extends { siteId: string }>(
|
|
140
|
+
opts: T
|
|
141
|
+
): T & {
|
|
142
|
+
baseUrl?: string;
|
|
143
|
+
fetch?: FetchLike;
|
|
144
|
+
next?: RequestNextOptions;
|
|
145
|
+
} => {
|
|
146
|
+
const out: T & {
|
|
147
|
+
baseUrl?: string;
|
|
148
|
+
fetch?: FetchLike;
|
|
149
|
+
next?: RequestNextOptions;
|
|
150
|
+
} = { ...opts };
|
|
151
|
+
if (options.baseUrl !== undefined) out.baseUrl = options.baseUrl;
|
|
152
|
+
if (options.fetch !== undefined) out.fetch = options.fetch;
|
|
153
|
+
return out;
|
|
154
|
+
};
|
|
155
|
+
|
|
88
156
|
// All methods are `async` so validation failures (e.g. missing siteId)
|
|
89
157
|
// surface as promise rejections rather than synchronous throws. SDK
|
|
90
158
|
// callers always `await` — this keeps error handling predictable.
|
|
91
159
|
return {
|
|
92
160
|
options: Object.freeze({ ...options }),
|
|
93
161
|
async getListings({ filters, siteId, next } = {}) {
|
|
94
|
-
const opts: GetListingsOptions = {
|
|
162
|
+
const opts: GetListingsOptions = withDefaults({
|
|
95
163
|
siteId: resolveSiteId(siteId),
|
|
96
|
-
};
|
|
164
|
+
});
|
|
97
165
|
if (filters !== undefined) opts.filters = filters;
|
|
98
|
-
if (options.baseUrl !== undefined) opts.baseUrl = options.baseUrl;
|
|
99
|
-
if (options.fetch !== undefined) opts.fetch = options.fetch;
|
|
100
166
|
const merged = mergeNext(next);
|
|
101
167
|
if (merged !== undefined) opts.next = merged;
|
|
102
168
|
return getListings(opts);
|
|
103
169
|
},
|
|
104
170
|
async getListing({ listingId, siteId, next }) {
|
|
105
|
-
const opts: GetListingOptions = {
|
|
171
|
+
const opts: GetListingOptions = withDefaults({
|
|
106
172
|
siteId: resolveSiteId(siteId),
|
|
107
173
|
listingId,
|
|
108
|
-
};
|
|
109
|
-
if (options.baseUrl !== undefined) opts.baseUrl = options.baseUrl;
|
|
110
|
-
if (options.fetch !== undefined) opts.fetch = options.fetch;
|
|
174
|
+
});
|
|
111
175
|
const merged = mergeNext(next);
|
|
112
176
|
if (merged !== undefined) opts.next = merged;
|
|
113
177
|
return getListing(opts);
|
|
@@ -131,15 +195,61 @@ export function createGaragemClient(
|
|
|
131
195
|
siteId?: string;
|
|
132
196
|
next?: RequestNextOptions;
|
|
133
197
|
}) {
|
|
134
|
-
const opts: SearchListingsOptions = {
|
|
198
|
+
const opts: SearchListingsOptions = withDefaults({
|
|
135
199
|
siteId: resolveSiteId(siteId),
|
|
136
200
|
params,
|
|
137
|
-
};
|
|
138
|
-
if (options.baseUrl !== undefined) opts.baseUrl = options.baseUrl;
|
|
139
|
-
if (options.fetch !== undefined) opts.fetch = options.fetch;
|
|
201
|
+
});
|
|
140
202
|
const merged = mergeNext(next);
|
|
141
203
|
if (merged !== undefined) opts.next = merged;
|
|
142
204
|
return searchListings<TDocument>(opts);
|
|
143
205
|
},
|
|
206
|
+
async getSimilarListings({ listingId, limit, siteId, next }) {
|
|
207
|
+
const opts: GetSimilarListingsOptions = withDefaults({
|
|
208
|
+
siteId: resolveSiteId(siteId),
|
|
209
|
+
listingId,
|
|
210
|
+
});
|
|
211
|
+
if (limit !== undefined) opts.limit = limit;
|
|
212
|
+
const merged = mergeNext(next);
|
|
213
|
+
if (merged !== undefined) opts.next = merged;
|
|
214
|
+
return getSimilarListings(opts);
|
|
215
|
+
},
|
|
216
|
+
async getCmsCollections({ siteId, next } = {}) {
|
|
217
|
+
const opts: GetCmsCollectionsOptions = withDefaults({
|
|
218
|
+
siteId: resolveSiteId(siteId),
|
|
219
|
+
});
|
|
220
|
+
const merged = mergeNext(next);
|
|
221
|
+
if (merged !== undefined) opts.next = merged;
|
|
222
|
+
return getCmsCollections(opts);
|
|
223
|
+
},
|
|
224
|
+
async getCmsCollection({ slug, siteId, next }) {
|
|
225
|
+
const opts: GetCmsCollectionOptions = withDefaults({
|
|
226
|
+
siteId: resolveSiteId(siteId),
|
|
227
|
+
slug,
|
|
228
|
+
});
|
|
229
|
+
const merged = mergeNext(next);
|
|
230
|
+
if (merged !== undefined) opts.next = merged;
|
|
231
|
+
return getCmsCollection(opts);
|
|
232
|
+
},
|
|
233
|
+
async getCmsEntries({ collection, limit, offset, siteId, next }) {
|
|
234
|
+
const opts: GetCmsEntriesOptions = withDefaults({
|
|
235
|
+
siteId: resolveSiteId(siteId),
|
|
236
|
+
collection,
|
|
237
|
+
});
|
|
238
|
+
if (limit !== undefined) opts.limit = limit;
|
|
239
|
+
if (offset !== undefined) opts.offset = offset;
|
|
240
|
+
const merged = mergeNext(next);
|
|
241
|
+
if (merged !== undefined) opts.next = merged;
|
|
242
|
+
return getCmsEntries(opts);
|
|
243
|
+
},
|
|
244
|
+
async getCmsEntry({ collection, slug, siteId, next }) {
|
|
245
|
+
const opts: GetCmsEntryOptions = withDefaults({
|
|
246
|
+
siteId: resolveSiteId(siteId),
|
|
247
|
+
collection,
|
|
248
|
+
slug,
|
|
249
|
+
});
|
|
250
|
+
const merged = mergeNext(next);
|
|
251
|
+
if (merged !== undefined) opts.next = merged;
|
|
252
|
+
return getCmsEntry(opts);
|
|
253
|
+
},
|
|
144
254
|
};
|
|
145
255
|
}
|
package/src/fetchers.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
CmsCollection,
|
|
3
|
+
CmsEntriesResult,
|
|
4
|
+
CmsEntry,
|
|
2
5
|
Listing,
|
|
3
6
|
ListingDetail,
|
|
4
7
|
ListingFilters,
|
|
@@ -6,12 +9,11 @@ import type {
|
|
|
6
9
|
RequestNextOptions,
|
|
7
10
|
SearchListingsParams,
|
|
8
11
|
SearchListingsResult,
|
|
12
|
+
SimilarListingsResult,
|
|
9
13
|
Site,
|
|
10
14
|
} from "./types";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
normalizeBaseUrl,
|
|
14
|
-
} from "./internal/url";
|
|
15
|
+
import { listingLookupKey } from "./listing-path";
|
|
16
|
+
import { buildListingFiltersQuery, normalizeBaseUrl } from "./internal/url";
|
|
15
17
|
|
|
16
18
|
/** Custom fetch implementation. Defaults to the global `fetch`. */
|
|
17
19
|
export type FetchLike = typeof fetch;
|
|
@@ -50,6 +52,35 @@ export interface SearchListingsOptions extends BaseOptions {
|
|
|
50
52
|
params: SearchListingsParams;
|
|
51
53
|
}
|
|
52
54
|
|
|
55
|
+
export interface GetSimilarListingsOptions extends BaseOptions {
|
|
56
|
+
siteId: string;
|
|
57
|
+
listingId: string;
|
|
58
|
+
/** Max similar cards to return (API default 6, cap 12). */
|
|
59
|
+
limit?: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface GetCmsCollectionsOptions extends BaseOptions {
|
|
63
|
+
siteId: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface GetCmsCollectionOptions extends BaseOptions {
|
|
67
|
+
siteId: string;
|
|
68
|
+
slug: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface GetCmsEntriesOptions extends BaseOptions {
|
|
72
|
+
siteId: string;
|
|
73
|
+
collection: string;
|
|
74
|
+
limit?: number;
|
|
75
|
+
offset?: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface GetCmsEntryOptions extends BaseOptions {
|
|
79
|
+
siteId: string;
|
|
80
|
+
collection: string;
|
|
81
|
+
slug: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
53
84
|
interface FetchInit extends RequestInit {
|
|
54
85
|
/**
|
|
55
86
|
* Next.js attaches this on `fetch` (App Router). Adding it here keeps
|
|
@@ -109,7 +140,7 @@ export async function getListings(
|
|
|
109
140
|
|
|
110
141
|
const data = await callJson<{ listings?: Listing[]; total?: number }>(
|
|
111
142
|
url,
|
|
112
|
-
{ method: "GET", headers: {
|
|
143
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
113
144
|
fetchImpl ?? fetch
|
|
114
145
|
);
|
|
115
146
|
return {
|
|
@@ -120,11 +151,22 @@ export async function getListings(
|
|
|
120
151
|
|
|
121
152
|
/**
|
|
122
153
|
* Fetch a single listing's full detail (amenities, gallery, building).
|
|
154
|
+
* `listingId` accepts a code, UUID, or `id-{code}` URL segment.
|
|
123
155
|
*/
|
|
124
156
|
export async function getListing(
|
|
125
157
|
options: GetListingOptions
|
|
126
158
|
): Promise<ListingDetail> {
|
|
127
|
-
const {
|
|
159
|
+
const {
|
|
160
|
+
siteId,
|
|
161
|
+
listingId: rawListingId,
|
|
162
|
+
baseUrl,
|
|
163
|
+
fetch: fetchImpl,
|
|
164
|
+
next,
|
|
165
|
+
} = options;
|
|
166
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
167
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
168
|
+
throw new TypeError("getListing: `listingId` is required");
|
|
169
|
+
}
|
|
128
170
|
const base = normalizeBaseUrl(baseUrl);
|
|
129
171
|
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
130
172
|
listingId
|
|
@@ -132,7 +174,7 @@ export async function getListing(
|
|
|
132
174
|
|
|
133
175
|
return callJson<ListingDetail>(
|
|
134
176
|
url,
|
|
135
|
-
{ method: "GET", headers: {
|
|
177
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
136
178
|
fetchImpl ?? fetch
|
|
137
179
|
);
|
|
138
180
|
}
|
|
@@ -145,9 +187,7 @@ export async function getListing(
|
|
|
145
187
|
* const site = await getSite({ domain: "imobiliariax.com.br" });
|
|
146
188
|
* if (!site) return notFound();
|
|
147
189
|
*/
|
|
148
|
-
export async function getSite(
|
|
149
|
-
options: GetSiteOptions
|
|
150
|
-
): Promise<Site | null> {
|
|
190
|
+
export async function getSite(options: GetSiteOptions): Promise<Site | null> {
|
|
151
191
|
const { siteId, domain, baseUrl, fetch: fetchImpl, next } = options;
|
|
152
192
|
if (!siteId && !domain) {
|
|
153
193
|
throw new TypeError(
|
|
@@ -169,7 +209,7 @@ export async function getSite(
|
|
|
169
209
|
const f = fetchImpl ?? fetch;
|
|
170
210
|
const response = await f(url, {
|
|
171
211
|
method: "GET",
|
|
172
|
-
headers: {
|
|
212
|
+
headers: { Accept: "application/json" },
|
|
173
213
|
next,
|
|
174
214
|
} as FetchInit);
|
|
175
215
|
if (response.status === 404) return null;
|
|
@@ -218,9 +258,7 @@ export async function getSite(
|
|
|
218
258
|
* console.log(hit.document, hit.highlight);
|
|
219
259
|
* }
|
|
220
260
|
*/
|
|
221
|
-
export async function searchListings<
|
|
222
|
-
TDocument = Record<string, unknown>,
|
|
223
|
-
>(
|
|
261
|
+
export async function searchListings<TDocument = Record<string, unknown>>(
|
|
224
262
|
options: SearchListingsOptions
|
|
225
263
|
): Promise<SearchListingsResult<TDocument>> {
|
|
226
264
|
const { siteId, params, baseUrl, fetch: fetchImpl, next } = options;
|
|
@@ -242,3 +280,154 @@ export async function searchListings<
|
|
|
242
280
|
fetchImpl ?? fetch
|
|
243
281
|
);
|
|
244
282
|
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Fetch similar listings for a detail page carousel.
|
|
286
|
+
* Returns an empty list when the reference listing is missing.
|
|
287
|
+
*/
|
|
288
|
+
export async function getSimilarListings(
|
|
289
|
+
options: GetSimilarListingsOptions
|
|
290
|
+
): Promise<SimilarListingsResult> {
|
|
291
|
+
const {
|
|
292
|
+
siteId,
|
|
293
|
+
listingId: rawListingId,
|
|
294
|
+
limit,
|
|
295
|
+
baseUrl,
|
|
296
|
+
fetch: fetchImpl,
|
|
297
|
+
next,
|
|
298
|
+
} = options;
|
|
299
|
+
const listingId = listingLookupKey(rawListingId ?? "");
|
|
300
|
+
if (!listingId || listingId === "undefined" || listingId === "null") {
|
|
301
|
+
throw new TypeError("getSimilarListings: `listingId` is required");
|
|
302
|
+
}
|
|
303
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
304
|
+
const params = new URLSearchParams();
|
|
305
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
306
|
+
params.set("limit", String(Math.floor(limit)));
|
|
307
|
+
}
|
|
308
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
309
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/listings/${encodeURIComponent(
|
|
310
|
+
listingId
|
|
311
|
+
)}/similar${qs}`;
|
|
312
|
+
|
|
313
|
+
const data = await callJson<{ listings?: Listing[]; total?: number }>(
|
|
314
|
+
url,
|
|
315
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
316
|
+
fetchImpl ?? fetch
|
|
317
|
+
);
|
|
318
|
+
return {
|
|
319
|
+
listings: data.listings ?? [],
|
|
320
|
+
total: data.total ?? 0,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** List CMS collections for a site. */
|
|
325
|
+
export async function getCmsCollections(
|
|
326
|
+
options: GetCmsCollectionsOptions
|
|
327
|
+
): Promise<CmsCollection[]> {
|
|
328
|
+
const { siteId, baseUrl, fetch: fetchImpl, next } = options;
|
|
329
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
330
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections`;
|
|
331
|
+
const data = await callJson<{ collections?: CmsCollection[] }>(
|
|
332
|
+
url,
|
|
333
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
334
|
+
fetchImpl ?? fetch
|
|
335
|
+
);
|
|
336
|
+
return data.collections ?? [];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Fetch one CMS collection schema by slug. */
|
|
340
|
+
export async function getCmsCollection(
|
|
341
|
+
options: GetCmsCollectionOptions
|
|
342
|
+
): Promise<CmsCollection> {
|
|
343
|
+
const { siteId, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
344
|
+
if (!slug) {
|
|
345
|
+
throw new TypeError("getCmsCollection: `slug` is required");
|
|
346
|
+
}
|
|
347
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
348
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/collections/${encodeURIComponent(
|
|
349
|
+
slug
|
|
350
|
+
)}`;
|
|
351
|
+
const data = await callJson<{ collection?: CmsCollection } | CmsCollection>(
|
|
352
|
+
url,
|
|
353
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
354
|
+
fetchImpl ?? fetch
|
|
355
|
+
);
|
|
356
|
+
if (
|
|
357
|
+
data &&
|
|
358
|
+
typeof data === "object" &&
|
|
359
|
+
"collection" in data &&
|
|
360
|
+
data.collection
|
|
361
|
+
) {
|
|
362
|
+
return data.collection;
|
|
363
|
+
}
|
|
364
|
+
return data as CmsCollection;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** List published CMS entries for a collection. */
|
|
368
|
+
export async function getCmsEntries(
|
|
369
|
+
options: GetCmsEntriesOptions
|
|
370
|
+
): Promise<CmsEntriesResult> {
|
|
371
|
+
const {
|
|
372
|
+
siteId,
|
|
373
|
+
collection,
|
|
374
|
+
limit,
|
|
375
|
+
offset,
|
|
376
|
+
baseUrl,
|
|
377
|
+
fetch: fetchImpl,
|
|
378
|
+
next,
|
|
379
|
+
} = options;
|
|
380
|
+
if (!collection) {
|
|
381
|
+
throw new TypeError("getCmsEntries: `collection` is required");
|
|
382
|
+
}
|
|
383
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
384
|
+
const params = new URLSearchParams();
|
|
385
|
+
if (typeof limit === "number" && Number.isFinite(limit) && limit > 0) {
|
|
386
|
+
params.set("limit", String(Math.floor(limit)));
|
|
387
|
+
}
|
|
388
|
+
if (typeof offset === "number" && Number.isFinite(offset) && offset >= 0) {
|
|
389
|
+
params.set("offset", String(Math.floor(offset)));
|
|
390
|
+
}
|
|
391
|
+
const qs = params.size > 0 ? `?${params.toString()}` : "";
|
|
392
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
393
|
+
collection
|
|
394
|
+
)}/entries${qs}`;
|
|
395
|
+
|
|
396
|
+
const data = await callJson<Partial<CmsEntriesResult>>(
|
|
397
|
+
url,
|
|
398
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
399
|
+
fetchImpl ?? fetch
|
|
400
|
+
);
|
|
401
|
+
return {
|
|
402
|
+
entries: data.entries ?? [],
|
|
403
|
+
total: data.total ?? 0,
|
|
404
|
+
limit: data.limit ?? 0,
|
|
405
|
+
offset: data.offset ?? 0,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Fetch one published CMS entry by slug. */
|
|
410
|
+
export async function getCmsEntry(
|
|
411
|
+
options: GetCmsEntryOptions
|
|
412
|
+
): Promise<CmsEntry> {
|
|
413
|
+
const { siteId, collection, slug, baseUrl, fetch: fetchImpl, next } = options;
|
|
414
|
+
if (!collection) {
|
|
415
|
+
throw new TypeError("getCmsEntry: `collection` is required");
|
|
416
|
+
}
|
|
417
|
+
if (!slug) {
|
|
418
|
+
throw new TypeError("getCmsEntry: `slug` is required");
|
|
419
|
+
}
|
|
420
|
+
const base = normalizeBaseUrl(baseUrl);
|
|
421
|
+
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/cms/${encodeURIComponent(
|
|
422
|
+
collection
|
|
423
|
+
)}/entries/${encodeURIComponent(slug)}`;
|
|
424
|
+
const data = await callJson<{ entry?: CmsEntry } | CmsEntry>(
|
|
425
|
+
url,
|
|
426
|
+
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
427
|
+
fetchImpl ?? fetch
|
|
428
|
+
);
|
|
429
|
+
if (data && typeof data === "object" && "entry" in data && data.entry) {
|
|
430
|
+
return data.entry;
|
|
431
|
+
}
|
|
432
|
+
return data as CmsEntry;
|
|
433
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,13 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
export type {
|
|
23
|
+
CmsCollection,
|
|
24
|
+
CmsEntriesResult,
|
|
25
|
+
CmsEntry,
|
|
26
|
+
CmsEntryStatus,
|
|
27
|
+
CmsFieldDefinition,
|
|
28
|
+
CmsFieldType,
|
|
29
|
+
CmsSeoData,
|
|
23
30
|
Listing,
|
|
24
31
|
ListingBusinessType,
|
|
25
32
|
ListingDetail,
|
|
@@ -32,22 +39,41 @@ export type {
|
|
|
32
39
|
RequestNextOptions,
|
|
33
40
|
SearchListingsParams,
|
|
34
41
|
SearchListingsResult,
|
|
42
|
+
SimilarListingsResult,
|
|
35
43
|
Site,
|
|
36
44
|
} from "./types";
|
|
37
45
|
|
|
38
46
|
export {
|
|
47
|
+
getCmsCollection,
|
|
48
|
+
getCmsCollections,
|
|
49
|
+
getCmsEntries,
|
|
50
|
+
getCmsEntry,
|
|
39
51
|
getListing,
|
|
40
52
|
getListings,
|
|
53
|
+
getSimilarListings,
|
|
41
54
|
getSite,
|
|
42
55
|
searchListings,
|
|
43
56
|
GaragemSdkError,
|
|
44
57
|
type FetchLike,
|
|
58
|
+
type GetCmsCollectionOptions,
|
|
59
|
+
type GetCmsCollectionsOptions,
|
|
60
|
+
type GetCmsEntriesOptions,
|
|
61
|
+
type GetCmsEntryOptions,
|
|
45
62
|
type GetListingOptions,
|
|
46
63
|
type GetListingsOptions,
|
|
64
|
+
type GetSimilarListingsOptions,
|
|
47
65
|
type GetSiteOptions,
|
|
48
66
|
type SearchListingsOptions,
|
|
49
67
|
} from "./fetchers";
|
|
50
68
|
|
|
69
|
+
export { listingLookupKey, listingPublicPath } from "./listing-path";
|
|
70
|
+
|
|
71
|
+
export {
|
|
72
|
+
resolvePublicSiteId,
|
|
73
|
+
isStudioPreviewHost,
|
|
74
|
+
type ResolvePublicSiteIdOptions,
|
|
75
|
+
} from "./resolve-site-id";
|
|
76
|
+
|
|
51
77
|
export {
|
|
52
78
|
createGaragemClient,
|
|
53
79
|
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,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
|
+
}
|