@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.
- package/dist/index.d.cts +583 -144
- package/dist/index.d.ts +583 -144
- package/dist/index.js +697 -267
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +691 -269
- package/dist/index.mjs.map +1 -1
- package/dist/nextjs/index.js +43 -24
- package/dist/nextjs/index.js.map +1 -1
- package/dist/nextjs/index.mjs +43 -24
- package/dist/nextjs/index.mjs.map +1 -1
- package/dist/react/index.d.cts +61 -33
- package/dist/react/index.d.ts +61 -33
- package/dist/react/index.js +907 -749
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +907 -749
- 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 +529 -11
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +525 -11
- 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,21 +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
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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
|
+
}
|
|
468
478
|
const config = {
|
|
469
479
|
publicRoutes: userConfig.publicRoutes ?? ["/"],
|
|
470
480
|
ignoredRoutes: userConfig.ignoredRoutes ?? [],
|
|
@@ -487,14 +497,23 @@ function createSylphxMiddleware(userConfig = {}) {
|
|
|
487
497
|
}
|
|
488
498
|
};
|
|
489
499
|
const ctx = {
|
|
490
|
-
secretKey
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
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
|
+
},
|
|
494
512
|
config,
|
|
495
513
|
log
|
|
496
514
|
};
|
|
497
515
|
return async function middleware(request) {
|
|
516
|
+
resolveConfig();
|
|
498
517
|
const { pathname } = request.nextUrl;
|
|
499
518
|
log(`${request.method} ${pathname}`);
|
|
500
519
|
if (matchesAny(pathname, config.ignoredRoutes)) {
|