botton navi...
This commit is contained in:
+39
-15
@@ -1,4 +1,4 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import axios, { AxiosRequestConfig, isAxiosError } from "axios";
|
||||
import * as Keychain from "react-native-keychain";
|
||||
|
||||
/**
|
||||
@@ -14,10 +14,19 @@ export async function apiCall<T>(
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig
|
||||
): Promise<T | null> {
|
||||
// --- Request Logging ---
|
||||
console.log("--- API Request ---");
|
||||
console.log(`[${method}] ${url}`);
|
||||
if (data) {
|
||||
console.log("Payload:", JSON.stringify(data, null, 2));
|
||||
}
|
||||
console.log("-------------------");
|
||||
|
||||
try {
|
||||
// Get saved credentials from Keychain
|
||||
const creds = await Keychain.getGenericPassword();
|
||||
console.log("hihihi", creds);
|
||||
console.log("Keychain Credentials Found:", creds); // Log if creds were found
|
||||
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
...(creds ? { Authorization: `Bearer ${creds.password}` } : {}),
|
||||
@@ -32,22 +41,37 @@ export async function apiCall<T>(
|
||||
headers,
|
||||
});
|
||||
|
||||
// --- Response Logging ---
|
||||
console.log(`--- API Response: [${response.status}] ${url} ---`);
|
||||
// Uncomment the line below to see the full response data in logs
|
||||
// console.log("Response Data:", JSON.stringify(response.data, null, 2));
|
||||
console.log("---------------------------------------");
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
if (error.response) {
|
||||
// Server responded with a status other than 2xx
|
||||
console.error(
|
||||
"API Error:",
|
||||
`Status: ${error.response.status}`,
|
||||
`Data: ${JSON.stringify(error.response.data)}`
|
||||
);
|
||||
} else if (error.request) {
|
||||
// Request was made but no response received
|
||||
console.error("API Error: No response received", error.request);
|
||||
console.error("--- API Error ---");
|
||||
console.error(`Request Failed: [${method}] ${url}`);
|
||||
|
||||
if (isAxiosError(error)) {
|
||||
if (error.response) {
|
||||
// Server responded with a status other than 2xx
|
||||
console.error(`Status: ${error.response.status}`);
|
||||
console.error("Data:", JSON.stringify(error.response.data, null, 2));
|
||||
console.error("Headers:", JSON.stringify(error.response.headers, null, 2));
|
||||
} else if (error.request) {
|
||||
// Request was made but no response received
|
||||
console.error("No response received. This could be a network issue or CORS problem.");
|
||||
} else {
|
||||
// Something else went wrong while setting up the request
|
||||
console.error("Error setting up request:", error.message);
|
||||
}
|
||||
} else {
|
||||
// Something else went wrong while setting up the request
|
||||
console.error("API Error:", error.message);
|
||||
// Non-Axios error
|
||||
console.error("An unexpected error occurred:", error.message);
|
||||
}
|
||||
return null; // return null so the caller can handle it
|
||||
console.error("-----------------");
|
||||
|
||||
// Re-throw the error so that the calling function can handle it if needed
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user