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