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;
}
};

View File

@@ -24,7 +24,9 @@ export async function load_ae_obj_id__archive(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_id__archive() *** archive_id=${archive_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_id__archive() *** archive_id=${archive_id}`);
}
let params = {};
@@ -90,7 +92,7 @@ export async function load_ae_obj_id__archive(
}
// Updated 2024-09-25
// Updated 2024-11-20
export async function load_ae_obj_li__archive(
{
api_cfg,
@@ -112,7 +114,9 @@ export async function load_ae_obj_li__archive(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__archive() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__archive() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden
@@ -123,13 +127,47 @@ export async function load_ae_obj_li__archive(
// console.log('params_json:', params_json);
ae_promises.load__archive_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
// ae_promises.load__archive_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
// api_cfg: api_cfg,
// obj_type: 'archive',
// for_obj_type: for_obj_type,
// for_obj_id: for_obj_id,
// use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
// use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
// enabled: enabled,
// hidden: hidden,
// order_by_li: order_by_li,
// limit: limit,
// offset: offset,
// params_json: params_json,
// params: params,
// log_lvl: log_lvl
// })
// .then(function (archive_obj_li_get_result) {
// if (archive_obj_li_get_result) {
// if (try_cache) {
// db_save_ae_obj_li__archive({
// obj_type: 'archive',
// obj_li: archive_obj_li_get_result
// });
// }
// return archive_obj_li_get_result;
// } else {
// return [];
// }
// })
// .catch(function (error) {
// console.log('No results returned or failed.', error);
// });
ae_promises.load__archive_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'archive',
for_obj_type: for_obj_type,
for_obj_id: for_obj_id,
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
use_alt_tbl: false,
use_alt_mdl: false,
use_alt_exp: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,
@@ -151,9 +189,6 @@ export async function load_ae_obj_li__archive(
} else {
return [];
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
if (log_lvl) {
@@ -460,10 +495,12 @@ export async function qry__archive(
})
.then(function (archive_obj_li_get_result) {
if (archive_obj_li_get_result) {
db_save_ae_obj_li__archive({
obj_type: 'archive',
obj_li: archive_obj_li_get_result
});
if (try_cache) {
db_save_ae_obj_li__archive({
obj_type: 'archive',
obj_li: archive_obj_li_get_result
});
}
return archive_obj_li_get_result;
} else {
return [];

View File

@@ -23,7 +23,9 @@ export async function load_ae_obj_id__archive_content(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_id__archive_content() *** archive_content_id=${archive_content_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_id__archive_content() *** archive_content_id=${archive_content_id}`);
}
let params = {};
@@ -60,7 +62,7 @@ export async function load_ae_obj_id__archive_content(
}
// Updated 2024-09-25
// Updated 2024-11-20
export async function load_ae_obj_li__archive_content(
{
api_cfg,
@@ -88,7 +90,9 @@ export async function load_ae_obj_li__archive_content(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__archive_content() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__archive_content() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
// let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
// let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden
@@ -99,13 +103,14 @@ export async function load_ae_obj_li__archive_content(
// console('params_json:', params_json);
ae_promises.load__archive_content_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
ae_promises.load__archive_content_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'archive_content',
for_obj_type: for_obj_type,
for_obj_id: for_obj_id,
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
use_alt_tbl: false,
use_alt_mdl: false,
use_alt_exp: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,

View File

@@ -34,7 +34,9 @@ export async function load_ae_obj_id__event(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_id__event() *** event_id=${event_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_id__event() *** event_id=${event_id}`);
}
let params = {};
@@ -95,7 +97,7 @@ export async function load_ae_obj_id__event(
}
// Updated 2024-09-27
// Updated 2024-11-20
export async function load_ae_obj_li__event(
{
api_cfg,
@@ -139,7 +141,9 @@ export async function load_ae_obj_li__event(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__event() *** for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__event() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id} qry_conference=${qry_conference}`);
}
// There is probably a better way to handle this. I don't want to just start a new object if it is not passed. However, the qry_conference and qry_str are sort of a special case. -2024-10-01
if (!params_json) {
@@ -174,22 +178,6 @@ export async function load_ae_obj_li__event(
// console.log('params_json:', params_json);
// ae_promises.load__event_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
// api_cfg: api_cfg,
// obj_type: 'event',
// for_obj_type: for_obj_type,
// for_obj_id: for_obj_id,
// use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
// use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
// enabled: enabled,
// hidden: hidden,
// order_by_li: order_by_li,
// limit: limit,
// offset: offset,
// params_json: params_json,
// params: params,
// log_lvl: log_lvl
// })
ae_promises.load__event_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'event',
@@ -269,7 +257,9 @@ export async function qry_ae_obj_li__event(
log_lvl?: number
}
) {
console.log(`*** qry_ae_obj_li__event() *** for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** qry_ae_obj_li__event() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden

View File

@@ -24,7 +24,9 @@ export async function load_ae_obj_id__post(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_id__post() *** post_id=${post_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_id__post() *** post_id=${post_id}`);
}
let params = {};
@@ -90,7 +92,7 @@ export async function load_ae_obj_id__post(
}
// Updated 2024-09-25
// Updated 2024-11-20
export async function load_ae_obj_li__post(
{
api_cfg,
@@ -120,7 +122,9 @@ export async function load_ae_obj_li__post(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__post() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__post() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
// if (params.qry__enabled) { enabled = params.qry__enabled; }
// if (params.qry__hidden) { hidden = params.qry__hidden; }
@@ -131,13 +135,14 @@ export async function load_ae_obj_li__post(
// console.log('params_json:', params_json);
ae_promises.load__post_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
ae_promises.load__post_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'post',
for_obj_type: for_obj_type,
for_obj_id: for_obj_id,
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
use_alt_tbl: false,
use_alt_mdl: false,
use_alt_exp: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,

View File

@@ -20,7 +20,9 @@ export async function load_ae_obj_id__post_comment(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_id__post_comment() *** post_comment_id=${post_comment_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_id__post_comment() *** post_comment_id=${post_comment_id}`);
}
let params = {};
@@ -57,7 +59,7 @@ export async function load_ae_obj_id__post_comment(
}
// Updated 2024-09-25
// Updated 2024-11-20
export async function load_ae_obj_li__post_comment(
{
api_cfg,
@@ -85,7 +87,9 @@ export async function load_ae_obj_li__post_comment(
log_lvl?: number
}
) {
console.log(`*** load_ae_obj_li__post_comment() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
if (log_lvl) {
console.log(`*** load_ae_obj_li__post_comment() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
// if (params.qry__enabled) { enabled = params.qry__enabled; }
// if (params.qry__hidden) { hidden = params.qry__hidden; }
@@ -96,13 +100,14 @@ export async function load_ae_obj_li__post_comment(
// console('params_json:', params_json);
ae_promises.load__post_comment_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
ae_promises.load__post_comment_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'post_comment',
for_obj_type: for_obj_type,
for_obj_id: for_obj_id,
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
use_alt_tbl: false,
use_alt_mdl: false,
use_alt_exp: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,

View File

@@ -109,7 +109,7 @@ export let get_ae_obj_li_for_lu = async function get_ae_obj_li_for_lu(
endpoint: endpoint,
headers: headers,
params: params,
return_meta: return_meta,
// return_meta: return_meta,
log_lvl: log_lvl
});

View File

@@ -5,7 +5,7 @@ import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { archives_func } from '$lib/ae_archives/ae_archives_functions';
export async function load({ params, parent }) { // route
export async function load({ fetch, params, parent }) { // route
let log_lvl = 0;
let data = await parent();