-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
44 lines (38 loc) · 1.25 KB
/
javascript.js
File metadata and controls
44 lines (38 loc) · 1.25 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
37
38
39
40
41
42
43
44
// Postali API — vanilla browser JavaScript (no dependencies).
// CORS is open, so you can call this directly from any origin.
const BASE = "https://postali.app/api/v1";
// 1. Lookup by postal code
async function lookup(country, cp) {
const r = await fetch(`${BASE}/${country}/cp/${cp}`);
if (!r.ok) throw new Error((await r.json()).error.message);
return r.json();
}
// 2. Lightweight validation
async function validate(country, cp) {
const r = await fetch(`${BASE}/${country}/validate/${cp}`);
return r.json();
}
// 3. Fuzzy search (debounce in production)
async function search(country, q, limit = 10) {
const url = new URL(`${BASE}/${country}/search`);
url.searchParams.set("q", q);
url.searchParams.set("limit", limit);
const r = await fetch(url);
return r.json();
}
// 4. Bulk lookup
async function bulk(country, cps) {
const r = await fetch(`${BASE}/${country}/bulk`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ cps }),
});
return r.json();
}
// Example usage:
(async () => {
console.log(await lookup("mx", "06700"));
console.log(await validate("co", "050001"));
console.log(await search("es", "barcelona", 5));
console.log(await bulk("mx", ["06700", "44100"]));
})();