@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.
- package/dist/index.d.cts +938 -140
- package/dist/index.d.ts +938 -140
- package/dist/index.js +810 -267
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +791 -269
- package/dist/index.mjs.map +1 -1
- package/dist/nextjs/index.js +44 -20
- package/dist/nextjs/index.js.map +1 -1
- package/dist/nextjs/index.mjs +44 -20
- package/dist/nextjs/index.mjs.map +1 -1
- package/dist/react/index.d.cts +389 -32
- package/dist/react/index.d.ts +389 -32
- package/dist/react/index.js +1610 -1285
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +1284 -963
- package/dist/react/index.mjs.map +1 -1
- package/dist/server/index.d.cts +355 -18
- package/dist/server/index.d.ts +355 -18
- package/dist/server/index.js +559 -22
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +555 -22
- package/dist/server/index.mjs.map +1 -1
- package/dist/web-analytics.js.map +1 -1
- package/dist/web-analytics.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/web-analytics.d.cts +0 -90
- package/dist/web-analytics.d.ts +0 -90
package/dist/nextjs/index.mjs
CHANGED
|
@@ -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 = /^
|
|
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 =
|
|
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
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
)
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
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
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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() {
|