@superfeedback/widget 0.0.24 → 0.0.26

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.js CHANGED
@@ -7,15 +7,15 @@ var VISITOR_ID_KEY = "sf_visitor_id", SessionTracker = class {
7
7
  #i;
8
8
  #a = 0;
9
9
  #o = !0;
10
- constructor(e, r) {
11
- this.#e = e, this.#t = r, this.#i = Date.now(), this.#n = this.#s(), this.#r = crypto.randomUUID(), this.#m();
10
+ constructor(e, i) {
11
+ this.#e = e, this.#t = i, this.#i = Date.now(), this.#n = this.#s(), this.#r = crypto.randomUUID(), this.#m();
12
12
  }
13
13
  #s() {
14
14
  try {
15
15
  let e = localStorage.getItem(VISITOR_ID_KEY);
16
16
  if (e) return e;
17
- let i = crypto.randomUUID();
18
- return localStorage.setItem(VISITOR_ID_KEY, i), i;
17
+ let a = crypto.randomUUID();
18
+ return localStorage.setItem(VISITOR_ID_KEY, a), a;
19
19
  } catch {
20
20
  return crypto.randomUUID();
21
21
  }
@@ -45,11 +45,11 @@ var VISITOR_ID_KEY = "sf_visitor_id", SessionTracker = class {
45
45
  }
46
46
  #f() {
47
47
  if (this.#u() < 1e3) return;
48
- let e = this.#d(), r = `${this.#t}/api/external/v1/apps/${this.#e}/sessions`, i = JSON.stringify(e);
49
- fetch(r, {
48
+ let e = this.#d(), i = `${this.#t}/api/external/v1/apps/${this.#e}/sessions`, a = JSON.stringify(e);
49
+ fetch(i, {
50
50
  method: "POST",
51
51
  headers: { "Content-Type": "application/json" },
52
- body: i,
52
+ body: a,
53
53
  keepalive: !0,
54
54
  credentials: "omit"
55
55
  }).catch(() => {}), this.#a = 0, this.#i = Date.now();
@@ -70,17 +70,21 @@ var VISITOR_ID_KEY = "sf_visitor_id", SessionTracker = class {
70
70
  this.#f();
71
71
  }
72
72
  }, __widgetBaseUrl = null;
73
- typeof document < "u" && document.currentScript && (__widgetBaseUrl = document.currentScript.src);
73
+ typeof document < "u" && document.currentScript && (__widgetBaseUrl = document.currentScript.src), console.log(__widgetBaseUrl);
74
+ function buildUrl(e, i) {
75
+ let a = new URL(e);
76
+ return a.pathname = a.pathname.replace(/\/$/, "") + i, a.href;
77
+ }
74
78
  function loadScript(e) {
75
- return new Promise((r, i) => {
76
- let a = document.createElement("script");
77
- a.src = e, a.onload = () => r(), a.onerror = () => i(/* @__PURE__ */ Error(`Failed to load: ${e}`)), document.head.appendChild(a);
79
+ return new Promise((i, a) => {
80
+ let o = document.createElement("script");
81
+ o.src = e, o.onload = () => i(), o.onerror = () => a(/* @__PURE__ */ Error(`Failed to load: ${e}`)), document.head.appendChild(o);
78
82
  });
79
83
  }
80
84
  var iifeLoader = async () => {
81
85
  if (window.__superfeedback_widget) return window.__superfeedback_widget;
82
86
  if (!__widgetBaseUrl) throw Error("Cannot determine widget chunk URL");
83
- await loadScript(`${__widgetBaseUrl}/dist/widget.iife.js`);
87
+ console.log("load widget from base_url:", __widgetBaseUrl), await loadScript(buildUrl(__widgetBaseUrl, "/dist/widget.iife.js"));
84
88
  let e = window.__superfeedback_widget;
85
89
  if (!e) throw Error("Widget chunk failed to register");
86
90
  return e;
@@ -90,7 +94,7 @@ var iifeLoader = async () => {
90
94
  } catch {
91
95
  return iifeLoader();
92
96
  }
93
- }, Superfeedback = class r {
97
+ }, Superfeedback = class i {
94
98
  static #e = null;
95
99
  static #t = "superfeedback-widget";
96
100
  #n = null;
@@ -98,51 +102,53 @@ var iifeLoader = async () => {
98
102
  #i = null;
99
103
  #a = !1;
100
104
  constructor(e) {
101
- if (this.#r = document.createElement("div"), this.#r.id = r.#t, this.#r.style.position = "fixed", this.#r.style.bottom = "20px", this.#r.style.right = "20px", this.#r.style.zIndex = "9999", document.body.appendChild(this.#r), e.appId && (this.#i = new SessionTracker(e.appId, "https://app.superfeedback.ai")), !e.appId && !e.projectId) {
105
+ if (this.#r = document.createElement("div"), this.#r.id = i.#t, this.#r.style.position = "fixed", this.#r.style.bottom = "20px", this.#r.style.right = "20px", this.#r.style.zIndex = "9999", document.body.appendChild(this.#r), e.appId && (this.#i = new SessionTracker(e.appId, "https://app.superfeedback.ai")), !e.appId && !e.projectId) {
102
106
  console.error("Superfeedback: appId or projectId is required");
103
107
  return;
104
108
  }
105
109
  this.#o(e);
106
110
  }
107
- async #o(r) {
111
+ async #o(i) {
108
112
  if (this.#a) return;
109
113
  this.#a = !0;
110
- let i = r.projectId;
111
- if (!i && r.appId) {
112
- let [a, o] = await tryCatchAsync(this.#s(r.appId));
113
- if (o) {
114
- console.error("Superfeedback: Failed to resolve projectId from appId", o), this.#a = !1;
114
+ let a = i.projectId;
115
+ if (!a && i.appId) {
116
+ let [o, s] = await tryCatchAsync(this.#s(i.appId));
117
+ if (s) {
118
+ console.error("Superfeedback: Failed to resolve projectId from appId", s), this.#a = !1;
115
119
  return;
116
120
  }
117
- i = a;
121
+ a = o;
118
122
  }
119
- if (!i) {
123
+ if (!a) {
120
124
  console.log("Superfeedback: Could not determine projectId"), this.#a = !1;
121
125
  return;
122
126
  }
123
- let [a, o] = await tryCatchAsync(loadWidgetModule());
124
- if (o || !this.#r) {
125
- o && console.error("Superfeedback: Failed to load widget", o), this.#r || console.error("Superfeedback: Container element not found"), this.#a = !1;
127
+ let [o, s] = await tryCatchAsync(loadWidgetModule());
128
+ if (s || !this.#r) {
129
+ s && console.error("Superfeedback: Failed to load widget", s), this.#r || console.error("Superfeedback: Container element not found"), this.#a = !1;
126
130
  return;
127
131
  }
128
- let { createApp: s, Root: c } = a, l = s(c, {
129
- projectId: i,
130
- previewMode: r.previewMode,
131
- config: r.config,
132
- strategy: r.strategy
132
+ let { createApp: c, Root: l } = o, u = c(l, {
133
+ projectId: a,
134
+ previewMode: i.previewMode,
135
+ config: i.config,
136
+ strategy: i.strategy
133
137
  });
134
- this.#n = l, l.mount(this.#r), this.#a = !1;
138
+ this.#n = u, u.mount(this.#r), this.#a = !1;
135
139
  }
136
140
  async #s(e) {
137
- let r = await fetch(`https://app.superfeedback.ai/api/external/v1/apps/${e}/config`);
138
- if (!r.ok) throw Error(`Failed to resolve appId: ${r.statusText}`);
139
- return (await r.json()).projectId;
141
+ let i = "https://app.superfeedback.ai";
142
+ console.log("resolve project_id with url:", i);
143
+ let a = await fetch(`${i}/api/external/v1/apps/${e}/config`);
144
+ if (!a.ok) throw Error(`Failed to resolve appId: ${a.statusText}`);
145
+ return (await a.json()).projectId;
140
146
  }
141
147
  static init(e) {
142
- return r.#e ||= new r(e), r.#e;
148
+ return i.#e ||= new i(e), i.#e;
143
149
  }
144
150
  destroy() {
145
- this.#i &&= (this.#i.destroy(), null), this.#n &&= (this.#n.unmount(), null), this.#r &&= (this.#r.remove(), null), r.#e = null;
151
+ this.#i &&= (this.#i.destroy(), null), this.#n &&= (this.#n.unmount(), null), this.#r &&= (this.#r.remove(), null), i.#e = null;
146
152
  }
147
153
  };
148
154
  function getAppIdFromScript() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superfeedback/widget",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "files": [