@geophrase/core 1.6.1 → 1.8.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.
- package/README.md +4 -2
- package/index.d.ts +8 -0
- package/package.json +1 -1
- package/src/index.js +19 -1
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ The snippet below uses `mode: 'server'`, which needs only your API key id — no
|
|
|
42
42
|
mode: 'server', // widget returns a token your backend exchanges for the address
|
|
43
43
|
theme: 'system', // 'light' | 'dark' | 'system'
|
|
44
44
|
orderId: 'ORD-98765', // your internal reference id
|
|
45
|
-
phone: '9999999999', //
|
|
45
|
+
phone: '9999999999', // prefills the phone field
|
|
46
46
|
|
|
47
47
|
onSuccess: (result) => console.log('Success:', result),
|
|
48
48
|
onError: (error) => console.error('Error:', error.message),
|
|
@@ -85,7 +85,9 @@ const geo = new Geophrase({
|
|
|
85
85
|
| `key` | `string` | **Conditional** | Your secret [Geophrase API key](https://geophrase.com/docs/api-keys). **Required if `mode` is `'client'`.** Omit if using server mode. |
|
|
86
86
|
| `theme` | `string` | Optional | `'light'`, `'dark'`, or `'system'`. Defaults to `'system'`. |
|
|
87
87
|
| `orderId` | `string` | Optional | Your internal reference ID for this checkout session. |
|
|
88
|
-
| `
|
|
88
|
+
| `hubCode` | `string` | **Conditional** | Delivery hub code (from Settings > Delivery hubs). Client mode only; required when `mode` is `'client'` and your business has more than one active delivery hub. In server mode, send `hub_code` from your backend to the [resolve call](https://geophrase.com/docs/resolve-endpoint). |
|
|
89
|
+
| `noteForAgent` | `string` | Optional | Note shown to your delivery agent, never to the customer. Client mode only; notes over 300 characters are cut. It is sent from the customer's device, so the customer can change it; send payment instructions, such as cash to collect, from your server or the dashboard. In server mode, send `note_for_agent` with your [resolve call](https://geophrase.com/docs/resolve-endpoint). |
|
|
90
|
+
| `phone` | `string` | Optional | Pre-fills the phone field with a 10-digit Indian mobile number. |
|
|
89
91
|
| `onSuccess` | `function` | **Yes** | Called upon completion. Returns an `Address` object (`mode: 'client'`) or a `Token` object (`mode: 'server'`). |
|
|
90
92
|
| `onError` | `function` | Optional | Called if the API fails or a network error occurs. Returns an `Error` object. |
|
|
91
93
|
| `onClose` | `function` | Optional | Called when the user closes the modal without completing the flow. |
|
package/index.d.ts
CHANGED
|
@@ -13,12 +13,18 @@ export interface GeophraseAddressDetails {
|
|
|
13
13
|
verified_mobile_num: string;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export interface GeophraseHub {
|
|
17
|
+
name: string;
|
|
18
|
+
code: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
16
21
|
export interface GeophraseAddress {
|
|
17
22
|
short_code: string;
|
|
18
23
|
short_link: string;
|
|
19
24
|
qr_code: string;
|
|
20
25
|
captured_at: number;
|
|
21
26
|
order_id: string;
|
|
27
|
+
hub?: GeophraseHub;
|
|
22
28
|
address: GeophraseAddressDetails;
|
|
23
29
|
}
|
|
24
30
|
|
|
@@ -37,7 +43,9 @@ export interface GeophraseOptions {
|
|
|
37
43
|
mode?: 'client' | 'server';
|
|
38
44
|
theme?: 'light' | 'dark' | 'system';
|
|
39
45
|
key?: string;
|
|
46
|
+
hubCode?: string;
|
|
40
47
|
orderId?: string;
|
|
48
|
+
noteForAgent?: string;
|
|
41
49
|
phone?: string;
|
|
42
50
|
onSuccess: (result: GeophraseAddress | GeophraseRequestId) => void;
|
|
43
51
|
onError?: (error: GeophraseError) => void;
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
function extractApiErrorMessage(errorData, fallback) {
|
|
2
|
+
if (!errorData || typeof errorData !== 'object') return fallback;
|
|
3
|
+
if (typeof errorData.message === 'string' && errorData.message) return errorData.message;
|
|
4
|
+
if (typeof errorData.detail === 'string' && errorData.detail) return errorData.detail;
|
|
5
|
+
for (const value of Object.values(errorData)) {
|
|
6
|
+
if (Array.isArray(value) && value.length > 0 && typeof value[0] === 'string') return value[0];
|
|
7
|
+
if (typeof value === 'string' && value) return value;
|
|
8
|
+
}
|
|
9
|
+
return fallback;
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
class Geophrase {
|
|
2
13
|
static _instanceCounter = 0;
|
|
3
14
|
|
|
@@ -36,7 +47,9 @@ class Geophrase {
|
|
|
36
47
|
|
|
37
48
|
this.keyId = options.keyId;
|
|
38
49
|
this.apiKey = this.mode === 'client' ? options.key : null;
|
|
50
|
+
this.hubCode = options.hubCode;
|
|
39
51
|
this.orderId = options.orderId;
|
|
52
|
+
this.noteForAgent = options.noteForAgent;
|
|
40
53
|
this.phone = options.phone;
|
|
41
54
|
|
|
42
55
|
// 4. Theme validation
|
|
@@ -335,7 +348,12 @@ class Geophrase {
|
|
|
335
348
|
let callback = null;
|
|
336
349
|
|
|
337
350
|
const payload = { request_id: data.requestId };
|
|
351
|
+
if (this.hubCode != null && String(this.hubCode).trim()) {
|
|
352
|
+
payload.hub_code = String(this.hubCode).trim();
|
|
353
|
+
}
|
|
338
354
|
if (this.orderId) payload.order_id = this.orderId;
|
|
355
|
+
// Cut to the API's limit: a longer note would fail the whole address capture.
|
|
356
|
+
if (this.noteForAgent) payload.note_for_agent = String(this.noteForAgent).slice(0, 300);
|
|
339
357
|
|
|
340
358
|
try {
|
|
341
359
|
const response = await fetch(`${this.apiBase}/business/resolve/`, {
|
|
@@ -353,7 +371,7 @@ class Geophrase {
|
|
|
353
371
|
callback = () => this._safeCall(this.onError, {
|
|
354
372
|
type: 'API_ERROR',
|
|
355
373
|
status: response.status,
|
|
356
|
-
message: errorData
|
|
374
|
+
message: extractApiErrorMessage(errorData, 'Geophrase API resolution failed')
|
|
357
375
|
});
|
|
358
376
|
} else {
|
|
359
377
|
const responseData = await response.json();
|