@invonetwork/web-sdk 3.27.0 → 3.29.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/server.cjs CHANGED
@@ -1593,7 +1593,7 @@ function toPhoneChangeRequest(raw) {
1593
1593
  }
1594
1594
 
1595
1595
  // src/version.ts
1596
- var SDK_VERSION = "3.27.0";
1596
+ var SDK_VERSION = "3.29.0";
1597
1597
 
1598
1598
  // src/payouts.ts
1599
1599
  var AMOUNT = /^\d+(\.\d{1,2})?$/;
@@ -1955,6 +1955,74 @@ async function hmacHexSubtle(secret, message) {
1955
1955
  return hex;
1956
1956
  }
1957
1957
 
1958
+ // src/environment.ts
1959
+ var PRODUCTION_BASE_URL = "https://invo.network";
1960
+ var SANDBOX_BASE_URL = "https://sandbox.invo.network/sandbox";
1961
+ var DEMO_KEY_PREFIX = "ivsdk_demo_";
1962
+ var KEY_ENVIRONMENT_PATH = "/api/sdk/key-environment";
1963
+ var EnvironmentUnknownError = class extends Error {
1964
+ constructor(message) {
1965
+ super(message);
1966
+ this.name = "EnvironmentUnknownError";
1967
+ }
1968
+ };
1969
+ function isDemoKey(gameSecret) {
1970
+ return typeof gameSecret === "string" && gameSecret.startsWith(DEMO_KEY_PREFIX);
1971
+ }
1972
+ var cache = /* @__PURE__ */ new Map();
1973
+ async function ask(baseUrl, gameSecret, fetchImpl) {
1974
+ let res;
1975
+ try {
1976
+ res = await fetchImpl(baseUrl + KEY_ENVIRONMENT_PATH, {
1977
+ method: "GET",
1978
+ headers: { "X-Game-Secret-Key": gameSecret, Accept: "application/json" },
1979
+ redirect: "error"
1980
+ });
1981
+ } catch {
1982
+ throw new EnvironmentUnknownError("Could not reach INVO to check the key.");
1983
+ }
1984
+ if (res.status === 401 || res.status === 403 || res.status === 404) return null;
1985
+ if (!res.ok) throw new EnvironmentUnknownError(`INVO answered ${res.status} while checking the key.`);
1986
+ const body = await res.json().catch(() => ({}));
1987
+ return body.environment === "production" || body.environment === "sandbox" ? body.environment : null;
1988
+ }
1989
+ function resolveEnvironment(gameSecret, opts = {}) {
1990
+ if (!gameSecret) return Promise.reject(new Error("A game secret is required."));
1991
+ if (isDemoKey(gameSecret)) return Promise.resolve("sandbox");
1992
+ const hit = cache.get(gameSecret);
1993
+ if (hit) return hit;
1994
+ const fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1995
+ const p = (async () => {
1996
+ let prodError = null;
1997
+ let env = null;
1998
+ try {
1999
+ env = await ask(PRODUCTION_BASE_URL, gameSecret, fetchImpl);
2000
+ } catch (err) {
2001
+ prodError = err;
2002
+ }
2003
+ if (!env) {
2004
+ try {
2005
+ env = await ask(SANDBOX_BASE_URL, gameSecret, fetchImpl);
2006
+ } catch (err) {
2007
+ throw prodError ?? err;
2008
+ }
2009
+ }
2010
+ if (!env && prodError) throw prodError;
2011
+ if (!env) {
2012
+ throw new EnvironmentUnknownError(
2013
+ "Neither INVO production nor the INVO sandbox recognises this key. Check INVO_GAME_SECRET in your secrets."
2014
+ );
2015
+ }
2016
+ return env;
2017
+ })();
2018
+ cache.set(gameSecret, p);
2019
+ p.catch(() => cache.delete(gameSecret));
2020
+ return p;
2021
+ }
2022
+ async function baseUrlForKey(gameSecret, opts = {}) {
2023
+ return await resolveEnvironment(gameSecret, opts) === "production" ? PRODUCTION_BASE_URL : SANDBOX_BASE_URL;
2024
+ }
2025
+
1958
2026
  // src/server.ts
1959
2027
  var DEFAULT_UA = `invonetwork-web-sdk/${SDK_VERSION} (+https://invo.network)`;
1960
2028
  var MAX_USD_AMOUNT = 999999.99;
@@ -5269,16 +5337,23 @@ var InvoServer = class {
5269
5337
  };
5270
5338
 
5271
5339
  exports.CardsServer = CardsServer;
5340
+ exports.DEMO_KEY_PREFIX = DEMO_KEY_PREFIX;
5341
+ exports.EnvironmentUnknownError = EnvironmentUnknownError;
5272
5342
  exports.InvoError = InvoError;
5273
5343
  exports.InvoServer = InvoServer;
5344
+ exports.PRODUCTION_BASE_URL = PRODUCTION_BASE_URL;
5274
5345
  exports.PayoutsServer = PayoutsServer;
5275
5346
  exports.PlatformCommerceServer = PlatformCommerceServer;
5347
+ exports.SANDBOX_BASE_URL = SANDBOX_BASE_URL;
5276
5348
  exports.SDK_VERSION = SDK_VERSION;
5277
5349
  exports.SandboxServer = SandboxServer;
5278
5350
  exports.SandboxSubscriptionsServer = SandboxSubscriptionsServer;
5279
5351
  exports.SubscriptionsServer = SubscriptionsServer;
5352
+ exports.baseUrlForKey = baseUrlForKey;
5280
5353
  exports.createWebhookHandler = createWebhookHandler;
5354
+ exports.isDemoKey = isDemoKey;
5281
5355
  exports.isRefundPendingApproval = isRefundPendingApproval;
5356
+ exports.resolveEnvironment = resolveEnvironment;
5282
5357
  exports.verifyWebhook = verifyWebhook;
5283
5358
  exports.verifyWebhookAsync = verifyWebhookAsync;
5284
5359
  //# sourceMappingURL=server.cjs.map