-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup.mjs
More file actions
36 lines (33 loc) · 1.71 KB
/
Copy pathlookup.mjs
File metadata and controls
36 lines (33 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Predio — Node example with typed-error handling. Node 18+ (built-in fetch).
// Usage: PREDIO_API_KEY=pk_... node lookup.mjs 13077A01800039
const API_KEY = process.env.PREDIO_API_KEY;
if (!API_KEY) throw new Error("Set PREDIO_API_KEY (free key: https://prediohq.com)");
const rc = process.argv[2] ?? "9872023VH5797S0001WX";
const res = await fetch(`https://api.prediohq.com/v1/inmueble/${rc}`, {
headers: { "x-api-key": API_KEY },
});
// Every outcome is JSON with a stable shape — branch on HTTP status + error.code,
// never on parsing quirks (that's the point of not consuming the SOAP upstream).
const body = await res.json();
if (res.ok) {
const p = body.property;
console.log(`${p.address?.municipality ?? "?"} — ${p.class} / ${p.use}`);
console.log(`Built area: ${p.builtAreaM2 ?? "n/a"} m² · year: ${p.yearBuilt ?? "n/a"}`);
console.log(`Source: ${body.source.provider} · cached: ${body.source.cached}`);
} else {
switch (body.error?.code) {
case "NOT_FOUND": // 404 — the reference doesn't exist
case "INVALID_REFERENCE": // 422 — malformed (check digits are validated before calling upstream)
console.error(`Bad input (don't retry): ${body.error.message}`);
break;
case "PAYMENT_REQUIRED": // 402 — out of credits: top up, then retry is safe
console.error(`Out of credits: ${body.error.message}`);
break;
case "RATE_LIMITED": // 429 — back off and retry
console.error(`Rate limited, retry after: ${res.headers.get("retry-after") ?? "?"}s`);
break;
default: // 5xx — upstream/infra: retryable
console.error(`Transient (${res.status}): ${body.error?.message ?? "unknown"}`);
}
process.exitCode = 1;
}