@@ -23,17 +23,43 @@ import { IRequest } from "../typings/requestOptions";
23
23
async function getJsonResponse < T > ( resource : Resource , jsonRequest : T | string , requestOptions ?: IRequest . Options ) : Promise < string > ;
24
24
async function getJsonResponse < T , R > ( resource : Resource , jsonRequest : T | string , requestOptions ?: IRequest . Options ) : Promise < R > ;
25
25
26
+ /**
27
+ * Makes the API call and returns the parsed JSON response.
28
+ *
29
+ * @template T - The type of the request payload.
30
+ * @template R - The expected type of the parsed JSON response.
31
+ * @param resource - The API resource responsible for handling the request.
32
+ * @param jsonRequest - The request payload, either as an object or a JSON string.
33
+ * @param requestOptions - Optional request options to customize the request.
34
+ * @returns A promise that resolves to the parsed JSON response of type `R`, or the string "ok" for TerminalAPI responses.
35
+ */
26
36
async function getJsonResponse < T , R > (
27
37
resource : Resource ,
28
38
jsonRequest : T | string ,
29
39
requestOptions : IRequest . Options = { } ,
30
40
) : Promise < R | string > {
31
41
const request = typeof jsonRequest === "string" ? jsonRequest : JSON . stringify ( jsonRequest ) ;
32
42
const response = await resource . request ( request , requestOptions ) ;
43
+
44
+ if ( ! response ) {
45
+ return "" as string ;
46
+ }
47
+
48
+ if ( typeof response !== "string" ) {
49
+ return response ;
50
+ }
51
+
52
+ if ( response === "ok" ) {
53
+ // handling TerminalAPI responses
54
+ return response ;
55
+ }
56
+
33
57
try {
34
- return typeof response === "string" ? JSON . parse ( response ) : response ;
58
+ return JSON . parse ( response ) ;
35
59
} catch ( e ) {
36
- return response ;
60
+ console . warn ( "Unexpected error in getJsonResponse:" , ( e as Error ) . message ) ;
61
+ return response ; // or: return response as R | string;
37
62
}
38
63
}
64
+
39
65
export default getJsonResponse ;
0 commit comments