@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.js
CHANGED
|
@@ -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 = /^
|
|
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 =
|
|
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,16 +503,32 @@ async function refreshTokens(refreshToken, ctx) {
|
|
|
504
503
|
}
|
|
505
504
|
}
|
|
506
505
|
function createSylphxMiddleware(userConfig = {}) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
)
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
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
|
+
}
|
|
517
532
|
const config = {
|
|
518
533
|
publicRoutes: userConfig.publicRoutes ?? ["/"],
|
|
519
534
|
ignoredRoutes: userConfig.ignoredRoutes ?? [],
|
|
@@ -536,14 +551,23 @@ function createSylphxMiddleware(userConfig = {}) {
|
|
|
536
551
|
}
|
|
537
552
|
};
|
|
538
553
|
const ctx = {
|
|
539
|
-
secretKey
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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
|
+
},
|
|
543
566
|
config,
|
|
544
567
|
log
|
|
545
568
|
};
|
|
546
569
|
return async function middleware(request) {
|
|
570
|
+
resolveConfig();
|
|
547
571
|
const { pathname } = request.nextUrl;
|
|
548
572
|
log(`${request.method} ${pathname}`);
|
|
549
573
|
if (matchesAny(pathname, config.ignoredRoutes)) {
|
|
@@ -1869,7 +1893,7 @@ function configureServer(config) {
|
|
|
1869
1893
|
const secretKey = validateAndSanitizeSecretKey(config.secretKey);
|
|
1870
1894
|
serverConfig = {
|
|
1871
1895
|
secretKey,
|
|
1872
|
-
platformUrl: `https://${DEFAULT_SDK_API_HOST}`.trim()
|
|
1896
|
+
platformUrl: config.platformUrl?.trim() || `https://${DEFAULT_SDK_API_HOST}`.trim()
|
|
1873
1897
|
};
|
|
1874
1898
|
}
|
|
1875
1899
|
function getConfig() {
|