@sylphx/sdk 0.4.0 → 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.
@@ -86,7 +86,7 @@ var ETAG_CACHE_TTL_MS = 5 * 60 * 1e3;
86
86
 
87
87
  // src/key-validation.ts
88
88
  var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
89
- var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
89
+ var APP_ID_PATTERN = /^(app|pk)_(dev|stg|prod|prev)_[a-z0-9_-]+$/;
90
90
  var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
91
91
  var ENV_PREFIX_MAP = {
92
92
  dev: "development",
@@ -115,9 +115,8 @@ To fix permanently:
115
115
  The SDK will automatically sanitize the key, but fixing the source is recommended.`;
116
116
  }
117
117
  function createInvalidKeyError(keyType, key, envVarName) {
118
- const prefix = keyType === "appId" ? "app" : "sk";
119
118
  const maskedKey = key.length > 20 ? `${key.slice(0, 20)}...` : key;
120
- const formatHint = `${prefix}_(dev|stg|prod)_[identifier]`;
119
+ const formatHint = keyType === "appId" ? "pk_(dev|stg|prod)_{ref}_{hex} or app_(dev|stg|prod)_[id]" : "sk_(dev|stg|prod)_{ref}_{hex}";
121
120
  const keyTypeName = keyType === "appId" ? "App ID" : "Secret Key";
122
121
  return `[Sylphx] Invalid ${keyTypeName} format.
123
122
 
@@ -129,12 +128,12 @@ You can find your keys in the Sylphx Console \u2192 API Keys.
129
128
 
130
129
  Common issues:
131
130
  \u2022 Key has uppercase characters (must be lowercase)
132
- \u2022 Key has wrong prefix (App ID: app_, Secret Key: sk_)
131
+ \u2022 Key has wrong prefix (App ID: pk_ or app_, Secret Key: sk_)
133
132
  \u2022 Key has invalid environment (must be dev, stg, or prod)
134
133
  \u2022 Key was copied with extra whitespace`;
135
134
  }
136
135
  function extractEnvironment(key) {
137
- const match = key.match(/^(?:app|sk)_(dev|stg|prod)_/);
136
+ const match = key.match(/^(?:app|pk|sk)_(dev|stg|prod|prev)_/);
138
137
  if (!match) return void 0;
139
138
  return ENV_PREFIX_MAP[match[1]];
140
139
  }
@@ -504,21 +503,32 @@ async function refreshTokens(refreshToken, ctx) {
504
503
  }
505
504
  }
506
505
  function createSylphxMiddleware(userConfig = {}) {
507
- const rawSecretKey = userConfig.secretKey || process.env.SYLPHX_SECRET_KEY;
508
- if (!rawSecretKey) {
509
- throw new Error(
510
- "[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."
511
- );
512
- }
513
- const secretKey = validateAndSanitizeSecretKey(rawSecretKey);
514
- const platformUrl = (() => {
515
- if (userConfig.platformUrl) return userConfig.platformUrl.trim();
516
- const parts = secretKey.split("_");
517
- const ref = parts.length === 4 ? parts[2] : null;
518
- return ref ? `https://${ref}.${DEFAULT_SDK_API_HOST}` : `https://${DEFAULT_SDK_API_HOST}`;
519
- })();
520
- const namespace = getCookieNamespace(secretKey);
521
- const cookieNames = getCookieNames(namespace);
506
+ let secretKey = null;
507
+ function resolveSecretKey() {
508
+ if (secretKey) return secretKey;
509
+ const rawSecretKey = userConfig.secretKey || process.env.SYLPHX_SECRET_KEY;
510
+ if (!rawSecretKey) {
511
+ throw new Error(
512
+ "[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."
513
+ );
514
+ }
515
+ secretKey = validateAndSanitizeSecretKey(rawSecretKey);
516
+ return secretKey;
517
+ }
518
+ let platformUrl;
519
+ let namespace;
520
+ let cookieNames;
521
+ function resolveConfig() {
522
+ const key = resolveSecretKey();
523
+ platformUrl = (() => {
524
+ if (userConfig.platformUrl) return userConfig.platformUrl.trim();
525
+ const parts = key.split("_");
526
+ const ref = parts.length === 4 ? parts[2] : null;
527
+ return ref ? `https://${ref}.${DEFAULT_SDK_API_HOST}` : `https://${DEFAULT_SDK_API_HOST}`;
528
+ })();
529
+ namespace = getCookieNamespace(key);
530
+ cookieNames = getCookieNames(namespace);
531
+ }
522
532
  const config = {
523
533
  publicRoutes: userConfig.publicRoutes ?? ["/"],
524
534
  ignoredRoutes: userConfig.ignoredRoutes ?? [],
@@ -541,14 +551,23 @@ function createSylphxMiddleware(userConfig = {}) {
541
551
  }
542
552
  };
543
553
  const ctx = {
544
- secretKey,
545
- platformUrl,
546
- namespace,
547
- cookieNames,
554
+ get secretKey() {
555
+ return secretKey;
556
+ },
557
+ get platformUrl() {
558
+ return platformUrl;
559
+ },
560
+ get namespace() {
561
+ return namespace;
562
+ },
563
+ get cookieNames() {
564
+ return cookieNames;
565
+ },
548
566
  config,
549
567
  log
550
568
  };
551
569
  return async function middleware(request) {
570
+ resolveConfig();
552
571
  const { pathname } = request.nextUrl;
553
572
  log(`${request.method} ${pathname}`);
554
573
  if (matchesAny(pathname, config.ignoredRoutes)) {