@sanvika/geolocation 0.6.3 → 0.6.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanvika/geolocation",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Sanvika ecosystem geolocation SDK — fully centralized via geolocation.sanvikaproduction.com. IP lookup, reverse/forward geocoding, Places autocomplete + details, Maps JS loader. Zero Google API keys in consumer projects.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -16,7 +16,7 @@ import { createLogger } from "@sanvika/logger";
16
16
  const logger = createLogger({ namespace: "SanvikaGeolocation.MapsLoader" });
17
17
 
18
18
  let inflight = null;
19
- let cachedConfig = null;
19
+ const cachedConfigByPlatform = new Map();
20
20
 
21
21
  function readEnv(key) {
22
22
  try {
@@ -26,17 +26,22 @@ function readEnv(key) {
26
26
  }
27
27
  }
28
28
 
29
- async function fetchMapsConfig({ apiBase, clientId, mapsConfigPath }) {
30
- if (cachedConfig) return cachedConfig;
29
+ async function fetchMapsConfig({ apiBase, clientId, mapsConfigPath, platform = "web" }) {
30
+ const cacheKey = `${clientId}:${platform}`;
31
+ if (cachedConfigByPlatform.has(cacheKey)) return cachedConfigByPlatform.get(cacheKey);
31
32
  const configPath = mapsConfigPath || "/api/v1/mapsjs/config";
32
- const url = `${apiBase}${configPath}?clientId=${encodeURIComponent(clientId)}`;
33
+ const params = new URLSearchParams({
34
+ clientId,
35
+ platform,
36
+ });
37
+ const url = `${apiBase}${configPath}?${params.toString()}`;
33
38
  const res = await fetch(url, { credentials: "omit" });
34
39
  const data = await res.json().catch(() => ({}));
35
40
  if (!res.ok || !data?.success) {
36
41
  throw new Error(`mapsjs/config failed: ${data?.error || `HTTP_${res.status}`}`);
37
42
  }
38
- cachedConfig = data.config;
39
- return cachedConfig;
43
+ cachedConfigByPlatform.set(cacheKey, data.config);
44
+ return data.config;
40
45
  }
41
46
 
42
47
  export async function loadGoogleMapsScript({
@@ -45,6 +50,7 @@ export async function loadGoogleMapsScript({
45
50
  apiBase: apiBaseInput,
46
51
  clientId: clientIdInput,
47
52
  mapsConfigPath,
53
+ platform = "web",
48
54
  } = {}) {
49
55
  if (typeof window === "undefined") {
50
56
  throw new Error("loadGoogleMapsScript must run in the browser");
@@ -66,7 +72,7 @@ export async function loadGoogleMapsScript({
66
72
  }
67
73
 
68
74
  inflight = (async () => {
69
- const config = await fetchMapsConfig({ apiBase, clientId, mapsConfigPath });
75
+ const config = await fetchMapsConfig({ apiBase, clientId, mapsConfigPath, platform });
70
76
 
71
77
  return new Promise((resolve, reject) => {
72
78
  const callbackName = `__sanvika_maps_cb_${Date.now()}`;
@@ -120,11 +126,15 @@ export async function loadGoogleMapsScript({
120
126
  }
121
127
  }
122
128
 
123
- export function getMapsConfig() {
124
- return cachedConfig;
129
+ export function getMapsConfig(platform = "web") {
130
+ const clientId =
131
+ readEnv("NEXT_PUBLIC_CLIENT_ID") ||
132
+ readEnv("NEXT_PUBLIC_GEOLOCATION_CLIENT_ID") ||
133
+ "default";
134
+ return cachedConfigByPlatform.get(`${clientId}:${platform}`) || null;
125
135
  }
126
136
 
127
137
  export function resetMapsLoader() {
128
- cachedConfig = null;
138
+ cachedConfigByPlatform.clear();
129
139
  inflight = null;
130
140
  }
@@ -96,6 +96,14 @@ export async function placesDetails(placeId, { language = "en", fields, sessionT
96
96
  });
97
97
  }
98
98
 
99
+ /** S2S — resolve platform-specific Google Map ID (web | android | ios | static). */
100
+ export async function getMapsPlatformConfig(platform = "web") {
101
+ return getServiceClient().request("/api/v1/maps/config", {
102
+ method: "GET",
103
+ query: { platform },
104
+ });
105
+ }
106
+
99
107
  /**
100
108
  * Detect Indian state + city from coordinates.
101
109
  * Now proxied through reverseGeocode (which calls geolocation service) — no Google key in consumer env.