generator-kodly-react-app 1.0.2 → 1.0.4
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/generators/app/index.js
CHANGED
|
@@ -10,12 +10,22 @@ export const client = axios.create({
|
|
|
10
10
|
},
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
// Add response interceptor to handle non-2xx responses
|
|
13
|
+
// Add response interceptor to handle non-2xx responses and network errors
|
|
14
14
|
client.interceptors.response.use(
|
|
15
|
-
(response) => response,
|
|
16
|
-
(error) => {
|
|
17
|
-
//
|
|
18
|
-
if (error
|
|
15
|
+
(response: any) => response,
|
|
16
|
+
(error: unknown) => {
|
|
17
|
+
// Handle all errors: non-2xx responses and network errors
|
|
18
|
+
if (error && typeof error === 'object' && 'response' in error) {
|
|
19
|
+
const axiosError = error as { response?: { status?: number }; code?: string };
|
|
20
|
+
if (axiosError.response && axiosError.response.status && axiosError.response.status >= 300) {
|
|
21
|
+
// Backend returned an error response
|
|
22
|
+
handleBackendError(error);
|
|
23
|
+
} else if (axiosError.code === 'ERR_NETWORK' || !axiosError.response) {
|
|
24
|
+
// Network error or no response (connection refused, timeout, etc.)
|
|
25
|
+
handleBackendError(error);
|
|
26
|
+
}
|
|
27
|
+
} else {
|
|
28
|
+
// Handle non-axios errors
|
|
19
29
|
handleBackendError(error);
|
|
20
30
|
}
|
|
21
31
|
return Promise.reject(error);
|
|
@@ -21,10 +21,12 @@ interface BackendErrorResponse {
|
|
|
21
21
|
* from the exception instead.
|
|
22
22
|
*/
|
|
23
23
|
export const handleBackendError = (error: unknown): void => {
|
|
24
|
-
|
|
24
|
+
// Check if it's an AxiosError
|
|
25
|
+
if (error instanceof Error && 'isAxiosError' in error) {
|
|
25
26
|
const axiosError = error as AxiosError<BackendErrorResponse>;
|
|
26
27
|
const response = axiosError.response;
|
|
27
28
|
|
|
29
|
+
// Handle errors with response (non-2xx status codes)
|
|
28
30
|
if (response) {
|
|
29
31
|
const errorData = response.data;
|
|
30
32
|
|
|
@@ -45,7 +47,14 @@ export const handleBackendError = (error: unknown): void => {
|
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
|
|
48
|
-
//
|
|
50
|
+
// Handle network errors (no response) - connection refused, timeout, etc.
|
|
51
|
+
if (axiosError.code === 'ERR_NETWORK' || !response) {
|
|
52
|
+
const networkErrorMessage = axiosError.message || 'Network error. Please check your connection and try again.';
|
|
53
|
+
toast.error(networkErrorMessage);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Fallback for other AxiosError cases
|
|
49
58
|
const errorMessage = axiosError.message || 'An error occurred';
|
|
50
59
|
toast.error(errorMessage);
|
|
51
60
|
return;
|
|
@@ -30,7 +30,7 @@ export const useLogin = () => {
|
|
|
30
30
|
const navigate = useNavigate();
|
|
31
31
|
return useMutation({
|
|
32
32
|
mutationFn: async (data: { email: string; password: string }) => {
|
|
33
|
-
const response = await client.post('/auth/login', data);
|
|
33
|
+
const response = await client.post('/auth/user/login', data);
|
|
34
34
|
return response.data;
|
|
35
35
|
},
|
|
36
36
|
onSuccess: (authData) => {
|
|
@@ -54,7 +54,7 @@ export const useValidateToken = () => {
|
|
|
54
54
|
setTokenInAxios(authToken);
|
|
55
55
|
return useMutation({
|
|
56
56
|
mutationFn: async () => {
|
|
57
|
-
const response = await client.
|
|
57
|
+
const response = await client.post('/auth/user/validate');
|
|
58
58
|
return response.data;
|
|
59
59
|
},
|
|
60
60
|
retry: false,
|
|
@@ -75,7 +75,7 @@ export const useLogout = () => {
|
|
|
75
75
|
const navigate = useNavigate();
|
|
76
76
|
return useMutation({
|
|
77
77
|
mutationFn: async () => {
|
|
78
|
-
await client.post('/auth/logout');
|
|
78
|
+
await client.post('/auth/user/logout');
|
|
79
79
|
},
|
|
80
80
|
onSuccess: () => {
|
|
81
81
|
setAuthToken('');
|
package/package.json
CHANGED