// 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, retry_count = 5 // Number of retry attempts }: { api_cfg: any, endpoint: string, params?: any, data?: any, return_meta?: boolean, log_lvl?: number, retry_count?: number } ) { 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 > 1) { console.log('Cleaned Headers:', headers_cleaned); } const fetchOptions: RequestInit = { method: 'DELETE', 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) { 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; }