From d9ff625db6819d756ef229a6b07f63fdb3b78e1e Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Thu, 23 May 2024 19:45:17 -0400 Subject: [PATCH] More code clean up --- src/lib/api.ts | 216 ++++++++++++++++++++++--------------- src/lib/api_get_object.ts | 22 +--- src/lib/api_post_object.ts | 2 +- 3 files changed, 136 insertions(+), 104 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index cd897dd0..9ebc6816 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -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 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 { 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 @@ -9,26 +10,49 @@ import { post_object } from './api_post_object'; // Exported at the end of this // Updated 2023-12-01 -export let get_ae_obj_id_crud = async function get_ae_obj_id_crud({ - api_cfg, - no_account_id=false, - obj_type, - obj_id, - use_alt_table=false, - use_alt_base=false, - inc={}, - enabled='enabled', - hidden='not_hidden', - limit=999999, - offset=0, - data={}, - // key, - jwt=null, - headers={}, - params={}, - timeout=25000, - return_meta=false, - log_lvl=0}) { +export let get_ae_obj_id_crud = async function get_ae_obj_id_crud( + { + api_cfg, + no_account_id=false, + obj_type, + obj_id, + use_alt_table=false, + use_alt_base=false, + inc={}, + enabled='enabled', + hidden='not_hidden', + limit=999999, + offset=0, + data={}, + // key, + jwt=null, + headers={}, + params={}, + timeout=25000, + 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) { 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 -export let get_ae_obj_li_for_obj_id_crud = async function get_ae_obj_li_for_obj_id_crud({ - api_cfg, - obj_type, - for_obj_type, - for_obj_id=null, // NOTE: Changed 2023-12-06 to no longer required - use_alt_table=false, - use_alt_base=false, - inc={}, - enabled='enabled', - hidden='not_hidden', - order_by_li=null, - limit=999999, - offset=0, - key, - jwt=null, - headers={}, - 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, - log_lvl=0 - }) { +export let get_ae_obj_li_for_obj_id_crud = async function get_ae_obj_li_for_obj_id_crud( + { + api_cfg, + obj_type, + for_obj_type, + for_obj_id=null, // NOTE: Changed 2023-12-06 to no longer required + use_alt_table=false, + use_alt_base=false, + inc={}, + enabled='enabled', + hidden='not_hidden', + order_by_li=null, + limit=999999, + offset=0, + key, + jwt=null, + headers={}, + 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, + 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) { 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 */ -function resolved(result: any) { - console.log('Resolved'); -} +// function resolved(result: any) { +// console.log('Resolved'); +// } -function rejected(result: any) { - console.error(result); -} +// function rejected(result: any) { +// console.error(result); +// } -const parse_params = (params) => { - const keys = Object.keys(params); - let options = ''; +// const parse_params = (params) => { +// const keys = Object.keys(params); +// let options = ''; - keys.forEach((key) => { - const isParamTypeObject = typeof params[key] === 'object'; - const isParamTypeArray = isParamTypeObject && (params[key].length >= 0); +// keys.forEach((key) => { +// const isParamTypeObject = typeof params[key] === 'object'; +// const isParamTypeArray = isParamTypeObject && (params[key].length >= 0); - if (!isParamTypeObject) { - let encoded_value = encodeURIComponent(params[key]); - options += `${key}=${encoded_value}&`; - // options += `${key}=${params[key]}&`; - } +// if (!isParamTypeObject) { +// let encoded_value = encodeURIComponent(params[key]); +// options += `${key}=${encoded_value}&`; +// // options += `${key}=${params[key]}&`; +// } - if (isParamTypeObject && isParamTypeArray) { - params[key].forEach((element) => { - let encoded_value = encodeURIComponent(element); - options += `${key}=${encoded_value}&`; - // options += `${key}=${element}&`; - }); - } - }); +// if (isParamTypeObject && isParamTypeArray) { +// params[key].forEach((element) => { +// let encoded_value = encodeURIComponent(element); +// options += `${key}=${encoded_value}&`; +// // options += `${key}=${element}&`; +// }); +// } +// }); - return options ? options.slice(0, -1) : options; -}; +// return options ? options.slice(0, -1) : options; +// }; // export let test = async function test() { // console.log('Exported test function from API.'); // } -let obj = {}; -obj.delete_object = delete_object; -obj.get_object = get_object; -obj.patch_object = patch_object; -obj.post_object = post_object; -obj.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; -obj.create_ae_obj_crud = create_ae_obj_crud; -obj.update_ae_obj_id_crud = update_ae_obj_id_crud; -obj.delete_ae_obj_id_crud = delete_ae_obj_id_crud; -obj.download_hosted_file = download_hosted_file; -obj.delete_hosted_file = delete_hosted_file; -obj.get_data_store_obj_w_code = get_data_store_obj_w_code; -obj.send_email = send_email; -// obj.test = test; +let obj = { + delete_object: delete_object, + get_object: get_object, + patch_object: patch_object, + post_object: post_object, + get_ae_obj_id_crud: get_ae_obj_id_crud, + get_ae_obj_li_for_obj_id_crud: get_ae_obj_li_for_obj_id_crud, + create_ae_obj_crud: create_ae_obj_crud, + update_ae_obj_id_crud: update_ae_obj_id_crud, + delete_ae_obj_id_crud: delete_ae_obj_id_crud, + download_hosted_file: download_hosted_file, + delete_hosted_file: delete_hosted_file, + get_data_store_obj_w_code: get_data_store_obj_w_code, + send_email: send_email, + // test: test; +} export let api = obj; // module.exports = api; \ No newline at end of file diff --git a/src/lib/api_get_object.ts b/src/lib/api_get_object.ts index 4ce2ad2d..edb52194 100644 --- a/src/lib/api_get_object.ts +++ b/src/lib/api_get_object.ts @@ -25,8 +25,8 @@ export let get_object = async function get_object( auto_download=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. - task_id='get_object_x', - log_lvl=0 + task_id=crypto.randomUUID(), + log_lvl=1 } : { api_cfg: any, endpoint: string, @@ -44,7 +44,7 @@ export let get_object = async function get_object( } ) { 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); if (log_lvl > 1) { console.log('Data:', data); @@ -110,21 +110,6 @@ export let get_object = async function get_object( } 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( endpoint, { @@ -254,7 +239,6 @@ export let get_object = async function get_object( endpoint, { params: params, - // 'paramsSerializer': params => parse_params(params), responseType: 'blob', onDownloadProgress: (progressEvent) => { let percent_completed = Math.round( diff --git a/src/lib/api_post_object.ts b/src/lib/api_post_object.ts index 6933b7cf..6b713764 100644 --- a/src/lib/api_post_object.ts +++ b/src/lib/api_post_object.ts @@ -18,7 +18,7 @@ export let post_object = async function post_object( filename='', 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. - task_id='post_object_x', + task_id=crypto.randomUUID(), log_lvl=0 } : { api_cfg: any,