obi-sdk 0.1.1 → 0.1.3

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 CHANGED
@@ -18,19 +18,18 @@ Currently, the recommended way to use Obi SDK is through the inline loader scrip
18
18
 
19
19
  The loader accepts the following configuration options:
20
20
 
21
- | Option | Type | Default | Description |
22
- | ------------ | ------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------- |
23
- | `position` | string | `'bottom-right'` | Position of the widget on the page. Options: `'bottom-right'`, `'bottom-left'`, `'top-right'`, `'top-left'` |
24
- | `apiKey` | string | - | Your Obi API key (required for production) |
25
- | `sdkVersion` | string | `'latest'` | Version of the SDK to load |
26
- | `noCacheSDK` | boolean | `false` | Force fresh load of SDK without caching |
27
- | `apiBaseUrl` | string | `'https://www.iamobi.ai/api/'` | Base URL for API calls |
21
+ | Option | Type | Default | Description |
22
+ | ---------- | ------- | ---------------- | ----------------------------------------------------------------------------------------------------------- |
23
+ | `position` | string | `'bottom-right'` | Position of the widget on the page. Options: `'bottom-right'`, `'bottom-left'`, `'top-right'`, `'top-left'` |
24
+ | `apiKey` | string | - | Your Obi API key (required for production) |
25
+ | `autoInit` | boolean | `true` | Whether to initialize the widget automatically when loaded |
26
+ | `user` | object | - | User information with `id` (required), `email` (optional) and `metadata` (optional) |
28
27
 
29
28
  ## Quick Start
30
29
 
31
30
  The simplest way to add Obi to your website is by using the inline loader script:
32
31
 
33
- 1. Copy the full inline loader script from above
32
+ 1. Copy the full inline loader script from below
34
33
  2. Add it to your HTML page
35
34
  3. Configure the widget using `window.obiWidgetConfig`
36
35
 
@@ -44,7 +43,10 @@ The simplest way to add Obi to your website is by using the inline loader script
44
43
  window.obiWidgetConfig = {
45
44
  position: "bottom-right",
46
45
  apiKey: "YOUR_API_KEY",
47
- apiBaseUrl: "https://www.iamobi.ai/api/",
46
+ user: {
47
+ id: "user-123",
48
+ email: "user@example.com",
49
+ },
48
50
  }
49
51
  </script>
50
52
  </head>
@@ -57,9 +59,7 @@ The simplest way to add Obi to your website is by using the inline loader script
57
59
  "use strict"
58
60
  var c = {
59
61
  position: "bottom-right",
60
- apiBaseUrl: "https://www.iamobi.ai/api/",
61
- sdkVersion: "latest",
62
- noCacheSDK: !1,
62
+ autoInit: true,
63
63
  }
64
64
  w.__obiSDKLoading = w.__obiSDKLoading || !1
65
65
  function i() {
@@ -73,20 +73,12 @@ The simplest way to add Obi to your website is by using the inline loader script
73
73
  })
74
74
  return
75
75
  }
76
- if (f.sdkVersion === "latest" && !f.noCacheSDK) {
77
- m(function (v) {
78
- v && (f.sdkVersion = v)
79
- j(f, function () {
80
- w.__obiSDKLoading = !1
81
- g(f)
82
- })
83
- })
84
- } else {
85
- j(f, function () {
76
+ m(function (v) {
77
+ j(v, function () {
86
78
  w.__obiSDKLoading = !1
87
79
  g(f)
88
80
  })
89
- }
81
+ })
90
82
  }
91
83
  }
92
84
  function e(f) {
@@ -99,7 +91,7 @@ The simplest way to add Obi to your website is by using the inline loader script
99
91
  }
100
92
  function m(cb) {
101
93
  try {
102
- var x = new (w.XMLHttpRequest || w.ActiveXObject)("Microsoft.XMLHTTP")
94
+ var x = new XMLHttpRequest()
103
95
  x.open("GET", "https://registry.npmjs.org/obi-sdk/latest")
104
96
  x.onload = function () {
105
97
  if (x.status === 200) {
@@ -121,23 +113,23 @@ The simplest way to add Obi to your website is by using the inline loader script
121
113
  cb(null)
122
114
  }
123
115
  }
124
- function j(f, cb) {
116
+ function j(v, cb) {
125
117
  w.__obiSDKLoading = !0
126
118
  var s = d.createElement("script")
127
119
  s.type = "text/javascript"
128
120
  s.async = !0
129
- var b = "https://cdn.iamobi.ai/obi-sdk"
130
- s.src = f.noCacheSDK
131
- ? b + "-" + f.sdkVersion + ".js?t=" + Date.now()
132
- : b + "-" + f.sdkVersion + ".js"
121
+ var b = "https://unpkg.com/obi-sdk"
122
+ var u = v || "latest"
123
+ s.src = b + "@" + u + "/dist/obi-sdk.standalone.iife.js"
133
124
  s.onload = cb
134
125
  s.onerror = function () {
135
- w.__obiSDKLoading = !1
136
- w.console && w.console.error && w.console.error("Failed to load Obi SDK")
137
- if (f.sdkVersion !== "latest" && !f.noCacheSDK) {
138
- w.console && w.console.warn && w.console.warn("Falling back to latest version")
139
- f.sdkVersion = "latest"
140
- j(f, cb)
126
+ if (u !== "latest") {
127
+ console.warn("Failed to load specific version, falling back to latest")
128
+ s.src = b + "@latest/dist/obi-sdk.standalone.iife.js"
129
+ s.onload = cb
130
+ } else {
131
+ w.__obiSDKLoading = !1
132
+ console.error("Failed to load Obi SDK")
141
133
  }
142
134
  }
143
135
  var fs = d.getElementsByTagName("script")[0]
@@ -159,36 +151,22 @@ The simplest way to add Obi to your website is by using the inline loader script
159
151
  var t = d.createElement("obi-widget")
160
152
  f.theme && t.setAttribute("theme", f.theme)
161
153
  f.apiKey && t.setAttribute("api-key", f.apiKey)
162
- f.endpoint && t.setAttribute("endpoint", f.endpoint)
163
- f.apiBaseUrl && t.setAttribute("api-base-url", f.apiBaseUrl)
164
- t.style.position = "fixed"
165
- t.style.zIndex = "9999"
166
- switch (f.position) {
167
- case "bottom-right":
168
- t.style.bottom = "20px"
169
- t.style.right = "20px"
170
- break
171
- case "bottom-left":
172
- t.style.bottom = "20px"
173
- t.style.left = "20px"
174
- break
175
- case "top-right":
176
- t.style.top = "20px"
177
- t.style.right = "20px"
178
- break
179
- case "top-left":
180
- t.style.top = "20px"
181
- t.style.left = "20px"
182
- break
183
- }
154
+ t.setAttribute("api-base-url", "https://www.iamobi.ai/api/")
155
+ f.position && t.setAttribute("position", f.position)
156
+ f.user && t.setAttribute("user", JSON.stringify(f.user))
184
157
  d.body.appendChild(t)
185
158
  w.console && w.console.log && w.console.log("Obi Widget added to page")
186
159
  }
187
- d.readyState === "complete"
188
- ? i()
189
- : w.attachEvent
190
- ? w.attachEvent("onload", i)
191
- : w.addEventListener("load", i, !1)
160
+ if (w.obiWidgetConfig && w.obiWidgetConfig.autoInit === false) {
161
+ // Skip auto-initialization
162
+ console.log("Obi Widget auto-initialization disabled")
163
+ } else {
164
+ d.readyState === "complete"
165
+ ? i()
166
+ : w.attachEvent
167
+ ? w.attachEvent("onload", i)
168
+ : w.addEventListener("load", i, !1)
169
+ }
192
170
  })(window, document)
193
171
  </script>
194
172
  </body>
@@ -197,6 +175,72 @@ The simplest way to add Obi to your website is by using the inline loader script
197
175
 
198
176
  The widget will automatically appear in the specified position on your page.
199
177
 
178
+ ## How It Works
179
+
180
+ The loader script:
181
+
182
+ 1. Dynamically checks for the latest version using the npm registry API
183
+ 2. Loads the specific versioned SDK from unpkg.com for better browser caching
184
+ 3. Creates and positions the `<obi-widget>` custom element on your page
185
+ 4. Initializes the widget with your API key and configuration
186
+
187
+ ## Advanced Loader Usage
188
+
189
+ The inline loader script shown above is already minified for production use. If you need to customize it or understand how it works, you can find the full, commented version in the source code (`src/loader-inline.js`).
190
+
191
+ The loader script is also available as a separate file that can be loaded directly:
192
+
193
+ ```html
194
+ <!-- Configure the widget (optional) -->
195
+ <script>
196
+ window.obiWidgetConfig = {
197
+ position: "bottom-right",
198
+ apiKey: "YOUR_API_KEY",
199
+ }
200
+ </script>
201
+
202
+ <!-- Load the widget using the separate loader script -->
203
+ <script src="https://unpkg.com/obi-sdk@latest/dist/obi-loader.iife.js"></script>
204
+ ```
205
+
206
+ ## Installation for npm Projects
207
+
208
+ You can also install the SDK via npm:
209
+
210
+ ```bash
211
+ npm install obi-sdk
212
+ ```
213
+
214
+ And import it in your application:
215
+
216
+ ```javascript
217
+ import ObiSDK from "obi-sdk"
218
+
219
+ // Initialize the SDK
220
+ const sdk = new ObiSDK({
221
+ apiKey: "YOUR_API_KEY",
222
+ })
223
+ ```
224
+
225
+ ## Building the SDK
226
+
227
+ To build the SDK locally:
228
+
229
+ ```bash
230
+ # Install dependencies
231
+ npm install
232
+
233
+ # Build all formats (UMD, ES, standalone IIFE, and loader)
234
+ npm run build:all
235
+ ```
236
+
237
+ This generates the following in the `dist` directory:
238
+
239
+ - `obi-sdk.umd.js` - UMD format for CommonJS/AMD environments
240
+ - `obi-sdk.es.js` - ES Module format for modern bundlers
241
+ - `obi-sdk.standalone.iife.js` - IIFE format with all dependencies bundled
242
+ - `obi-loader.iife.js` - Standalone loader script
243
+
200
244
  ## Coming Soon
201
245
 
202
246
  The following features are under development and will be available in future releases:
@@ -218,14 +262,14 @@ The inline loader script provides a self-contained way to add the Obi Widget to
218
262
  5. It creates the `<obi-widget>` element and positions it on the page
219
263
  6. It configures the widget with the provided options
220
264
 
221
- Before using in production, you should update the CDN URL to point to where you host the SDK files:
265
+ Before using in production, you can use unpkg.com as a CDN which automatically serves files from npm packages:
222
266
 
223
267
  ```js
224
- // In the loader script, change this line:
225
- var b = "https://cdn.iamobi.ai/obi-sdk"
268
+ // The loader automatically uses unpkg.com to load the package:
269
+ var b = "https://unpkg.com/obi-sdk"
226
270
 
227
- // To point to your CDN:
228
- var b = "https://your-cdn.iamobi.ai/path/to/obi-sdk"
271
+ // Which resolves to URLs like:
272
+ // https://unpkg.com/obi-sdk@0.1.1/dist/obi-sdk.standalone.iife.js
229
273
  ```
230
274
 
231
275
  ### Widget Positioning
@@ -236,7 +280,6 @@ The widget can be positioned in one of four corners of the page:
236
280
  window.obiWidgetConfig = {
237
281
  position: "bottom-right", // Options: "bottom-right", "bottom-left", "top-right", "top-left"
238
282
  apiKey: "YOUR_API_KEY",
239
- apiBaseUrl: "https://www.iamobi.ai/api/",
240
283
  }
241
284
  ```
242
285
 
@@ -248,7 +291,6 @@ The widget supports light and dark themes:
248
291
  window.obiWidgetConfig = {
249
292
  theme: "light", // Options: "light", "dark"
250
293
  apiKey: "YOUR_API_KEY",
251
- apiBaseUrl: "https://www.iamobi.ai/api/",
252
294
  }
253
295
  ```
254
296
 
@@ -261,7 +303,6 @@ window.obiWidgetConfig = {
261
303
  sdkVersion: "latest", // Use "latest" or a specific version number
262
304
  noCacheSDK: false, // Set to true to bypass cache
263
305
  apiKey: "YOUR_API_KEY",
264
- apiBaseUrl: "https://www.iamobi.ai/api/",
265
306
  }
266
307
  ```
267
308