Re-working the API library functions and files
This commit is contained in:
984
src/lib/api.ts
Normal file
984
src/lib/api.ts
Normal file
@@ -0,0 +1,984 @@
|
||||
// Aether API Library (TS)
|
||||
// This is intended to be used with the Aether API.
|
||||
// This needs to be cleaned up and ideally removed the need for Axios.
|
||||
|
||||
import { delete_object } from './api_delete_object';
|
||||
import { get_object } from './api_get_object';
|
||||
import { patch_object } from './api_patch_object';
|
||||
import { post_object } from './api_post_object';
|
||||
|
||||
|
||||
// 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}) {
|
||||
if (log_lvl) {
|
||||
console.log('*** get_ae_obj_id_crud() ***');
|
||||
}
|
||||
|
||||
// data = {};
|
||||
// data['super_key'] = key;
|
||||
// data['jwt'] = jwt;
|
||||
// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST
|
||||
|
||||
let endpoint = '';
|
||||
if (obj_type == 'account') {
|
||||
endpoint = `/crud/account/${obj_id}`;
|
||||
} else if (obj_type == 'address') {
|
||||
endpoint = `/crud/address/${obj_id}`;
|
||||
} else if (obj_type == 'archive') {
|
||||
endpoint = `/crud/archive/${obj_id}`;
|
||||
} else if (obj_type == 'archive_content') {
|
||||
endpoint = `/crud/archive/content/${obj_id}`;
|
||||
} else if (obj_type == 'contact') {
|
||||
endpoint = `/crud/contact/${obj_id}`;
|
||||
} else if (obj_type == 'data_store') {
|
||||
endpoint = `/crud/data_store/${obj_id}`;
|
||||
} else if (obj_type == 'event') {
|
||||
endpoint = `/crud/event/${obj_id}`;
|
||||
} else if (obj_type == 'event_abstract') {
|
||||
endpoint = `/crud/event/abstract/${obj_id}`;
|
||||
} else if (obj_type == 'event_badge') {
|
||||
endpoint = `/crud/event/badge/${obj_id}`;
|
||||
} else if (obj_type == 'event_device') {
|
||||
endpoint = `/crud/event/device/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit') {
|
||||
endpoint = `/crud/event/exhibit/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit_tracking') {
|
||||
endpoint = `/crud/event/exhibit/tracking/${obj_id}`;
|
||||
} else if (obj_type == 'event_file') {
|
||||
endpoint = `/crud/event/file/${obj_id}`;
|
||||
} else if (obj_type == 'event_location') {
|
||||
endpoint = `/crud/event/location/${obj_id}`;
|
||||
} else if (obj_type == 'event_person') {
|
||||
endpoint = `/crud/event/person/${obj_id}`;
|
||||
} else if (obj_type == 'event_presentation') {
|
||||
endpoint = `/crud/event/presentation/${obj_id}`;
|
||||
} else if (obj_type == 'event_presenter') {
|
||||
endpoint = `/crud/event/presenter/${obj_id}`;
|
||||
} else if (obj_type == 'event_session') {
|
||||
endpoint = `/crud/event/session/${obj_id}`;
|
||||
} else if (obj_type == 'event_track') {
|
||||
endpoint = `/crud/event/track/${obj_id}`;
|
||||
} else if (obj_type == 'grant') {
|
||||
endpoint = `/crud/grant/${obj_id}`;
|
||||
} else if (obj_type == 'hosted_file') {
|
||||
endpoint = `/crud/hosted_file/${obj_id}`;
|
||||
} else if (obj_type == 'journal') {
|
||||
endpoint = `/crud/journal/${obj_id}`;
|
||||
} else if (obj_type == 'journal_entry') {
|
||||
endpoint = `/crud/journal/entry/${obj_id}`;
|
||||
} else if (obj_type == 'order') {
|
||||
endpoint = `/crud/order/${obj_id}`;
|
||||
} else if (obj_type == 'order_line') {
|
||||
endpoint = `/crud/order/line/${obj_id}`;
|
||||
} else if (obj_type == 'page') {
|
||||
endpoint = `/crud/page/${obj_id}`;
|
||||
} else if (obj_type == 'person') {
|
||||
endpoint = `/crud/person/${obj_id}`;
|
||||
} else if (obj_type == 'post') {
|
||||
endpoint = `/crud/post/${obj_id}`;
|
||||
} else if (obj_type == 'post_comment') {
|
||||
endpoint = `/crud/post/comment/${obj_id}`;
|
||||
} else if (obj_type == 'site') {
|
||||
endpoint = `/crud/site/${obj_id}`;
|
||||
} else if (obj_type == 'site_domain') {
|
||||
endpoint = `/crud/site/domain/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship_cfg') {
|
||||
endpoint = `/crud/sponsorship/cfg/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship') {
|
||||
endpoint = `/crud/sponsorship/${obj_id}`;
|
||||
// } else if (obj_type == 'user') {
|
||||
// endpoint = `/crud/user/${obj_id}`;
|
||||
} else {
|
||||
console.log(`Unknown object type: ${obj_type}`);
|
||||
return false;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Endpoint:', endpoint);
|
||||
}
|
||||
|
||||
params['use_alt_table'] = use_alt_table;
|
||||
params['use_alt_base'] = use_alt_base;
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Params:', params);
|
||||
}
|
||||
|
||||
if (no_account_id) {
|
||||
headers['x-no-account-id'] = 'Nothing to See Here';
|
||||
delete headers['x-account-id'];
|
||||
delete api_cfg['headers']['x-account-id'];
|
||||
// headers['x-account-id'] = null;
|
||||
// headers['x-account-id'] = '_XY7DXtc9Mxx';
|
||||
// params['x_no_account_id_token'] = 'Nothing to See Here';
|
||||
|
||||
// Remove the x-account-id header
|
||||
// if (headers['x-account-id']) {
|
||||
// delete headers['x-account-id'];
|
||||
// }
|
||||
|
||||
// headers['x-account-id'] = null;
|
||||
// headers['x-no-account-id-token'] = 'Nothing to See Here'; // get_object() will fix the underscores to dashes
|
||||
}
|
||||
|
||||
let object_obj_get_promise = await get_object({
|
||||
api_cfg: api_cfg,
|
||||
endpoint: endpoint,
|
||||
headers: headers,
|
||||
params: params,
|
||||
timeout: timeout,
|
||||
log_lvl: log_lvl
|
||||
});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log(object_obj_get_promise);
|
||||
}
|
||||
|
||||
return object_obj_get_promise;
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}) {
|
||||
if (log_lvl) {
|
||||
console.log('*** get_ae_obj_li_for_obj_id_crud() ***');
|
||||
}
|
||||
|
||||
// data = {};
|
||||
// data['super_key'] = key;
|
||||
// data['jwt'] = jwt;
|
||||
// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST
|
||||
|
||||
// const endpoint = `/crud/${obj_type}/list`;
|
||||
|
||||
let endpoint = '';
|
||||
if (obj_type == 'account') {
|
||||
endpoint = `/crud/account/list`;
|
||||
} else if (obj_type == 'address') {
|
||||
endpoint = `/crud/address/list`;
|
||||
} else if (obj_type == 'archive') {
|
||||
endpoint = `/crud/archive/list`;
|
||||
} else if (obj_type == 'archive_content') {
|
||||
endpoint = `/crud/archive/content/list`;
|
||||
} else if (obj_type == 'contact') {
|
||||
endpoint = `/crud/contact/list`;
|
||||
} else if (obj_type == 'data_store') {
|
||||
endpoint = `/crud/data_store/list`;
|
||||
} else if (obj_type == 'event') {
|
||||
endpoint = `/crud/event/list`;
|
||||
} else if (obj_type == 'event_abstract') {
|
||||
endpoint = `/crud/event/abstract/list`;
|
||||
} else if (obj_type == 'event_badge') {
|
||||
endpoint = `/crud/event/badge/list`;
|
||||
} else if (obj_type == 'event_device') {
|
||||
endpoint = `/crud/event/device/list`;
|
||||
} else if (obj_type == 'event_exhibit') {
|
||||
endpoint = `/crud/event/exhibit/list`;
|
||||
} else if (obj_type == 'event_exhibit_tracking') {
|
||||
endpoint = `/crud/event/exhibit/tracking/list`;
|
||||
} else if (obj_type == 'event_file') {
|
||||
endpoint = `/crud/event/file/list`;
|
||||
} else if (obj_type == 'event_location') {
|
||||
endpoint = `/crud/event/location/list`;
|
||||
} else if (obj_type == 'event_person') {
|
||||
endpoint = `/crud/event/person/list`;
|
||||
} else if (obj_type == 'event_presentation') {
|
||||
endpoint = `/crud/event/presentation/list`;
|
||||
} else if (obj_type == 'event_presenter') {
|
||||
endpoint = `/crud/event/presenter/list`;
|
||||
} else if (obj_type == 'event_session') {
|
||||
endpoint = `/crud/event/session/list`;
|
||||
} else if (obj_type == 'event_track') {
|
||||
endpoint = `/crud/event/track/list`;
|
||||
} else if (obj_type == 'grant') {
|
||||
endpoint = `/crud/grant/list`;
|
||||
} else if (obj_type == 'hosted_file') {
|
||||
endpoint = `/crud/hosted_file/list`;
|
||||
} else if (obj_type == 'journal') {
|
||||
endpoint = `/crud/journal/list`;
|
||||
} else if (obj_type == 'journal_entry') {
|
||||
endpoint = `/crud/journal/entry/list`;
|
||||
} else if (obj_type == 'order') {
|
||||
endpoint = `/crud/order/list`;
|
||||
} else if (obj_type == 'order_line') {
|
||||
endpoint = `/crud/order/line/list`;
|
||||
} else if (obj_type == 'page') {
|
||||
endpoint = `/crud/page/list`;
|
||||
} else if (obj_type == 'person') {
|
||||
endpoint = `/crud/person/list`;
|
||||
} else if (obj_type == 'post') {
|
||||
endpoint = `/crud/post/list`;
|
||||
} else if (obj_type == 'post_comment') {
|
||||
endpoint = `/crud/post/comment/list`;
|
||||
} else if (obj_type == 'site') {
|
||||
endpoint = `/crud/site/list`;
|
||||
} else if (obj_type == 'sponsorship_cfg') {
|
||||
endpoint = `/crud/sponsorship/cfg/list`;
|
||||
} else if (obj_type == 'sponsorship') {
|
||||
endpoint = `/crud/sponsorship/list`;
|
||||
// } else if (obj_type == 'user') {
|
||||
// endpoint = `/crud/user/list`;
|
||||
} else if (obj_type == 'lu' && for_obj_type == 'country_subdivision') {
|
||||
endpoint = `/crud/lu/country_subdivision/list`;
|
||||
for_obj_type = null;
|
||||
} else if (obj_type == 'lu' && for_obj_type == 'country') {
|
||||
endpoint = `/crud/lu/country/list`;
|
||||
for_obj_type = null;
|
||||
} else if (obj_type == 'lu' && for_obj_type == 'time_zone') {
|
||||
endpoint = `/crud/lu/time_zone/list`;
|
||||
for_obj_type = null;
|
||||
} else {
|
||||
console.log(`Unknown object type: ${obj_type}`);
|
||||
return false;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Endpoint:', endpoint);
|
||||
}
|
||||
|
||||
if (for_obj_type) {
|
||||
params['for_obj_type'] = for_obj_type;
|
||||
}
|
||||
if (for_obj_id) {
|
||||
params['for_obj_id'] = for_obj_id;
|
||||
}
|
||||
|
||||
params['use_alt_table'] = use_alt_table;
|
||||
params['use_alt_base'] = use_alt_base;
|
||||
|
||||
/* Need to deal with inc params here */
|
||||
|
||||
let allowed_enabled_list = ['all', 'enabled', 'not_enabled']
|
||||
if (allowed_enabled_list.includes(enabled) ) {
|
||||
params['enabled'] = enabled;
|
||||
}
|
||||
|
||||
let allowed_hidden_list = ['all', 'hidden', 'not_hidden'];
|
||||
if (allowed_hidden_list.includes(hidden) ) {
|
||||
params['hidden'] = hidden;
|
||||
}
|
||||
|
||||
// NOTE: The order_by_li variable is in the "headers" because if is a the URL GET params do not handle multiple values very well. Maybe base64 encore in the future or something? Reminder that GET requests should not have a body (no JSON).
|
||||
// NOTE: The order_by_li should be a key value pair of the property/DB field to sort and how to sort (ASC or DESC)
|
||||
if (order_by_li) {
|
||||
headers['order_by_li'] = order_by_li;
|
||||
}
|
||||
|
||||
if (limit >= 0) {
|
||||
params['limit'] = limit;
|
||||
}
|
||||
|
||||
if (offset >= 0) {
|
||||
params['offset'] = offset;
|
||||
}
|
||||
|
||||
if (params_json) {
|
||||
// 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.
|
||||
// Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge).
|
||||
console.log('JSON Object:', params_json);
|
||||
console.log(JSON.stringify(params_json));
|
||||
// NOTE: "jp" stands for "JSON Params"
|
||||
params['jp'] = encodeURIComponent(JSON.stringify(params_json));
|
||||
if (params['jp'].length > 2083) {
|
||||
console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['jp'].length} [THIS DOES NOT EXIST YET]`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// if (json_obj) {
|
||||
// // 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.
|
||||
// // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge).
|
||||
// console.log('JSON Object:', json_obj);
|
||||
// params['json_str'] = encodeURIComponent(JSON.stringify(json_obj));
|
||||
// if (params['json_str'].length > 2083) {
|
||||
// console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['json_str'].length} [THIS DOES NOT EXIST YET]`);
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Params:', params);
|
||||
}
|
||||
|
||||
let object_li_get_promise = await api.get_object({
|
||||
api_cfg: api_cfg,
|
||||
endpoint: endpoint,
|
||||
headers: headers,
|
||||
params: params,
|
||||
return_meta: return_meta,
|
||||
log_lvl: log_lvl
|
||||
});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log(object_li_get_promise);
|
||||
}
|
||||
|
||||
return object_li_get_promise;
|
||||
}
|
||||
|
||||
|
||||
// Updated 2023-07-24
|
||||
export let create_ae_obj_crud = async function create_ae_obj_crud({api_cfg, obj_type, field_name=null, field_value=null, fields={}, key, jwt=null, headers={}, params={}, data={}, return_obj=false, obj_v_name='', return_meta=false, log_lvl=0}) {
|
||||
if (log_lvl) {
|
||||
console.log('*** create_ae_obj_crud() ***');
|
||||
}
|
||||
|
||||
data['super_key'] = key;
|
||||
data['jwt'] = jwt;
|
||||
// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST
|
||||
|
||||
// This obj_v_name is the view name to use when returning data. Do not prefix it with v_. This is checked and done automatically by the API.
|
||||
// This is not currently being exposed to other areas of the code. It is only used here. For now?
|
||||
if (obj_v_name) {
|
||||
obj_v_name = '';
|
||||
}
|
||||
|
||||
let endpoint = '';
|
||||
if (obj_type == 'account') {
|
||||
endpoint = `/crud/account`;
|
||||
} else if (obj_type == 'address') {
|
||||
endpoint = `/crud/address`;
|
||||
} else if (obj_type == 'archive') {
|
||||
endpoint = `/crud/archive`;
|
||||
} else if (obj_type == 'archive_content') {
|
||||
endpoint = `/crud/archive/content`;
|
||||
} else if (obj_type == 'contact') {
|
||||
endpoint = `/crud/contact`;
|
||||
} else if (obj_type == 'data_store') {
|
||||
endpoint = `/crud/data_store`;
|
||||
} else if (obj_type == 'event') {
|
||||
endpoint = `/crud/event`;
|
||||
} else if (obj_type == 'event_abstract') {
|
||||
endpoint = `/crud/event/abstract`;
|
||||
} else if (obj_type == 'event_badge') {
|
||||
endpoint = `/crud/event/badge`;
|
||||
} else if (obj_type == 'event_device') {
|
||||
endpoint = `/crud/event/device`;
|
||||
} else if (obj_type == 'event_exhibit') {
|
||||
endpoint = `/crud/event/exhibit`;
|
||||
} else if (obj_type == 'event_exhibit_tracking') {
|
||||
endpoint = `/crud/event/exhibit/tracking`;
|
||||
} else if (obj_type == 'event_file') {
|
||||
endpoint = `/crud/event/file`;
|
||||
} else if (obj_type == 'event_location') {
|
||||
endpoint = `/crud/event/location`;
|
||||
} else if (obj_type == 'event_person') {
|
||||
endpoint = `/crud/event/person`;
|
||||
} else if (obj_type == 'event_presentation') {
|
||||
endpoint = `/crud/event/presentation`;
|
||||
} else if (obj_type == 'event_presenter') {
|
||||
endpoint = `/crud/event/presenter`;
|
||||
// obj_v_name = 'event_presenter_soft_links';
|
||||
} else if (obj_type == 'event_session') {
|
||||
endpoint = `/crud/event/session`;
|
||||
} else if (obj_type == 'event_track') {
|
||||
endpoint = `/crud/event/track`;
|
||||
} else if (obj_type == 'grant') {
|
||||
endpoint = `/crud/grant`;
|
||||
} else if (obj_type == 'hosted_file') {
|
||||
endpoint = `/crud/hosted_file`;
|
||||
} else if (obj_type == 'journal') {
|
||||
endpoint = `/crud/journal`;
|
||||
} else if (obj_type == 'journal_entry') {
|
||||
endpoint = `/crud/journal/entry`;
|
||||
} else if (obj_type == 'order') {
|
||||
endpoint = `/crud/order`;
|
||||
} else if (obj_type == 'order_line') {
|
||||
endpoint = `/crud/order/line`;
|
||||
} else if (obj_type == 'page') {
|
||||
endpoint = `/crud/page`;
|
||||
} else if (obj_type == 'person') {
|
||||
endpoint = `/crud/person`;
|
||||
} else if (obj_type == 'post') {
|
||||
endpoint = `/crud/post`;
|
||||
} else if (obj_type == 'post_comment') {
|
||||
endpoint = `/crud/post/comment`;
|
||||
} else if (obj_type == 'sponsorship_cfg') {
|
||||
endpoint = `/crud/sponsorship/cfg`;
|
||||
} else if (obj_type == 'sponsorship') {
|
||||
endpoint = `/crud/sponsorship`;
|
||||
} else if (obj_type == 'site') {
|
||||
endpoint = `/crud/site`;
|
||||
// } else if (obj_type == 'user') {
|
||||
// endpoint = `/crud/user`;
|
||||
} else {
|
||||
console.log(`Unknown object type: ${obj_type}`);
|
||||
return false;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Endpoint:', endpoint);
|
||||
}
|
||||
|
||||
if (return_obj) {
|
||||
params['return_obj'] = true;
|
||||
|
||||
// Pass along the view name to use for returning data.
|
||||
if (obj_v_name) {
|
||||
params['obj_v_name'] = obj_v_name;
|
||||
}
|
||||
} else {
|
||||
params['return_obj'] = false; // NOTE: This is needed because the current default on the API is to return the object.
|
||||
}
|
||||
|
||||
if (field_name) {
|
||||
data['data_list'] = {}; // Really an object/dict
|
||||
data['data_list'][field_name] = field_value;
|
||||
// data['data_list']['testing'] = 'asdf 1234';
|
||||
} else if (fields) {
|
||||
data['data_list'] = fields; // Really an object/dict
|
||||
}
|
||||
|
||||
// NOTE: The data object may contain objects that need to be converted to JSON strings. This is done by adding "_json" to the end of the property name. This is done because the API does not support nested objects. This is a limitation of the API.
|
||||
if (data['data_list']) {
|
||||
if (log_lvl > 1) {
|
||||
console.log('Data List:', data['data_list']);
|
||||
}
|
||||
for (const [key, value] of Object.entries(data['data_list'])) {
|
||||
// console.log(key, value);
|
||||
if (key.endsWith('_json')) {
|
||||
if (log_lvl) {
|
||||
console.log(`${key}: ${value}`);
|
||||
}
|
||||
data['data_list'][key] = JSON.stringify(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Data:', data);
|
||||
}
|
||||
// params['xxxxx run_safety_check xxxxx'] = false;
|
||||
params['by_alias'] = false;
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Params:', params);
|
||||
}
|
||||
|
||||
let object_obj_post_promise = await post_object({api_cfg: api_cfg, endpoint: endpoint, params: params, data: data, log_lvl: log_lvl});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log(object_obj_post_promise);
|
||||
}
|
||||
|
||||
return object_obj_post_promise;
|
||||
}
|
||||
|
||||
|
||||
// Updated 2023-06-28
|
||||
export let update_ae_obj_id_crud = async function update_ae_obj_id_crud({api_cfg, obj_type, obj_id, field_name, field_value, fields={}, key, jwt=null, headers={}, params={}, data={}, return_obj=false, obj_v_name='', return_meta=false, log_lvl=0}) {
|
||||
if (log_lvl) {
|
||||
console.log('*** update_ae_obj_id_crud() ***');
|
||||
}
|
||||
|
||||
data['super_key'] = key;
|
||||
data['jwt'] = jwt;
|
||||
// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST
|
||||
|
||||
// This obj_v_name is the view name to use when returning data. Do not prefix it with v_. This is checked and done automatically by the API.
|
||||
// This is not currently being exposed to other areas of the code. It is only used here. For now?
|
||||
if (obj_v_name) {
|
||||
obj_v_name = '';
|
||||
}
|
||||
|
||||
let endpoint = '';
|
||||
if (obj_type == 'account') {
|
||||
endpoint = `/crud/account/${obj_id}`;
|
||||
} else if (obj_type == 'address') {
|
||||
endpoint = `/crud/address/${obj_id}`;
|
||||
} else if (obj_type == 'archive') {
|
||||
endpoint = `/crud/archive/${obj_id}`;
|
||||
} else if (obj_type == 'archive_content') {
|
||||
endpoint = `/crud/archive/content/${obj_id}`;
|
||||
} else if (obj_type == 'contact') {
|
||||
endpoint = `/crud/contact/${obj_id}`;
|
||||
} else if (obj_type == 'data_store') {
|
||||
endpoint = `/crud/data_store/${obj_id}`;
|
||||
} else if (obj_type == 'event') {
|
||||
endpoint = `/crud/event/${obj_id}`;
|
||||
} else if (obj_type == 'event_abstract') {
|
||||
endpoint = `/crud/event/abstract/${obj_id}`;
|
||||
} else if (obj_type == 'event_badge') {
|
||||
endpoint = `/crud/event/badge/${obj_id}`;
|
||||
} else if (obj_type == 'event_device') {
|
||||
endpoint = `/crud/event/device/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit') {
|
||||
endpoint = `/crud/event/exhibit/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit_tracking') {
|
||||
endpoint = `/crud/event/exhibit/tracking/${obj_id}`;
|
||||
} else if (obj_type == 'event_file') {
|
||||
endpoint = `/crud/event/file/${obj_id}`;
|
||||
} else if (obj_type == 'event_location') {
|
||||
endpoint = `/crud/event/location/${obj_id}`;
|
||||
} else if (obj_type == 'event_person') {
|
||||
endpoint = `/crud/event/person/${obj_id}`;
|
||||
} else if (obj_type == 'event_presentation') {
|
||||
endpoint = `/crud/event/presentation/${obj_id}`;
|
||||
} else if (obj_type == 'event_presenter') {
|
||||
endpoint = `/crud/event/presenter/${obj_id}`;
|
||||
// obj_v_name = 'event_presenter_soft_links';
|
||||
} else if (obj_type == 'event_session') {
|
||||
endpoint = `/crud/event/session/${obj_id}`;
|
||||
} else if (obj_type == 'event_track') {
|
||||
endpoint = `/crud/event/track/${obj_id}`;
|
||||
} else if (obj_type == 'grant') {
|
||||
endpoint = `/crud/grant/${obj_id}`;
|
||||
} else if (obj_type == 'hosted_file') {
|
||||
endpoint = `/crud/hosted_file/${obj_id}`;
|
||||
} else if (obj_type == 'journal') {
|
||||
endpoint = `/crud/journal/${obj_id}`;
|
||||
} else if (obj_type == 'journal_entry') {
|
||||
endpoint = `/crud/journal/entry/${obj_id}`;
|
||||
} else if (obj_type == 'order') {
|
||||
endpoint = `/crud/order/${obj_id}`;
|
||||
} else if (obj_type == 'order_line') {
|
||||
endpoint = `/crud/order/line/${obj_id}`;
|
||||
} else if (obj_type == 'page') {
|
||||
endpoint = `/crud/page/${obj_id}`;
|
||||
} else if (obj_type == 'person') {
|
||||
endpoint = `/crud/person/${obj_id}`;
|
||||
} else if (obj_type == 'post') {
|
||||
endpoint = `/crud/post/${obj_id}`;
|
||||
} else if (obj_type == 'post_comment') {
|
||||
endpoint = `/crud/post/comment/${obj_id}`;
|
||||
} else if (obj_type == 'site') {
|
||||
endpoint = `/crud/site/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship_cfg') {
|
||||
endpoint = `/crud/sponsorship/cfg/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship') {
|
||||
endpoint = `/crud/sponsorship/${obj_id}`;
|
||||
// } else if (obj_type == 'user') {
|
||||
// endpoint = `/crud/user/${obj_id}`;
|
||||
} else {
|
||||
console.log(`Unknown object type: ${obj_type}`);
|
||||
return false;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Endpoint:', endpoint);
|
||||
}
|
||||
|
||||
if (return_obj) {
|
||||
params['return_obj'] = true;
|
||||
|
||||
// Pass along the view name to use for returning data.
|
||||
if (obj_v_name) {
|
||||
params['obj_v_name'] = obj_v_name;
|
||||
}
|
||||
} else {
|
||||
params['return_obj'] = false; // NOTE: This is needed because the current default on the API is to return the object.
|
||||
}
|
||||
|
||||
if (field_name) {
|
||||
data['data_list'] = {}; // Really an object/dict
|
||||
data['data_list'][field_name] = field_value;
|
||||
// data['data_list']['testing'] = 'asdf 1234';
|
||||
} else if (fields) {
|
||||
data['data_list'] = fields; // Really an object/dict
|
||||
}
|
||||
|
||||
// NOTE: The data object may contain objects that need to be converted to JSON strings. This is done by adding "_json" to the end of the property name. This is done because the API does not support nested objects. This is a limitation of the API.
|
||||
if (data['data_list']) {
|
||||
if (log_lvl > 1) {
|
||||
console.log('Data List:', data['data_list']);
|
||||
}
|
||||
for (const [key, value] of Object.entries(data['data_list'])) {
|
||||
// console.log(key, value);
|
||||
if (key.endsWith('_json')) {
|
||||
if (log_lvl) {
|
||||
console.log(`${key}: ${value}`);
|
||||
}
|
||||
data['data_list'][key] = JSON.stringify(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the data is an object then we need to loop through the object and convert any objects to JSON strings, but only if the property name ends with "_json".
|
||||
// if (Array.isArray(data)) {
|
||||
// // console.log('Data is an array');
|
||||
// for (let i = 0; i < data.length; i++) {
|
||||
// // console.log(data[i]);
|
||||
// if (typeof data[i] == 'object') {
|
||||
// // console.log('Data is an object');
|
||||
// for (const [key, value] of Object.entries(data[i])) {
|
||||
// // console.log(key, value);
|
||||
// if (key.endsWith('_json')) {
|
||||
// console.log(`${key}: ${value}`);
|
||||
// data[i][key] = JSON.stringify(value);
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
// }
|
||||
// } else if (typeof data == 'object') {
|
||||
// // console.log('Data is an object');
|
||||
// for (const [key, value] of Object.entries(data)) {
|
||||
// // console.log(key, value);
|
||||
// if (key.endsWith('_json')) {
|
||||
// console.log(`${key}: ${value}`);
|
||||
// data[key] = JSON.stringify(value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Data:', data);
|
||||
}
|
||||
// params['xxxxx run_safety_check xxxxx'] = false;
|
||||
params['by_alias'] = false;
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Params:', params);
|
||||
}
|
||||
|
||||
let object_obj_patch_promise = await patch_object({api_cfg: api_cfg, endpoint: endpoint, params: params, data: data, log_lvl: log_lvl});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log(object_obj_patch_promise);
|
||||
}
|
||||
|
||||
return object_obj_patch_promise;
|
||||
}
|
||||
|
||||
|
||||
// Updated 2023-11-14
|
||||
export let delete_ae_obj_id_crud = async function delete_ae_obj_id_crud({api_cfg, obj_type, obj_id, key, jwt=null, headers={}, params={}, data={}, method='delete', return_meta=false, log_lvl=0}) {
|
||||
if (log_lvl) {
|
||||
console.log(`*** delete_ae_obj_id_crud() *** obj_type: ${obj_type} obj_id: ${obj_id}`);
|
||||
}
|
||||
|
||||
data['super_key'] = key;
|
||||
data['jwt'] = jwt;
|
||||
// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST
|
||||
|
||||
let endpoint = '';
|
||||
if (obj_type == 'account') {
|
||||
endpoint = `/crud/account/${obj_id}`;
|
||||
} else if (obj_type == 'address') {
|
||||
endpoint = `/crud/address/${obj_id}`;
|
||||
} else if (obj_type == 'archive') {
|
||||
endpoint = `/crud/archive/${obj_id}`;
|
||||
} else if (obj_type == 'archive_content') {
|
||||
endpoint = `/crud/archive/content/${obj_id}`;
|
||||
} else if (obj_type == 'contact') {
|
||||
endpoint = `/crud/contact/${obj_id}`;
|
||||
} else if (obj_type == 'data_store') {
|
||||
endpoint = `/crud/data_store/${obj_id}`;
|
||||
} else if (obj_type == 'event') {
|
||||
endpoint = `/crud/event/${obj_id}`;
|
||||
} else if (obj_type == 'event_abstract') {
|
||||
endpoint = `/crud/event/abstract/${obj_id}`;
|
||||
} else if (obj_type == 'event_badge') {
|
||||
endpoint = `/crud/event/badge/${obj_id}`;
|
||||
} else if (obj_type == 'event_device') {
|
||||
endpoint = `/crud/event/device/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit') {
|
||||
endpoint = `/crud/event/exhibit/${obj_id}`;
|
||||
} else if (obj_type == 'event_exhibit_tracking') {
|
||||
endpoint = `/crud/event/exhibit/tracking/${obj_id}`;
|
||||
} else if (obj_type == 'event_file') {
|
||||
endpoint = `/crud/event/file/${obj_id}`;
|
||||
} else if (obj_type == 'event_location') {
|
||||
endpoint = `/crud/event/location/${obj_id}`;
|
||||
} else if (obj_type == 'event_person') {
|
||||
endpoint = `/crud/event/person/${obj_id}`;
|
||||
} else if (obj_type == 'event_presentation') {
|
||||
endpoint = `/crud/event/presentation/${obj_id}`;
|
||||
} else if (obj_type == 'event_presenter') {
|
||||
endpoint = `/crud/event/presenter/${obj_id}`;
|
||||
} else if (obj_type == 'event_session') {
|
||||
endpoint = `/crud/event/session/${obj_id}`;
|
||||
} else if (obj_type == 'event_track') {
|
||||
endpoint = `/crud/event/track/${obj_id}`;
|
||||
} else if (obj_type == 'grant') {
|
||||
endpoint = `/crud/grant/${obj_id}`;
|
||||
} else if (obj_type == 'hosted_file') {
|
||||
endpoint = `/crud/hosted_file/${obj_id}`;
|
||||
} else if (obj_type == 'journal') {
|
||||
endpoint = `/crud/journal/${obj_id}`;
|
||||
} else if (obj_type == 'journal_entry') {
|
||||
endpoint = `/crud/journal/entry/${obj_id}`;
|
||||
} else if (obj_type == 'order') {
|
||||
endpoint = `/crud/order/${obj_id}`;
|
||||
} else if (obj_type == 'order_line') {
|
||||
endpoint = `/crud/order/line/${obj_id}`;
|
||||
} else if (obj_type == 'page') {
|
||||
endpoint = `/crud/page/${obj_id}`;
|
||||
} else if (obj_type == 'person') {
|
||||
endpoint = `/crud/person/${obj_id}`;
|
||||
} else if (obj_type == 'post') {
|
||||
endpoint = `/crud/post/${obj_id}`;
|
||||
} else if (obj_type == 'post_comment') {
|
||||
endpoint = `/crud/post/comment/${obj_id}`;
|
||||
} else if (obj_type == 'site') {
|
||||
endpoint = `/crud/site/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship_cfg') {
|
||||
endpoint = `/crud/sponsorship/cfg/${obj_id}`;
|
||||
} else if (obj_type == 'sponsorship') {
|
||||
endpoint = `/crud/sponsorship/${obj_id}`;
|
||||
// } else if (obj_type == 'user') {
|
||||
// endpoint = `/crud/user/${obj_id}`;
|
||||
} else {
|
||||
console.log(`Unknown object type: ${obj_type}`);
|
||||
return false;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Endpoint:', endpoint);
|
||||
}
|
||||
|
||||
if (method) { // NOTE: method options: 'delete', 'disable', 'hide'
|
||||
params['method'] = method;
|
||||
}
|
||||
|
||||
if (log_lvl) {
|
||||
console.log('Params:', params);
|
||||
}
|
||||
|
||||
let object_obj_delete_promise = await delete_object({api_cfg: api_cfg, endpoint: endpoint, params: params, data: data, log_lvl: log_lvl});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log(object_obj_delete_promise);
|
||||
}
|
||||
|
||||
return object_obj_delete_promise;
|
||||
}
|
||||
|
||||
|
||||
/* BEGIN: Hosted File Related */
|
||||
|
||||
// Updated 2023-08-17
|
||||
export let download_hosted_file = async function download_hosted_file({
|
||||
api_cfg,
|
||||
hosted_file_id,
|
||||
return_file=true,
|
||||
filename=null,
|
||||
auto_download=false,
|
||||
params={},
|
||||
log_lvl=0
|
||||
}) {
|
||||
console.log('*** stores_hosted_api.js: download_hosted_file() ***');
|
||||
|
||||
const endpoint = `/hosted_file/${hosted_file_id}/download`;
|
||||
if (filename) {
|
||||
params['filename'] = filename;
|
||||
}
|
||||
params['return_file'] = true;
|
||||
|
||||
let hosted_file_download_get_promise = await api.get_object({api_cfg: api_cfg, endpoint: endpoint, params: params, return_blob: true, filename: filename, auto_download: auto_download, log_lvl: log_lvl});
|
||||
// console.log(hosted_file_download_get_promise);
|
||||
return hosted_file_download_get_promise;
|
||||
}
|
||||
|
||||
// Updated 2023-12-15
|
||||
export let delete_hosted_file = async function delete_hosted_file({api_cfg, hosted_file_id, link_to_type=null, link_to_id=null, rm_orphan=false, params={}, data={}, log_lvl=1}) {
|
||||
console.log('*** stores_hosted_api.js: delete_hosted_file() ***');
|
||||
|
||||
const endpoint = `/hosted_file/${hosted_file_id}`;
|
||||
if (link_to_type) {
|
||||
params['link_to_type'] = link_to_type;
|
||||
}
|
||||
if (link_to_id) {
|
||||
params['link_to_id'] = link_to_id;
|
||||
}
|
||||
if (rm_orphan) {
|
||||
params['rm_orphan'] = rm_orphan;
|
||||
}
|
||||
|
||||
let hosted_file_obj_delete_promise = await api.delete_object({api_cfg: api_cfg, endpoint: endpoint, params: params, data: data, log_lvl: log_lvl});
|
||||
// console.log(hosted_file_obj_delete_promise);
|
||||
return hosted_file_obj_delete_promise;
|
||||
}
|
||||
/* END: Hosted File Related */
|
||||
|
||||
|
||||
/* BEGIN: Data Store Related */
|
||||
|
||||
// Updated 2023-06-29
|
||||
export let get_data_store_obj_w_code = async function get_data_store_obj_w_code({
|
||||
api_cfg,
|
||||
data_store_code,
|
||||
data_type='text',
|
||||
headers={},
|
||||
params={},
|
||||
timeout=25000,
|
||||
log_lvl=0
|
||||
}) {
|
||||
if (log_lvl) {
|
||||
console.log('*** get_data_store_obj_w_code() ***');
|
||||
}
|
||||
|
||||
// let get_item_result = window.localStorage.getItem(code);
|
||||
|
||||
const endpoint = `/data_store/code/${data_store_code}`;
|
||||
let data_store_obj_get_promise = await api.get_object({api_cfg: api_cfg, endpoint: endpoint, headers: headers, params: params, timeout: timeout, log_lvl: log_lvl});
|
||||
|
||||
if (data_store_obj_get_promise === false) {
|
||||
console.log('Data Store - RUN AGAIN WITH BACKUP');
|
||||
let original_api_base_url = api_cfg['base_url'];
|
||||
|
||||
let temp_api = api_cfg;
|
||||
temp_api['base_url'] = temp_api['base_url_bak']
|
||||
|
||||
data_store_obj_get_promise = await api.get_object({api_cfg: temp_api, endpoint: endpoint, headers: headers, params: params, timeout: timeout, log_lvl: log_lvl});
|
||||
temp_api['base_url'] = original_api_base_url;
|
||||
}
|
||||
|
||||
let data_store_obj = data_store_obj_get_promise;
|
||||
|
||||
if (data_type == 'text') {
|
||||
// console.log(data_store_obj.text);
|
||||
// window.localStorage.setItem(data_store_code, data_store_obj.text);
|
||||
// localStorage.setItem(data_store_code, data_store_obj.text);
|
||||
} else if (data_type == 'json') {
|
||||
// console.log(data_store_obj.json);
|
||||
// window.localStorage.setItem(data_store_code, JSON.stringify(data_store_obj.json));
|
||||
// localStorage.setItem(data_store_code, JSON.stringify(data_store_obj.json));
|
||||
}
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log('Response Data:', data_store_obj);
|
||||
}
|
||||
return data_store_obj;
|
||||
}
|
||||
/* END: Data Store Related */
|
||||
|
||||
|
||||
/* BEGIN: Utility: Email Related */
|
||||
|
||||
// Updated 2023-06-29
|
||||
export let send_email = async function send_email({api_cfg, from_email, from_name='', to_email, to_name='', cc_email=null, cc_name=null, bcc_email=null, bcc_name=null, subject, body_html, body_text=null, headers={}, params={}, data={}, return_obj=false, return_meta=false, log_lvl=0}) {
|
||||
console.log('*** send_email() ***');
|
||||
|
||||
let endpoint = `/util/email/send`;
|
||||
|
||||
data['from_email'] = from_email; // Required
|
||||
data['from_name'] = from_name;
|
||||
|
||||
data['to_email'] = to_email; // Required
|
||||
data['to_name'] = to_name;
|
||||
|
||||
data['cc_email'] = cc_email;
|
||||
data['cc_name'] = cc_name;
|
||||
|
||||
data['bcc_email'] = bcc_email;
|
||||
data['bcc_name'] = bcc_name;
|
||||
|
||||
data['subject'] = subject;
|
||||
if (log_lvl) {
|
||||
console.log('Data:', data);
|
||||
}
|
||||
|
||||
data['body_html'] = body_html;
|
||||
|
||||
data['body_text'] = body_text;
|
||||
|
||||
if (return_obj) {
|
||||
params['return_obj'] = true;
|
||||
}
|
||||
|
||||
let send_email_post_promise = await api.post_object({api_cfg: api_cfg, endpoint: endpoint, headers: headers, params: params, data: data, return_meta: return_meta, log_lvl: log_lvl});
|
||||
|
||||
if (log_lvl > 1) {
|
||||
console.log('Response Data:', send_email_post_promise);
|
||||
}
|
||||
if (return_obj) {
|
||||
return send_email_post_promise;
|
||||
} else {
|
||||
return send_email_post_promise.event_abstract_id_random;
|
||||
}
|
||||
}
|
||||
|
||||
/* END: Utility: Email Related */
|
||||
|
||||
function resolved(result) {
|
||||
console.log('Resolved');
|
||||
}
|
||||
|
||||
function rejected(result) {
|
||||
console.error(result);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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}&`;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
export let api = obj;
|
||||
// module.exports = api;
|
||||
Reference in New Issue
Block a user