-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.mjs
More file actions
33 lines (26 loc) · 1.07 KB
/
node.mjs
File metadata and controls
33 lines (26 loc) · 1.07 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
// Postali API — Node.js 18+ (native fetch, no dependencies).
// Run with: node examples/node.mjs
const BASE = "https://postali.app/api/v1";
const lookup = (country, cp) =>
fetch(`${BASE}/${country}/cp/${cp}`).then(r => r.json());
const validate = (country, cp) =>
fetch(`${BASE}/${country}/validate/${cp}`).then(r => r.json());
const search = (country, q, limit = 10) => {
const url = new URL(`${BASE}/${country}/search`);
url.searchParams.set("q", q);
url.searchParams.set("limit", String(limit));
return fetch(url).then(r => r.json());
};
const listEstados = (country) =>
fetch(`${BASE}/${country}/estados`).then(r => r.json());
const bulk = (country, cps) =>
fetch(`${BASE}/${country}/bulk`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ cps }),
}).then(r => r.json());
console.log(await lookup("mx", "06700"));
console.log(await validate("co", "050001"));
console.log(await search("es", "barcelona", 5));
console.log(await listEstados("co"));
console.log(await bulk("mx", ["06700", "44100", "00000"]));