239 lines
8.5 KiB
TypeScript
239 lines
8.5 KiB
TypeScript
/** @type {import('./$types').LayoutLoad} */
|
|
// console.log(`ae_root +layout.ts start`);
|
|
|
|
import { error } from '@sveltejs/kit';
|
|
import { get } from 'svelte/store';
|
|
import { browser } from '$app/environment';
|
|
|
|
// import { api } from '$lib/api';
|
|
import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';
|
|
import { core_func } from '$lib/ae_core_functions';
|
|
import type { key_val } from '$lib/ae_stores';
|
|
|
|
import { PUBLIC_AE_API_PROTOCOL, PUBLIC_AE_API_SERVER, PUBLIC_AE_API_BAK_SERVER, PUBLIC_AE_API_PORT, PUBLIC_AE_API_PATH, PUBLIC_AE_API_SECRET_KEY, PUBLIC_AE_API_CRUD_SUPER_KEY, PUBLIC_AE_NO_ACCOUNT_ID, PUBLIC_AE_NO_ACCOUNT_ID_TOKEN } from '$env/static/public';
|
|
const api_base_url = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;
|
|
const api_base_url_bak = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_BAK_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;
|
|
|
|
const api_secret_key = PUBLIC_AE_API_SECRET_KEY;
|
|
const api_crud_super_key = PUBLIC_AE_API_CRUD_SUPER_KEY;
|
|
|
|
let ae_account_id: null|string = null;
|
|
const ae_no_account_id = PUBLIC_AE_NO_ACCOUNT_ID;
|
|
const ae_no_account_id_token = PUBLIC_AE_NO_ACCOUNT_ID_TOKEN;
|
|
|
|
let ae_api_init: key_val = {
|
|
'ver': '2024-03-27_11',
|
|
'base_url': api_base_url,
|
|
'base_url_bak': api_base_url_bak,
|
|
'api_secret_key': api_secret_key, // 'YOUR_API_SECRET_KEY',
|
|
'api_secret_key_bak': api_secret_key, // 'YOUR_API_SECRET_KEY',
|
|
'api_crud_super_key': api_crud_super_key, // 'YOUR_SUPER_KEY' 'zp5PtX4zUsI'
|
|
'headers': {},
|
|
'account_id': ae_account_id,
|
|
};
|
|
|
|
let ae_api_headers: key_val = {};
|
|
ae_api_headers['Access-Control-Allow-Origin'] = '*';
|
|
ae_api_headers['content-type'] = 'application/json';
|
|
ae_api_headers['x-aether-api-key'] = ae_api_init.api_secret_key;
|
|
ae_api_headers['x-aether-api-token'] = 'fake-temp-token';
|
|
ae_api_headers['x-aether-api-expire-on'] = '';
|
|
if (ae_account_id) {
|
|
ae_api_headers['x-account-id'] = ae_account_id;
|
|
} else {
|
|
// ae_api_headers['x-account-id'] = ;
|
|
}
|
|
if (ae_no_account_id) {
|
|
ae_api_headers['x-no-account-id'] = ae_no_account_id;
|
|
}
|
|
|
|
ae_api_init['headers'] = ae_api_headers;
|
|
|
|
let ae_loc_init: key_val = {
|
|
};
|
|
|
|
// export const prerender = false;
|
|
|
|
// There is not an initial data store from SvelteKit for this, so we will just use the API to get the data.
|
|
// properties: params, route, url
|
|
// functions: fetch, setHeaders, parent, depends, untrack
|
|
export async function load({ fetch, params, parent, route, url }) { // params, route, url
|
|
// console.log(`Svelte root layout.ts params:`, params);
|
|
// console.log(`Svelte root layout.ts route:`, route);
|
|
// console.log(`Svelte root layout.ts url:`, url);
|
|
|
|
let account_id: Promise<any>;
|
|
|
|
let ae_acct: key_val = {
|
|
api: ae_api_init,
|
|
ds: {},
|
|
loc: {
|
|
'account_id': '',
|
|
'site_id': '',
|
|
'site_domain_id': '',
|
|
'iframe': false,
|
|
},
|
|
sess: {},
|
|
slct: {},
|
|
}
|
|
|
|
// let ae_loc_tmp = get(ae_loc);
|
|
// console.log(`ae_loc = `, ae_loc_tmp);
|
|
|
|
// let ae_api_tmp = get(ae_api);
|
|
// console.log(`ae_api = `, ae_api_tmp);
|
|
|
|
let ds_code_li: null|key_val = {};
|
|
// if (ae_loc_tmp && ae_loc_tmp.ds) {
|
|
// ds_code_li = ae_loc_tmp.ds;
|
|
// }
|
|
// console.log(`ae_ ds_code_li = `, ds_code_li);
|
|
let ds_code: null|string = null;
|
|
|
|
let data_struct = {
|
|
// ae_acct should only be updated and referenced by the corresponding account_id.
|
|
account_id: null,
|
|
ae_acct: {
|
|
// '_XY7DXtc9MY': ae_acct,
|
|
},
|
|
|
|
ae_loc: {},
|
|
ae_api: ae_api_init,
|
|
ae_ds: {},
|
|
ae_hub: {}, // was ae_core
|
|
ae_m_sponsorships: {},
|
|
ae_m_events: {},
|
|
ae_m_events_speakers: {},
|
|
ae_slct: {},
|
|
|
|
iframe: false,
|
|
|
|
ae_root_layout_ts: true,
|
|
|
|
params: params,
|
|
|
|
url: url,
|
|
|
|
// Not really used yet
|
|
sections: [
|
|
{ slug: 'new', title: 'New Test' },
|
|
{ slug: 'manage', title: 'Manage Test' },
|
|
{ slug: 'test', title: 'Test Test' },
|
|
],
|
|
|
|
// Not really used yet
|
|
submenu: {},
|
|
};
|
|
|
|
// First do a site_domain look up to check if it is valid and get the account_id.
|
|
// ae_loc.url_host = data.url.host; // Use this to look up? sub.example.com:123
|
|
// ae_loc.fqdn = url.host; // Use this to look up? sub.example.com:123
|
|
// ae_loc.url_hostname = data.url.hostname; // sub.example.com
|
|
// ae_loc.url_origin = data.url.origin; // Use this to look up? https://sub.example.com:123
|
|
// ae_loc.site_domain = data.url.origin;
|
|
// console.log(`ae_loc = `, ae_loc);
|
|
|
|
account_id = await core_func.handle_load_ae_obj_id__site_domain({api_cfg: ae_api_init, fqdn: url.host, try_cache: false, log_lvl: 1})
|
|
|
|
.then(function (site_domain_results) {
|
|
if (site_domain_results) {
|
|
// console.log(`ae_ site_domain_results = `, site_domain_results);
|
|
|
|
data_struct.account_id = site_domain_results.account_id_random;
|
|
|
|
ae_acct.account_id = site_domain_results.account_id_random;
|
|
|
|
ae_api_init['account_id'] = site_domain_results.account_id_random;
|
|
ae_api_init['headers']['x-account-id'] = site_domain_results.account_id_random;
|
|
ae_api_init['headers']['x-no-account-id'] = null;
|
|
|
|
ae_loc_init['account_id'] = site_domain_results.account_id_random;
|
|
ae_loc_init['account_code'] = site_domain_results.account_code; // Useful for export file naming
|
|
ae_loc_init['account_name'] = site_domain_results.account_name; // Generally useful for display
|
|
|
|
ae_loc_init['site_id'] = site_domain_results.site_id_random;
|
|
ae_loc_init['site_domain_id'] = site_domain_results.site_domain_id_random;
|
|
ae_loc_init['site_enable'] = site_domain_results.enable;
|
|
ae_loc_init['site_header_image_path'] = site_domain_results.header_image_path;
|
|
ae_loc_init['site_style_href'] = site_domain_results.style_href;
|
|
ae_loc_init['site_google_tracking_id'] = site_domain_results.google_tracking_id;
|
|
ae_loc_init['site_cfg_json'] = site_domain_results.cfg_json;
|
|
|
|
console.log(`root layout.ts: Returning account_id = `, site_domain_results.account_id_random);
|
|
|
|
return site_domain_results.account_id_random;
|
|
}
|
|
console.log(`root layout.ts: Site domain results not found!!!`)
|
|
|
|
return null;
|
|
});
|
|
|
|
if (!account_id) {
|
|
console.log(`root layout.ts: The account_id was not found in API response!!!`);
|
|
// return false;
|
|
// return;
|
|
error(500, {
|
|
message: 'Not found'
|
|
});
|
|
}
|
|
// console.log(ae_loc_init);
|
|
|
|
// if (browser) {
|
|
// localStorage.setItem('ae_account_id', await account_id);
|
|
// }
|
|
|
|
// NOTE: We need to wait for the account_id to be returned before we can continue. It is required for the api_cfg.
|
|
let ds_type: null|string = 'json';
|
|
ds_code = 'hub__page__access_code_li_json';
|
|
// console.log(`INFO: ae_ account_id = `, account_id);
|
|
// ae_acct['ds'][ds_code]
|
|
ds_code_li[ds_code] = await core_func.handle_load_ae_obj_code__data_store({api_cfg: ae_api_init, code: ds_code, data_type: ds_type, save_idb: false})
|
|
.then(function (ds_results) {
|
|
if (ds_results) {
|
|
console.log(`ae_ ds_results = `, ds_results);
|
|
return ds_results;
|
|
}
|
|
|
|
}
|
|
);
|
|
|
|
ae_loc_init['page_access_code_li'] = ds_code_li['hub__page__access_code_li_json'];
|
|
|
|
ae_acct['api'] = ae_api_init;
|
|
ae_acct['loc'] = ae_loc_init;
|
|
ae_acct['ds'] = ds_code_li;
|
|
ae_acct['slct'] = {
|
|
'account_id': account_id,
|
|
'site_domain_id': ae_loc_init.site_domain_id,
|
|
'site_id': ae_loc_init.site_id,
|
|
|
|
// For events_leads, events_badges, events_speakers
|
|
'event_id': ae_loc_init.site_cfg_json.slct__event_id,
|
|
'event_badge_template': ae_loc_init.site_cfg_json.slct__event_badge_template,
|
|
|
|
// For sponsorships
|
|
'sponsorship_cfg_id': ae_loc_init.site_cfg_json.slct__sponsorship_cfg_id,
|
|
}
|
|
// console.log(`ae_acct = `, ae_acct);
|
|
|
|
// WARNING: Precaution against shared data between sites and sessions.
|
|
// data_struct.ae_acct[ae_loc_init.account_id] = ae_acct;
|
|
|
|
data_struct[ae_loc_init.account_id] = ae_acct;
|
|
|
|
return data_struct;
|
|
}
|
|
|
|
|
|
// ds_type = 'text';
|
|
// ds_code = 'hub__site__appshell_header';
|
|
// ds_code_li[ds_code] = await core_func.handle_load_ae_obj_code__data_store({api_cfg: ae_api_tmp, code: ds_code, data_type: ds_type})
|
|
// .then(function (ds_results) {
|
|
// if (ds_results) {
|
|
// console.log(`ae_ ds_results = `, ds_results);
|
|
// return ds_results;
|
|
// }
|
|
|
|
// }
|
|
// );
|