@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.
@@ -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 data = await response.json();
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
- return {
426
- success: false,
427
- error: data.error || { code: "UPLOAD_FAILED", message: "Upload failed" }
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
- return {
433
- success: false,
434
- error: {
435
- code: "UPLOAD_ERROR",
436
- message: err instanceof Error ? err.message : "Upload failed"
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.data.secret)
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 data = await response.json();
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 = data.error || {
638
+ const error = responseData?.error || {
619
639
  code: `HTTP_${response.status}`,
620
- message: data.message || response.statusText
640
+ message: responseData?.message || text || response.statusText
621
641
  };
622
- return { success: false, error };
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
- return {
627
- success: false,
628
- error: {
629
- code: "SERVER_ERROR",
630
- message: err instanceof Error ? err.message : "Request failed"
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
- const result = await sm.auth.register({ email, password, full_name, username, phone });
799
- if (!result.success) {
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
- result.error?.code || "REGISTER_FAILED",
802
- result.error?.message || "Registration failed",
825
+ apiErr?.code || "REGISTER_FAILED",
826
+ apiErr?.message || "Registration failed",
803
827
  400
804
828
  );
805
829
  }
806
- if (config.onRegister && result.data) {
807
- await config.onRegister({ id: result.data.id, email: result.data.email });
830
+ if (config.onRegister) {
831
+ await config.onRegister({ id: registeredUser.id, email: registeredUser.email });
808
832
  }
809
- return successResponse({ user: result.data, message: "Registration successful" }, 201);
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
- const result = await sm.auth.login({ email, password, remember_me });
818
- if (!result.success || !result.data) {
819
- const errorCode = result.error?.code || "LOGIN_FAILED";
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
- result.error?.message || "Login failed",
854
+ apiErr?.message || "Login failed",
828
855
  status
829
856
  );
830
857
  }
831
858
  if (config.onLogin) {
832
859
  await config.onLogin({
833
- id: result.data.user.id,
834
- email: result.data.user.email
860
+ id: loginData.user.id,
861
+ email: loginData.user.email
835
862
  });
836
863
  }
837
- return withSession(result.data, { user: result.data.user }, cookieOptions);
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
- const result = await sm.auth.resetPassword(token, new_password);
866
- if (!result.success) {
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
- result.error?.code || "RESET_FAILED",
869
- result.error?.message || "Password reset failed",
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
- const result = await sm.auth.verifyEmail(token);
882
- if (!result.success) {
910
+ try {
911
+ await sm.auth.verifyEmail(token);
912
+ } catch (err) {
913
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
883
914
  return errorResponse(
884
- result.error?.code || "VERIFY_FAILED",
885
- result.error?.message || "Email verification failed",
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
- const result2 = await sm.auth.resendVerification(email);
898
- if (!result2.success) {
928
+ try {
929
+ await sm.auth.resendVerification(email);
930
+ } catch (err) {
931
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
899
932
  return errorResponse(
900
- result2.error?.code || "RESEND_FAILED",
901
- result2.error?.message || "Failed to resend verification",
902
- result2.error?.code === "RATE_LIMITED" ? 429 : 400
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
- const result = await sm.auth.resendVerification(session.sessionToken);
911
- if (!result.success) {
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
- result.error?.code || "RESEND_FAILED",
914
- result.error?.message || "Failed to resend verification",
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
- const result = await sm.auth.refresh(session.sessionToken);
927
- if (!result.success || !result.data) {
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
- result.data.session_token,
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
- const result = await sm.user.changePassword(
951
- session.sessionToken,
952
- current_password,
953
- new_password
954
- );
955
- if (!result.success) {
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
- result.error?.code || "CHANGE_FAILED",
958
- result.error?.message || "Failed to change password",
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
- const result = await sm.auth.me(session.sessionToken);
984
- if (!result.success || !result.data) {
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: result.data });
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
- const result = await sm.user.deleteAccount(session.sessionToken, password);
1026
- if (!result.success) {
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
- result.error?.code || "DELETE_FAILED",
1029
- result.error?.message || "Failed to delete account",
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
- const result = await sm.user.update(session.sessionToken, { full_name, avatar_url });
1058
- if (!result.success || !result.data) {
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
- result.error?.code || "UPDATE_FAILED",
1061
- result.error?.message || "Failed to update profile",
1106
+ apiErr?.code || "UPDATE_FAILED",
1107
+ apiErr?.message || "Failed to update profile",
1062
1108
  400
1063
1109
  );
1064
1110
  }
1065
- return successResponse({ user: result.data });
1111
+ return successResponse({ user: updatedUser });
1066
1112
  }
1067
1113
  default:
1068
1114
  return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);