@deflectbot/deflect-sdk 1.3.7 → 1.3.8

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/dist/index.d.ts CHANGED
@@ -12,7 +12,9 @@ declare class Deflect {
12
12
  private scriptCache;
13
13
  private isWarmupInProgress;
14
14
  private hasWarmupError;
15
+ private static readonly SDK_ERROR_MARKER;
15
16
  constructor();
17
+ private initializeSentry;
16
18
  private initializeGlobalState;
17
19
  private setupAutomaticWarmup;
18
20
  private tryWarmup;
package/dist/index.esm.js CHANGED
@@ -1,12 +1,58 @@
1
+ import * as Sentry from "@sentry/browser";
1
2
  class Deflect {
2
3
  constructor() {
3
4
  this.config = null;
4
5
  this.scriptCache = null;
5
6
  this.isWarmupInProgress = false;
6
7
  this.hasWarmupError = false;
8
+ this.initializeSentry();
7
9
  this.initializeGlobalState();
8
10
  this.setupAutomaticWarmup();
9
11
  }
12
+ static {
13
+ this.SDK_ERROR_MARKER = "__DEFLECT_SDK_ERROR__";
14
+ }
15
+ initializeSentry() {
16
+ try {
17
+ Sentry.init({
18
+ dsn: "https://4a62ed75ab362d4694ae4215c2b4f621@o4509968565665792.ingest.de.sentry.io/4510257986469968",
19
+ sendDefaultPii: true,
20
+ // Enable to capture IP address
21
+ environment: typeof window !== "undefined" ? window.location.hostname : "unknown",
22
+ beforeSend(event) {
23
+ const isSDKError = event.tags?.deflect_sdk_error === "true";
24
+ if (!isSDKError) {
25
+ return null;
26
+ }
27
+ if (typeof window !== "undefined" && typeof navigator !== "undefined") {
28
+ event.contexts = event.contexts || {};
29
+ event.contexts.browser = {
30
+ name: navigator.userAgent,
31
+ version: navigator.appVersion
32
+ };
33
+ event.request = event.request || {};
34
+ event.request.headers = event.request.headers || {};
35
+ event.request.headers["User-Agent"] = navigator.userAgent;
36
+ event.contexts.page = {
37
+ url: window.location.href,
38
+ referrer: document.referrer,
39
+ title: document.title
40
+ };
41
+ event.contexts.device = {
42
+ screen_width: window.screen.width,
43
+ screen_height: window.screen.height,
44
+ viewport_width: window.innerWidth,
45
+ viewport_height: window.innerHeight,
46
+ language: navigator.language,
47
+ platform: navigator.platform
48
+ };
49
+ }
50
+ return event;
51
+ }
52
+ });
53
+ } catch {
54
+ }
55
+ }
10
56
  initializeGlobalState() {
11
57
  if (typeof window === "undefined") return;
12
58
  window.Deflect = window.Deflect || {};
@@ -51,7 +97,12 @@ class Deflect {
51
97
  try {
52
98
  const maybeError = JSON.parse(content);
53
99
  if (maybeError.success === false || maybeError.error) {
54
- throw new Error(maybeError.error || "Script fetch failed");
100
+ const errorMessage = maybeError.error || "Script fetch failed";
101
+ const error = new Error(errorMessage);
102
+ if (errorMessage === "action_does_not_exist" || errorMessage.includes("invalid action")) {
103
+ error.isUserError = true;
104
+ }
105
+ throw error;
55
106
  }
56
107
  } catch (e) {
57
108
  if (e instanceof SyntaxError) {
@@ -131,20 +182,34 @@ class Deflect {
131
182
  }
132
183
  }
133
184
  configure(params) {
134
- if (!params.actionId?.trim()) {
135
- throw new Error("actionId is required and cannot be empty");
136
- }
137
- if (this.config?.actionId === params.actionId) {
138
- return;
139
- }
140
- this.hasWarmupError = false;
141
- this.scriptCache = null;
142
- this.config = { ...params };
143
- if (typeof window !== "undefined") {
144
- window.Deflect.actionId = params.actionId;
145
- }
146
- if (!this.isTestMode()) {
147
- this.tryWarmup();
185
+ try {
186
+ if (!params.actionId?.trim()) {
187
+ throw new Error("actionId is required and cannot be empty");
188
+ }
189
+ if (this.config?.actionId === params.actionId) {
190
+ return;
191
+ }
192
+ this.hasWarmupError = false;
193
+ this.scriptCache = null;
194
+ this.config = { ...params };
195
+ if (typeof window !== "undefined") {
196
+ window.Deflect.actionId = params.actionId;
197
+ }
198
+ if (!this.isTestMode()) {
199
+ this.tryWarmup();
200
+ }
201
+ } catch (error) {
202
+ const isUserError = error && typeof error === "object" && "isUserError" in error && error.isUserError === true;
203
+ if (!isUserError) {
204
+ Sentry.captureException(error, {
205
+ tags: {
206
+ deflect_sdk_error: "true",
207
+ method: "configure",
208
+ actionId: params?.actionId || "unknown"
209
+ }
210
+ });
211
+ }
212
+ throw error;
148
213
  }
149
214
  }
150
215
  isTestMode() {
@@ -155,20 +220,39 @@ class Deflect {
155
220
  return this.getToken();
156
221
  }
157
222
  async getToken() {
158
- if (!this.config?.actionId) {
159
- throw new Error("Must call configure() before solveChallenge()");
160
- }
161
- if (this.isTestMode()) {
162
- return "TESTTOKEN";
163
- }
164
- let script;
165
- if (this.scriptCache && !this.isWarmupInProgress) {
166
- script = this.scriptCache;
167
- this.scriptCache = null;
168
- } else {
169
- script = await this.fetchScript();
223
+ try {
224
+ if (!this.config?.actionId) {
225
+ throw new Error("Must call configure() before solveChallenge()");
226
+ }
227
+ if (this.isTestMode()) {
228
+ return "TESTTOKEN";
229
+ }
230
+ if (this.config.actionId === "SENTRY_TEST") {
231
+ throw new Error("SENTRY_TEST: This is a test error to verify Sentry integration is working");
232
+ }
233
+ let script;
234
+ if (this.scriptCache && !this.isWarmupInProgress) {
235
+ script = this.scriptCache;
236
+ this.scriptCache = null;
237
+ } else {
238
+ script = await this.fetchScript();
239
+ }
240
+ return this.executeScript(script);
241
+ } catch (error) {
242
+ const isUserError = error && typeof error === "object" && "isUserError" in error && error.isUserError === true;
243
+ if (!isUserError) {
244
+ Sentry.captureException(error, {
245
+ tags: {
246
+ deflect_sdk_error: "true",
247
+ method: "getToken",
248
+ actionId: this.config?.actionId || "unknown",
249
+ hasCache: this.scriptCache !== null,
250
+ hasWarmupError: this.hasWarmupError
251
+ }
252
+ });
253
+ }
254
+ throw error;
170
255
  }
171
- return this.executeScript(script);
172
256
  }
173
257
  async warmup() {
174
258
  if (!this.config?.actionId) {