@geophrase/core 1.5.1 → 1.6.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 +7 -4
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/index.js +16 -6
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ import Geophrase from '@geophrase/core';
|
|
|
28
28
|
|
|
29
29
|
## Quick Start
|
|
30
30
|
|
|
31
|
-
The snippet below uses `mode: 'server'
|
|
31
|
+
The snippet below uses `mode: 'server'`, which needs only your API key id — no secret API key. Drop it into an HTML file, swap in your own `keyId`, and the widget runs.
|
|
32
32
|
|
|
33
33
|
```html
|
|
34
34
|
<button id="geophrase-btn">Select Delivery Address</button>
|
|
@@ -36,6 +36,7 @@ The snippet below uses `mode: 'server'` so you can paste it into an HTML file an
|
|
|
36
36
|
<script>
|
|
37
37
|
document.addEventListener('DOMContentLoaded', function () {
|
|
38
38
|
const geo = new Geophrase({
|
|
39
|
+
keyId: 'YOUR_API_KEY_ID', // required — your API key id (both modes)
|
|
39
40
|
mode: 'server', // widget returns a token your backend exchanges for the address
|
|
40
41
|
theme: 'system', // 'light' | 'dark' | 'system'
|
|
41
42
|
orderId: 'ORD-98765', // your internal reference id
|
|
@@ -59,13 +60,14 @@ The SDK supports two modes. Pick whichever fits your architecture:
|
|
|
59
60
|
|
|
60
61
|
- **`client`**: the widget resolves the address in the browser and passes it straight to `onSuccess`. Requires `key` in the SDK options.
|
|
61
62
|
|
|
62
|
-
>
|
|
63
|
+
> Every integration requires your API key id (`keyId`) in the SDK options — it identifies your account to the widget. On top of that, `client` mode needs your secret API key (`key`) in the browser, while `server` mode uses that secret only on your backend. See [creating and securing an API key](https://geophrase.com/docs/api-keys).
|
|
63
64
|
>
|
|
64
|
-
> If your app has a backend, `server` mode keeps your API key entirely off the frontend, which is the most secure option. If you don't have a backend (or want the fastest possible integration), `client` mode with a domain-restricted key is fine.
|
|
65
|
+
> If your app has a backend, `server` mode keeps your secret API key entirely off the frontend, which is the most secure option. If you don't have a backend (or want the fastest possible integration), `client` mode with a domain-restricted key is fine.
|
|
65
66
|
|
|
66
67
|
```javascript
|
|
67
68
|
// client mode, no backend integration required
|
|
68
69
|
const geo = new Geophrase({
|
|
70
|
+
keyId: 'YOUR_API_KEY_ID',
|
|
69
71
|
mode: 'client',
|
|
70
72
|
key: 'YOUR_API_KEY',
|
|
71
73
|
onSuccess: (address) => console.log('Address:', address)
|
|
@@ -76,8 +78,9 @@ const geo = new Geophrase({
|
|
|
76
78
|
|
|
77
79
|
| Parameter | Type | Required | Description |
|
|
78
80
|
| :---------- | :--------- | :-------------- | :----------------------------------------------------------- |
|
|
81
|
+
| `keyId` | `string` | **Yes** | Your 8-character [Geophrase API key id](https://geophrase.com/docs/api-keys). Required in **both** modes; sent to the widget to identify your account. |
|
|
79
82
|
| `mode` | `string` | Optional | `'client'` (default) or `'server'`. Determines the architectural flow of the SDK. |
|
|
80
|
-
| `key` | `string` | **Conditional** | Your [Geophrase API key](https://geophrase.com/docs/api-keys). **Required if `mode` is `'client'`.** Omit if using server mode. |
|
|
83
|
+
| `key` | `string` | **Conditional** | Your secret [Geophrase API key](https://geophrase.com/docs/api-keys). **Required if `mode` is `'client'`.** Omit if using server mode. |
|
|
81
84
|
| `theme` | `string` | Optional | `'light'`, `'dark'`, or `'system'`. Defaults to `'system'`. |
|
|
82
85
|
| `orderId` | `string` | Optional | Your internal reference ID for this checkout session. |
|
|
83
86
|
| `phone` | `string` | Optional | The customer's 10-digit phone number (pre-fills the widget). |
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -14,10 +14,18 @@ class Geophrase {
|
|
|
14
14
|
// 1. Mode validation
|
|
15
15
|
this.mode = options.mode || 'client';
|
|
16
16
|
if (!['client', 'server'].includes(this.mode)) {
|
|
17
|
-
throw new Error(`Geophrase: Invalid mode '${this.mode}'. Expected 'client' or 'server'.`);
|
|
17
|
+
throw new Error(`Geophrase Error: Invalid mode '${this.mode}'. Expected 'client' or 'server'.`);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
// 2.
|
|
20
|
+
// 2. Key ID validation — required in every mode; the widget needs it to identify the merchant.
|
|
21
|
+
if (!options.keyId) {
|
|
22
|
+
throw new Error("Geophrase Error: 'keyId' is required.");
|
|
23
|
+
}
|
|
24
|
+
if (typeof options.keyId !== 'string' || options.keyId.length !== 8) {
|
|
25
|
+
throw new Error("Geophrase Error: 'keyId' must be an 8-character string.");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 3. API key validation
|
|
21
29
|
if (this.mode === 'client' && !options.key) {
|
|
22
30
|
throw new Error("Geophrase Error: 'key' is required when mode is 'client'.");
|
|
23
31
|
}
|
|
@@ -26,11 +34,12 @@ class Geophrase {
|
|
|
26
34
|
console.warn("Geophrase Warning: 'key' is ignored when mode is 'server'. Ensure you are not exposing a Secret Key in your frontend.");
|
|
27
35
|
}
|
|
28
36
|
|
|
37
|
+
this.keyId = options.keyId;
|
|
29
38
|
this.apiKey = this.mode === 'client' ? options.key : null;
|
|
30
39
|
this.orderId = options.orderId;
|
|
31
40
|
this.phone = options.phone;
|
|
32
41
|
|
|
33
|
-
//
|
|
42
|
+
// 4. Theme validation
|
|
34
43
|
this.theme = options.theme;
|
|
35
44
|
if (this.theme && !['light', 'dark', 'system'].includes(this.theme)) {
|
|
36
45
|
console.warn(`Geophrase Warning: Invalid theme '${this.theme}'. Falling back to default.`);
|
|
@@ -41,13 +50,13 @@ class Geophrase {
|
|
|
41
50
|
this.onError = options.onError;
|
|
42
51
|
this.onClose = options.onClose;
|
|
43
52
|
|
|
44
|
-
//
|
|
53
|
+
// 5. Per-instance DOM IDs so multiple instances don't collide
|
|
45
54
|
const instanceId = ++Geophrase._instanceCounter;
|
|
46
55
|
this.iframeId = `geophrase-iframe-${instanceId}`;
|
|
47
56
|
this.overlayId = `geophrase-overlay-${instanceId}`;
|
|
48
57
|
this.styleId = `geophrase-styles-${instanceId}`;
|
|
49
58
|
|
|
50
|
-
//
|
|
59
|
+
// 6. Endpoints (undocumented override for staging)
|
|
51
60
|
const endpoints = options._endpoints || {};
|
|
52
61
|
this.widgetOrigin = endpoints.widgetOrigin || 'https://connect.geophrase.com';
|
|
53
62
|
this.apiBase = endpoints.apiBase || 'https://api.geophrase.com';
|
|
@@ -57,8 +66,9 @@ class Geophrase {
|
|
|
57
66
|
// null when not applied; an object of saved values otherwise.
|
|
58
67
|
this._savedHostState = null;
|
|
59
68
|
|
|
60
|
-
//
|
|
69
|
+
// 7. Pre-compute the secure widget URL
|
|
61
70
|
const url = new URL(this.widgetOrigin);
|
|
71
|
+
url.searchParams.append('key-id', this.keyId);
|
|
62
72
|
if (this.phone) url.searchParams.append('phone', this.phone);
|
|
63
73
|
if (this.theme) url.searchParams.append('theme', this.theme);
|
|
64
74
|
this.widgetUrl = url.toString();
|