@sylphx/sdk 0.3.3 → 0.3.5
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 +73 -28
- package/dist/index.d.ts +73 -28
- package/dist/index.js +104 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +103 -14
- package/dist/index.mjs.map +1 -1
- package/dist/nextjs/index.d.cts +14 -11
- package/dist/nextjs/index.d.ts +14 -11
- package/dist/nextjs/index.js +55 -22
- package/dist/nextjs/index.js.map +1 -1
- package/dist/nextjs/index.mjs +55 -22
- package/dist/nextjs/index.mjs.map +1 -1
- package/dist/react/index.d.cts +35 -37
- package/dist/react/index.d.ts +35 -37
- package/dist/react/index.js +110 -21
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +110 -21
- package/dist/react/index.mjs.map +1 -1
- package/dist/server/index.d.cts +35 -25
- package/dist/server/index.d.ts +35 -25
- package/dist/server/index.js +60 -23
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +60 -23
- 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/react/index.mjs
CHANGED
|
@@ -42966,6 +42966,7 @@ var PlatformContext = createContext63(null);
|
|
|
42966
42966
|
init_constants();
|
|
42967
42967
|
|
|
42968
42968
|
// src/key-validation.ts
|
|
42969
|
+
var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
|
|
42969
42970
|
var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
|
|
42970
42971
|
var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
|
|
42971
42972
|
var ENV_PREFIX_MAP = {
|
|
@@ -43056,6 +43057,9 @@ function validateKeyForType(key, keyType, pattern, envVarName) {
|
|
|
43056
43057
|
issues: [...issues, "invalid-format"]
|
|
43057
43058
|
};
|
|
43058
43059
|
}
|
|
43060
|
+
function validatePublicKey(key) {
|
|
43061
|
+
return validateKeyForType(key, "publicKey", PUBLIC_KEY_PATTERN, "NEXT_PUBLIC_SYLPHX_KEY");
|
|
43062
|
+
}
|
|
43059
43063
|
function validateAppId(key) {
|
|
43060
43064
|
return validateKeyForType(key, "appId", APP_ID_PATTERN, "NEXT_PUBLIC_SYLPHX_APP_ID");
|
|
43061
43065
|
}
|
|
@@ -43084,22 +43088,23 @@ function validateAndSanitizeSecretKey(key) {
|
|
|
43084
43088
|
}
|
|
43085
43089
|
function detectEnvironment(key) {
|
|
43086
43090
|
const sanitized = key.trim().toLowerCase();
|
|
43091
|
+
if (sanitized.startsWith("pk_")) {
|
|
43092
|
+
const result = validatePublicKey(sanitized);
|
|
43093
|
+
if (!result.valid) throw new Error(result.error);
|
|
43094
|
+
return result.environment;
|
|
43095
|
+
}
|
|
43087
43096
|
if (sanitized.startsWith("sk_")) {
|
|
43088
43097
|
const result = validateSecretKey(sanitized);
|
|
43089
|
-
if (!result.valid)
|
|
43090
|
-
throw new Error(result.error);
|
|
43091
|
-
}
|
|
43098
|
+
if (!result.valid) throw new Error(result.error);
|
|
43092
43099
|
return result.environment;
|
|
43093
43100
|
}
|
|
43094
43101
|
if (sanitized.startsWith("app_")) {
|
|
43095
43102
|
const result = validateAppId(sanitized);
|
|
43096
|
-
if (!result.valid)
|
|
43097
|
-
throw new Error(result.error);
|
|
43098
|
-
}
|
|
43103
|
+
if (!result.valid) throw new Error(result.error);
|
|
43099
43104
|
return result.environment;
|
|
43100
43105
|
}
|
|
43101
43106
|
throw new Error(
|
|
43102
|
-
`[Sylphx] Invalid key format. Key must start with 'sk_' (secret) or 'app_' (App ID).`
|
|
43107
|
+
`[Sylphx] Invalid key format. Key must start with 'pk_' (publishable), 'sk_' (secret), or 'app_' (legacy App ID).`
|
|
43103
43108
|
);
|
|
43104
43109
|
}
|
|
43105
43110
|
function isDevelopmentKey(key) {
|
|
@@ -43115,6 +43120,7 @@ function getCookieNamespace(secretKey) {
|
|
|
43115
43120
|
}
|
|
43116
43121
|
function detectKeyType(key) {
|
|
43117
43122
|
const sanitized = key.trim().toLowerCase();
|
|
43123
|
+
if (sanitized.startsWith("pk_")) return "publicKey";
|
|
43118
43124
|
if (sanitized.startsWith("app_")) return "appId";
|
|
43119
43125
|
if (sanitized.startsWith("sk_")) return "secret";
|
|
43120
43126
|
return null;
|
|
@@ -43127,6 +43133,9 @@ function isSecretKey(key) {
|
|
|
43127
43133
|
}
|
|
43128
43134
|
function validateKey(key) {
|
|
43129
43135
|
const keyType = key ? detectKeyType(key) : null;
|
|
43136
|
+
if (keyType === "publicKey") {
|
|
43137
|
+
return validatePublicKey(key);
|
|
43138
|
+
}
|
|
43130
43139
|
if (keyType === "appId") {
|
|
43131
43140
|
return validateAppId(key);
|
|
43132
43141
|
}
|
|
@@ -43136,7 +43145,7 @@ function validateKey(key) {
|
|
|
43136
43145
|
return {
|
|
43137
43146
|
valid: false,
|
|
43138
43147
|
sanitizedKey: "",
|
|
43139
|
-
error: key ? `Invalid key format. Keys must start with 'app_' (
|
|
43148
|
+
error: key ? `Invalid key format. Keys must start with 'pk_' (publishable), 'app_' (legacy), or 'sk_' (secret), followed by environment (dev/stg/prod). Got: ${key.slice(0, 20)}...` : "API key is required but was not provided.",
|
|
43140
43149
|
issues: key ? ["invalid_format"] : ["missing"]
|
|
43141
43150
|
};
|
|
43142
43151
|
}
|
|
@@ -43161,6 +43170,57 @@ function isDevelopmentRuntime() {
|
|
|
43161
43170
|
}
|
|
43162
43171
|
|
|
43163
43172
|
// src/config.ts
|
|
43173
|
+
function parseKey(key) {
|
|
43174
|
+
const sanitized = key.trim().toLowerCase();
|
|
43175
|
+
if (sanitized.startsWith("app_")) {
|
|
43176
|
+
throw new SylphxError(
|
|
43177
|
+
"[Sylphx] API key format has changed (ADR-021). Keys now embed the project ref.\n\nOld format: app_prod_xxx\nNew format: pk_prod_{ref}_xxx\n\nPlease regenerate your API keys from the Sylphx Console \u2192 Your App \u2192 Environments.\nOr contact support@sylphx.com for assistance.",
|
|
43178
|
+
{ code: "BAD_REQUEST" }
|
|
43179
|
+
);
|
|
43180
|
+
}
|
|
43181
|
+
const prefix = sanitized.startsWith("pk_") ? "pk" : sanitized.startsWith("sk_") ? "sk" : null;
|
|
43182
|
+
if (!prefix) {
|
|
43183
|
+
throw new SylphxError(
|
|
43184
|
+
`[Sylphx] Invalid key format. Keys must start with 'pk_' (publishable) or 'sk_' (secret). Got: "${sanitized.slice(0, 15)}..."`,
|
|
43185
|
+
{ code: "BAD_REQUEST" }
|
|
43186
|
+
);
|
|
43187
|
+
}
|
|
43188
|
+
const parts = sanitized.split("_");
|
|
43189
|
+
if (parts.length !== 4) {
|
|
43190
|
+
throw new SylphxError(
|
|
43191
|
+
`[Sylphx] Invalid key structure. Expected format: ${prefix}_{env}_{ref}_{token} (4 segments separated by '_'). Got ${parts.length} segment(s).`,
|
|
43192
|
+
{ code: "BAD_REQUEST" }
|
|
43193
|
+
);
|
|
43194
|
+
}
|
|
43195
|
+
const [, env, ref, token] = parts;
|
|
43196
|
+
if (env !== "prod" && env !== "dev" && env !== "stg") {
|
|
43197
|
+
throw new SylphxError(
|
|
43198
|
+
`[Sylphx] Invalid key environment "${env}". Must be 'prod', 'dev', or 'stg'.`,
|
|
43199
|
+
{ code: "BAD_REQUEST" }
|
|
43200
|
+
);
|
|
43201
|
+
}
|
|
43202
|
+
if (!/^[a-z0-9]{12}$/.test(ref)) {
|
|
43203
|
+
throw new SylphxError(
|
|
43204
|
+
`[Sylphx] Invalid project ref in key: "${ref}". Must be a 12-character lowercase alphanumeric string.`,
|
|
43205
|
+
{ code: "BAD_REQUEST" }
|
|
43206
|
+
);
|
|
43207
|
+
}
|
|
43208
|
+
const expectedTokenLen = prefix === "pk" ? 32 : 64;
|
|
43209
|
+
if (token.length !== expectedTokenLen || !/^[a-f0-9]+$/.test(token)) {
|
|
43210
|
+
throw new SylphxError(
|
|
43211
|
+
`[Sylphx] Invalid key token. ${prefix === "pk" ? "Publishable" : "Secret"} keys must have a ${expectedTokenLen}-char hex token.`,
|
|
43212
|
+
{ code: "BAD_REQUEST" }
|
|
43213
|
+
);
|
|
43214
|
+
}
|
|
43215
|
+
return {
|
|
43216
|
+
type: prefix,
|
|
43217
|
+
env,
|
|
43218
|
+
ref,
|
|
43219
|
+
token,
|
|
43220
|
+
isPublic: prefix === "pk",
|
|
43221
|
+
baseUrl: `https://${ref}.${DEFAULT_SDK_API_HOST}${SDK_API_PATH}`
|
|
43222
|
+
};
|
|
43223
|
+
}
|
|
43164
43224
|
function httpStatusToErrorCode(status) {
|
|
43165
43225
|
switch (status) {
|
|
43166
43226
|
case 400:
|
|
@@ -43193,33 +43253,60 @@ function httpStatusToErrorCode(status) {
|
|
|
43193
43253
|
return status >= 500 ? "INTERNAL_SERVER_ERROR" : "BAD_REQUEST";
|
|
43194
43254
|
}
|
|
43195
43255
|
}
|
|
43196
|
-
var REF_PATTERN = /^[a-z0-9]{
|
|
43256
|
+
var REF_PATTERN = /^[a-z0-9]{12}$/;
|
|
43197
43257
|
function createConfig(input) {
|
|
43258
|
+
const keyForParsing = input.secretKey || input.publicKey;
|
|
43259
|
+
if (!keyForParsing) {
|
|
43260
|
+
if (input.ref) {
|
|
43261
|
+
const trimmedRef = input.ref.trim();
|
|
43262
|
+
if (!REF_PATTERN.test(trimmedRef)) {
|
|
43263
|
+
throw new SylphxError(
|
|
43264
|
+
`[Sylphx] Invalid project ref format: "${input.ref}". Expected a 12-character lowercase alphanumeric string (e.g. "abc123def456"). Get your ref from Platform Console \u2192 Projects \u2192 Your Project \u2192 Overview.`,
|
|
43265
|
+
{ code: "BAD_REQUEST" }
|
|
43266
|
+
);
|
|
43267
|
+
}
|
|
43268
|
+
const baseUrl2 = `https://${trimmedRef}.${DEFAULT_SDK_API_HOST}${SDK_API_PATH}`;
|
|
43269
|
+
console.warn(
|
|
43270
|
+
"[Sylphx] Providing only ref without a key is deprecated. Provide secretKey or publicKey \u2014 the ref is now embedded in keys (ADR-021)."
|
|
43271
|
+
);
|
|
43272
|
+
return Object.freeze({ ref: trimmedRef, baseUrl: baseUrl2, accessToken: input.accessToken });
|
|
43273
|
+
}
|
|
43274
|
+
throw new SylphxError(
|
|
43275
|
+
"[Sylphx] Either publicKey or secretKey must be provided to createConfig().",
|
|
43276
|
+
{ code: "BAD_REQUEST" }
|
|
43277
|
+
);
|
|
43278
|
+
}
|
|
43279
|
+
const parsed = parseKey(keyForParsing);
|
|
43280
|
+
const ref = parsed.ref;
|
|
43281
|
+
const baseUrl = parsed.baseUrl;
|
|
43198
43282
|
let secretKey;
|
|
43199
43283
|
if (input.secretKey) {
|
|
43200
43284
|
const result = validateKey(input.secretKey);
|
|
43201
43285
|
if (!result.valid) {
|
|
43202
|
-
throw new SylphxError(result.error || "Invalid
|
|
43286
|
+
throw new SylphxError(result.error || "Invalid secret key", {
|
|
43203
43287
|
code: "BAD_REQUEST",
|
|
43204
43288
|
data: { issues: result.issues }
|
|
43205
43289
|
});
|
|
43206
43290
|
}
|
|
43207
|
-
if (result.warning) {
|
|
43208
|
-
console.warn(`[Sylphx] ${result.warning}`);
|
|
43209
|
-
}
|
|
43291
|
+
if (result.warning) console.warn(`[Sylphx] ${result.warning}`);
|
|
43210
43292
|
secretKey = result.sanitizedKey;
|
|
43211
43293
|
}
|
|
43212
|
-
|
|
43213
|
-
if (
|
|
43214
|
-
|
|
43215
|
-
|
|
43216
|
-
|
|
43217
|
-
|
|
43294
|
+
let publicKey;
|
|
43295
|
+
if (input.publicKey) {
|
|
43296
|
+
const result = validateKey(input.publicKey);
|
|
43297
|
+
if (!result.valid) {
|
|
43298
|
+
throw new SylphxError(result.error || "Invalid public key", {
|
|
43299
|
+
code: "BAD_REQUEST",
|
|
43300
|
+
data: { issues: result.issues }
|
|
43301
|
+
});
|
|
43302
|
+
}
|
|
43303
|
+
if (result.warning) console.warn(`[Sylphx] ${result.warning}`);
|
|
43304
|
+
publicKey = result.sanitizedKey;
|
|
43218
43305
|
}
|
|
43219
|
-
const baseUrl = `https://${trimmedRef}.${DEFAULT_SDK_API_HOST}${SDK_API_PATH}`;
|
|
43220
43306
|
return Object.freeze({
|
|
43221
43307
|
secretKey,
|
|
43222
|
-
|
|
43308
|
+
publicKey,
|
|
43309
|
+
ref,
|
|
43223
43310
|
baseUrl,
|
|
43224
43311
|
accessToken: input.accessToken
|
|
43225
43312
|
});
|
|
@@ -43230,6 +43317,8 @@ function buildHeaders(config2) {
|
|
|
43230
43317
|
};
|
|
43231
43318
|
if (config2.secretKey) {
|
|
43232
43319
|
headers["x-app-secret"] = config2.secretKey;
|
|
43320
|
+
} else if (config2.publicKey) {
|
|
43321
|
+
headers["x-app-secret"] = config2.publicKey;
|
|
43233
43322
|
}
|
|
43234
43323
|
if (config2.accessToken) {
|
|
43235
43324
|
headers.Authorization = `Bearer ${config2.accessToken}`;
|