ggez-banking-sdk 0.2.20 → 0.2.21

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.
@@ -1,9 +1,49 @@
1
1
  import type { GeoCoordinates } from "../types";
2
2
  import { IPAddressAndLocationService } from "../api/services/ipAddressAndLocation";
3
3
  declare class GeoHelper {
4
- static GetGeoCoordinates(locationService: IPAddressAndLocationService): Promise<GeoCoordinates>;
5
- static GetIPAddress(locationService: IPAddressAndLocationService): Promise<string>;
6
- static GetGeoCoordinatesAndIPAddress(locationService: IPAddressAndLocationService): Promise<{
4
+ private static readonly CACHE_KEY;
5
+ private static readonly DEFAULT_TTL_MS;
6
+ private static cache;
7
+ /**
8
+ * Get cached geo coordinates if available and not expired
9
+ * @param ttlMs Time to live in milliseconds (default: 24 hours)
10
+ * @returns Cached geo coordinates or null if expired/not found
11
+ */
12
+ private static getCachedGeoCoordinates;
13
+ /**
14
+ * Cache geo coordinates
15
+ * @param geo_coordinates Geo coordinates to cache
16
+ * @param ip_address Optional IP address to cache
17
+ */
18
+ private static setCachedGeoCoordinates;
19
+ /**
20
+ * Clear cached geo coordinates
21
+ */
22
+ static clearCache(): void;
23
+ /**
24
+ * Get geo coordinates with caching support
25
+ * @param locationService IP address and location service
26
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
27
+ * @param forceRefresh Force refresh even if cache is valid
28
+ * @returns Geo coordinates
29
+ */
30
+ static GetGeoCoordinates(locationService: IPAddressAndLocationService, ttlMs?: number, forceRefresh?: boolean): Promise<GeoCoordinates>;
31
+ /**
32
+ * Get IP address with caching support
33
+ * @param locationService IP address and location service
34
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
35
+ * @param forceRefresh Force refresh even if cache is valid
36
+ * @returns IP address
37
+ */
38
+ static GetIPAddress(locationService: IPAddressAndLocationService, ttlMs?: number, forceRefresh?: boolean): Promise<string>;
39
+ /**
40
+ * Get geo coordinates and IP address with caching support
41
+ * @param locationService IP address and location service
42
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
43
+ * @param forceRefresh Force refresh even if cache is valid
44
+ * @returns Object containing geo coordinates and IP address
45
+ */
46
+ static GetGeoCoordinatesAndIPAddress(locationService: IPAddressAndLocationService, ttlMs?: number, forceRefresh?: boolean): Promise<{
7
47
  geo_coordinates: GeoCoordinates;
8
48
  ip_address: string;
9
49
  }>;
@@ -1,5 +1,98 @@
1
1
  class GeoHelper {
2
- static async GetGeoCoordinates(locationService) {
2
+ // Cache configuration
3
+ static CACHE_KEY = "geo_coordinates_cache";
4
+ static DEFAULT_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
5
+ static cache = null;
6
+ /**
7
+ * Get cached geo coordinates if available and not expired
8
+ * @param ttlMs Time to live in milliseconds (default: 24 hours)
9
+ * @returns Cached geo coordinates or null if expired/not found
10
+ */
11
+ static getCachedGeoCoordinates(ttlMs = GeoHelper.DEFAULT_TTL_MS) {
12
+ // Check in-memory cache first
13
+ if (GeoHelper.cache) {
14
+ const now = Date.now();
15
+ const age = now - GeoHelper.cache.timestamp;
16
+ if (age < ttlMs) {
17
+ return GeoHelper.cache.geo_coordinates;
18
+ }
19
+ // Cache expired, clear it
20
+ GeoHelper.cache = null;
21
+ }
22
+ // Check localStorage cache
23
+ try {
24
+ const cachedDataStr = localStorage.getItem(GeoHelper.CACHE_KEY);
25
+ if (cachedDataStr) {
26
+ const cachedData = JSON.parse(cachedDataStr);
27
+ const now = Date.now();
28
+ const age = now - cachedData.timestamp;
29
+ if (age < ttlMs) {
30
+ // Restore to in-memory cache
31
+ GeoHelper.cache = cachedData;
32
+ return cachedData.geo_coordinates;
33
+ }
34
+ else {
35
+ // Cache expired, remove it
36
+ localStorage.removeItem(GeoHelper.CACHE_KEY);
37
+ }
38
+ }
39
+ }
40
+ catch (error) {
41
+ // If localStorage is not available or parsing fails, continue without cache
42
+ console.warn("Failed to read geo coordinates from cache:", error);
43
+ }
44
+ return null;
45
+ }
46
+ /**
47
+ * Cache geo coordinates
48
+ * @param geo_coordinates Geo coordinates to cache
49
+ * @param ip_address Optional IP address to cache
50
+ */
51
+ static setCachedGeoCoordinates(geo_coordinates, ip_address) {
52
+ const cachedData = {
53
+ geo_coordinates,
54
+ ip_address,
55
+ timestamp: Date.now(),
56
+ };
57
+ // Store in memory
58
+ GeoHelper.cache = cachedData;
59
+ // Store in localStorage for persistence
60
+ try {
61
+ localStorage.setItem(GeoHelper.CACHE_KEY, JSON.stringify(cachedData));
62
+ }
63
+ catch (error) {
64
+ // If localStorage is not available, continue with in-memory cache only
65
+ console.warn("Failed to store geo coordinates in cache:", error);
66
+ }
67
+ }
68
+ /**
69
+ * Clear cached geo coordinates
70
+ */
71
+ static clearCache() {
72
+ GeoHelper.cache = null;
73
+ try {
74
+ localStorage.removeItem(GeoHelper.CACHE_KEY);
75
+ }
76
+ catch (error) {
77
+ // Ignore errors when clearing cache
78
+ }
79
+ }
80
+ /**
81
+ * Get geo coordinates with caching support
82
+ * @param locationService IP address and location service
83
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
84
+ * @param forceRefresh Force refresh even if cache is valid
85
+ * @returns Geo coordinates
86
+ */
87
+ static async GetGeoCoordinates(locationService, ttlMs = GeoHelper.DEFAULT_TTL_MS, forceRefresh = false) {
88
+ // Check cache first (unless force refresh is requested)
89
+ if (!forceRefresh && ttlMs > 0) {
90
+ const cached = GeoHelper.getCachedGeoCoordinates(ttlMs);
91
+ if (cached) {
92
+ return cached;
93
+ }
94
+ }
95
+ // Fetch fresh data
3
96
  const { data } = await locationService.getIPAddressAndLocation();
4
97
  if (data) {
5
98
  const ipAddressAndLocation = JSON.parse(data);
@@ -9,20 +102,76 @@ class GeoHelper {
9
102
  longitude: longitude || 0,
10
103
  position_description: `${city || "N/A"}, ${country || "N/A"}`,
11
104
  };
105
+ // Cache the result
106
+ if (ttlMs > 0) {
107
+ GeoHelper.setCachedGeoCoordinates(geo_coordinates);
108
+ }
12
109
  return geo_coordinates;
13
110
  }
14
- return { latitude: 0, longitude: 0, position_description: "N/A, N/A" };
111
+ const fallback = {
112
+ latitude: 0,
113
+ longitude: 0,
114
+ position_description: "N/A, N/A",
115
+ };
116
+ // Cache fallback as well to avoid repeated failed requests
117
+ if (ttlMs > 0) {
118
+ GeoHelper.setCachedGeoCoordinates(fallback);
119
+ }
120
+ return fallback;
15
121
  }
16
- static async GetIPAddress(locationService) {
122
+ /**
123
+ * Get IP address with caching support
124
+ * @param locationService IP address and location service
125
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
126
+ * @param forceRefresh Force refresh even if cache is valid
127
+ * @returns IP address
128
+ */
129
+ static async GetIPAddress(locationService, ttlMs = GeoHelper.DEFAULT_TTL_MS, forceRefresh = false) {
130
+ // Check cache first (unless force refresh is requested)
131
+ if (!forceRefresh && ttlMs > 0 && GeoHelper.cache?.ip_address) {
132
+ const cached = GeoHelper.getCachedGeoCoordinates(ttlMs);
133
+ if (cached && GeoHelper.cache?.ip_address) {
134
+ return GeoHelper.cache.ip_address;
135
+ }
136
+ }
137
+ // Fetch fresh data
17
138
  const { data } = await locationService.getIPAddressAndLocation();
18
139
  if (data) {
19
140
  const ipAddressAndLocation = JSON.parse(data);
20
141
  const { ip_address } = ipAddressAndLocation;
21
- return ip_address;
142
+ // Cache the result (we need to get geo coordinates too for caching)
143
+ if (ttlMs > 0 && ip_address) {
144
+ const { latitude, longitude, city, country } = ipAddressAndLocation;
145
+ const geo_coordinates = {
146
+ latitude: latitude || 0,
147
+ longitude: longitude || 0,
148
+ position_description: `${city || "N/A"}, ${country || "N/A"}`,
149
+ };
150
+ GeoHelper.setCachedGeoCoordinates(geo_coordinates, ip_address);
151
+ }
152
+ return ip_address || "";
22
153
  }
23
154
  return "";
24
155
  }
25
- static async GetGeoCoordinatesAndIPAddress(locationService) {
156
+ /**
157
+ * Get geo coordinates and IP address with caching support
158
+ * @param locationService IP address and location service
159
+ * @param ttlMs Time to live in milliseconds (default: 24 hours). Set to 0 to disable cache.
160
+ * @param forceRefresh Force refresh even if cache is valid
161
+ * @returns Object containing geo coordinates and IP address
162
+ */
163
+ static async GetGeoCoordinatesAndIPAddress(locationService, ttlMs = GeoHelper.DEFAULT_TTL_MS, forceRefresh = false) {
164
+ // Check cache first (unless force refresh is requested)
165
+ if (!forceRefresh && ttlMs > 0) {
166
+ const cached = GeoHelper.getCachedGeoCoordinates(ttlMs);
167
+ if (cached && GeoHelper.cache?.ip_address) {
168
+ return {
169
+ geo_coordinates: cached,
170
+ ip_address: GeoHelper.cache.ip_address,
171
+ };
172
+ }
173
+ }
174
+ // Fetch fresh data
26
175
  const { data } = await locationService.getIPAddressAndLocation();
27
176
  if (data) {
28
177
  const ipAddressAndLocation = JSON.parse(data);
@@ -32,9 +181,13 @@ class GeoHelper {
32
181
  longitude: longitude || 0,
33
182
  position_description: `${city || "N/A"}, ${country || "N/A"}`,
34
183
  };
35
- return { geo_coordinates, ip_address };
184
+ // Cache the result
185
+ if (ttlMs > 0) {
186
+ GeoHelper.setCachedGeoCoordinates(geo_coordinates, ip_address || "");
187
+ }
188
+ return { geo_coordinates, ip_address: ip_address || "" };
36
189
  }
37
- return {
190
+ const fallback = {
38
191
  geo_coordinates: {
39
192
  latitude: 0,
40
193
  longitude: 0,
@@ -42,6 +195,11 @@ class GeoHelper {
42
195
  },
43
196
  ip_address: "",
44
197
  };
198
+ // Cache fallback as well to avoid repeated failed requests
199
+ if (ttlMs > 0) {
200
+ GeoHelper.setCachedGeoCoordinates(fallback.geo_coordinates, fallback.ip_address);
201
+ }
202
+ return fallback;
45
203
  }
46
204
  }
47
205
  export { GeoHelper };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ggez-banking-sdk",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "description": "A Node.js package to handle GGEZ Banking API endpoints, Simplify the process of managing CRUD operations with this efficient and easy-to-use package.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",