More code clean up

This commit is contained in:
Scott Idem
2024-05-23 19:45:17 -04:00
parent 0005ba7dc9
commit d9ff625db6
3 changed files with 136 additions and 104 deletions

View File

@@ -2,6 +2,7 @@
// This is intended to be used with the Aether API. The goal is to just import all of the core functions and re-export them as a single module. This is to make it easier to import the functions into other modules. // This is intended to be used with the Aether API. The goal is to just import all of the core functions and re-export them as a single module. This is to make it easier to import the functions into other modules.
// This needs to be cleaned up and ideally removed the need for Axios. // This needs to be cleaned up and ideally removed the need for Axios.
import type { key_val } from '$lib/ae_stores';
import { delete_object } from './api_delete_object'; // Exported at the end of this file import { delete_object } from './api_delete_object'; // Exported at the end of this file
import { get_object } from './api_get_object'; // Exported at the end of this file import { get_object } from './api_get_object'; // Exported at the end of this file
import { patch_object } from './api_patch_object'; // Exported at the end of this file import { patch_object } from './api_patch_object'; // Exported at the end of this file
@@ -9,26 +10,49 @@ import { post_object } from './api_post_object'; // Exported at the end of this
// Updated 2023-12-01 // Updated 2023-12-01
export let get_ae_obj_id_crud = async function get_ae_obj_id_crud({ export let get_ae_obj_id_crud = async function get_ae_obj_id_crud(
api_cfg, {
no_account_id=false, api_cfg,
obj_type, no_account_id=false,
obj_id, obj_type,
use_alt_table=false, obj_id,
use_alt_base=false, use_alt_table=false,
inc={}, use_alt_base=false,
enabled='enabled', inc={},
hidden='not_hidden', enabled='enabled',
limit=999999, hidden='not_hidden',
offset=0, limit=999999,
data={}, offset=0,
// key, data={},
jwt=null, // key,
headers={}, jwt=null,
params={}, headers={},
timeout=25000, params={},
return_meta=false, timeout=25000,
log_lvl=0}) { return_meta=false,
log_lvl=0
} : {
api_cfg: any,
no_account_id?: boolean,
obj_type: string,
obj_id: string,
use_alt_table?: boolean,
use_alt_base?: boolean,
inc?: any,
enabled?: string,
hidden?: string,
limit?: number,
offset?: number,
data?: any,
// key: string,
jwt?: string,
headers?: any,
params?: any,
timeout?: number,
return_meta?: boolean,
log_lvl?: number
}
) {
if (log_lvl) { if (log_lvl) {
console.log('*** get_ae_obj_id_crud() ***'); console.log('*** get_ae_obj_id_crud() ***');
} }
@@ -157,28 +181,51 @@ export let get_ae_obj_id_crud = async function get_ae_obj_id_crud({
// Updated 2023-11-15 // Updated 2023-11-15
export let get_ae_obj_li_for_obj_id_crud = async function get_ae_obj_li_for_obj_id_crud({ export let get_ae_obj_li_for_obj_id_crud = async function get_ae_obj_li_for_obj_id_crud(
api_cfg, {
obj_type, api_cfg,
for_obj_type, obj_type,
for_obj_id=null, // NOTE: Changed 2023-12-06 to no longer required for_obj_type,
use_alt_table=false, for_obj_id=null, // NOTE: Changed 2023-12-06 to no longer required
use_alt_base=false, use_alt_table=false,
inc={}, use_alt_base=false,
enabled='enabled', inc={},
hidden='not_hidden', enabled='enabled',
order_by_li=null, hidden='not_hidden',
limit=999999, order_by_li=null,
offset=0, limit=999999,
key, offset=0,
jwt=null, key,
headers={}, jwt=null,
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" } } headers={},
// 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_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" } }
params={}, // 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.
return_meta=false, params={},
log_lvl=0 return_meta=false,
}) { log_lvl=1
} : {
api_cfg: any,
obj_type: string,
for_obj_type: string,
for_obj_id?: string,
use_alt_table?: boolean,
use_alt_base?: boolean,
inc?: any,
enabled?: string,
hidden?: string,
order_by_li?: any,
limit?: number,
offset?: number,
key: string,
jwt?: string,
headers?: any,
params_json?: any,
// json_obj?: any,
params?: any,
return_meta?: boolean,
log_lvl?: number
}
) {
if (log_lvl) { if (log_lvl) {
console.log('*** get_ae_obj_li_for_obj_id_crud() ***'); console.log('*** get_ae_obj_li_for_obj_id_crud() ***');
} }
@@ -926,59 +973,60 @@ export let send_email = async function send_email({api_cfg, from_email, from_nam
/* END: Utility: Email Related */ /* END: Utility: Email Related */
function resolved(result: any) { // function resolved(result: any) {
console.log('Resolved'); // console.log('Resolved');
} // }
function rejected(result: any) { // function rejected(result: any) {
console.error(result); // console.error(result);
} // }
const parse_params = (params) => { // const parse_params = (params) => {
const keys = Object.keys(params); // const keys = Object.keys(params);
let options = ''; // let options = '';
keys.forEach((key) => { // keys.forEach((key) => {
const isParamTypeObject = typeof params[key] === 'object'; // const isParamTypeObject = typeof params[key] === 'object';
const isParamTypeArray = isParamTypeObject && (params[key].length >= 0); // const isParamTypeArray = isParamTypeObject && (params[key].length >= 0);
if (!isParamTypeObject) { // if (!isParamTypeObject) {
let encoded_value = encodeURIComponent(params[key]); // let encoded_value = encodeURIComponent(params[key]);
options += `${key}=${encoded_value}&`; // options += `${key}=${encoded_value}&`;
// options += `${key}=${params[key]}&`; // // options += `${key}=${params[key]}&`;
} // }
if (isParamTypeObject && isParamTypeArray) { // if (isParamTypeObject && isParamTypeArray) {
params[key].forEach((element) => { // params[key].forEach((element) => {
let encoded_value = encodeURIComponent(element); // let encoded_value = encodeURIComponent(element);
options += `${key}=${encoded_value}&`; // options += `${key}=${encoded_value}&`;
// options += `${key}=${element}&`; // // options += `${key}=${element}&`;
}); // });
} // }
}); // });
return options ? options.slice(0, -1) : options; // return options ? options.slice(0, -1) : options;
}; // };
// export let test = async function test() { // export let test = async function test() {
// console.log('Exported test function from API.'); // console.log('Exported test function from API.');
// } // }
let obj = {}; let obj = {
obj.delete_object = delete_object; delete_object: delete_object,
obj.get_object = get_object; get_object: get_object,
obj.patch_object = patch_object; patch_object: patch_object,
obj.post_object = post_object; post_object: post_object,
obj.get_ae_obj_id_crud = get_ae_obj_id_crud; get_ae_obj_id_crud: get_ae_obj_id_crud,
obj.get_ae_obj_li_for_obj_id_crud = get_ae_obj_li_for_obj_id_crud; get_ae_obj_li_for_obj_id_crud: get_ae_obj_li_for_obj_id_crud,
obj.create_ae_obj_crud = create_ae_obj_crud; create_ae_obj_crud: create_ae_obj_crud,
obj.update_ae_obj_id_crud = update_ae_obj_id_crud; update_ae_obj_id_crud: update_ae_obj_id_crud,
obj.delete_ae_obj_id_crud = delete_ae_obj_id_crud; delete_ae_obj_id_crud: delete_ae_obj_id_crud,
obj.download_hosted_file = download_hosted_file; download_hosted_file: download_hosted_file,
obj.delete_hosted_file = delete_hosted_file; delete_hosted_file: delete_hosted_file,
obj.get_data_store_obj_w_code = get_data_store_obj_w_code; get_data_store_obj_w_code: get_data_store_obj_w_code,
obj.send_email = send_email; send_email: send_email,
// obj.test = test; // test: test;
}
export let api = obj; export let api = obj;
// module.exports = api; // module.exports = api;

View File

@@ -25,8 +25,8 @@ export let get_object = async function get_object(
auto_download=false, auto_download=false,
as_list=false, as_list=false,
// 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. // 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='get_object_x', task_id=crypto.randomUUID(),
log_lvl=0 log_lvl=1
} : { } : {
api_cfg: any, api_cfg: any,
endpoint: string, endpoint: string,
@@ -44,7 +44,7 @@ export let get_object = async function get_object(
} }
) { ) {
if (log_lvl) { if (log_lvl) {
console.log(`*** get_object() *** Endpoint: ${endpoint} Task ID: ${task_id}`); console.log(`*** get_object() *** Endpoint: ${endpoint} AE Task ID: ${task_id}`);
console.log('Params:', params); console.log('Params:', params);
if (log_lvl > 1) { if (log_lvl > 1) {
console.log('Data:', data); console.log('Data:', data);
@@ -110,21 +110,6 @@ export let get_object = async function get_object(
} }
if (!return_blob) { if (!return_blob) {
// let response_data_promise = await axios_api.get(
// endpoint,
// {
// params: params,
// 'paramsSerializer': params => parse_params(params),
// onDownloadProgress: (progressEvent) => {
// let percent_completed = Math.round(
// (progressEvent.loaded * 100) / progressEvent.total
// );
// console.log('GET Data Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed);
// temp_get_object_percent_completed = percent_completed;
// }
// }
// )
let response_data_promise = await axios_api.get( let response_data_promise = await axios_api.get(
endpoint, endpoint,
{ {
@@ -254,7 +239,6 @@ export let get_object = async function get_object(
endpoint, endpoint,
{ {
params: params, params: params,
// 'paramsSerializer': params => parse_params(params),
responseType: 'blob', responseType: 'blob',
onDownloadProgress: (progressEvent) => { onDownloadProgress: (progressEvent) => {
let percent_completed = Math.round( let percent_completed = Math.round(

View File

@@ -18,7 +18,7 @@ export let post_object = async function post_object(
filename='', filename='',
auto_download=false, auto_download=false,
// 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. // 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='post_object_x', task_id=crypto.randomUUID(),
log_lvl=0 log_lvl=0
} : { } : {
api_cfg: any, api_cfg: any,