Axios should not longer be needed.

This commit is contained in:
Scott Idem
2025-05-07 16:34:39 -04:00
parent cdc4ee1af9
commit 86221e6aec
3 changed files with 133 additions and 72 deletions

View File

@@ -189,9 +189,9 @@ export let get_object = async function get_object(
}
}
} catch (error) {
if (log_lvl) {
// if (log_lvl) {
console.log(`API GET object request *fetch* error on attempt ${attempt}:`, error);
}
// }
if (attempt === retry_count) {
return false;
}

View File

@@ -1,4 +1,4 @@
import axios from 'axios';
// import axios from 'axios';
// Updated 2024-05-23
export let patch_object = async function patch_object(
@@ -8,7 +8,7 @@ export let patch_object = async function patch_object(
params = {},
data = {},
return_meta = false,
log_lvl = 2,
log_lvl = 1,
retry_count = 5 // Number of retry attempts
}: {
api_cfg: any,
@@ -20,71 +20,132 @@ export let patch_object = async function patch_object(
retry_count?: number
}
) {
if (log_lvl) {
console.log(`*** patch_object() *** Endpoint: ${endpoint}`);
console.log('Params:', params);
if (log_lvl > 1) {
console.log('Data:', data);
}
}
if (log_lvl) {
// console.log(api_cfg);
console.log(endpoint);
console.log(params);
if (log_lvl > 1) {
console.log(data);
}
// console.log(return_meta);
// console.log(as_list);
}
if (!api_cfg) {
console.error('No API Config was provided. Returning false.');
return false;
}
let axios_api = axios.create({
baseURL: api_cfg['base_url'],
/* other custom settings */
});
axios_api.defaults.headers = api_cfg['headers'];
for (let attempt = 1; attempt <= retry_count; attempt++) {
try {
const response = await axios_api.patch(endpoint, data, { params: params });
if (log_lvl) {
console.log(`Response: status=${response.status} attempt=${attempt}`);
}
if (log_lvl) {
console.log(`*** patch_object() *** Endpoint: ${endpoint}`);
console.log('Params:', params);
if (log_lvl > 1) {
console.log('Response Data:', response.data);
}
// Return the response data
return response.data['data'];
} catch (error) {
if (log_lvl) {
console.error(`Error on attempt ${attempt}:`, error);
}
// Handle specific errors (e.g., 404)
if (error.response && error.response.status === 404) {
console.warn('404 Not Found. Returning null.');
return null; // Returning null since there were no results
}
// If this is the last attempt, return false
if (attempt === retry_count) {
console.error('Max retry attempts reached. Returning false.');
return false;
}
// Log retry information
if (log_lvl) {
console.log(`Retrying... (${attempt}/${retry_count})`);
console.log('Data:', data);
}
}
}
// return response_data;
if (!api_cfg) {
console.error('No API Config was provided. Returning false.');
return false;
}
// Construct the URL with query parameters
const url = new URL(endpoint, api_cfg['base_url']);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
// Clean the headers
let headers_cleaned: Record<string, string> = {};
for (const prop in api_cfg['headers']) {
let prop_cleaned = prop.replaceAll('_', '-');
headers_cleaned[prop_cleaned] = api_cfg['headers'][prop];
}
if (log_lvl > 1) {
console.log('Cleaned Headers:', headers_cleaned);
}
const fetchOptions: RequestInit = {
method: 'PATCH',
headers: {
...headers_cleaned,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
if (log_lvl > 1) {
console.log('Fetch Options:', fetchOptions);
}
for (let attempt = 1; attempt <= retry_count; attempt++) {
try {
const response = await fetch(url.toString(), fetchOptions);
if (log_lvl) {
console.log(`Response: status=${response.status} attempt=${attempt}`);
}
if (!response.ok) {
if (response.status === 404) {
console.warn('404 Not Found. Returning null.');
return null; // Returning null since there were no results
}
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
if (log_lvl > 1) {
console.log('Response JSON:', json);
}
// Return the response data or metadata
return return_meta ? json : json.data;
} catch (error) {
// if (log_lvl) {
console.error(`API PATCH error on attempt ${attempt}:`, error);
// }
// If this is the last attempt, return false
if (attempt === retry_count) {
console.error('Max retry attempts reached. Returning false.');
return false;
}
// Log retry information
if (log_lvl) {
console.log(`Retrying... (${attempt}/${retry_count})`);
}
}
}
// let axios_api = axios.create({
// baseURL: api_cfg['base_url'],
// /* other custom settings */
// });
// axios_api.defaults.headers = api_cfg['headers'];
// for (let attempt = 1; attempt <= retry_count; attempt++) {
// try {
// const response = await axios_api.patch(endpoint, data, { params: params });
// if (log_lvl) {
// console.log(`Response: status=${response.status} attempt=${attempt}`);
// }
// if (log_lvl > 1) {
// console.log('Response Data:', response.data);
// }
// // Return the response data
// return response.data['data'];
// } catch (error) {
// if (log_lvl) {
// console.error(`Error on attempt ${attempt}:`, error);
// }
// // Handle specific errors (e.g., 404)
// if (error.response && error.response.status === 404) {
// console.warn('404 Not Found. Returning null.');
// return null; // Returning null since there were no results
// }
// // If this is the last attempt, return false
// if (attempt === retry_count) {
// console.error('Max retry attempts reached. Returning false.');
// return false;
// }
// // Log retry information
// if (log_lvl) {
// console.log(`Retrying... (${attempt}/${retry_count})`);
// }
// }
// }
// return response_data;
}

View File

@@ -149,7 +149,7 @@ async function update_journal_entry() {
return;
}
log_lvl = 1;
// log_lvl = 1;
let data_kv: key_val = {
alert: tmp_entry_obj?.alert,
@@ -326,7 +326,7 @@ async function update_journal_entry() {
api_cfg: $ae_api,
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
data_kv: data_kv,
log_lvl: 0,
log_lvl: 1,
});
updated_obj = true;
updated_idb = false;
@@ -353,7 +353,7 @@ async function change_journal_id() {
api_cfg: $ae_api,
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
data_kv: data_kv,
log_lvl: 0,
log_lvl: log_lvl,
});
updated_obj = true;
updated_idb = false;
@@ -487,7 +487,7 @@ $effect(() => {
// }
async function handle_decrypt_string(encrypted_string: string, passcode: string) {
log_lvl = 1;
// log_lvl = 1;
if (log_lvl) {
console.log(`TEST: handle_decrypt_string: ${passcode}`, encrypted_string);
}
@@ -523,7 +523,7 @@ async function handle_decrypt_string(encrypted_string: string, passcode: string)
}
async function handle_encrypt_string(text_string: string, passcode: string) {
log_lvl = 1;
// log_lvl = 1;
if (log_lvl) {
console.log('TEST: handle_encrypt_string');
}