@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/auth.js
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
var headers = require('next/headers');
|
|
4
4
|
require('next/server');
|
|
5
5
|
|
|
6
|
+
// src/types/index.ts
|
|
7
|
+
var ScaleMuleApiError = class extends Error {
|
|
8
|
+
constructor(error) {
|
|
9
|
+
super(error.message);
|
|
10
|
+
this.name = "ScaleMuleApiError";
|
|
11
|
+
this.code = error.code;
|
|
12
|
+
this.field = error.field;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
6
16
|
// src/server/context.ts
|
|
7
17
|
function buildClientContextHeaders(context) {
|
|
8
18
|
const headers = {};
|
|
@@ -420,22 +430,27 @@ var ScaleMuleServer = class {
|
|
|
420
430
|
headers,
|
|
421
431
|
body: formData
|
|
422
432
|
});
|
|
423
|
-
const
|
|
433
|
+
const text = await response.text();
|
|
434
|
+
let responseData = null;
|
|
435
|
+
try {
|
|
436
|
+
responseData = text ? JSON.parse(text) : null;
|
|
437
|
+
} catch {
|
|
438
|
+
}
|
|
424
439
|
if (!response.ok) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
};
|
|
440
|
+
throw new ScaleMuleApiError(
|
|
441
|
+
responseData?.error || { code: "UPLOAD_FAILED", message: text || "Upload failed" }
|
|
442
|
+
);
|
|
429
443
|
}
|
|
444
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
430
445
|
return data;
|
|
431
446
|
} catch (err) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
};
|
|
447
|
+
if (err instanceof ScaleMuleApiError) {
|
|
448
|
+
throw err;
|
|
449
|
+
}
|
|
450
|
+
throw new ScaleMuleApiError({
|
|
451
|
+
code: "UPLOAD_ERROR",
|
|
452
|
+
message: err instanceof Error ? err.message : "Upload failed"
|
|
453
|
+
});
|
|
439
454
|
}
|
|
440
455
|
}
|
|
441
456
|
};
|
|
@@ -458,7 +473,7 @@ var ScaleMuleServer = class {
|
|
|
458
473
|
* })
|
|
459
474
|
*
|
|
460
475
|
* // Store the secret for signature verification
|
|
461
|
-
* console.log('Webhook secret:', result.
|
|
476
|
+
* console.log('Webhook secret:', result.secret)
|
|
462
477
|
* ```
|
|
463
478
|
*/
|
|
464
479
|
create: async (data) => {
|
|
@@ -613,23 +628,29 @@ var ScaleMuleServer = class {
|
|
|
613
628
|
headers,
|
|
614
629
|
body: options.body ? JSON.stringify(options.body) : void 0
|
|
615
630
|
});
|
|
616
|
-
const
|
|
631
|
+
const text = await response.text();
|
|
632
|
+
let responseData = null;
|
|
633
|
+
try {
|
|
634
|
+
responseData = text ? JSON.parse(text) : null;
|
|
635
|
+
} catch {
|
|
636
|
+
}
|
|
617
637
|
if (!response.ok) {
|
|
618
|
-
const error =
|
|
638
|
+
const error = responseData?.error || {
|
|
619
639
|
code: `HTTP_${response.status}`,
|
|
620
|
-
message:
|
|
640
|
+
message: responseData?.message || text || response.statusText
|
|
621
641
|
};
|
|
622
|
-
|
|
642
|
+
throw new ScaleMuleApiError(error);
|
|
623
643
|
}
|
|
644
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
624
645
|
return data;
|
|
625
646
|
} catch (err) {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
};
|
|
647
|
+
if (err instanceof ScaleMuleApiError) {
|
|
648
|
+
throw err;
|
|
649
|
+
}
|
|
650
|
+
throw new ScaleMuleApiError({
|
|
651
|
+
code: "SERVER_ERROR",
|
|
652
|
+
message: err instanceof Error ? err.message : "Request failed"
|
|
653
|
+
});
|
|
633
654
|
}
|
|
634
655
|
}
|
|
635
656
|
};
|
|
@@ -795,18 +816,21 @@ function createAuthRoutes(config = {}) {
|
|
|
795
816
|
if (!email || !password) {
|
|
796
817
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
797
818
|
}
|
|
798
|
-
|
|
799
|
-
|
|
819
|
+
let registeredUser;
|
|
820
|
+
try {
|
|
821
|
+
registeredUser = await sm.auth.register({ email, password, full_name, username, phone });
|
|
822
|
+
} catch (err) {
|
|
823
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
800
824
|
return errorResponse(
|
|
801
|
-
|
|
802
|
-
|
|
825
|
+
apiErr?.code || "REGISTER_FAILED",
|
|
826
|
+
apiErr?.message || "Registration failed",
|
|
803
827
|
400
|
|
804
828
|
);
|
|
805
829
|
}
|
|
806
|
-
if (config.onRegister
|
|
807
|
-
await config.onRegister({ id:
|
|
830
|
+
if (config.onRegister) {
|
|
831
|
+
await config.onRegister({ id: registeredUser.id, email: registeredUser.email });
|
|
808
832
|
}
|
|
809
|
-
return successResponse({ user:
|
|
833
|
+
return successResponse({ user: registeredUser, message: "Registration successful" }, 201);
|
|
810
834
|
}
|
|
811
835
|
// ==================== Login ====================
|
|
812
836
|
case "login": {
|
|
@@ -814,9 +838,12 @@ function createAuthRoutes(config = {}) {
|
|
|
814
838
|
if (!email || !password) {
|
|
815
839
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
816
840
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
841
|
+
let loginData;
|
|
842
|
+
try {
|
|
843
|
+
loginData = await sm.auth.login({ email, password, remember_me });
|
|
844
|
+
} catch (err) {
|
|
845
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
846
|
+
const errorCode = apiErr?.code || "LOGIN_FAILED";
|
|
820
847
|
let status = 400;
|
|
821
848
|
if (errorCode === "INVALID_CREDENTIALS" || errorCode === "UNAUTHORIZED") status = 401;
|
|
822
849
|
if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED", "MFA_REQUIRED"].includes(errorCode)) {
|
|
@@ -824,17 +851,17 @@ function createAuthRoutes(config = {}) {
|
|
|
824
851
|
}
|
|
825
852
|
return errorResponse(
|
|
826
853
|
errorCode,
|
|
827
|
-
|
|
854
|
+
apiErr?.message || "Login failed",
|
|
828
855
|
status
|
|
829
856
|
);
|
|
830
857
|
}
|
|
831
858
|
if (config.onLogin) {
|
|
832
859
|
await config.onLogin({
|
|
833
|
-
id:
|
|
834
|
-
email:
|
|
860
|
+
id: loginData.user.id,
|
|
861
|
+
email: loginData.user.email
|
|
835
862
|
});
|
|
836
863
|
}
|
|
837
|
-
return withSession(
|
|
864
|
+
return withSession(loginData, { user: loginData.user }, cookieOptions);
|
|
838
865
|
}
|
|
839
866
|
// ==================== Logout ====================
|
|
840
867
|
case "logout": {
|
|
@@ -862,11 +889,13 @@ function createAuthRoutes(config = {}) {
|
|
|
862
889
|
if (!token || !new_password) {
|
|
863
890
|
return errorResponse("VALIDATION_ERROR", "Token and new password required", 400);
|
|
864
891
|
}
|
|
865
|
-
|
|
866
|
-
|
|
892
|
+
try {
|
|
893
|
+
await sm.auth.resetPassword(token, new_password);
|
|
894
|
+
} catch (err) {
|
|
895
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
867
896
|
return errorResponse(
|
|
868
|
-
|
|
869
|
-
|
|
897
|
+
apiErr?.code || "RESET_FAILED",
|
|
898
|
+
apiErr?.message || "Password reset failed",
|
|
870
899
|
400
|
|
871
900
|
);
|
|
872
901
|
}
|
|
@@ -878,11 +907,13 @@ function createAuthRoutes(config = {}) {
|
|
|
878
907
|
if (!token) {
|
|
879
908
|
return errorResponse("VALIDATION_ERROR", "Token required", 400);
|
|
880
909
|
}
|
|
881
|
-
|
|
882
|
-
|
|
910
|
+
try {
|
|
911
|
+
await sm.auth.verifyEmail(token);
|
|
912
|
+
} catch (err) {
|
|
913
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
883
914
|
return errorResponse(
|
|
884
|
-
|
|
885
|
-
|
|
915
|
+
apiErr?.code || "VERIFY_FAILED",
|
|
916
|
+
apiErr?.message || "Email verification failed",
|
|
886
917
|
400
|
|
887
918
|
);
|
|
888
919
|
}
|
|
@@ -894,12 +925,14 @@ function createAuthRoutes(config = {}) {
|
|
|
894
925
|
const { email } = body;
|
|
895
926
|
const session = await getSession();
|
|
896
927
|
if (email) {
|
|
897
|
-
|
|
898
|
-
|
|
928
|
+
try {
|
|
929
|
+
await sm.auth.resendVerification(email);
|
|
930
|
+
} catch (err) {
|
|
931
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
899
932
|
return errorResponse(
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
933
|
+
apiErr?.code || "RESEND_FAILED",
|
|
934
|
+
apiErr?.message || "Failed to resend verification",
|
|
935
|
+
apiErr?.code === "RATE_LIMITED" ? 429 : 400
|
|
903
936
|
);
|
|
904
937
|
}
|
|
905
938
|
return successResponse({ message: "Verification email sent" });
|
|
@@ -907,11 +940,13 @@ function createAuthRoutes(config = {}) {
|
|
|
907
940
|
if (!session) {
|
|
908
941
|
return errorResponse("UNAUTHORIZED", "Email or session required", 401);
|
|
909
942
|
}
|
|
910
|
-
|
|
911
|
-
|
|
943
|
+
try {
|
|
944
|
+
await sm.auth.resendVerification(session.sessionToken);
|
|
945
|
+
} catch (err) {
|
|
946
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
912
947
|
return errorResponse(
|
|
913
|
-
|
|
914
|
-
|
|
948
|
+
apiErr?.code || "RESEND_FAILED",
|
|
949
|
+
apiErr?.message || "Failed to resend verification",
|
|
915
950
|
400
|
|
916
951
|
);
|
|
917
952
|
}
|
|
@@ -923,15 +958,17 @@ function createAuthRoutes(config = {}) {
|
|
|
923
958
|
if (!session) {
|
|
924
959
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
925
960
|
}
|
|
926
|
-
|
|
927
|
-
|
|
961
|
+
let refreshData;
|
|
962
|
+
try {
|
|
963
|
+
refreshData = await sm.auth.refresh(session.sessionToken);
|
|
964
|
+
} catch {
|
|
928
965
|
return clearSession(
|
|
929
966
|
{ message: "Session expired" },
|
|
930
967
|
cookieOptions
|
|
931
968
|
);
|
|
932
969
|
}
|
|
933
970
|
return withRefreshedSession(
|
|
934
|
-
|
|
971
|
+
refreshData.session_token,
|
|
935
972
|
session.userId,
|
|
936
973
|
{ message: "Session refreshed" },
|
|
937
974
|
cookieOptions
|
|
@@ -947,15 +984,17 @@ function createAuthRoutes(config = {}) {
|
|
|
947
984
|
if (!current_password || !new_password) {
|
|
948
985
|
return errorResponse("VALIDATION_ERROR", "Current and new password required", 400);
|
|
949
986
|
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
987
|
+
try {
|
|
988
|
+
await sm.user.changePassword(
|
|
989
|
+
session.sessionToken,
|
|
990
|
+
current_password,
|
|
991
|
+
new_password
|
|
992
|
+
);
|
|
993
|
+
} catch (err) {
|
|
994
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
956
995
|
return errorResponse(
|
|
957
|
-
|
|
958
|
-
|
|
996
|
+
apiErr?.code || "CHANGE_FAILED",
|
|
997
|
+
apiErr?.message || "Failed to change password",
|
|
959
998
|
400
|
|
960
999
|
);
|
|
961
1000
|
}
|
|
@@ -980,14 +1019,16 @@ function createAuthRoutes(config = {}) {
|
|
|
980
1019
|
if (!session) {
|
|
981
1020
|
return errorResponse("UNAUTHORIZED", "Authentication required", 401);
|
|
982
1021
|
}
|
|
983
|
-
|
|
984
|
-
|
|
1022
|
+
let userData;
|
|
1023
|
+
try {
|
|
1024
|
+
userData = await sm.auth.me(session.sessionToken);
|
|
1025
|
+
} catch {
|
|
985
1026
|
return clearSession(
|
|
986
1027
|
{ error: { code: "SESSION_EXPIRED", message: "Session expired" } },
|
|
987
1028
|
cookieOptions
|
|
988
1029
|
);
|
|
989
1030
|
}
|
|
990
|
-
return successResponse({ user:
|
|
1031
|
+
return successResponse({ user: userData });
|
|
991
1032
|
}
|
|
992
1033
|
// ==================== Get Session Status ====================
|
|
993
1034
|
case "session": {
|
|
@@ -1022,11 +1063,13 @@ function createAuthRoutes(config = {}) {
|
|
|
1022
1063
|
if (!password) {
|
|
1023
1064
|
return errorResponse("VALIDATION_ERROR", "Password required", 400);
|
|
1024
1065
|
}
|
|
1025
|
-
|
|
1026
|
-
|
|
1066
|
+
try {
|
|
1067
|
+
await sm.user.deleteAccount(session.sessionToken, password);
|
|
1068
|
+
} catch (err) {
|
|
1069
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1027
1070
|
return errorResponse(
|
|
1028
|
-
|
|
1029
|
-
|
|
1071
|
+
apiErr?.code || "DELETE_FAILED",
|
|
1072
|
+
apiErr?.message || "Failed to delete account",
|
|
1030
1073
|
400
|
|
1031
1074
|
);
|
|
1032
1075
|
}
|
|
@@ -1054,15 +1097,18 @@ function createAuthRoutes(config = {}) {
|
|
|
1054
1097
|
}
|
|
1055
1098
|
const body = await request.json().catch(() => ({}));
|
|
1056
1099
|
const { full_name, avatar_url } = body;
|
|
1057
|
-
|
|
1058
|
-
|
|
1100
|
+
let updatedUser;
|
|
1101
|
+
try {
|
|
1102
|
+
updatedUser = await sm.user.update(session.sessionToken, { full_name, avatar_url });
|
|
1103
|
+
} catch (err) {
|
|
1104
|
+
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1059
1105
|
return errorResponse(
|
|
1060
|
-
|
|
1061
|
-
|
|
1106
|
+
apiErr?.code || "UPDATE_FAILED",
|
|
1107
|
+
apiErr?.message || "Failed to update profile",
|
|
1062
1108
|
400
|
|
1063
1109
|
);
|
|
1064
1110
|
}
|
|
1065
|
-
return successResponse({ user:
|
|
1111
|
+
return successResponse({ user: updatedUser });
|
|
1066
1112
|
}
|
|
1067
1113
|
default:
|
|
1068
1114
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|