@geophrase/core 1.0.4 → 1.0.6

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +14 -5
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "The core vanilla JavaScript library for Geophrase Connect.",
5
- "homepage": "https://github.com/geophrase/geophrase-js",
5
+ "homepage": "https://business.geophrase.com/docs",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/geophrase/geophrase-js.git"
package/src/index.js CHANGED
@@ -97,18 +97,27 @@ class Geophrase {
97
97
  async _handleMessage(event) {
98
98
  if (event.origin !== this.widgetOrigin) return;
99
99
 
100
- if (event.data?.type === 'GEOPHRASE_CLOSE_WIDGET') {
100
+ let data = event.data;
101
+ if (typeof data === 'string') {
102
+ try {
103
+ data = JSON.parse(data);
104
+ } catch (e) {
105
+ return;
106
+ }
107
+ }
108
+
109
+ if (data?.type === 'GEOPHRASE_CLOSE_WIDGET') {
101
110
  this.close();
102
111
  if (this.onClose) this.onClose();
103
112
  }
104
113
 
105
- if (event.data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
114
+ if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
106
115
  this.close();
107
116
  try {
108
117
  const response = await fetch(`${this.apiBase}/business/resolve/`, {
109
118
  method: "POST",
110
119
  headers: { "X-API-Key": this.apiKey, "Content-Type": "application/json" },
111
- body: JSON.stringify({ token: event.data.token }),
120
+ body: JSON.stringify({ token: data.token }),
112
121
  });
113
122
 
114
123
  if (!response.ok) {
@@ -123,8 +132,8 @@ class Geophrase {
123
132
  return;
124
133
  }
125
134
 
126
- const data = await response.json();
127
- if (this.onSuccess) this.onSuccess(data);
135
+ const responseData = await response.json();
136
+ if (this.onSuccess) this.onSuccess(responseData);
128
137
 
129
138
  } catch (error) {
130
139
  console.error("Geophrase Resolution Error:", error);