@geophrase/core 1.0.5 → 1.1.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/package.json +1 -1
- package/src/index.js +45 -17
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
class Geophrase {
|
|
2
2
|
constructor(options = {}) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
// 1. Strict Constructor Validation
|
|
4
|
+
if (!options.key) {
|
|
5
|
+
throw new Error("Geophrase: 'key' is required.");
|
|
6
|
+
}
|
|
7
|
+
if (typeof document === 'undefined') {
|
|
8
|
+
return; // SSR safe no-op
|
|
6
9
|
}
|
|
7
10
|
|
|
8
11
|
this.apiKey = options.key;
|
|
@@ -21,7 +24,12 @@ class Geophrase {
|
|
|
21
24
|
|
|
22
25
|
this._boundHandleMessage = this._handleMessage.bind(this);
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
// 2. Pre-compute the URL, but DO NOT inject or load it yet
|
|
28
|
+
const url = new URL(this.widgetOrigin);
|
|
29
|
+
url.searchParams.append('api-key', this.apiKey);
|
|
30
|
+
if (this.orderId) url.searchParams.append('order-id', this.orderId);
|
|
31
|
+
if (this.phone) url.searchParams.append('phone', this.phone);
|
|
32
|
+
this.widgetUrl = url.toString();
|
|
25
33
|
}
|
|
26
34
|
|
|
27
35
|
_injectDOM() {
|
|
@@ -56,19 +64,22 @@ class Geophrase {
|
|
|
56
64
|
iframe.id = this.iframeId;
|
|
57
65
|
iframe.allow = "geolocation";
|
|
58
66
|
|
|
59
|
-
const url = new URL(this.widgetOrigin);
|
|
60
|
-
url.searchParams.append('api-key', this.apiKey);
|
|
61
|
-
if (this.orderId) url.searchParams.append('order-id', this.orderId);
|
|
62
|
-
if (this.phone) url.searchParams.append('phone', this.phone);
|
|
63
|
-
iframe.src = url.toString();
|
|
64
|
-
|
|
65
67
|
overlay.appendChild(iframe);
|
|
66
68
|
document.body.appendChild(overlay);
|
|
67
69
|
|
|
70
|
+
// Prevent duplicate listeners
|
|
71
|
+
window.removeEventListener('message', this._boundHandleMessage);
|
|
68
72
|
window.addEventListener('message', this._boundHandleMessage);
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
open() {
|
|
76
|
+
// 3. Lazy-load the DOM and iframe src exactly when clicked
|
|
77
|
+
this._injectDOM();
|
|
78
|
+
const iframe = document.getElementById(this.iframeId);
|
|
79
|
+
if (iframe && !iframe.hasAttribute('src')) {
|
|
80
|
+
iframe.src = this.widgetUrl;
|
|
81
|
+
}
|
|
82
|
+
|
|
72
83
|
document.body.style.overflow = 'hidden';
|
|
73
84
|
document.getElementById(this.overlayId)?.classList.add('geophrase-active');
|
|
74
85
|
}
|
|
@@ -78,7 +89,7 @@ class Geophrase {
|
|
|
78
89
|
document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
|
|
79
90
|
|
|
80
91
|
const iframe = document.getElementById(this.iframeId);
|
|
81
|
-
if (iframe) iframe.src
|
|
92
|
+
if (iframe) iframe.removeAttribute('src'); // 4. Kill the background session safely
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
destroy() {
|
|
@@ -97,18 +108,33 @@ class Geophrase {
|
|
|
97
108
|
async _handleMessage(event) {
|
|
98
109
|
if (event.origin !== this.widgetOrigin) return;
|
|
99
110
|
|
|
100
|
-
|
|
111
|
+
let data = event.data;
|
|
112
|
+
if (typeof data === 'string') {
|
|
113
|
+
try {
|
|
114
|
+
data = JSON.parse(data);
|
|
115
|
+
} catch (e) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (data?.type === 'GEOPHRASE_CLOSE_WIDGET') {
|
|
101
121
|
this.close();
|
|
102
122
|
if (this.onClose) this.onClose();
|
|
103
123
|
}
|
|
104
124
|
|
|
105
|
-
if (
|
|
125
|
+
if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
|
|
106
126
|
this.close();
|
|
127
|
+
|
|
128
|
+
// 5. Critical API Timeout protection
|
|
129
|
+
const controller = new AbortController();
|
|
130
|
+
const timeoutId = setTimeout(() => controller.abort(), 15000);
|
|
131
|
+
|
|
107
132
|
try {
|
|
108
133
|
const response = await fetch(`${this.apiBase}/business/resolve/`, {
|
|
109
134
|
method: "POST",
|
|
110
135
|
headers: { "X-API-Key": this.apiKey, "Content-Type": "application/json" },
|
|
111
|
-
body: JSON.stringify({ token:
|
|
136
|
+
body: JSON.stringify({ token: data.token }),
|
|
137
|
+
signal: controller.signal
|
|
112
138
|
});
|
|
113
139
|
|
|
114
140
|
if (!response.ok) {
|
|
@@ -123,17 +149,19 @@ class Geophrase {
|
|
|
123
149
|
return;
|
|
124
150
|
}
|
|
125
151
|
|
|
126
|
-
const
|
|
127
|
-
if (this.onSuccess) this.onSuccess(
|
|
152
|
+
const responseData = await response.json();
|
|
153
|
+
if (this.onSuccess) this.onSuccess(responseData);
|
|
128
154
|
|
|
129
155
|
} catch (error) {
|
|
130
156
|
console.error("Geophrase Resolution Error:", error);
|
|
131
157
|
if (this.onError) {
|
|
132
158
|
this.onError({
|
|
133
159
|
type: 'NETWORK_ERROR',
|
|
134
|
-
message: error.
|
|
160
|
+
message: error.name === 'AbortError' ? 'Geophrase API request timed out' : error.message
|
|
135
161
|
});
|
|
136
162
|
}
|
|
163
|
+
} finally {
|
|
164
|
+
clearTimeout(timeoutId);
|
|
137
165
|
}
|
|
138
166
|
}
|
|
139
167
|
}
|