@garagem-ai/site-sdk 0.7.0 → 0.8.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 +169 -28
- package/dist/index.d.mts +178 -4
- package/dist/index.d.ts +178 -4
- package/dist/index.js +538 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +528 -32
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +6 -1
- package/dist/react.d.ts +6 -1
- package/dist/react.js +140 -14
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +140 -14
- package/dist/react.mjs.map +1 -1
- package/dist/{types-photo-search-OeONNDxl.d.mts → types-photo-search-CmGRj6my.d.mts} +25 -0
- package/dist/{types-photo-search-OeONNDxl.d.ts → types-photo-search-CmGRj6my.d.ts} +25 -0
- package/package.json +1 -1
- package/src/client.ts +23 -0
- package/src/fetchers-photos.ts +98 -0
- package/src/fetchers.ts +92 -29
- package/src/index.ts +31 -2
- package/src/internal/sdk-http.ts +122 -0
- package/src/leads.ts +34 -3
- package/src/listing-photos.ts +312 -0
- package/src/react/types.ts +5 -0
- package/src/react/use-public-search.ts +23 -5
- package/src/resolve-site-id.ts +3 -0
- package/src/studio-contract.ts +74 -0
- package/src/types-photos.ts +46 -0
- package/src/types.ts +11 -0
package/dist/react.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-
|
|
1
|
+
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-CmGRj6my.mjs';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -13,6 +13,11 @@ type PublicSearchSnapshot = {
|
|
|
13
13
|
type UsePublicSearchOptions = {
|
|
14
14
|
siteId: SiteIdInput;
|
|
15
15
|
baseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Site publishable key (`pk_live_…`). Sent on schema, location, and
|
|
18
|
+
* listing requests. Not a secret. Omit it to stay on the anonymous budget.
|
|
19
|
+
*/
|
|
20
|
+
publishableKey?: string;
|
|
16
21
|
/**
|
|
17
22
|
* Query string without `?`, from the server page's `searchParams`.
|
|
18
23
|
* The hook must not call Next's search-params hook: that suspends the
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-
|
|
1
|
+
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-CmGRj6my.js';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -13,6 +13,11 @@ type PublicSearchSnapshot = {
|
|
|
13
13
|
type UsePublicSearchOptions = {
|
|
14
14
|
siteId: SiteIdInput;
|
|
15
15
|
baseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Site publishable key (`pk_live_…`). Sent on schema, location, and
|
|
18
|
+
* listing requests. Not a secret. Omit it to stay on the anonymous budget.
|
|
19
|
+
*/
|
|
20
|
+
publishableKey?: string;
|
|
16
21
|
/**
|
|
17
22
|
* Query string without `?`, from the server page's `searchParams`.
|
|
18
23
|
* The hook must not call Next's search-params hook: that suspends the
|
package/dist/react.js
CHANGED
|
@@ -31,6 +31,90 @@ module.exports = __toCommonJS(react_exports);
|
|
|
31
31
|
var import_react = require("react");
|
|
32
32
|
var import_navigation = require("next/navigation");
|
|
33
33
|
|
|
34
|
+
// src/internal/sdk-http.ts
|
|
35
|
+
var PUBLISHABLE_KEY_HEADER = "X-Garagem-Publishable-Key";
|
|
36
|
+
function publishableKeyHeaders(publishableKey) {
|
|
37
|
+
const key = publishableKey?.trim();
|
|
38
|
+
if (!key) return {};
|
|
39
|
+
return { [PUBLISHABLE_KEY_HEADER]: key };
|
|
40
|
+
}
|
|
41
|
+
function withPublishableKeyHeader(init, publishableKey) {
|
|
42
|
+
const extra = publishableKeyHeaders(publishableKey);
|
|
43
|
+
if (Object.keys(extra).length === 0) return init;
|
|
44
|
+
const current = init.headers;
|
|
45
|
+
const base = plainHeaders(current);
|
|
46
|
+
return { ...init, headers: { ...base, ...extra } };
|
|
47
|
+
}
|
|
48
|
+
function plainHeaders(headers) {
|
|
49
|
+
const out = {};
|
|
50
|
+
if (!headers) return out;
|
|
51
|
+
if (typeof Headers !== "undefined" && headers instanceof Headers) {
|
|
52
|
+
headers.forEach((value, name) => {
|
|
53
|
+
out[name] = value;
|
|
54
|
+
});
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(headers)) {
|
|
58
|
+
for (const pair of headers) {
|
|
59
|
+
out[String(pair[0])] = String(pair[1]);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
for (const [name, value] of Object.entries(
|
|
64
|
+
headers
|
|
65
|
+
)) {
|
|
66
|
+
if (typeof value === "string") out[name] = value;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function finiteNonNegative(value) {
|
|
71
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
if (typeof value === "string" && value.trim()) {
|
|
75
|
+
const parsed = Number(value);
|
|
76
|
+
if (Number.isFinite(parsed) && parsed >= 0) return parsed;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
function parseSdkErrorBody(text) {
|
|
81
|
+
const raw = text ? text.slice(0, 200) : "";
|
|
82
|
+
if (!text) {
|
|
83
|
+
return { reason: null, retryAfterSeconds: null, summary: "" };
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const body = JSON.parse(text);
|
|
87
|
+
if (!body || typeof body !== "object") {
|
|
88
|
+
return { reason: null, retryAfterSeconds: null, summary: raw };
|
|
89
|
+
}
|
|
90
|
+
const record = body;
|
|
91
|
+
const reason = typeof record.reason === "string" && record.reason ? record.reason : null;
|
|
92
|
+
const retryAfterSeconds = finiteNonNegative(
|
|
93
|
+
record.retryAfterSeconds ?? record.retryAfter
|
|
94
|
+
);
|
|
95
|
+
const errorText = typeof record.error === "string" && record.error ? record.error : raw;
|
|
96
|
+
return {
|
|
97
|
+
reason,
|
|
98
|
+
retryAfterSeconds,
|
|
99
|
+
summary: errorText.slice(0, 200)
|
|
100
|
+
};
|
|
101
|
+
} catch {
|
|
102
|
+
return { reason: null, retryAfterSeconds: null, summary: raw };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function retryAfterFromResponse(response, bodySeconds, nowMs = Date.now()) {
|
|
106
|
+
const header = response.headers.get("Retry-After")?.trim();
|
|
107
|
+
if (header) {
|
|
108
|
+
const delta = finiteNonNegative(header);
|
|
109
|
+
if (delta != null) return delta;
|
|
110
|
+
const at = Date.parse(header);
|
|
111
|
+
if (Number.isFinite(at)) {
|
|
112
|
+
return Math.max(0, Math.ceil((at - nowMs) / 1e3));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return bodySeconds;
|
|
116
|
+
}
|
|
117
|
+
|
|
34
118
|
// src/internal/url.ts
|
|
35
119
|
var DEFAULT_BASE_URL = "https://app.garagem.ai";
|
|
36
120
|
function normalizeBaseUrl(baseUrl) {
|
|
@@ -49,27 +133,50 @@ async function coerceSiteId(siteId) {
|
|
|
49
133
|
}
|
|
50
134
|
|
|
51
135
|
// src/fetchers.ts
|
|
52
|
-
async function callJson(url, init, fetchImpl) {
|
|
53
|
-
const response = await fetchImpl(
|
|
136
|
+
async function callJson(url, init, fetchImpl, publishableKey) {
|
|
137
|
+
const response = await fetchImpl(
|
|
138
|
+
url,
|
|
139
|
+
withPublishableKeyHeader(init, publishableKey)
|
|
140
|
+
);
|
|
54
141
|
if (!response.ok) {
|
|
55
|
-
|
|
56
|
-
throw new GaragemSdkError(
|
|
57
|
-
`Garagem SDK: request failed (${response.status} ${response.statusText}) for ${url}${text ? ` \u2014 ${text.slice(0, 200)}` : ""}`,
|
|
58
|
-
{ status: response.status, url }
|
|
59
|
-
);
|
|
142
|
+
throw await readGaragemSdkError(response, url);
|
|
60
143
|
}
|
|
61
144
|
return await response.json();
|
|
62
145
|
}
|
|
63
146
|
var GaragemSdkError = class extends Error {
|
|
64
147
|
status;
|
|
65
148
|
url;
|
|
149
|
+
/** Machine reason from the JSON body (`invalid_publishable_key`, `rate_limited`, …). */
|
|
150
|
+
reason;
|
|
151
|
+
/**
|
|
152
|
+
* Seconds to wait before retrying. Set on HTTP 429 from `Retry-After`
|
|
153
|
+
* or the body's `retryAfterSeconds`. `null` when the response has neither.
|
|
154
|
+
*/
|
|
155
|
+
retryAfter;
|
|
66
156
|
constructor(message, info) {
|
|
67
157
|
super(message);
|
|
68
158
|
this.name = "GaragemSdkError";
|
|
69
159
|
this.status = info.status;
|
|
70
160
|
this.url = info.url;
|
|
161
|
+
this.reason = info.reason ?? null;
|
|
162
|
+
this.retryAfter = info.retryAfter ?? null;
|
|
71
163
|
}
|
|
72
164
|
};
|
|
165
|
+
async function readGaragemSdkError(response, url) {
|
|
166
|
+
const text = await response.text().catch(() => "");
|
|
167
|
+
const parsed = parseSdkErrorBody(text);
|
|
168
|
+
const retryAfter = response.status === 429 ? retryAfterFromResponse(response, parsed.retryAfterSeconds) : parsed.retryAfterSeconds;
|
|
169
|
+
const detail = parsed.summary;
|
|
170
|
+
return new GaragemSdkError(
|
|
171
|
+
`Garagem SDK: request failed (${response.status} ${response.statusText}) for ${url}${detail ? ` \u2014 ${detail}` : ""}`,
|
|
172
|
+
{
|
|
173
|
+
status: response.status,
|
|
174
|
+
url,
|
|
175
|
+
reason: parsed.reason,
|
|
176
|
+
retryAfter
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
73
180
|
async function getSearchUi(options) {
|
|
74
181
|
const { baseUrl, fetch: fetchImpl, next } = options;
|
|
75
182
|
const siteId = await coerceSiteId(options.siteId);
|
|
@@ -78,7 +185,8 @@ async function getSearchUi(options) {
|
|
|
78
185
|
return callJson(
|
|
79
186
|
url,
|
|
80
187
|
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
81
|
-
fetchImpl ?? fetch
|
|
188
|
+
fetchImpl ?? fetch,
|
|
189
|
+
options.publishableKey
|
|
82
190
|
);
|
|
83
191
|
}
|
|
84
192
|
async function queryListings(options) {
|
|
@@ -97,7 +205,8 @@ async function queryListings(options) {
|
|
|
97
205
|
body: JSON.stringify(params ?? {}),
|
|
98
206
|
next
|
|
99
207
|
},
|
|
100
|
-
fetchImpl ?? fetch
|
|
208
|
+
fetchImpl ?? fetch,
|
|
209
|
+
options.publishableKey
|
|
101
210
|
);
|
|
102
211
|
}
|
|
103
212
|
|
|
@@ -835,7 +944,13 @@ function currentHref(pathname, search) {
|
|
|
835
944
|
return search ? `${pathname}?${search}` : pathname;
|
|
836
945
|
}
|
|
837
946
|
function usePublicSearch(options) {
|
|
838
|
-
const {
|
|
947
|
+
const {
|
|
948
|
+
siteId,
|
|
949
|
+
baseUrl,
|
|
950
|
+
publishableKey,
|
|
951
|
+
initial,
|
|
952
|
+
search: searchFromServer = ""
|
|
953
|
+
} = options;
|
|
839
954
|
const router = (0, import_navigation.useRouter)();
|
|
840
955
|
const pathname = (0, import_navigation.usePathname)() || "/imoveis";
|
|
841
956
|
const search = searchFromServer;
|
|
@@ -924,13 +1039,23 @@ function usePublicSearch(options) {
|
|
|
924
1039
|
}
|
|
925
1040
|
async function loadSchema() {
|
|
926
1041
|
try {
|
|
927
|
-
const nextSchema = await getSearchUi({
|
|
1042
|
+
const nextSchema = await getSearchUi({
|
|
1043
|
+
siteId: id,
|
|
1044
|
+
baseUrl,
|
|
1045
|
+
publishableKey
|
|
1046
|
+
});
|
|
928
1047
|
if (!active) return;
|
|
929
1048
|
let locations = null;
|
|
930
1049
|
if (nextSchema.locationsPath.startsWith("/")) {
|
|
931
1050
|
try {
|
|
932
1051
|
const response = await fetch(
|
|
933
|
-
`${normalizeBaseUrl(baseUrl)}${nextSchema.locationsPath}
|
|
1052
|
+
`${normalizeBaseUrl(baseUrl)}${nextSchema.locationsPath}`,
|
|
1053
|
+
{
|
|
1054
|
+
headers: {
|
|
1055
|
+
Accept: "application/json",
|
|
1056
|
+
...publishableKeyHeaders(publishableKey)
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
934
1059
|
);
|
|
935
1060
|
if (response.ok) {
|
|
936
1061
|
locations = await response.json();
|
|
@@ -964,7 +1089,7 @@ function usePublicSearch(options) {
|
|
|
964
1089
|
return () => {
|
|
965
1090
|
active = false;
|
|
966
1091
|
};
|
|
967
|
-
}, [baseUrl, catalogId]);
|
|
1092
|
+
}, [baseUrl, catalogId, publishableKey]);
|
|
968
1093
|
(0, import_react.useEffect)(() => {
|
|
969
1094
|
if (!schema || !catalogId) return;
|
|
970
1095
|
const currentSchema = schema;
|
|
@@ -987,6 +1112,7 @@ function usePublicSearch(options) {
|
|
|
987
1112
|
const response = await queryListings({
|
|
988
1113
|
siteId: id,
|
|
989
1114
|
baseUrl,
|
|
1115
|
+
publishableKey,
|
|
990
1116
|
params: buildQueryListingsParams(
|
|
991
1117
|
pathFilters,
|
|
992
1118
|
applied,
|
|
@@ -1023,7 +1149,7 @@ function usePublicSearch(options) {
|
|
|
1023
1149
|
return () => {
|
|
1024
1150
|
active = false;
|
|
1025
1151
|
};
|
|
1026
|
-
}, [pathname, search, baseUrl, schema, catalogId]);
|
|
1152
|
+
}, [pathname, search, baseUrl, schema, catalogId, publishableKey]);
|
|
1027
1153
|
const go = (0, import_react.useCallback)(
|
|
1028
1154
|
(href) => {
|
|
1029
1155
|
if (href && href !== currentHref(pathname, search)) router.push(href);
|