diff --git a/src/lib/ae_api/api_delete_object.ts b/src/lib/ae_api/api_delete_object.ts index 7cbcff3a..08d8cefd 100644 --- a/src/lib/ae_api/api_delete_object.ts +++ b/src/lib/ae_api/api_delete_object.ts @@ -1,63 +1,133 @@ -import axios from 'axios'; +// import axios from 'axios'; // Updated 2024-05-23 export let delete_object = async function delete_object( { - api_cfg=null, - endpoint='', - params={}, - data={}, - return_meta=false, - log_lvl=0 + api_cfg = null, + endpoint = '', + params = {}, + data = {}, + return_meta = false, + log_lvl = 0, + retry_count = 5 // Number of retry attempts }: { api_cfg: any, endpoint: string, params?: any, data?: any, return_meta?: boolean, - log_lvl?: number + log_lvl?: number, + retry_count?: number } ) { -console.log('*** delete_object() ***'); + if (log_lvl) { + console.log(`*** delete_object() *** Endpoint: ${endpoint}`); + console.log('Params:', params); + if (log_lvl > 1) { + console.log('Data:', 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 = {}; + for (const prop in api_cfg['headers']) { + let prop_cleaned = prop.replaceAll('_', '-'); + headers_cleaned[prop_cleaned] = api_cfg['headers'][prop]; + } -if (log_lvl) { - // console.log(api_cfg); - console.log(endpoint); - console.log(params); if (log_lvl > 1) { - console.log('Data:', data); - console.log(typeof data); + console.log('Cleaned Headers:', headers_cleaned); } - // console.log(return_meta); - // console.log(as_list); -} -// https://stackoverflow.com/questions/51069552/axios-delete-request-with-body-and-headers + const fetchOptions: RequestInit = { + method: 'DELETE', + headers: { + ...headers_cleaned, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }; -let axios_api = axios.create({ - baseURL: api_cfg['base_url'], - // timeout: 2000, - /* other custom settings */ -}); -axios_api.defaults.headers = api_cfg['headers']; - -//OLD: axios_api.delete(endpoint, { 'data': data }) -let response_data = await axios_api.delete(endpoint, { params: params, 'data': data }) -.then(function (response) { - console.log(response.data); - return response.data; -}) -.catch(function (error) { - if (error.response && error.response.status === 404) { - return null; // Returning null since there were no results + if (log_lvl > 1) { + console.log('Fetch Options:', fetchOptions); } - console.log(error); - return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. - // return error; -}); -if (log_lvl > 1) { - console.log(response_data); -} -return response_data; + 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) { + console.error(`API DELETE 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})`); + } + } + } + + + // https://stackoverflow.com/questions/51069552/axios-delete-request-with-body-and-headers + + // let axios_api = axios.create({ + // baseURL: api_cfg['base_url'], + // // timeout: 2000, + // /* other custom settings */ + // }); + // axios_api.defaults.headers = api_cfg['headers']; + + // //OLD: axios_api.delete(endpoint, { 'data': data }) + // let response_data = await axios_api.delete(endpoint, { params: params, 'data': data }) + // .then(function (response) { + // console.log(response.data); + // return response.data; + // }) + // .catch(function (error) { + // if (error.response && error.response.status === 404) { + // return null; // Returning null since there were no results + // } + // console.log(error); + // return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. + // // return error; + // }); + + // if (log_lvl > 1) { + // console.log(response_data); + // } + // return response_data; } diff --git a/src/lib/ae_api/api_get_object.ts b/src/lib/ae_api/api_get_object.ts index d05ce891..e3ec3505 100644 --- a/src/lib/ae_api/api_get_object.ts +++ b/src/lib/ae_api/api_get_object.ts @@ -189,12 +189,17 @@ export let get_object = async function get_object( } } } catch (error) { - // if (log_lvl) { - console.log(`API GET object request *fetch* error on attempt ${attempt}:`, error); - // } + console.log(`API GET object request *fetch* error on attempt ${attempt}:`, error); + if (attempt === retry_count) { + console.log('Max retry attempts reached. Returning false.'); return false; } + + // Log retry information + if (log_lvl) { + console.log(`Retrying... (${attempt}/${retry_count})`); + } } } }; \ No newline at end of file diff --git a/src/lib/ae_api/api_patch_object.ts b/src/lib/ae_api/api_patch_object.ts index a0771d0d..9ac6edc9 100644 --- a/src/lib/ae_api/api_patch_object.ts +++ b/src/lib/ae_api/api_patch_object.ts @@ -86,9 +86,7 @@ export let patch_object = async function patch_object( // 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); - // } + console.error(`API PATCH error on attempt ${attempt}:`, error); // If this is the last attempt, return false if (attempt === retry_count) { diff --git a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte index 9128b877..cd2f0794 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte @@ -1110,9 +1110,9 @@ function handle_cut_string(old_string: string) { console.error('Error deleting journal entry:', error); alert('Failed to delete journal entry.'); }).finally(() => { - $journals_slct.journal_id = null; - $journals_slct.journal_obj = null; - goto(`/journals/${$lq__journal_entry_obj?.journal_id}`); + goto(`/journals/${$journals_slct.journal_id}`); + // $journals_slct.journal_id = null; + // $journals_slct.journal_obj = null; }); } }}