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