General clean up. Less debug. Things work better?

This commit is contained in:
Scott Idem
2024-03-29 12:15:50 -04:00
parent c0e1d666f4
commit 0e26765312
10 changed files with 181 additions and 95 deletions

View File

@@ -59,13 +59,37 @@ export let temp_get_object_percent_completed = 0;
export let get_object_percent_completed = temp_get_object_percent_completed;
// Updated 2022-10-28
export let get_object = async function get_object({api_cfg, endpoint='', headers={}, params={}, data={}, timeout=60000, return_meta=false, return_blob=false, filename=null, auto_download=false, as_list=false, log_lvl=0}) {
export let get_object = async function get_object(
{
api_cfg,
endpoint='',
headers={},
params={},
data={},
timeout=60000,
return_meta=false,
return_blob=false,
filename=null,
auto_download=false,
as_list=false,
log_lvl=0
// } : {
// api_cfg: any,
// endpoint: string,
// headers?: any,
// params?: any,
// data?: any,
// timeout?: number,
// return_meta?: boolean,
// return_blob?: boolean,
// filename?: string,
// auto_download?: boolean,
// as_list?: boolean,
// log_lvl?: number
}
) {
if (log_lvl) {
console.log('*** get_object() ***');
}
if (log_lvl) {
console.log(`Endpoint: ${endpoint}`);
console.log(`*** get_object() *** Endpoint: ${endpoint}`);
console.log('Params:', params);
if (log_lvl > 1) {
console.log('Data:', data);
@@ -164,6 +188,9 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
)
.then(function (response) {
if (log_lvl) {
console.log(`GET Response: status=${response.status} statusText=${response.statusText} baseURL=${response.config.baseURL} url=${response.config.url} method=${response.config.method} headers=${response.config.headers} params=${response.config.params}`);
}
if (log_lvl > 1) {
console.log('GET Response:', response);
}
if (!Array.isArray(response.data['data']) && as_list) {
@@ -285,12 +312,15 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
)
.then(function (response) {
if (log_lvl) {
console.log(response);
console.log(`GET (blob) Response: status=${response.status} statusText=${response.statusText} baseURL=${response.config.baseURL} url=${response.config.url} method=${response.config.method} headers=${response.config.headers} params=${response.config.params}`);
}
if (log_lvl > 1) {
console.log('GET (blob) Response:', response);
}
const { data, headers } = response
console.log(headers);
const { data, headers } = response;
// Careful if this download filename needs to be changed to a different file extension. The browser/client may not know how to handle it.
if (filename) {
} else if (headers['content-disposition']) {
filename = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1');
@@ -299,11 +329,13 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
}
if (auto_download) {
if (log_lvl) {
console.log(`Auto Download: ${filename}`);
}
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
// link.setAttribute('download', 'event_exhibit_tracking_export.xlsx'); //or any other extension
link.setAttribute('download', filename); //or any other extension
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
return true;
@@ -322,7 +354,7 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
return response_data_promise;
} else {
// This generally should not happen. It likely means the query was bad or an API issue.
console.log('Returning unknown. This should not happen in most cases.');
console.log('Returning (blob) unknown. This should not happen in most cases.');
Promise.reject(new Error('fail')).then(resolved, rejected);
}
}