@sylphx/sdk 0.3.7 → 0.5.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.
@@ -32,7 +32,7 @@ var ETAG_CACHE_TTL_MS = 5 * 60 * 1e3;
32
32
 
33
33
  // src/key-validation.ts
34
34
  var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
35
- var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
35
+ var APP_ID_PATTERN = /^(app|pk)_(dev|stg|prod|prev)_[a-z0-9_-]+$/;
36
36
  var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
37
37
  var ENV_PREFIX_MAP = {
38
38
  dev: "development",
@@ -61,9 +61,8 @@ To fix permanently:
61
61
  The SDK will automatically sanitize the key, but fixing the source is recommended.`;
62
62
  }
63
63
  function createInvalidKeyError(keyType, key, envVarName) {
64
- const prefix = keyType === "appId" ? "app" : "sk";
65
64
  const maskedKey = key.length > 20 ? `${key.slice(0, 20)}...` : key;
66
- const formatHint = `${prefix}_(dev|stg|prod)_[identifier]`;
65
+ const formatHint = keyType === "appId" ? "pk_(dev|stg|prod)_{ref}_{hex} or app_(dev|stg|prod)_[id]" : "sk_(dev|stg|prod)_{ref}_{hex}";
67
66
  const keyTypeName = keyType === "appId" ? "App ID" : "Secret Key";
68
67
  return `[Sylphx] Invalid ${keyTypeName} format.
69
68
 
@@ -75,12 +74,12 @@ You can find your keys in the Sylphx Console \u2192 API Keys.
75
74
 
76
75
  Common issues:
77
76
  \u2022 Key has uppercase characters (must be lowercase)
78
- \u2022 Key has wrong prefix (App ID: app_, Secret Key: sk_)
77
+ \u2022 Key has wrong prefix (App ID: pk_ or app_, Secret Key: sk_)
79
78
  \u2022 Key has invalid environment (must be dev, stg, or prod)
80
79
  \u2022 Key was copied with extra whitespace`;
81
80
  }
82
81
  function extractEnvironment(key) {
83
- const match = key.match(/^(?:app|sk)_(dev|stg|prod)_/);
82
+ const match = key.match(/^(?:app|pk|sk)_(dev|stg|prod|prev)_/);
84
83
  if (!match) return void 0;
85
84
  return ENV_PREFIX_MAP[match[1]];
86
85
  }
@@ -450,16 +449,32 @@ async function refreshTokens(refreshToken, ctx) {
450
449
  }
451
450
  }
452
451
  function createSylphxMiddleware(userConfig = {}) {
453
- const rawSecretKey = userConfig.secretKey || process.env.SYLPHX_SECRET_KEY;
454
- if (!rawSecretKey) {
455
- throw new Error(
456
- "[Sylphx] Secret key is required.\nEither pass secretKey in config or set SYLPHX_SECRET_KEY env var.\nGet your key from Sylphx Console \u2192 API Keys."
457
- );
458
- }
459
- const secretKey = validateAndSanitizeSecretKey(rawSecretKey);
460
- const platformUrl = (userConfig.platformUrl || `https://${DEFAULT_SDK_API_HOST}`).trim();
461
- const namespace = getCookieNamespace(secretKey);
462
- const cookieNames = getCookieNames(namespace);
452
+ let secretKey = null;
453
+ function resolveSecretKey() {
454
+ if (secretKey) return secretKey;
455
+ const rawSecretKey = userConfig.secretKey || process.env.SYLPHX_SECRET_KEY;
456
+ if (!rawSecretKey) {
457
+ throw new Error(
458
+ "[Sylphx] Secret key is required.\nEither pass secretKey in config or set SYLPHX_SECRET_KEY env var.\nGet your key from Sylphx Console \u2192 API Keys."
459
+ );
460
+ }
461
+ secretKey = validateAndSanitizeSecretKey(rawSecretKey);
462
+ return secretKey;
463
+ }
464
+ let platformUrl;
465
+ let namespace;
466
+ let cookieNames;
467
+ function resolveConfig() {
468
+ const key = resolveSecretKey();
469
+ platformUrl = (() => {
470
+ if (userConfig.platformUrl) return userConfig.platformUrl.trim();
471
+ const parts = key.split("_");
472
+ const ref = parts.length === 4 ? parts[2] : null;
473
+ return ref ? `https://${ref}.${DEFAULT_SDK_API_HOST}` : `https://${DEFAULT_SDK_API_HOST}`;
474
+ })();
475
+ namespace = getCookieNamespace(key);
476
+ cookieNames = getCookieNames(namespace);
477
+ }
463
478
  const config = {
464
479
  publicRoutes: userConfig.publicRoutes ?? ["/"],
465
480
  ignoredRoutes: userConfig.ignoredRoutes ?? [],
@@ -482,14 +497,23 @@ function createSylphxMiddleware(userConfig = {}) {
482
497
  }
483
498
  };
484
499
  const ctx = {
485
- secretKey,
486
- platformUrl,
487
- namespace,
488
- cookieNames,
500
+ get secretKey() {
501
+ return secretKey;
502
+ },
503
+ get platformUrl() {
504
+ return platformUrl;
505
+ },
506
+ get namespace() {
507
+ return namespace;
508
+ },
509
+ get cookieNames() {
510
+ return cookieNames;
511
+ },
489
512
  config,
490
513
  log
491
514
  };
492
515
  return async function middleware(request) {
516
+ resolveConfig();
493
517
  const { pathname } = request.nextUrl;
494
518
  log(`${request.method} ${pathname}`);
495
519
  if (matchesAny(pathname, config.ignoredRoutes)) {
@@ -1815,7 +1839,7 @@ function configureServer(config) {
1815
1839
  const secretKey = validateAndSanitizeSecretKey(config.secretKey);
1816
1840
  serverConfig = {
1817
1841
  secretKey,
1818
- platformUrl: `https://${DEFAULT_SDK_API_HOST}`.trim()
1842
+ platformUrl: config.platformUrl?.trim() || `https://${DEFAULT_SDK_API_HOST}`.trim()
1819
1843
  };
1820
1844
  }
1821
1845
  function getConfig() {