1
1
import axios , { AxiosResponse } from "axios" ;
2
+ import { NavigateFunction } from "react-router-dom" ;
2
3
3
4
export abstract class ApiHelper {
4
- static async get < T > ( path : string ) : Promise < T | null > {
5
+ // Add an optional navigate parameter
6
+ static async get < T > ( path : string , navigate ?: NavigateFunction ) : Promise < T | null > {
5
7
try {
6
8
const response : AxiosResponse < T > = await axios . get ( "/api/" + path , {
7
- timeout : 10000 // 10 seconds timeout
9
+ timeout : 10000 , // 10 seconds timeout
8
10
} ) ;
9
11
return response . data ;
10
12
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -13,14 +15,18 @@ export abstract class ApiHelper {
13
15
console . log ( "Unauthorized" ) ;
14
16
// Only redirect if we're not already on the login page
15
17
if ( ! window . location . pathname . includes ( "/login" ) ) {
16
- window . location . href = "/#/login" ;
18
+ if ( navigate ) {
19
+ navigate ( "/login" ) ;
20
+ } else {
21
+ window . location . href = "/#/login" ;
22
+ }
17
23
}
18
24
return null ;
19
25
}
20
26
if (
21
27
( error . response ?. status >= 500 && error . response ?. status < 600 ) ||
22
28
error . code === "ERR_CONNECTION_REFUSED" ||
23
- error . code === "ERR_CONNECTION_TIMED_OUT" ||
29
+ error . code === "ERR_CONNECTION_TIMED_OUT" ||
24
30
error . code === "ERR_CONNECTION_RESET" ||
25
31
error . code === "ECONNABORTED" ||
26
32
error . code === "ERR_NETWORK" ||
@@ -31,21 +37,29 @@ export abstract class ApiHelper {
31
37
console . log ( "Unable to connect to server" , {
32
38
code : error . code ,
33
39
message : error . message ,
34
- status : error . request ?. status
40
+ status : error . request ?. status ,
35
41
} ) ;
36
- // Force redirect using document.location for more reliable navigation
37
- document . location . href = "/#/system-unavailable" ;
42
+ // Use navigate when available, fallback to window.location
43
+ if ( navigate ) {
44
+ navigate ( "/system-unavailable" ) ;
45
+ } else {
46
+ window . location . href = "/#/system-unavailable" ;
47
+ }
38
48
return null ;
39
49
}
40
50
console . error ( "Error getting api " + path + ":" , error ) ;
41
51
return null ;
42
52
}
43
53
}
44
54
45
- static async post < T > ( path : string , data : unknown ) : Promise < T | null > {
55
+ static async post < T > (
56
+ path : string ,
57
+ data : unknown ,
58
+ navigate ?: NavigateFunction
59
+ ) : Promise < T | null > {
46
60
try {
47
61
const response : AxiosResponse < T > = await axios . post ( "/api/" + path , data , {
48
- timeout : 10000 // 10 seconds timeout
62
+ timeout : 10000 , // 10 seconds timeout
49
63
} ) ;
50
64
return response . data ;
51
65
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -54,14 +68,18 @@ export abstract class ApiHelper {
54
68
console . log ( "Unauthorized" ) ;
55
69
// Only redirect if we're not already on the login page
56
70
if ( ! window . location . pathname . includes ( "/login" ) ) {
57
- window . location . href = "/#/login" ;
71
+ if ( navigate ) {
72
+ navigate ( "/login" ) ;
73
+ } else {
74
+ window . location . href = "/#/login" ;
75
+ }
58
76
}
59
77
return null ;
60
78
}
61
79
if (
62
80
( error . response ?. status >= 500 && error . response ?. status < 600 ) ||
63
81
error . code === "ERR_CONNECTION_REFUSED" ||
64
- error . code === "ERR_CONNECTION_TIMED_OUT" ||
82
+ error . code === "ERR_CONNECTION_TIMED_OUT" ||
65
83
error . code === "ERR_CONNECTION_RESET" ||
66
84
error . code === "ECONNABORTED" ||
67
85
error . code === "ERR_NETWORK" ||
@@ -72,10 +90,14 @@ export abstract class ApiHelper {
72
90
console . log ( "Unable to connect to server" , {
73
91
code : error . code ,
74
92
message : error . message ,
75
- status : error . request ?. status
93
+ status : error . request ?. status ,
76
94
} ) ;
77
- // Force redirect using document.location for more reliable navigation
78
- document . location . href = "/#/system-unavailable" ;
95
+ // Use navigate when available, fallback to window.location
96
+ if ( navigate ) {
97
+ navigate ( "/system-unavailable" ) ;
98
+ } else {
99
+ window . location . href = "/#/system-unavailable" ;
100
+ }
79
101
return null ;
80
102
}
81
103
console . error ( "Error posting to api " + path + ":" , error ) ;
0 commit comments