@scalemule/nextjs 0.0.1 → 0.0.3
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 +78 -60
- package/dist/client.mjs +78 -60
- 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 +468 -515
- package/dist/index.mjs +468 -516
- package/dist/server/auth.js +124 -78
- package/dist/server/auth.mjs +124 -78
- package/dist/server/index.d.mts +14 -11
- package/dist/server/index.d.ts +14 -11
- package/dist/server/index.js +217 -175
- package/dist/server/index.mjs +217 -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,27 @@ var ScaleMuleServer = class {
|
|
|
544
554
|
headers,
|
|
545
555
|
body: formData
|
|
546
556
|
});
|
|
547
|
-
const
|
|
557
|
+
const text = await response.text();
|
|
558
|
+
let responseData = null;
|
|
559
|
+
try {
|
|
560
|
+
responseData = text ? JSON.parse(text) : null;
|
|
561
|
+
} catch {
|
|
562
|
+
}
|
|
548
563
|
if (!response.ok) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
};
|
|
564
|
+
throw new ScaleMuleApiError(
|
|
565
|
+
responseData?.error || { code: "UPLOAD_FAILED", message: text || "Upload failed" }
|
|
566
|
+
);
|
|
553
567
|
}
|
|
568
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
554
569
|
return data;
|
|
555
570
|
} catch (err) {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
};
|
|
571
|
+
if (err instanceof ScaleMuleApiError) {
|
|
572
|
+
throw err;
|
|
573
|
+
}
|
|
574
|
+
throw new ScaleMuleApiError({
|
|
575
|
+
code: "UPLOAD_ERROR",
|
|
576
|
+
message: err instanceof Error ? err.message : "Upload failed"
|
|
577
|
+
});
|
|
563
578
|
}
|
|
564
579
|
}
|
|
565
580
|
};
|
|
@@ -582,7 +597,7 @@ var ScaleMuleServer = class {
|
|
|
582
597
|
* })
|
|
583
598
|
*
|
|
584
599
|
* // Store the secret for signature verification
|
|
585
|
-
* console.log('Webhook secret:', result.
|
|
600
|
+
* console.log('Webhook secret:', result.secret)
|
|
586
601
|
* ```
|
|
587
602
|
*/
|
|
588
603
|
create: async (data) => {
|
|
@@ -737,23 +752,29 @@ var ScaleMuleServer = class {
|
|
|
737
752
|
headers,
|
|
738
753
|
body: options.body ? JSON.stringify(options.body) : void 0
|
|
739
754
|
});
|
|
740
|
-
const
|
|
755
|
+
const text = await response.text();
|
|
756
|
+
let responseData = null;
|
|
757
|
+
try {
|
|
758
|
+
responseData = text ? JSON.parse(text) : null;
|
|
759
|
+
} catch {
|
|
760
|
+
}
|
|
741
761
|
if (!response.ok) {
|
|
742
|
-
const error =
|
|
762
|
+
const error = responseData?.error || {
|
|
743
763
|
code: `HTTP_${response.status}`,
|
|
744
|
-
message:
|
|
764
|
+
message: responseData?.message || text || response.statusText
|
|
745
765
|
};
|
|
746
|
-
|
|
766
|
+
throw new ScaleMuleApiError(error);
|
|
747
767
|
}
|
|
768
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
748
769
|
return data;
|
|
749
770
|
} catch (err) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
};
|
|
771
|
+
if (err instanceof ScaleMuleApiError) {
|
|
772
|
+
throw err;
|
|
773
|
+
}
|
|
774
|
+
throw new ScaleMuleApiError({
|
|
775
|
+
code: "SERVER_ERROR",
|
|
776
|
+
message: err instanceof Error ? err.message : "Request failed"
|
|
777
|
+
});
|
|
757
778
|
}
|
|
758
779
|
}
|
|
759
780
|
};
|
|
@@ -1012,18 +1033,21 @@ function createAuthRoutes(config = {}) {
|
|
|
1012
1033
|
if (!email || !password) {
|
|
1013
1034
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
1014
1035
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1036
|
+
let registeredUser;
|
|
1037
|
+
try {
|
|
1038
|
+
registeredUser = await sm.auth.register({ email, password, full_name, username, phone });
|
|
1039
|
+
} catch (err) {
|
|
1040
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1017
1041
|
return errorResponse(
|
|
1018
|
-
|
|
1019
|
-
|
|
1042
|
+
apiErr?.code || "REGISTER_FAILED",
|
|
1043
|
+
apiErr?.message || "Registration failed",
|
|
1020
1044
|
400
|
|
1021
1045
|
);
|
|
1022
1046
|
}
|
|
1023
|
-
if (config.onRegister
|
|
1024
|
-
await config.onRegister({ id:
|
|
1047
|
+
if (config.onRegister) {
|
|
1048
|
+
await config.onRegister({ id: registeredUser.id, email: registeredUser.email });
|
|
1025
1049
|
}
|
|
1026
|
-
return successResponse({ user:
|
|
1050
|
+
return successResponse({ user: registeredUser, message: "Registration successful" }, 201);
|
|
1027
1051
|
}
|
|
1028
1052
|
// ==================== Login ====================
|
|
1029
1053
|
case "login": {
|
|
@@ -1031,9 +1055,12 @@ function createAuthRoutes(config = {}) {
|
|
|
1031
1055
|
if (!email || !password) {
|
|
1032
1056
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
1033
1057
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1058
|
+
let loginData;
|
|
1059
|
+
try {
|
|
1060
|
+
loginData = await sm.auth.login({ email, password, remember_me });
|
|
1061
|
+
} catch (err) {
|
|
1062
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1063
|
+
const errorCode = apiErr?.code || "LOGIN_FAILED";
|
|
1037
1064
|
let status = 400;
|
|
1038
1065
|
if (errorCode === "INVALID_CREDENTIALS" || errorCode === "UNAUTHORIZED") status = 401;
|
|
1039
1066
|
if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED", "MFA_REQUIRED"].includes(errorCode)) {
|
|
@@ -1041,17 +1068,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1041
1068
|
}
|
|
1042
1069
|
return errorResponse(
|
|
1043
1070
|
errorCode,
|
|
1044
|
-
|
|
1071
|
+
apiErr?.message || "Login failed",
|
|
1045
1072
|
status
|
|
1046
1073
|
);
|
|
1047
1074
|
}
|
|
1048
1075
|
if (config.onLogin) {
|
|
1049
1076
|
await config.onLogin({
|
|
1050
|
-
id:
|
|
1051
|
-
email:
|
|
1077
|
+
id: loginData.user.id,
|
|
1078
|
+
email: loginData.user.email
|
|
1052
1079
|
});
|
|
1053
1080
|
}
|
|
1054
|
-
return withSession(
|
|
1081
|
+
return withSession(loginData, { user: loginData.user }, cookieOptions);
|
|
1055
1082
|
}
|
|
1056
1083
|
// ==================== Logout ====================
|
|
1057
1084
|
case "logout": {
|
|
@@ -1079,11 +1106,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1079
1106
|
if (!token || !new_password) {
|
|
1080
1107
|
return errorResponse("VALIDATION_ERROR", "Token and new password required", 400);
|
|
1081
1108
|
}
|
|
1082
|
-
|
|
1083
|
-
|
|
1109
|
+
try {
|
|
1110
|
+
await sm.auth.resetPassword(token, new_password);
|
|
1111
|
+
} catch (err) {
|
|
1112
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1084
1113
|
return errorResponse(
|
|
1085
|
-
|
|
1086
|
-
|
|
1114
|
+
apiErr?.code || "RESET_FAILED",
|
|
1115
|
+
apiErr?.message || "Password reset failed",
|
|
1087
1116
|
400
|
|
1088
1117
|
);
|
|
1089
1118
|
}
|
|
@@ -1095,11 +1124,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1095
1124
|
if (!token) {
|
|
1096
1125
|
return errorResponse("VALIDATION_ERROR", "Token required", 400);
|
|
1097
1126
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1127
|
+
try {
|
|
1128
|
+
await sm.auth.verifyEmail(token);
|
|
1129
|
+
} catch (err) {
|
|
1130
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1100
1131
|
return errorResponse(
|
|
1101
|
-
|
|
1102
|
-
|
|
1132
|
+
apiErr?.code || "VERIFY_FAILED",
|
|
1133
|
+
apiErr?.message || "Email verification failed",
|
|
1103
1134
|
400
|
|
1104
1135
|
);
|
|
1105
1136
|
}
|
|
@@ -1111,12 +1142,14 @@ function createAuthRoutes(config = {}) {
|
|
|
1111
1142
|
const { email } = body;
|
|
1112
1143
|
const session = await getSession();
|
|
1113
1144
|
if (email) {
|
|
1114
|
-
|
|
1115
|
-
|
|
1145
|
+
try {
|
|
1146
|
+
await sm.auth.resendVerification(email);
|
|
1147
|
+
} catch (err) {
|
|
1148
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1116
1149
|
return errorResponse(
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1150
|
+
apiErr?.code || "RESEND_FAILED",
|
|
1151
|
+
apiErr?.message || "Failed to resend verification",
|
|
1152
|
+
apiErr?.code === "RATE_LIMITED" ? 429 : 400
|
|
1120
1153
|
);
|
|
1121
1154
|
}
|
|
1122
1155
|
return successResponse({ message: "Verification email sent" });
|
|
@@ -1124,11 +1157,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1124
1157
|
if (!session) {
|
|
1125
1158
|
return errorResponse("UNAUTHORIZED", "Email or session required", 401);
|
|
1126
1159
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1160
|
+
try {
|
|
1161
|
+
await sm.auth.resendVerification(session.sessionToken);
|
|
1162
|
+
} catch (err) {
|
|
1163
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1129
1164
|
return errorResponse(
|
|
1130
|
-
|
|
1131
|
-
|
|
1165
|
+
apiErr?.code || "RESEND_FAILED",
|
|
1166
|
+
apiErr?.message || "Failed to resend verification",
|
|
1132
1167
|
400
|
|
1133
1168
|
);
|
|
1134
1169
|
}
|
|
@@ -1140,15 +1175,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1140
1175
|
if (!session) {
|
|
1141
1176
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
1142
1177
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1178
|
+
let refreshData;
|
|
1179
|
+
try {
|
|
1180
|
+
refreshData = await sm.auth.refresh(session.sessionToken);
|
|
1181
|
+
} catch {
|
|
1145
1182
|
return clearSession(
|
|
1146
1183
|
{ message: "Session expired" },
|
|
1147
1184
|
cookieOptions
|
|
1148
1185
|
);
|
|
1149
1186
|
}
|
|
1150
1187
|
return withRefreshedSession(
|
|
1151
|
-
|
|
1188
|
+
refreshData.session_token,
|
|
1152
1189
|
session.userId,
|
|
1153
1190
|
{ message: "Session refreshed" },
|
|
1154
1191
|
cookieOptions
|
|
@@ -1164,15 +1201,17 @@ function createAuthRoutes(config = {}) {
|
|
|
1164
1201
|
if (!current_password || !new_password) {
|
|
1165
1202
|
return errorResponse("VALIDATION_ERROR", "Current and new password required", 400);
|
|
1166
1203
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1204
|
+
try {
|
|
1205
|
+
await sm.user.changePassword(
|
|
1206
|
+
session.sessionToken,
|
|
1207
|
+
current_password,
|
|
1208
|
+
new_password
|
|
1209
|
+
);
|
|
1210
|
+
} catch (err) {
|
|
1211
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1173
1212
|
return errorResponse(
|
|
1174
|
-
|
|
1175
|
-
|
|
1213
|
+
apiErr?.code || "CHANGE_FAILED",
|
|
1214
|
+
apiErr?.message || "Failed to change password",
|
|
1176
1215
|
400
|
|
1177
1216
|
);
|
|
1178
1217
|
}
|
|
@@ -1197,14 +1236,16 @@ function createAuthRoutes(config = {}) {
|
|
|
1197
1236
|
if (!session) {
|
|
1198
1237
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
1199
1238
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1239
|
+
let userData;
|
|
1240
|
+
try {
|
|
1241
|
+
userData = await sm.auth.me(session.sessionToken);
|
|
1242
|
+
} catch {
|
|
1202
1243
|
return clearSession(
|
|
1203
1244
|
{ error: { code: "SESSION_EXPIRED", message: "Session expired" } },
|
|
1204
1245
|
cookieOptions
|
|
1205
1246
|
);
|
|
1206
1247
|
}
|
|
1207
|
-
return successResponse({ user:
|
|
1248
|
+
return successResponse({ user: userData });
|
|
1208
1249
|
}
|
|
1209
1250
|
// ==================== Get Session Status ====================
|
|
1210
1251
|
case "session": {
|
|
@@ -1239,11 +1280,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1239
1280
|
if (!password) {
|
|
1240
1281
|
return errorResponse("VALIDATION_ERROR", "Password required", 400);
|
|
1241
1282
|
}
|
|
1242
|
-
|
|
1243
|
-
|
|
1283
|
+
try {
|
|
1284
|
+
await sm.user.deleteAccount(session.sessionToken, password);
|
|
1285
|
+
} catch (err) {
|
|
1286
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1244
1287
|
return errorResponse(
|
|
1245
|
-
|
|
1246
|
-
|
|
1288
|
+
apiErr?.code || "DELETE_FAILED",
|
|
1289
|
+
apiErr?.message || "Failed to delete account",
|
|
1247
1290
|
400
|
|
1248
1291
|
);
|
|
1249
1292
|
}
|
|
@@ -1271,15 +1314,18 @@ function createAuthRoutes(config = {}) {
|
|
|
1271
1314
|
}
|
|
1272
1315
|
const body = await request.json().catch(() => ({}));
|
|
1273
1316
|
const { full_name, avatar_url } = body;
|
|
1274
|
-
|
|
1275
|
-
|
|
1317
|
+
let updatedUser;
|
|
1318
|
+
try {
|
|
1319
|
+
updatedUser = await sm.user.update(session.sessionToken, { full_name, avatar_url });
|
|
1320
|
+
} catch (err) {
|
|
1321
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1276
1322
|
return errorResponse(
|
|
1277
|
-
|
|
1278
|
-
|
|
1323
|
+
apiErr?.code || "UPDATE_FAILED",
|
|
1324
|
+
apiErr?.message || "Failed to update profile",
|
|
1279
1325
|
400
|
|
1280
1326
|
);
|
|
1281
1327
|
}
|
|
1282
|
-
return successResponse({ user:
|
|
1328
|
+
return successResponse({ user: updatedUser });
|
|
1283
1329
|
}
|
|
1284
1330
|
default:
|
|
1285
1331
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|
|
@@ -1327,48 +1373,51 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1327
1373
|
if (!event_name) {
|
|
1328
1374
|
return errorResponse("VALIDATION_ERROR", "event_name is required", 400);
|
|
1329
1375
|
}
|
|
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
|
-
|
|
1376
|
+
let trackResult;
|
|
1377
|
+
try {
|
|
1378
|
+
trackResult = await sm.analytics.trackEvent(
|
|
1379
|
+
{
|
|
1380
|
+
event_name,
|
|
1381
|
+
event_category,
|
|
1382
|
+
properties,
|
|
1383
|
+
user_id,
|
|
1384
|
+
session_id,
|
|
1385
|
+
anonymous_id,
|
|
1386
|
+
session_duration_seconds,
|
|
1387
|
+
page_url,
|
|
1388
|
+
page_title,
|
|
1389
|
+
referrer,
|
|
1390
|
+
landing_page,
|
|
1391
|
+
device_type,
|
|
1392
|
+
device_brand,
|
|
1393
|
+
device_model,
|
|
1394
|
+
browser,
|
|
1395
|
+
browser_version,
|
|
1396
|
+
os,
|
|
1397
|
+
os_version,
|
|
1398
|
+
screen_resolution,
|
|
1399
|
+
viewport_size,
|
|
1400
|
+
utm_source,
|
|
1401
|
+
utm_medium,
|
|
1402
|
+
utm_campaign,
|
|
1403
|
+
utm_term,
|
|
1404
|
+
utm_content,
|
|
1405
|
+
client_timestamp: client_timestamp || timestamp
|
|
1406
|
+
},
|
|
1407
|
+
{ clientContext }
|
|
1408
|
+
);
|
|
1409
|
+
} catch (err) {
|
|
1410
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1362
1411
|
return errorResponse(
|
|
1363
|
-
|
|
1364
|
-
|
|
1412
|
+
apiErr?.code || "TRACK_FAILED",
|
|
1413
|
+
apiErr?.message || "Failed to track event",
|
|
1365
1414
|
400
|
|
1366
1415
|
);
|
|
1367
1416
|
}
|
|
1368
1417
|
if (config.onEvent) {
|
|
1369
|
-
await config.onEvent({ event_name, session_id:
|
|
1418
|
+
await config.onEvent({ event_name, session_id: trackResult?.session_id });
|
|
1370
1419
|
}
|
|
1371
|
-
return successResponse({ tracked:
|
|
1420
|
+
return successResponse({ tracked: trackResult?.tracked || 1, session_id: trackResult?.session_id });
|
|
1372
1421
|
};
|
|
1373
1422
|
const POST = async (request, context) => {
|
|
1374
1423
|
try {
|
|
@@ -1395,15 +1444,18 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1395
1444
|
if (events.length > 100) {
|
|
1396
1445
|
return errorResponse("VALIDATION_ERROR", "Maximum 100 events per batch", 400);
|
|
1397
1446
|
}
|
|
1398
|
-
|
|
1399
|
-
|
|
1447
|
+
let batchResult;
|
|
1448
|
+
try {
|
|
1449
|
+
batchResult = await sm.analytics.trackBatch(events, { clientContext });
|
|
1450
|
+
} catch (err) {
|
|
1451
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1400
1452
|
return errorResponse(
|
|
1401
|
-
|
|
1402
|
-
|
|
1453
|
+
apiErr?.code || "BATCH_FAILED",
|
|
1454
|
+
apiErr?.message || "Failed to track events",
|
|
1403
1455
|
400
|
|
1404
1456
|
);
|
|
1405
1457
|
}
|
|
1406
|
-
return successResponse({ tracked:
|
|
1458
|
+
return successResponse({ tracked: batchResult?.tracked || events.length });
|
|
1407
1459
|
}
|
|
1408
1460
|
// ==================== Track Page View ====================
|
|
1409
1461
|
case "page-view":
|
|
@@ -1412,21 +1464,24 @@ function createAnalyticsRoutes(config = {}) {
|
|
|
1412
1464
|
if (!page_url) {
|
|
1413
1465
|
return errorResponse("VALIDATION_ERROR", "page_url is required", 400);
|
|
1414
1466
|
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1467
|
+
let pageViewResult;
|
|
1468
|
+
try {
|
|
1469
|
+
pageViewResult = await sm.analytics.trackPageView(
|
|
1470
|
+
{ page_url, page_title, referrer, session_id, user_id },
|
|
1471
|
+
{ clientContext }
|
|
1472
|
+
);
|
|
1473
|
+
} catch (err) {
|
|
1474
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1420
1475
|
return errorResponse(
|
|
1421
|
-
|
|
1422
|
-
|
|
1476
|
+
apiErr?.code || "TRACK_FAILED",
|
|
1477
|
+
apiErr?.message || "Failed to track page view",
|
|
1423
1478
|
400
|
|
1424
1479
|
);
|
|
1425
1480
|
}
|
|
1426
1481
|
if (config.onEvent) {
|
|
1427
|
-
await config.onEvent({ event_name: "page_viewed", session_id:
|
|
1482
|
+
await config.onEvent({ event_name: "page_viewed", session_id: pageViewResult?.session_id });
|
|
1428
1483
|
}
|
|
1429
|
-
return successResponse({ tracked:
|
|
1484
|
+
return successResponse({ tracked: pageViewResult?.tracked || 1, session_id: pageViewResult?.session_id });
|
|
1430
1485
|
}
|
|
1431
1486
|
default:
|
|
1432
1487
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|
|
@@ -1489,18 +1544,22 @@ function errorCodeToStatus(code) {
|
|
|
1489
1544
|
return CODE_TO_STATUS[code.toLowerCase()] || 400;
|
|
1490
1545
|
}
|
|
1491
1546
|
function unwrap(result) {
|
|
1492
|
-
if (result
|
|
1493
|
-
const
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
code
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1547
|
+
if (result !== null && result !== void 0 && typeof result === "object" && ("success" in result || "error" in result) && "data" in result) {
|
|
1548
|
+
const envelope = result;
|
|
1549
|
+
if (envelope.error || envelope.success === false) {
|
|
1550
|
+
const err = envelope.error;
|
|
1551
|
+
const code = err?.code || "UNKNOWN_ERROR";
|
|
1552
|
+
const status = err?.status || errorCodeToStatus(code);
|
|
1553
|
+
throw new ScaleMuleError(
|
|
1554
|
+
code,
|
|
1555
|
+
err?.message || "An error occurred",
|
|
1556
|
+
status,
|
|
1557
|
+
err?.details
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
return envelope.data;
|
|
1502
1561
|
}
|
|
1503
|
-
return result
|
|
1562
|
+
return result;
|
|
1504
1563
|
}
|
|
1505
1564
|
|
|
1506
1565
|
// src/server/handler.ts
|
|
@@ -1578,12 +1637,9 @@ async function registerVideoWebhook(url, options) {
|
|
|
1578
1637
|
url,
|
|
1579
1638
|
events: options?.events || ["video.ready", "video.failed"]
|
|
1580
1639
|
});
|
|
1581
|
-
if (!result.success || !result.data) {
|
|
1582
|
-
throw new Error(result.error?.message || "Failed to register webhook");
|
|
1583
|
-
}
|
|
1584
1640
|
return {
|
|
1585
|
-
id: result.
|
|
1586
|
-
secret: result.
|
|
1641
|
+
id: result.id,
|
|
1642
|
+
secret: result.secret
|
|
1587
1643
|
};
|
|
1588
1644
|
}
|
|
1589
1645
|
function createWebhookRoutes(config = {}) {
|
|
@@ -1724,13 +1780,7 @@ function createAuthMiddleware(config = {}) {
|
|
|
1724
1780
|
if (!skipValidation) {
|
|
1725
1781
|
try {
|
|
1726
1782
|
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
|
-
}
|
|
1783
|
+
await sm.auth.me(session.sessionToken);
|
|
1734
1784
|
} catch (error) {
|
|
1735
1785
|
console.error("[ScaleMule Middleware] Session validation failed, blocking request:", error);
|
|
1736
1786
|
const response = server.NextResponse.redirect(new URL(redirectTo, request.url));
|
|
@@ -1821,22 +1871,18 @@ async function getAppSecret(key) {
|
|
|
1821
1871
|
try {
|
|
1822
1872
|
const client = createServerClient();
|
|
1823
1873
|
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) {
|
|
1874
|
+
if (!noCache && result) {
|
|
1832
1875
|
secretsCache[key] = {
|
|
1833
|
-
value: result.
|
|
1834
|
-
version: result.
|
|
1876
|
+
value: result.value,
|
|
1877
|
+
version: result.version,
|
|
1835
1878
|
cachedAt: Date.now()
|
|
1836
1879
|
};
|
|
1837
1880
|
}
|
|
1838
|
-
return result
|
|
1881
|
+
return result?.value;
|
|
1839
1882
|
} catch (error) {
|
|
1883
|
+
if (error instanceof ScaleMuleApiError && error.code === "SECRET_NOT_FOUND") {
|
|
1884
|
+
return void 0;
|
|
1885
|
+
}
|
|
1840
1886
|
console.error(`[ScaleMule Secrets] Error fetching ${key}:`, error);
|
|
1841
1887
|
return void 0;
|
|
1842
1888
|
}
|
|
@@ -1884,24 +1930,20 @@ async function getBundle(key, resolve = true) {
|
|
|
1884
1930
|
try {
|
|
1885
1931
|
const client = createServerClient();
|
|
1886
1932
|
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) {
|
|
1933
|
+
if (!noCache && result) {
|
|
1895
1934
|
bundlesCache[key] = {
|
|
1896
|
-
type: result.
|
|
1897
|
-
data: result.data
|
|
1898
|
-
version: result.
|
|
1899
|
-
inheritsFrom: result.
|
|
1935
|
+
type: result.type,
|
|
1936
|
+
data: result.data,
|
|
1937
|
+
version: result.version,
|
|
1938
|
+
inheritsFrom: result.inherits_from,
|
|
1900
1939
|
cachedAt: Date.now()
|
|
1901
1940
|
};
|
|
1902
1941
|
}
|
|
1903
|
-
return result
|
|
1942
|
+
return result?.data;
|
|
1904
1943
|
} catch (error) {
|
|
1944
|
+
if (error instanceof ScaleMuleApiError && error.code === "BUNDLE_NOT_FOUND") {
|
|
1945
|
+
return void 0;
|
|
1946
|
+
}
|
|
1905
1947
|
console.error(`[ScaleMule Bundles] Error fetching ${key}:`, error);
|
|
1906
1948
|
return void 0;
|
|
1907
1949
|
}
|