@looplay/sdk 0.8.7 → 0.8.9

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.
@@ -682,6 +682,43 @@ var ApiClient = class {
682
682
  }
683
683
  };
684
684
 
685
+ // src/constants.ts
686
+ var DEFAULT_API_URL = "https://miniapp-api.looplay.gg";
687
+ function resolveEnvParentOrigin() {
688
+ try {
689
+ if (typeof import.meta !== "undefined" && import.meta.env?.VITE_PARENT_ORIGIN) {
690
+ const val = import.meta.env.VITE_PARENT_ORIGIN;
691
+ if (typeof val === "string" && val.trim().length > 0) return val.trim();
692
+ }
693
+ } catch {
694
+ }
695
+ try {
696
+ if (typeof process !== "undefined" && process.env?.VITE_PARENT_ORIGIN) {
697
+ const val = process.env.VITE_PARENT_ORIGIN;
698
+ if (typeof val === "string" && val.trim().length > 0) return val.trim();
699
+ }
700
+ } catch {
701
+ }
702
+ return void 0;
703
+ }
704
+ function resolveEnvApiUrl() {
705
+ try {
706
+ if (typeof import.meta !== "undefined" && import.meta.env) {
707
+ const val = import.meta.env.VITE_API_URL ?? import.meta.env.VITE_LOOPLAY_API_URL;
708
+ if (typeof val === "string" && val.trim().length > 0) return val.trim();
709
+ }
710
+ } catch {
711
+ }
712
+ try {
713
+ if (typeof process !== "undefined" && process.env) {
714
+ const val = process.env.VITE_API_URL ?? process.env.VITE_LOOPLAY_API_URL;
715
+ if (typeof val === "string" && val.trim().length > 0) return val.trim();
716
+ }
717
+ } catch {
718
+ }
719
+ return void 0;
720
+ }
721
+
685
722
  // src/looplay-sdk.ts
686
723
  var LooplaySDK = class {
687
724
  auth;
@@ -689,7 +726,12 @@ var LooplaySDK = class {
689
726
  gameId;
690
727
  initialized = false;
691
728
  constructor(options = {}) {
692
- const { auth, baseUrl, fetch, defaultHeaders } = options;
729
+ const {
730
+ auth,
731
+ baseUrl = resolveEnvApiUrl() ?? DEFAULT_API_URL,
732
+ fetch,
733
+ defaultHeaders
734
+ } = options;
693
735
  if (auth) {
694
736
  this.auth = new LooplayAuth(auth);
695
737
  }
@@ -774,9 +816,23 @@ var LooplaySDK = class {
774
816
  if (!this.api) throw new MissingBaseUrlError();
775
817
  await this.api.listGameAssets(params.gameId);
776
818
  }
819
+ /**
820
+ * Best-effort self-report — must never block init(). A missing/misconfigured
821
+ * whitelist (a separate setup step from SDK integration) makes the backend
822
+ * reject this call; if that rejection propagated, the whole init() would
823
+ * reject too and every other SDK method would throw NotInitializedError,
824
+ * breaking a game over what's really just an unfinished onboarding step.
825
+ */
777
826
  async detectIntegration(params) {
778
827
  if (!this.api) return;
779
- await this.api.detectIntegration(params.gameId);
828
+ try {
829
+ await this.api.detectIntegration(params.gameId);
830
+ } catch (error) {
831
+ console.warn(
832
+ "[LooplaySDK] detectIntegration failed (SDK will still initialize) \u2014 check that this game has a whitelisted origin/domain configured:",
833
+ error
834
+ );
835
+ }
780
836
  }
781
837
  };
782
838
 
@@ -948,7 +1004,7 @@ var LooplayIframeAuth = class {
948
1004
  return this.state.jwt;
949
1005
  }
950
1006
  getApiUrl() {
951
- return this.state.apiUrl ?? this.options.apiUrl ?? null;
1007
+ return this.state.apiUrl ?? this.options.apiUrl ?? resolveEnvApiUrl() ?? DEFAULT_API_URL;
952
1008
  }
953
1009
  /** Resolves to the configured `appId`, falling back to whatever the parent pushed as `gameId`. */
954
1010
  getGameId() {
@@ -1150,12 +1206,13 @@ var LooplayIframeAuth = class {
1150
1206
  }
1151
1207
  /**
1152
1208
  * Idempotent; auto-emits GAME_STARTED exactly once per distinct auth
1153
- * session (bearer or anonymous), and self-reports SDK integration the
1154
- * same way the standalone `LooplaySDK.init()` does without this, games
1155
- * embedded via iframe never trigger the active detect call, only the
1156
- * passive heartbeat that tracking calls leave behind (which doesn't fire
1157
- * at all while the game is still IN_REVIEW/not LIVE, since tracking is
1158
- * blocked until then).
1209
+ * session (bearer or anonymous). SDK integration self-reporting for the
1210
+ * iframe path lives in `detectIntegrationOnce()` (fired from `setState()`
1211
+ * on every auth/config change, not gated behind lifecycle tracking being
1212
+ * enabled at all) without it, games embedded via iframe would never
1213
+ * trigger the active detect call, only the passive heartbeat that
1214
+ * tracking calls leave behind (which doesn't fire while the game is still
1215
+ * IN_REVIEW/not LIVE, since tracking is blocked until then).
1159
1216
  */
1160
1217
  initLifecycleTracking() {
1161
1218
  if (this.lifecycleTrackingInitialized || typeof window === "undefined")
@@ -1166,11 +1223,6 @@ var LooplayIframeAuth = class {
1166
1223
  const key = this.getTrackingKey();
1167
1224
  if (!key || this.lastGameStartedKey === key) return;
1168
1225
  this.lastGameStartedKey = key;
1169
- const gameId = this.getGameId();
1170
- if (gameId) {
1171
- this.debug("auto detectIntegration dispatch", { gameId });
1172
- void this.getClient().detectIntegration(gameId).catch(() => void 0);
1173
- }
1174
1226
  this.debug("auto GAME_STARTED dispatch");
1175
1227
  void this.emitGameEvent("GAME_STARTED");
1176
1228
  });
@@ -1328,6 +1380,8 @@ ${gameId}`;
1328
1380
  }
1329
1381
  resolveParentOrigin() {
1330
1382
  if (this.options.parentOrigin) return this.options.parentOrigin;
1383
+ const envOrigin = resolveEnvParentOrigin();
1384
+ if (envOrigin) return envOrigin;
1331
1385
  if (typeof document === "undefined" || !document.referrer) return null;
1332
1386
  try {
1333
1387
  return new URL(document.referrer).origin;
@@ -1369,6 +1423,6 @@ ${gameId}`;
1369
1423
  }
1370
1424
  };
1371
1425
 
1372
- export { ApiClient, BrowserLocalStorageAuthStorage, HttpClient, HttpError, LOOPLAY_SDK_VERSION, LooplayAuth, LooplayIframeAuth, LooplaySDK, LooplaySDKError, MemoryAuthStorage, MissingAuthError, MissingBaseUrlError, NotAuthenticatedError, NotInitializedError, ServiceClient, TelegramAuthProvider };
1426
+ export { ApiClient, BrowserLocalStorageAuthStorage, DEFAULT_API_URL, HttpClient, HttpError, LOOPLAY_SDK_VERSION, LooplayAuth, LooplayIframeAuth, LooplaySDK, LooplaySDKError, MemoryAuthStorage, MissingAuthError, MissingBaseUrlError, NotAuthenticatedError, NotInitializedError, ServiceClient, TelegramAuthProvider, resolveEnvApiUrl, resolveEnvParentOrigin };
1373
1427
  //# sourceMappingURL=looplay-sdk.esm.js.map
1374
1428
  //# sourceMappingURL=looplay-sdk.esm.js.map