@scalemule/nextjs 0.0.1 → 0.0.2
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/client.d.mts +8 -8
- package/dist/client.d.ts +8 -8
- package/dist/client.js +65 -59
- package/dist/client.mjs +65 -59
- package/dist/{index-BkacIKdu.d.mts → index-9v0SaLgg.d.mts} +8 -1
- package/dist/{index-BkacIKdu.d.ts → index-9v0SaLgg.d.ts} +8 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +455 -514
- package/dist/index.mjs +455 -515
- package/dist/server/auth.js +116 -78
- package/dist/server/auth.mjs +116 -78
- package/dist/server/index.d.mts +14 -11
- package/dist/server/index.d.ts +14 -11
- package/dist/server/index.js +209 -175
- package/dist/server/index.mjs +209 -175
- package/dist/server/webhook-handler.d.mts +2 -2
- package/dist/server/webhook-handler.d.ts +2 -2
- package/dist/testing.d.mts +10 -10
- package/dist/testing.d.ts +10 -10
- package/dist/testing.js +13 -7
- package/dist/testing.mjs +13 -7
- package/dist/{webhook-handler-BPNqhuwL.d.ts → webhook-handler-DCSwldKC.d.mts} +66 -66
- package/dist/{webhook-handler-C-5_Ey1T.d.mts → webhook-handler-Ymeice_x.d.ts} +66 -66
- package/package.json +1 -1
package/dist/server/index.js
CHANGED
|
@@ -4,6 +4,16 @@ var headers = require('next/headers');
|
|
|
4
4
|
var server = require('next/server');
|
|
5
5
|
var crypto$1 = require('crypto');
|
|
6
6
|
|
|
7
|
+
// src/types/index.ts
|
|
8
|
+
var ScaleMuleApiError = class extends Error {
|
|
9
|
+
constructor(error) {
|
|
10
|
+
super(error.message);
|
|
11
|
+
this.name = "ScaleMuleApiError";
|
|
12
|
+
this.code = error.code;
|
|
13
|
+
this.field = error.field;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
7
17
|
// src/server/context.ts
|
|
8
18
|
function validateIP(ip) {
|
|
9
19
|
if (!ip) return void 0;
|
|
@@ -544,22 +554,23 @@ var ScaleMuleServer = class {
|
|
|
544
554
|
headers,
|
|
545
555
|
body: formData
|
|
546
556
|
});
|
|
547
|
-
const
|
|
557
|
+
const text = await response.text();
|
|
558
|
+
const responseData = text ? JSON.parse(text) : null;
|
|
548
559
|
if (!response.ok) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
};
|
|
560
|
+
throw new ScaleMuleApiError(
|
|
561
|
+
responseData?.error || { code: "UPLOAD_FAILED", message: "Upload failed" }
|
|
562
|
+
);
|
|
553
563
|
}
|
|
564
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
554
565
|
return data;
|
|
555
566
|
} catch (err) {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
};
|
|
567
|
+
if (err instanceof ScaleMuleApiError) {
|
|
568
|
+
throw err;
|
|
569
|
+
}
|
|
570
|
+
throw new ScaleMuleApiError({
|
|
571
|
+
code: "UPLOAD_ERROR",
|
|
572
|
+
message: err instanceof Error ? err.message : "Upload failed"
|
|
573
|
+
});
|
|
563
574
|
}
|
|
564
575
|
}
|
|
565
576
|
};
|
|
@@ -582,7 +593,7 @@ var ScaleMuleServer = class {
|
|
|
582
593
|
* })
|
|
583
594
|
*
|
|
584
595
|
* // Store the secret for signature verification
|
|
585
|
-
* console.log('Webhook secret:', result.
|
|
596
|
+
* console.log('Webhook secret:', result.secret)
|
|
586
597
|
* ```
|
|
587
598
|
*/
|
|
588
599
|
create: async (data) => {
|
|
@@ -737,23 +748,25 @@ var ScaleMuleServer = class {
|
|
|
737
748
|
headers,
|
|
738
749
|
body: options.body ? JSON.stringify(options.body) : void 0
|
|
739
750
|
});
|
|
740
|
-
const
|
|
751
|
+
const text = await response.text();
|
|
752
|
+
const responseData = text ? JSON.parse(text) : null;
|
|
741
753
|
if (!response.ok) {
|
|
742
|
-
const error =
|
|
754
|
+
const error = responseData?.error || {
|
|
743
755
|
code: `HTTP_${response.status}`,
|
|
744
|
-
message:
|
|
756
|
+
message: responseData?.message || response.statusText
|
|
745
757
|
};
|
|
746
|
-
|
|
758
|
+
throw new ScaleMuleApiError(error);
|
|
747
759
|
}
|
|
760
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
748
761
|
return data;
|
|
749
762
|
} catch (err) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
};
|
|
763
|
+
if (err instanceof ScaleMuleApiError) {
|
|
764
|
+
throw err;
|
|
765
|
+
}
|
|
766
|
+
throw new ScaleMuleApiError({
|
|
767
|
+
code: "SERVER_ERROR",
|
|
768
|
+
message: err instanceof Error ? err.message : "Request failed"
|
|
769
|
+
});
|
|
757
770
|
}
|
|
758
771
|
}
|
|
759
772
|
};
|
|
@@ -1012,18 +1025,21 @@ function createAuthRoutes(config = {}) {
|
|
|
1012
1025
|
if (!email || !password) {
|
|
1013
1026
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
1014
1027
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1028
|
+
let registeredUser;
|
|
1029
|
+
try {
|
|
1030
|
+
registeredUser = await sm.auth.register({ email, password, full_name, username, phone });
|
|
1031
|
+
} catch (err) {
|
|
1032
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1017
1033
|
return errorResponse(
|
|
1018
|
-
|
|
1019
|
-
|
|
1034
|
+
apiErr?.code || "REGISTER_FAILED",
|
|
1035
|
+
apiErr?.message || "Registration failed",
|
|
1020
1036
|
400
|
|
1021
1037
|
);
|
|
1022
1038
|
}
|
|
1023
|
-
if (config.onRegister
|
|
1024
|
-
await config.onRegister({ id:
|
|
1039
|
+
if (config.onRegister) {
|
|
1040
|
+
await config.onRegister({ id: registeredUser.id, email: registeredUser.email });
|
|
1025
1041
|
}
|
|
1026
|
-
return successResponse({ user:
|
|
1042
|
+
return successResponse({ user: registeredUser, message: "Registration successful" }, 201);
|
|
1027
1043
|
}
|
|
1028
1044
|
// ==================== Login ====================
|
|
1029
1045
|
case "login": {
|
|
@@ -1031,9 +1047,12 @@ function createAuthRoutes(config = {}) {
|
|
|
1031
1047
|
if (!email || !password) {
|
|
1032
1048
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
1033
1049
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1050
|
+
let loginData;
|
|
1051
|
+
try {
|
|
1052
|
+
loginData = await sm.auth.login({ email, password, remember_me });
|
|
1053
|
+
} catch (err) {
|
|
1054
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1055
|
+
const errorCode = apiErr?.code || "LOGIN_FAILED";
|
|
1037
1056
|
let status = 400;
|
|
1038
1057
|
if (errorCode === "INVALID_CREDENTIALS" || errorCode === "UNAUTHORIZED") status = 401;
|
|
1039
1058
|
if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED", "MFA_REQUIRED"].includes(errorCode)) {
|
|
@@ -1041,17 +1060,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1041
1060
|
}
|
|
1042
1061
|
return errorResponse(
|
|
1043
1062
|
errorCode,
|
|
1044
|
-
|
|
1063
|
+
apiErr?.message || "Login failed",
|
|
1045
1064
|
status
|
|
1046
1065
|
);
|
|
1047
1066
|
}
|
|
1048
1067
|
if (config.onLogin) {
|
|
1049
1068
|
await config.onLogin({
|
|
1050
|
-
id:
|
|
1051
|
-
email:
|
|
1069
|
+
id: loginData.user.id,
|
|
1070
|
+
email: loginData.user.email
|
|
1052
1071
|
});
|
|
1053
1072
|
}
|
|
1054
|
-
return withSession(
|
|
1073
|
+
return withSession(loginData, { user: loginData.user }, cookieOptions);
|
|
1055
1074
|
}
|
|
1056
1075
|
// ==================== Logout ====================
|
|
1057
1076
|
case "logout": {
|
|
@@ -1079,11 +1098,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1079
1098
|
if (!token || !new_password) {
|
|
1080
1099
|
return errorResponse("VALIDATION_ERROR", "Token and new password required", 400);
|
|
1081
1100
|
}
|
|
1082
|
-
|
|
1083
|
-
|
|
1101
|
+
try {
|
|
1102
|
+
await sm.auth.resetPassword(token, new_password);
|
|
1103
|
+
} catch (err) {
|
|
1104
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1084
1105
|
return errorResponse(
|
|
1085
|
-
|
|
1086
|
-
|
|
1106
|
+
apiErr?.code || "RESET_FAILED",
|
|
1107
|
+
apiErr?.message || "Password reset failed",
|
|
1087
1108
|
400
|
|
1088
1109
|
);
|
|
1089
1110
|
}
|
|
@@ -1095,11 +1116,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1095
1116
|
if (!token) {
|
|
1096
1117
|
return errorResponse("VALIDATION_ERROR", "Token required", 400);
|
|
1097
1118
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1119
|
+
try {
|
|
1120
|
+
await sm.auth.verifyEmail(token);
|
|
1121
|
+
} catch (err) {
|
|
1122
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1100
1123
|
return errorResponse(
|
|
1101
|
-
|
|
1102
|
-
|
|
1124
|
+
apiErr?.code || "VERIFY_FAILED",
|
|
1125
|
+
apiErr?.message || "Email verification failed",
|
|
1103
1126
|
400
|
|
1104
1127
|
);
|
|
1105
1128
|
}
|
|
@@ -1111,12 +1134,14 @@ function createAuthRoutes(config = {}) {
|
|
|
1111
1134
|
const { email } = body;
|
|
1112
1135
|
const session = await getSession();
|
|
1113
1136
|
if (email) {
|
|
1114
|
-
|
|
1115
|
-
|
|
1137
|
+
try {
|
|
1138
|
+
await sm.auth.resendVerification(email);
|
|
1139
|
+
} catch (err) {
|
|
1140
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1116
1141
|
return errorResponse(
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1142
|
+
apiErr?.code || "RESEND_FAILED",
|
|
1143
|
+
apiErr?.message || "Failed to resend verification",
|
|
1144
|
+
apiErr?.code === "RATE_LIMITED" ? 429 : 400
|
|
1120
1145
|
);
|
|
1121
1146
|
}
|
|
1122
1147
|
return successResponse({ message: "Verification email sent" });
|
|
@@ -1124,11 +1149,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1124
1149
|
if (!session) {
|
|
1125
1150
|
return errorResponse("UNAUTHORIZED", "Email or session required", 401);
|
|
1126
1151
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1152
|
+
try {
|
|
1153
|
+
await sm.auth.resendVerification(session.sessionToken);
|
|
1154
|
+
} catch (err) {
|
|
1155
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1129
1156
|
return errorResponse(
|
|
1130
|
-
|
|
1131
|
-
|
|
1157
|
+
apiErr?.code || "RESEND_FAILED",
|
|
1158
|
+
apiErr?.message || "Failed to resend verification",
|
|
1132
1159
|
400
|
|
1133
1160
|
);
|
|
1134
1161
|
}
|
|
@@ -1140,15 +1167,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1140
1167
|
if (!session) {
|
|
1141
1168
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
1142
1169
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1170
|
+
let refreshData;
|
|
1171
|
+
try {
|
|
1172
|
+
refreshData = await sm.auth.refresh(session.sessionToken);
|
|
1173
|
+
} catch {
|
|
1145
1174
|
return clearSession(
|
|
1146
1175
|
{ message: "Session expired" },
|
|
1147
1176
|
cookieOptions
|
|
1148
1177
|
);
|
|
1149
1178
|
}
|
|
1150
1179
|
return withRefreshedSession(
|
|
1151
|
-
|
|
1180
|
+
refreshData.session_token,
|
|
1152
1181
|
session.userId,
|
|
1153
1182
|
{ message: "Session refreshed" },
|
|
1154
1183
|
cookieOptions
|
|
@@ -1164,15 +1193,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1164
1193
|
if (!current_password || !new_password) {
|
|
1165
1194
|
return errorResponse("VALIDATION_ERROR", "Current and new password required", 400);
|
|
1166
1195
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1196
|
+
try {
|
|
1197
|
+
await sm.user.changePassword(
|
|
1198
|
+
session.sessionToken,
|
|
1199
|
+
current_password,
|
|
1200
|
+
new_password
|
|
1201
|
+
);
|
|
1202
|
+
} catch (err) {
|
|
1203
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1173
1204
|
return errorResponse(
|
|
1174
|
-
|
|
1175
|
-
|
|
1205
|
+
apiErr?.code || "CHANGE_FAILED",
|
|
1206
|
+
apiErr?.message || "Failed to change password",
|
|
1176
1207
|
400
|
|
1177
1208
|
);
|
|
1178
1209
|
}
|
|
@@ -1197,14 +1228,16 @@ function createAuthRoutes(config = {}) {
|
|
|
1197
1228
|
if (!session) {
|
|
1198
1229
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
1199
1230
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1231
|
+
let userData;
|
|
1232
|
+
try {
|
|
1233
|
+
userData = await sm.auth.me(session.sessionToken);
|
|
1234
|
+
} catch {
|
|
1202
1235
|
return clearSession(
|
|
1203
1236
|
{ error: { code: "SESSION_EXPIRED", message: "Session expired" } },
|
|
1204
1237
|
cookieOptions
|
|
1205
1238
|
);
|
|
1206
1239
|
}
|
|
1207
|
-
return successResponse({ user:
|
|
1240
|
+
return successResponse({ user: userData });
|
|
1208
1241
|
}
|
|
1209
1242
|
// ==================== Get Session Status ====================
|
|
1210
1243
|
case "session": {
|
|
@@ -1239,11 +1272,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1239
1272
|
if (!password) {
|
|
1240
1273
|
return errorResponse("VALIDATION_ERROR", "Password required", 400);
|
|
1241
1274
|
}
|
|
1242
|
-
|
|
1243
|
-
|
|
1275
|
+
try {
|
|
1276
|
+
await sm.user.deleteAccount(session.sessionToken, password);
|
|
1277
|
+
} catch (err) {
|
|
1278
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1244
1279
|
return errorResponse(
|
|
1245
|
-
|
|
1246
|
-
|
|
1280
|
+
apiErr?.code || "DELETE_FAILED",
|
|
1281
|
+
apiErr?.message || "Failed to delete account",
|
|
1247
1282
|
400
|
|
1248
1283
|
);
|
|
1249
1284
|
}
|
|
@@ -1271,15 +1306,18 @@ function createAuthRoutes(config = {}) {
|
|
|
1271
1306
|
}
|
|
1272
1307
|
const body = await request.json().catch(() => ({}));
|
|
1273
1308
|
const { full_name, avatar_url } = body;
|
|
1274
|
-
|
|
1275
|
-
|
|
1309
|
+
let updatedUser;
|
|
1310
|
+
try {
|
|
1311
|
+
updatedUser = await sm.user.update(session.sessionToken, { full_name, avatar_url });
|
|
1312
|
+
} catch (err) {
|
|
1313
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1276
1314
|
return errorResponse(
|
|
1277
|
-
|
|
1278
|
-
|
|
1315
|
+
apiErr?.code || "UPDATE_FAILED",
|
|
1316
|
+
apiErr?.message || "Failed to update profile",
|
|
1279
1317
|
400
|
|
1280
1318
|
);
|
|
1281
1319
|
}
|
|
1282
|
-
return successResponse({ user:
|
|
1320
|
+
return successResponse({ user: updatedUser });
|
|
1283
1321
|
}
|
|
1284
1322
|
default:
|
|
1285
1323
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|
|
@@ -1327,48 +1365,51 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1327
1365
|
if (!event_name) {
|
|
1328
1366
|
return errorResponse("VALIDATION_ERROR", "event_name is required", 400);
|
|
1329
1367
|
}
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1368
|
+
let trackResult;
|
|
1369
|
+
try {
|
|
1370
|
+
trackResult = await sm.analytics.trackEvent(
|
|
1371
|
+
{
|
|
1372
|
+
event_name,
|
|
1373
|
+
event_category,
|
|
1374
|
+
properties,
|
|
1375
|
+
user_id,
|
|
1376
|
+
session_id,
|
|
1377
|
+
anonymous_id,
|
|
1378
|
+
session_duration_seconds,
|
|
1379
|
+
page_url,
|
|
1380
|
+
page_title,
|
|
1381
|
+
referrer,
|
|
1382
|
+
landing_page,
|
|
1383
|
+
device_type,
|
|
1384
|
+
device_brand,
|
|
1385
|
+
device_model,
|
|
1386
|
+
browser,
|
|
1387
|
+
browser_version,
|
|
1388
|
+
os,
|
|
1389
|
+
os_version,
|
|
1390
|
+
screen_resolution,
|
|
1391
|
+
viewport_size,
|
|
1392
|
+
utm_source,
|
|
1393
|
+
utm_medium,
|
|
1394
|
+
utm_campaign,
|
|
1395
|
+
utm_term,
|
|
1396
|
+
utm_content,
|
|
1397
|
+
client_timestamp: client_timestamp || timestamp
|
|
1398
|
+
},
|
|
1399
|
+
{ clientContext }
|
|
1400
|
+
);
|
|
1401
|
+
} catch (err) {
|
|
1402
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1362
1403
|
return errorResponse(
|
|
1363
|
-
|
|
1364
|
-
|
|
1404
|
+
apiErr?.code || "TRACK_FAILED",
|
|
1405
|
+
apiErr?.message || "Failed to track event",
|
|
1365
1406
|
400
|
|
1366
1407
|
);
|
|
1367
1408
|
}
|
|
1368
1409
|
if (config.onEvent) {
|
|
1369
|
-
await config.onEvent({ event_name, session_id:
|
|
1410
|
+
await config.onEvent({ event_name, session_id: trackResult?.session_id });
|
|
1370
1411
|
}
|
|
1371
|
-
return successResponse({ tracked:
|
|
1412
|
+
return successResponse({ tracked: trackResult?.tracked || 1, session_id: trackResult?.session_id });
|
|
1372
1413
|
};
|
|
1373
1414
|
const POST = async (request, context) => {
|
|
1374
1415
|
try {
|
|
@@ -1395,15 +1436,18 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1395
1436
|
if (events.length > 100) {
|
|
1396
1437
|
return errorResponse("VALIDATION_ERROR", "Maximum 100 events per batch", 400);
|
|
1397
1438
|
}
|
|
1398
|
-
|
|
1399
|
-
|
|
1439
|
+
let batchResult;
|
|
1440
|
+
try {
|
|
1441
|
+
batchResult = await sm.analytics.trackBatch(events, { clientContext });
|
|
1442
|
+
} catch (err) {
|
|
1443
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1400
1444
|
return errorResponse(
|
|
1401
|
-
|
|
1402
|
-
|
|
1445
|
+
apiErr?.code || "BATCH_FAILED",
|
|
1446
|
+
apiErr?.message || "Failed to track events",
|
|
1403
1447
|
400
|
|
1404
1448
|
);
|
|
1405
1449
|
}
|
|
1406
|
-
return successResponse({ tracked:
|
|
1450
|
+
return successResponse({ tracked: batchResult?.tracked || events.length });
|
|
1407
1451
|
}
|
|
1408
1452
|
// ==================== Track Page View ====================
|
|
1409
1453
|
case "page-view":
|
|
@@ -1412,21 +1456,24 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1412
1456
|
if (!page_url) {
|
|
1413
1457
|
return errorResponse("VALIDATION_ERROR", "page_url is required", 400);
|
|
1414
1458
|
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1459
|
+
let pageViewResult;
|
|
1460
|
+
try {
|
|
1461
|
+
pageViewResult = await sm.analytics.trackPageView(
|
|
1462
|
+
{ page_url, page_title, referrer, session_id, user_id },
|
|
1463
|
+
{ clientContext }
|
|
1464
|
+
);
|
|
1465
|
+
} catch (err) {
|
|
1466
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1420
1467
|
return errorResponse(
|
|
1421
|
-
|
|
1422
|
-
|
|
1468
|
+
apiErr?.code || "TRACK_FAILED",
|
|
1469
|
+
apiErr?.message || "Failed to track page view",
|
|
1423
1470
|
400
|
|
1424
1471
|
);
|
|
1425
1472
|
}
|
|
1426
1473
|
if (config.onEvent) {
|
|
1427
|
-
await config.onEvent({ event_name: "page_viewed", session_id:
|
|
1474
|
+
await config.onEvent({ event_name: "page_viewed", session_id: pageViewResult?.session_id });
|
|
1428
1475
|
}
|
|
1429
|
-
return successResponse({ tracked:
|
|
1476
|
+
return successResponse({ tracked: pageViewResult?.tracked || 1, session_id: pageViewResult?.session_id });
|
|
1430
1477
|
}
|
|
1431
1478
|
default:
|
|
1432
1479
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|
|
@@ -1489,18 +1536,22 @@ function errorCodeToStatus(code) {
|
|
|
1489
1536
|
return CODE_TO_STATUS[code.toLowerCase()] || 400;
|
|
1490
1537
|
}
|
|
1491
1538
|
function unwrap(result) {
|
|
1492
|
-
if (result
|
|
1493
|
-
const
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
code
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1539
|
+
if (result !== null && result !== void 0 && typeof result === "object" && ("success" in result || "error" in result) && "data" in result) {
|
|
1540
|
+
const envelope = result;
|
|
1541
|
+
if (envelope.error || envelope.success === false) {
|
|
1542
|
+
const err = envelope.error;
|
|
1543
|
+
const code = err?.code || "UNKNOWN_ERROR";
|
|
1544
|
+
const status = err?.status || errorCodeToStatus(code);
|
|
1545
|
+
throw new ScaleMuleError(
|
|
1546
|
+
code,
|
|
1547
|
+
err?.message || "An error occurred",
|
|
1548
|
+
status,
|
|
1549
|
+
err?.details
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
return envelope.data;
|
|
1502
1553
|
}
|
|
1503
|
-
return result
|
|
1554
|
+
return result;
|
|
1504
1555
|
}
|
|
1505
1556
|
|
|
1506
1557
|
// src/server/handler.ts
|
|
@@ -1578,12 +1629,9 @@ async function registerVideoWebhook(url, options) {
|
|
|
1578
1629
|
url,
|
|
1579
1630
|
events: options?.events || ["video.ready", "video.failed"]
|
|
1580
1631
|
});
|
|
1581
|
-
if (!result.success || !result.data) {
|
|
1582
|
-
throw new Error(result.error?.message || "Failed to register webhook");
|
|
1583
|
-
}
|
|
1584
1632
|
return {
|
|
1585
|
-
id: result.
|
|
1586
|
-
secret: result.
|
|
1633
|
+
id: result.id,
|
|
1634
|
+
secret: result.secret
|
|
1587
1635
|
};
|
|
1588
1636
|
}
|
|
1589
1637
|
function createWebhookRoutes(config = {}) {
|
|
@@ -1724,13 +1772,7 @@ function createAuthMiddleware(config = {}) {
|
|
|
1724
1772
|
if (!skipValidation) {
|
|
1725
1773
|
try {
|
|
1726
1774
|
const sm = createServerClient();
|
|
1727
|
-
|
|
1728
|
-
if (!result.success) {
|
|
1729
|
-
const response = server.NextResponse.redirect(new URL(redirectTo, request.url));
|
|
1730
|
-
response.cookies.delete(SESSION_COOKIE_NAME);
|
|
1731
|
-
response.cookies.delete(USER_ID_COOKIE_NAME);
|
|
1732
|
-
return response;
|
|
1733
|
-
}
|
|
1775
|
+
await sm.auth.me(session.sessionToken);
|
|
1734
1776
|
} catch (error) {
|
|
1735
1777
|
console.error("[ScaleMule Middleware] Session validation failed, blocking request:", error);
|
|
1736
1778
|
const response = server.NextResponse.redirect(new URL(redirectTo, request.url));
|
|
@@ -1821,22 +1863,18 @@ async function getAppSecret(key) {
|
|
|
1821
1863
|
try {
|
|
1822
1864
|
const client = createServerClient();
|
|
1823
1865
|
const result = await client.secrets.get(key);
|
|
1824
|
-
if (!result
|
|
1825
|
-
if (result.error?.code === "SECRET_NOT_FOUND") {
|
|
1826
|
-
return void 0;
|
|
1827
|
-
}
|
|
1828
|
-
console.error(`[ScaleMule Secrets] Failed to fetch ${key}:`, result.error);
|
|
1829
|
-
return void 0;
|
|
1830
|
-
}
|
|
1831
|
-
if (!noCache && result.data) {
|
|
1866
|
+
if (!noCache && result) {
|
|
1832
1867
|
secretsCache[key] = {
|
|
1833
|
-
value: result.
|
|
1834
|
-
version: result.
|
|
1868
|
+
value: result.value,
|
|
1869
|
+
version: result.version,
|
|
1835
1870
|
cachedAt: Date.now()
|
|
1836
1871
|
};
|
|
1837
1872
|
}
|
|
1838
|
-
return result
|
|
1873
|
+
return result?.value;
|
|
1839
1874
|
} catch (error) {
|
|
1875
|
+
if (error instanceof ScaleMuleApiError && error.code === "SECRET_NOT_FOUND") {
|
|
1876
|
+
return void 0;
|
|
1877
|
+
}
|
|
1840
1878
|
console.error(`[ScaleMule Secrets] Error fetching ${key}:`, error);
|
|
1841
1879
|
return void 0;
|
|
1842
1880
|
}
|
|
@@ -1884,24 +1922,20 @@ async function getBundle(key, resolve = true) {
|
|
|
1884
1922
|
try {
|
|
1885
1923
|
const client = createServerClient();
|
|
1886
1924
|
const result = await client.bundles.get(key, resolve);
|
|
1887
|
-
if (!result
|
|
1888
|
-
if (result.error?.code === "BUNDLE_NOT_FOUND") {
|
|
1889
|
-
return void 0;
|
|
1890
|
-
}
|
|
1891
|
-
console.error(`[ScaleMule Bundles] Failed to fetch ${key}:`, result.error);
|
|
1892
|
-
return void 0;
|
|
1893
|
-
}
|
|
1894
|
-
if (!noCache && result.data) {
|
|
1925
|
+
if (!noCache && result) {
|
|
1895
1926
|
bundlesCache[key] = {
|
|
1896
|
-
type: result.
|
|
1897
|
-
data: result.data
|
|
1898
|
-
version: result.
|
|
1899
|
-
inheritsFrom: result.
|
|
1927
|
+
type: result.type,
|
|
1928
|
+
data: result.data,
|
|
1929
|
+
version: result.version,
|
|
1930
|
+
inheritsFrom: result.inherits_from,
|
|
1900
1931
|
cachedAt: Date.now()
|
|
1901
1932
|
};
|
|
1902
1933
|
}
|
|
1903
|
-
return result
|
|
1934
|
+
return result?.data;
|
|
1904
1935
|
} catch (error) {
|
|
1936
|
+
if (error instanceof ScaleMuleApiError && error.code === "BUNDLE_NOT_FOUND") {
|
|
1937
|
+
return void 0;
|
|
1938
|
+
}
|
|
1905
1939
|
console.error(`[ScaleMule Bundles] Error fetching ${key}:`, error);
|
|
1906
1940
|
return void 0;
|
|
1907
1941
|
}
|