kuid-sdk 1.0.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.
Files changed (3) hide show
  1. package/index.js +2 -0
  2. package/kuid.js +55 -0
  3. package/package.json +10 -0
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import Kuid from "./kuid.js";
2
+ export default Kuid;
package/kuid.js ADDED
@@ -0,0 +1,55 @@
1
+ export default class Kuid {
2
+ constructor(apiKey, baseUrl = "https://kuidapi.ndeas.cloud/api/v1") {
3
+ this.apiKey = apiKey;
4
+ this.baseUrl = baseUrl;
5
+ this.cache = new Map();
6
+ }
7
+
8
+ async getAddress(kuid) {
9
+ if (!this.isValid(kuid)) {
10
+ throw new Error("Formato de KUID inválido");
11
+ }
12
+
13
+ // cache (5 min)
14
+ if (this.cache.has(kuid)) {
15
+ return this.cache.get(kuid);
16
+ }
17
+
18
+ const res = await fetch(`${this.baseUrl}/addresses/${kuid}/`, {
19
+ headers: {
20
+ "Authorization": `ApiKey ${this.apiKey}`,
21
+ "Accept": "application/json"
22
+ }
23
+ });
24
+
25
+ if (!res.ok) {
26
+ throw new Error("Erro ao obter KUID");
27
+ }
28
+
29
+ const data = await res.json();
30
+ const normalized = this.normalize(data);
31
+
32
+ this.cache.set(kuid, normalized);
33
+
34
+ setTimeout(() => this.cache.delete(kuid), 300000);
35
+
36
+ return normalized;
37
+ }
38
+
39
+ normalize(data) {
40
+ return {
41
+ kuid: data.kuid,
42
+ address: data.description,
43
+ latitude: data.latest_location?.latitude,
44
+ longitude: data.latest_location?.longitude
45
+ };
46
+ }
47
+
48
+ isValid(kuid) {
49
+ return /^[A-Z]{2}-[A-Z]{3}-[A-Z]{3}-[A-Z0-9]{4}$/.test(kuid);
50
+ }
51
+
52
+ getNavigationUrl(lat, lng) {
53
+ return `https://maps.google.com/?q=${lat},${lng}`;
54
+ }
55
+ }
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "kuid-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official KUID SDK",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "keywords": ["kuid", "geolocation", "address"],
8
+ "author": "KUID",
9
+ "license": "MIT"
10
+ }