Finally really starting to move things to v2 of the CRUD API.

This commit is contained in:
Scott Idem
2024-11-20 15:11:57 -05:00
parent fd602a46ac
commit 6b60c14159
9 changed files with 180 additions and 139 deletions

View File

@@ -24,7 +24,7 @@ export async function get_ae_obj_li_for_obj_id_crud_v2(
params_json = null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the API endpoint. Example: { "fulltext_search": { "default_qry_str": "Search string for default", "address_default_qry_str": "Search string for address", "contact_1_default_qry_str": "Search string for contact_1" } }
// json_obj = null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint.
params = {},
return_meta = false,
// return_meta = false,
log_lvl = 1
}: {
api_cfg: any,
@@ -46,7 +46,7 @@ export async function get_ae_obj_li_for_obj_id_crud_v2(
params_json?: any,
// json_obj?: any,
params?: key_val,
return_meta?: boolean,
// return_meta?: boolean,
log_lvl?: number
}
) {
@@ -225,7 +225,7 @@ export async function get_ae_obj_li_for_obj_id_crud_v2(
endpoint: endpoint,
headers: headers,
params: params,
return_meta: return_meta,
// return_meta: return_meta,
log_lvl: log_lvl
});

View File

@@ -6,8 +6,6 @@ export let get_blob_percent_completed = temp_get_blob_percent_completed;
export let temp_get_object_percent_completed = 0;
export let get_object_percent_completed = temp_get_object_percent_completed;
// This is now a complete re-write without Axios. This is a simple fetch API call.
// Updated 2024-11-20
export let get_object = async function get_object(
{
api_cfg = null,
@@ -23,7 +21,8 @@ export let get_object = async function get_object(
as_list = false, // Is this still really needed?
// The task_id value should be a random string that is unique to the task. This is used to identify the task in the message event.
task_id = crypto.randomUUID(),
log_lvl = 0
log_lvl = 0,
retry_count = 3 // Number of retry attempts
}: {
api_cfg: any,
endpoint: string,
@@ -37,7 +36,8 @@ export let get_object = async function get_object(
auto_download?: boolean,
as_list?: boolean,
task_id?: string,
log_lvl?: number
log_lvl?: number,
retry_count?: number
}
) {
if (log_lvl) {
@@ -64,19 +64,14 @@ export let get_object = async function get_object(
delete api_cfg['headers']['x-no-account-id'];
}
// console.log('Clean the headers. No _underscores_!')
// Clean the headers
let headers_cleaned: key_val = {};
for (const prop in headers) {
// No underscores allowed in the header parameters!
let prop_cleaned = prop.replaceAll('_', '-');
// The value must be a string for the header!
if (typeof headers[prop] != 'string') {
headers[prop] = JSON.stringify(headers[prop]);
}
headers_cleaned[prop_cleaned] = headers[prop];
if (log_lvl) {
console.log(`${prop_cleaned}: ${headers_cleaned[prop_cleaned]}`);
}
@@ -99,88 +94,92 @@ export let get_object = async function get_object(
console.log('Fetch options:', fetchOptions);
}
try {
const response = await fetch(url.toString(), fetchOptions);
clearTimeout(timeoutId);
for (let attempt = 1; attempt <= retry_count; attempt++) {
try {
const response = await fetch(url.toString(), fetchOptions);
clearTimeout(timeoutId);
if (log_lvl) {
console.log(`Response: status=${response.status} statusText=${response.statusText} url=${response.url}`);
}
if (!response.ok) {
if (response.status === 404) {
if (log_lvl) {
console.log('The response was a 404 not found "error". Returning null.');
}
return null;
if (log_lvl) {
console.log(`Response: status=${response.status} statusText=${response.statusText} url=${response.url} attempt=${attempt}`);
}
throw new Error(`HTTP error! status: ${response.status}`);
}
if (!return_blob) {
const json = await response.json();
if (log_lvl > 1) {
console.log('Response JSON:', json);
}
if (!Array.isArray(json.data) && as_list) {
return [json.data];
}
return json.data || json;
} else {
const reader = response.body?.getReader();
const contentLength = +response.headers.get('Content-Length')!;
let receivedLength = 0;
const chunks = [];
while (true) {
const { done, value } = await reader!.read();
if (done) break;
chunks.push(value);
receivedLength += value.length;
const percent_completed = Math.round((receivedLength * 100) / contentLength);
if (log_lvl > 1) {
console.log('GET Blob Progress:', percent_completed, 'Total:', contentLength, 'Loaded:', receivedLength, 'Percent Completed', percent_completed);
}
temp_get_blob_percent_completed = percent_completed;
try {
if (typeof window !== 'undefined') {
window.postMessage({
type: 'api_download_blob',
status: 'downloading',
task_id: task_id,
endpoint: endpoint,
filename: filename,
size_total: contentLength,
size_loaded: receivedLength,
percent_completed: percent_completed
}, '*');
if (!response.ok) {
if (response.status === 404) {
if (log_lvl) {
console.log('The response was a 404 not found "error". Returning null.');
}
} catch (e) {
console.error('Error posting message:', e);
return null;
}
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = new Blob(chunks);
if (auto_download) {
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', filename || 'download');
document.body.appendChild(link);
link.click();
link.remove();
return true;
if (!return_blob) {
const json = await response.json();
if (log_lvl > 1) {
console.log('Response JSON:', json);
}
if (!Array.isArray(json.data) && as_list) {
return [json.data];
}
return json.data || json;
} else {
return blob;
const reader = response.body?.getReader();
const contentLength = +response.headers.get('Content-Length')!;
let receivedLength = 0;
const chunks = [];
while (true) {
const { done, value } = await reader!.read();
if (done) break;
chunks.push(value);
receivedLength += value.length;
const percent_completed = Math.round((receivedLength * 100) / contentLength);
if (log_lvl > 1) {
console.log('GET Blob Progress:', percent_completed, 'Total:', contentLength, 'Loaded:', receivedLength, 'Percent Completed', percent_completed);
}
temp_get_blob_percent_completed = percent_completed;
try {
if (typeof window !== 'undefined') {
window.postMessage({
type: 'api_download_blob',
status: 'downloading',
task_id: task_id,
endpoint: endpoint,
filename: filename,
size_total: contentLength,
size_loaded: receivedLength,
percent_completed: percent_completed
}, '*');
}
} catch (e) {
console.error('Error posting message:', e);
}
}
const blob = new Blob(chunks);
if (auto_download) {
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', filename || 'download');
document.body.appendChild(link);
link.click();
link.remove();
return true;
} else {
return blob;
}
}
} catch (error) {
if (log_lvl) {
console.log(`Fetch error on attempt ${attempt}:`, error);
}
if (attempt === retry_count) {
return false;
}
}
} catch (error) {
if (log_lvl) {
console.log('Fetch error:', error);
}
return false;
}
};