@geophrase/core 1.4.0 → 1.5.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.d.ts +22 -3
  2. package/package.json +1 -1
  3. package/src/index.js +55 -16
package/index.d.ts CHANGED
@@ -1,10 +1,29 @@
1
+ export interface GeophraseAddressDetails {
2
+ city: string;
3
+ state: string;
4
+ digi_pin: string;
5
+ landmark: string;
6
+ latitude: number;
7
+ longitude: number;
8
+ postal_code: number;
9
+ address_type: string;
10
+ address_line_one: string;
11
+ address_line_two: string;
12
+ contact_full_name: string;
13
+ verified_mobile_num: string;
14
+ }
15
+
1
16
  export interface GeophraseAddress {
2
- phrase: string;
3
- [key: string]: any;
17
+ short_code: string;
18
+ short_link: string;
19
+ qr_code: string;
20
+ captured_at: number;
21
+ order_id: string;
22
+ address: GeophraseAddressDetails;
4
23
  }
5
24
 
6
25
  export interface GeophraseToken {
7
- token: string;
26
+ requestId: string;
8
27
  }
9
28
 
10
29
  export interface GeophraseError {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Drop-in address picker widget for Indian e-commerce checkouts. Captures structured addresses with coordinates to reduce RTO.",
5
5
  "license": "MIT",
6
6
  "author": "Geophrase",
package/src/index.js CHANGED
@@ -59,7 +59,6 @@ class Geophrase {
59
59
 
60
60
  // 6. Pre-compute the secure widget URL
61
61
  const url = new URL(this.widgetOrigin);
62
- if (this.orderId) url.searchParams.append('order-id', this.orderId);
63
62
  if (this.phone) url.searchParams.append('phone', this.phone);
64
63
  if (this.theme) url.searchParams.append('theme', this.theme);
65
64
  this.widgetUrl = url.toString();
@@ -151,6 +150,14 @@ class Geophrase {
151
150
  return `${this.styleId}-tc`;
152
151
  }
153
152
 
153
+ _loaderId() {
154
+ return `${this.styleId}-loader`;
155
+ }
156
+
157
+ _spinKeyframesName() {
158
+ return `${this.styleId}-spin`;
159
+ }
160
+
154
161
  /**
155
162
  * Hide the iframe until its document has actually painted. Browsers render
156
163
  * an unloaded iframe as a white rectangle, which (a) flashes visibly and
@@ -188,7 +195,22 @@ class Geophrase {
188
195
  }
189
196
  #${this.overlayId}.geophrase-active { opacity: 1; visibility: visible; }
190
197
 
191
- #${this.iframeId} { width: 100%; height: 100%; border: none; }
198
+ #${this._loaderId()} {
199
+ position: absolute;
200
+ top: 50%; left: 50%;
201
+ width: 40px; height: 40px;
202
+ margin: -20px 0 0 -20px;
203
+ border: 3px solid rgba(255, 255, 255, 0.25);
204
+ border-top-color: rgba(255, 255, 255, 0.9);
205
+ border-radius: 50%;
206
+ animation: ${this._spinKeyframesName()} 0.8s linear infinite;
207
+ }
208
+ @keyframes ${this._spinKeyframesName()} { to { transform: rotate(360deg); } }
209
+
210
+ #${this.iframeId} {
211
+ width: 100%; height: 100%; border: none;
212
+ position: relative; z-index: 1;
213
+ }
192
214
  ${this._getIframeBgCSS()}
193
215
 
194
216
  @media (min-width: 768px) {
@@ -204,11 +226,17 @@ class Geophrase {
204
226
  const overlay = document.createElement('div');
205
227
  overlay.id = this.overlayId;
206
228
 
229
+ // Spinner shown while the iframe is loading. The iframe stacks above
230
+ // it (z-index: 1) and covers it once paint completes — no state to toggle.
231
+ const loader = document.createElement('div');
232
+ loader.id = this._loaderId();
233
+ overlay.appendChild(loader);
234
+
207
235
  const iframe = document.createElement('iframe');
208
236
  iframe.id = this.iframeId;
209
237
  iframe.allow = 'geolocation';
210
-
211
238
  overlay.appendChild(iframe);
239
+
212
240
  document.body.appendChild(overlay);
213
241
 
214
242
  window.removeEventListener('message', this._boundHandleMessage);
@@ -276,18 +304,29 @@ class Geophrase {
276
304
  }
277
305
 
278
306
  if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
279
- this.close();
280
-
281
- // Server mode: hand the raw token to the merchant for backend exchange
307
+ // Server mode: nothing to wait for — close immediately and hand
308
+ // the raw token to the merchant for backend exchange.
282
309
  if (this.mode === 'server') {
283
- this._safeCall(this.onSuccess, { token: data.token });
310
+ this.close();
311
+ this._safeCall(this.onSuccess, { requestId: data.requestId });
284
312
  return;
285
313
  }
286
314
 
287
- // Client mode: resolve the token via the public API
315
+ // Client mode: keep the overlay + spinner visible while we exchange
316
+ // the token for the resolved address. Closing now would leave the
317
+ // merchant's page blank for the duration of the API round-trip
318
+ iframe.style.opacity = '0';
319
+
288
320
  const controller = new AbortController();
289
321
  const timeoutId = setTimeout(() => controller.abort(), 15000);
290
322
 
323
+ // Build the callback to fire after we close the overlay, so the
324
+ // merchant sees the modal disappear before their handler runs.
325
+ let callback = null;
326
+
327
+ const payload = { request_id: data.requestId };
328
+ if (this.orderId) payload.order_id = this.orderId;
329
+
291
330
  try {
292
331
  const response = await fetch(`${this.apiBase}/business/resolve/`, {
293
332
  method: 'POST',
@@ -295,26 +334,24 @@ class Geophrase {
295
334
  'X-API-Key': this.apiKey,
296
335
  'Content-Type': 'application/json'
297
336
  },
298
- body: JSON.stringify({ token: data.token }),
337
+ body: JSON.stringify(payload),
299
338
  signal: controller.signal
300
339
  });
301
340
 
302
341
  if (!response.ok) {
303
342
  const errorData = await response.json().catch(() => ({}));
304
- this._safeCall(this.onError, {
343
+ callback = () => this._safeCall(this.onError, {
305
344
  type: 'API_ERROR',
306
345
  status: response.status,
307
346
  message: errorData.message || 'Geophrase API resolution failed'
308
347
  });
309
- return;
348
+ } else {
349
+ const responseData = await response.json();
350
+ callback = () => this._safeCall(this.onSuccess, responseData);
310
351
  }
311
-
312
- const responseData = await response.json();
313
- this._safeCall(this.onSuccess, responseData);
314
-
315
352
  } catch (error) {
316
353
  console.error('Geophrase Resolution Error:', error);
317
- this._safeCall(this.onError, {
354
+ callback = () => this._safeCall(this.onError, {
318
355
  type: 'NETWORK_ERROR',
319
356
  message: error.name === 'AbortError'
320
357
  ? 'Geophrase API request timed out'
@@ -322,6 +359,8 @@ class Geophrase {
322
359
  });
323
360
  } finally {
324
361
  clearTimeout(timeoutId);
362
+ this.close();
363
+ callback?.();
325
364
  }
326
365
  }
327
366
  }