opencloud-platform-sdk 1.2.0 → 1.3.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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * opencloud-platform-sdk v1.2.0
2
+ * opencloud-platform-sdk v1.3.0
3
3
  * Official SDK for OpenCloud - AI App Marketplace
4
4
  *
5
5
  * Features:
@@ -7,6 +7,7 @@
7
7
  * - Works in bolt.diy/WebContainers preview
8
8
  * - Auto-detects API keys from bolt.diy builder
9
9
  * - Seamless transition to production with credits
10
+ * - Safe for Vite/Rollup production builds (no side effects at import time)
10
11
  */
11
12
  interface OpenCloudConfig {
12
13
  appId: string;
@@ -52,6 +53,7 @@ declare class OpenCloudSDK {
52
53
  init(options: OpenCloudConfig): void;
53
54
  /**
54
55
  * Set API key for preview mode
56
+ * Safe to call during build - will be a no-op if not in browser
55
57
  */
56
58
  setPreviewApiKey(key: string, provider?: 'openrouter' | 'anthropic' | 'openai'): void;
57
59
  /**
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * opencloud-platform-sdk v1.2.0
2
+ * opencloud-platform-sdk v1.3.0
3
3
  * Official SDK for OpenCloud - AI App Marketplace
4
4
  *
5
5
  * Features:
@@ -7,6 +7,7 @@
7
7
  * - Works in bolt.diy/WebContainers preview
8
8
  * - Auto-detects API keys from bolt.diy builder
9
9
  * - Seamless transition to production with credits
10
+ * - Safe for Vite/Rollup production builds (no side effects at import time)
10
11
  */
11
12
  interface OpenCloudConfig {
12
13
  appId: string;
@@ -52,6 +53,7 @@ declare class OpenCloudSDK {
52
53
  init(options: OpenCloudConfig): void;
53
54
  /**
54
55
  * Set API key for preview mode
56
+ * Safe to call during build - will be a no-op if not in browser
55
57
  */
56
58
  setPreviewApiKey(key: string, provider?: 'openrouter' | 'anthropic' | 'openai'): void;
57
59
  /**
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  opencloud: () => opencloud
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
+ var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
27
28
  var STORAGE_KEYS = {
28
29
  OPENROUTER_KEY: "openrouter_api_key",
29
30
  ANTHROPIC_KEY: "anthropic_api_key",
@@ -31,7 +32,7 @@ var STORAGE_KEYS = {
31
32
  PREVIEW_PROVIDER: "opencloud_preview_provider"
32
33
  };
33
34
  function getCookie(name) {
34
- if (typeof document === "undefined") return null;
35
+ if (!isBrowser) return null;
35
36
  const value = `; ${document.cookie}`;
36
37
  const parts = value.split(`; ${name}=`);
37
38
  if (parts.length === 2) {
@@ -60,10 +61,10 @@ var OpenCloudSDK = class {
60
61
  this._isPreviewMode = false;
61
62
  this.config = {
62
63
  appId: "",
63
- apiUrl: typeof window !== "undefined" ? window.location.origin : "http://localhost:3000",
64
+ apiUrl: isBrowser ? window.location.origin : "http://localhost:3000",
64
65
  previewApiKey: void 0
65
66
  };
66
- if (typeof window !== "undefined") {
67
+ if (isBrowser) {
67
68
  this._isPreviewMode = this.detectPreviewMode();
68
69
  }
69
70
  }
@@ -71,7 +72,7 @@ var OpenCloudSDK = class {
71
72
  * Detect if running in preview/development mode
72
73
  */
73
74
  detectPreviewMode() {
74
- if (typeof window === "undefined") return false;
75
+ if (!isBrowser) return false;
75
76
  const hostname = window.location.hostname;
76
77
  return hostname.includes("webcontainer") || hostname.includes("localhost") || hostname.includes("127.0.0.1") || hostname.includes(".local") || hostname.includes("stackblitz") || hostname.includes("codesandbox");
77
78
  }
@@ -101,9 +102,10 @@ var OpenCloudSDK = class {
101
102
  }
102
103
  /**
103
104
  * Set API key for preview mode
105
+ * Safe to call during build - will be a no-op if not in browser
104
106
  */
105
107
  setPreviewApiKey(key, provider = "openrouter") {
106
- if (typeof window === "undefined") return;
108
+ if (!isBrowser) return;
107
109
  const storageKey = provider === "openrouter" ? STORAGE_KEYS.OPENROUTER_KEY : provider === "anthropic" ? STORAGE_KEYS.ANTHROPIC_KEY : STORAGE_KEYS.OPENAI_KEY;
108
110
  localStorage.setItem(storageKey, key);
109
111
  localStorage.setItem(STORAGE_KEYS.PREVIEW_PROVIDER, provider);
@@ -114,7 +116,7 @@ var OpenCloudSDK = class {
114
116
  * Checks in order: config, localStorage, bolt.diy cookies
115
117
  */
116
118
  getPreviewApiKey() {
117
- if (typeof window === "undefined") return null;
119
+ if (!isBrowser) return null;
118
120
  if (this.config.previewApiKey) {
119
121
  return { key: this.config.previewApiKey, provider: "openrouter" };
120
122
  }
@@ -346,7 +348,7 @@ var OpenCloudSDK = class {
346
348
  * Request user to purchase more tokens
347
349
  */
348
350
  async requestTopUp() {
349
- if (typeof window === "undefined") return;
351
+ if (!isBrowser) return;
350
352
  if (this._isPreviewMode) {
351
353
  console.warn("[OpenCloudSDK] Top-up not available in preview mode");
352
354
  return;
@@ -364,7 +366,7 @@ var OpenCloudSDK = class {
364
366
  * Start sending heartbeats
365
367
  */
366
368
  startHeartbeat() {
367
- if (typeof window === "undefined") return;
369
+ if (!isBrowser) return;
368
370
  this.sendHeartbeat();
369
371
  this.heartbeatInterval = setInterval(() => this.sendHeartbeat(), 3e4);
370
372
  }
@@ -372,7 +374,7 @@ var OpenCloudSDK = class {
372
374
  * Send heartbeat to platform
373
375
  */
374
376
  sendHeartbeat() {
375
- if (!this.config.appId || typeof window === "undefined") return;
377
+ if (!this.config.appId || !isBrowser) return;
376
378
  const data = JSON.stringify({
377
379
  appId: this.config.appId,
378
380
  timestamp: Date.now(),
@@ -387,7 +389,7 @@ var OpenCloudSDK = class {
387
389
  * Get user session token
388
390
  */
389
391
  async getUserSession() {
390
- if (typeof window === "undefined") {
392
+ if (!isBrowser) {
391
393
  throw new Error("getUserSession can only be called in browser");
392
394
  }
393
395
  return new Promise((resolve, reject) => {
@@ -431,7 +433,7 @@ var OpenCloudSDK = class {
431
433
  }
432
434
  };
433
435
  var opencloud = new OpenCloudSDK();
434
- if (typeof window !== "undefined") {
436
+ if (isBrowser) {
435
437
  window.OpenCloudSDK = opencloud;
436
438
  window.PlatformSDK = opencloud;
437
439
  }
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/index.ts
2
+ var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
2
3
  var STORAGE_KEYS = {
3
4
  OPENROUTER_KEY: "openrouter_api_key",
4
5
  ANTHROPIC_KEY: "anthropic_api_key",
@@ -6,7 +7,7 @@ var STORAGE_KEYS = {
6
7
  PREVIEW_PROVIDER: "opencloud_preview_provider"
7
8
  };
8
9
  function getCookie(name) {
9
- if (typeof document === "undefined") return null;
10
+ if (!isBrowser) return null;
10
11
  const value = `; ${document.cookie}`;
11
12
  const parts = value.split(`; ${name}=`);
12
13
  if (parts.length === 2) {
@@ -35,10 +36,10 @@ var OpenCloudSDK = class {
35
36
  this._isPreviewMode = false;
36
37
  this.config = {
37
38
  appId: "",
38
- apiUrl: typeof window !== "undefined" ? window.location.origin : "http://localhost:3000",
39
+ apiUrl: isBrowser ? window.location.origin : "http://localhost:3000",
39
40
  previewApiKey: void 0
40
41
  };
41
- if (typeof window !== "undefined") {
42
+ if (isBrowser) {
42
43
  this._isPreviewMode = this.detectPreviewMode();
43
44
  }
44
45
  }
@@ -46,7 +47,7 @@ var OpenCloudSDK = class {
46
47
  * Detect if running in preview/development mode
47
48
  */
48
49
  detectPreviewMode() {
49
- if (typeof window === "undefined") return false;
50
+ if (!isBrowser) return false;
50
51
  const hostname = window.location.hostname;
51
52
  return hostname.includes("webcontainer") || hostname.includes("localhost") || hostname.includes("127.0.0.1") || hostname.includes(".local") || hostname.includes("stackblitz") || hostname.includes("codesandbox");
52
53
  }
@@ -76,9 +77,10 @@ var OpenCloudSDK = class {
76
77
  }
77
78
  /**
78
79
  * Set API key for preview mode
80
+ * Safe to call during build - will be a no-op if not in browser
79
81
  */
80
82
  setPreviewApiKey(key, provider = "openrouter") {
81
- if (typeof window === "undefined") return;
83
+ if (!isBrowser) return;
82
84
  const storageKey = provider === "openrouter" ? STORAGE_KEYS.OPENROUTER_KEY : provider === "anthropic" ? STORAGE_KEYS.ANTHROPIC_KEY : STORAGE_KEYS.OPENAI_KEY;
83
85
  localStorage.setItem(storageKey, key);
84
86
  localStorage.setItem(STORAGE_KEYS.PREVIEW_PROVIDER, provider);
@@ -89,7 +91,7 @@ var OpenCloudSDK = class {
89
91
  * Checks in order: config, localStorage, bolt.diy cookies
90
92
  */
91
93
  getPreviewApiKey() {
92
- if (typeof window === "undefined") return null;
94
+ if (!isBrowser) return null;
93
95
  if (this.config.previewApiKey) {
94
96
  return { key: this.config.previewApiKey, provider: "openrouter" };
95
97
  }
@@ -321,7 +323,7 @@ var OpenCloudSDK = class {
321
323
  * Request user to purchase more tokens
322
324
  */
323
325
  async requestTopUp() {
324
- if (typeof window === "undefined") return;
326
+ if (!isBrowser) return;
325
327
  if (this._isPreviewMode) {
326
328
  console.warn("[OpenCloudSDK] Top-up not available in preview mode");
327
329
  return;
@@ -339,7 +341,7 @@ var OpenCloudSDK = class {
339
341
  * Start sending heartbeats
340
342
  */
341
343
  startHeartbeat() {
342
- if (typeof window === "undefined") return;
344
+ if (!isBrowser) return;
343
345
  this.sendHeartbeat();
344
346
  this.heartbeatInterval = setInterval(() => this.sendHeartbeat(), 3e4);
345
347
  }
@@ -347,7 +349,7 @@ var OpenCloudSDK = class {
347
349
  * Send heartbeat to platform
348
350
  */
349
351
  sendHeartbeat() {
350
- if (!this.config.appId || typeof window === "undefined") return;
352
+ if (!this.config.appId || !isBrowser) return;
351
353
  const data = JSON.stringify({
352
354
  appId: this.config.appId,
353
355
  timestamp: Date.now(),
@@ -362,7 +364,7 @@ var OpenCloudSDK = class {
362
364
  * Get user session token
363
365
  */
364
366
  async getUserSession() {
365
- if (typeof window === "undefined") {
367
+ if (!isBrowser) {
366
368
  throw new Error("getUserSession can only be called in browser");
367
369
  }
368
370
  return new Promise((resolve, reject) => {
@@ -406,7 +408,7 @@ var OpenCloudSDK = class {
406
408
  }
407
409
  };
408
410
  var opencloud = new OpenCloudSDK();
409
- if (typeof window !== "undefined") {
411
+ if (isBrowser) {
410
412
  window.OpenCloudSDK = opencloud;
411
413
  window.PlatformSDK = opencloud;
412
414
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencloud-platform-sdk",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Official SDK for OpenCloud - AI App Marketplace",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",