From 19f9983c9af2524e9f1d6d2d7f2178ab94c664c3 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Thu, 15 Feb 2024 11:55:42 -0500 Subject: [PATCH] Most things are working in the template now. --- .gitignore | 13 + package-lock.json | 91 +++ package.json | 25 +- src/app.html | 25 +- src/lib/ae_stores.ts | 68 ++ src/lib/api.js | 1380 +++++++++++++++++++++++++++++++++++++ src/routes/+layout.svelte | 209 ++++-- src/routes/+page.svelte | 78 +-- 8 files changed, 1770 insertions(+), 119 deletions(-) create mode 100644 src/lib/ae_stores.ts create mode 100644 src/lib/api.js diff --git a/.gitignore b/.gitignore index 6635cf55..738f2ee9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,16 @@ node_modules !.env.example vite.config.js.timestamp-* vite.config.ts.timestamp-* + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +# Backups and archives +*.bak +*.tar.gz diff --git a/package-lock.json b/package-lock.json index 683985ad..c891db4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "dependencies": { "@floating-ui/dom": "1.6.3", + "axios": "^1.6.7", "highlight.js": "11.9.0" }, "devDependencies": { @@ -1543,6 +1544,11 @@ "node": "*" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/autoprefixer": { "version": "10.4.17", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.17.tgz", @@ -1580,6 +1586,16 @@ "postcss": "^8.1.0" } }, + "node_modules/axios": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/axobject-query": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-4.0.0.tgz", @@ -1826,6 +1842,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", @@ -1933,6 +1960,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/dequal": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", @@ -2474,6 +2509,25 @@ "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/foreground-child": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.1.1.tgz", @@ -2490,6 +2544,19 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fraction.js": { "version": "4.3.7", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.3.7.tgz", @@ -3100,6 +3167,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", @@ -3814,6 +3900,11 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", diff --git a/package.json b/package.json index 175d30e8..5c7b93be 100644 --- a/package.json +++ b/package.json @@ -16,36 +16,37 @@ }, "devDependencies": { "@playwright/test": "^1.28.1", + "@skeletonlabs/skeleton": "2.8.0", + "@skeletonlabs/tw-plugin": "0.3.1", "@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/kit": "^2.0.0", "@sveltejs/vite-plugin-svelte": "^3.0.0", + "@tailwindcss/forms": "0.5.7", + "@tailwindcss/typography": "0.5.10", "@types/eslint": "8.56.0", + "@types/node": "20.11.18", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", + "autoprefixer": "10.4.17", "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-svelte": "^2.35.1", + "postcss": "8.4.35", "prettier": "^3.1.1", "prettier-plugin-svelte": "^3.1.2", "svelte": "^4.2.7", "svelte-check": "^3.6.0", + "tailwindcss": "3.4.1", "tslib": "^2.4.1", "typescript": "^5.0.0", "vite": "^5.0.3", - "vitest": "^1.2.0", - "postcss": "8.4.35", - "autoprefixer": "10.4.17", - "tailwindcss": "3.4.1", - "@skeletonlabs/skeleton": "2.8.0", - "@skeletonlabs/tw-plugin": "0.3.1", "vite-plugin-tailwind-purgecss": "0.2.0", - "@tailwindcss/typography": "0.5.10", - "@tailwindcss/forms": "0.5.7", - "@types/node": "20.11.18" + "vitest": "^1.2.0" }, "type": "module", "dependencies": { - "highlight.js": "11.9.0", - "@floating-ui/dom": "1.6.3" + "@floating-ui/dom": "1.6.3", + "axios": "^1.6.7", + "highlight.js": "11.9.0" } -} \ No newline at end of file +} diff --git a/src/app.html b/src/app.html index 60cefdc9..f9e7e389 100644 --- a/src/app.html +++ b/src/app.html @@ -1,12 +1,19 @@ - - - - - %sveltekit.head% - - -
%sveltekit.body%
- + + + + + + + + + + %sveltekit.head% + + +
%sveltekit.body%
+ diff --git a/src/lib/ae_stores.ts b/src/lib/ae_stores.ts new file mode 100644 index 00000000..ae57e3db --- /dev/null +++ b/src/lib/ae_stores.ts @@ -0,0 +1,68 @@ +import { readable, writable } from 'svelte/store'; + +type key_val = { + [key: string]: any; // variable key + // name: string; +}; + +// *** BEGIN *** Longer-term app data. This should be stored to local storage. +export let ae_app_local_data_struct: key_val = { + 'ver': '0.0.1', + 'name': 'Aether App Template', + 'theme': 'light', + 'account_id': '_XY7DXtc9MY', // OSIT Demo _XY7DXtc9MY + 'site_domain': 'https://dev-demo.oneskyit.com', + 'ds': {}, + 'hub': { + 'ds': {}, + }, +} +console.log(`Aether Config - App Local Storage Data:`, ae_app_local_data_struct); +export let ae_loc = writable(ae_app_local_data_struct); + + +// *** BEGIN *** Temporary app data. This should be stored to session storage. +export let ae_app_session_data_struct: key_val = { + 'ver': '0.0.1', + // 'name': 'Aether App Template', + // 'theme': 'light', + 'account_id': ae_app_local_data_struct.account_id, + // 'obj': {}, +} +console.log(`Aether Config - App Session Storage Data:`, ae_app_session_data_struct); +export let ae_sess = writable(ae_app_session_data_struct); + + +// *** BEGIN *** Temporary API data. This should be stored to session storage. +export let ae_api_data_struct: key_val = { + 'ver': '0.0.2', + 'base_url': 'https://dev-api.oneskyit.com', + 'base_url_bak': 'https://dev-api.oneskyit.com', + 'api_secret_key': 'dFP6J9DVj9hUgIMn-fNIqg', // 'YOUR_API_SECRET_KEY', + 'api_secret_key_bak': 'dFP6J9DVj9hUgIMn-fNIqg', // 'YOUR_API_SECRET_KEY', + 'api_crud_super_key': 'zp5PtX4zUsI', // 'YOUR_SUPER_KEY' 'zp5PtX4zUsI' + 'headers': {}, + 'account_id': ae_app_local_data_struct.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_data_struct.api_secret_key; +ae_api_headers['x-aether-api-token'] = 'fake-temp-token'; +ae_api_headers['x-aether-api-expire-on'] = ''; +ae_api_headers['x-account-id'] = ae_app_local_data_struct.account_id; +ae_api_data_struct['headers'] = ae_api_headers; + +console.log(`Aether Config - API Data:`, ae_api_data_struct); +export let ae_api = writable(ae_api_data_struct); + + +/* *** BEGIN *** Initialize slct_trigger and slct variables */ +// Updated 2024-02-15 +export let slct_trigger: any = writable(null); +console.log(`Aether Config - Selected Trigger:`, slct_trigger); + +let slct_obj_template: key_val = { 'sponsorship_id': null, 'sponsorship_obj': {}, 'post_id': null, 'post_obj': {}, 'post_obj_li': [], 'post_comment_id': null, 'post_comment_obj': {}, 'post_comment_obj_li': [] }; +console.log(`Aether Config - Selected Objects:`, slct_obj_template); +export let slct = writable(slct_obj_template); diff --git a/src/lib/api.js b/src/lib/api.js new file mode 100644 index 00000000..515f1a0d --- /dev/null +++ b/src/lib/api.js @@ -0,0 +1,1380 @@ +import axios from 'axios'; +// import { readable, writable } from 'svelte/store'; + +// console.log('*** Using api.js ***'); + + +export let delete_object = async function delete_object({api_cfg=null, endpoint='', params={}, data={}, return_meta=false, log_lvl=0}) { + console.log('*** delete_object() ***'); + + if (log_lvl) { + // console.log(api_cfg); + console.log(endpoint); + console.log(params); + if (log_lvl > 1) { + console.log('Data:', data); + console.log(typeof data); + } + // console.log(return_meta); + // console.log(as_list); + } + + // https://stackoverflow.com/questions/51069552/axios-delete-request-with-body-and-headers + + let axios_api = axios.create({ + baseURL: api_cfg['base_url'], + // timeout: 2000, + /* other custom settings */ + }); + axios_api.defaults.headers = api_cfg['headers']; + + //OLD: axios_api.delete(endpoint, { 'data': data }) + let response_data = await axios_api.delete(endpoint, { params: params, 'data': data }) + .then(function (response) { + console.log(response.data); + return response.data; + }) + .catch(function (error) { + if (error.response && error.response.status === 404) { + return null; // Returning null since there were no results + } + console.log(error); + return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. + // return error; + }); + + if (log_lvl > 1) { + console.log(response_data); + } + return response_data; +} + + +export let temp_get_blob_percent_completed = 0; +// export let get_blob_percent_completed = readable(temp_get_blob_percent_completed); +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 = readable(temp_get_object_percent_completed); +export let get_object_percent_completed = temp_get_object_percent_completed; + +// Updated 2022-10-28 +export let get_object = async function get_object({api_cfg=null, endpoint='', headers={}, params={}, data={}, timeout=600000, return_meta=false, return_blob=false, filename=null, auto_download=false, as_list=false, log_lvl=0}) { + if (log_lvl) { + console.log('*** get_object() ***'); + } + + if (log_lvl) { + console.log(`Endpoint: ${endpoint}`); + console.log('Params:', params); + if (log_lvl > 1) { + console.log('Data:', data); + console.log(`Base URL: ${api_cfg['base_url']}`); + console.log('API Config:', api_cfg); + } + if (log_lvl > 2) { + console.log(`Return Meta: ${return_meta}`); + console.log(`Return Blob: ${return_blob}`); + console.log(`Filename: ${filename}`); + console.log(`Auto Download: ${auto_download}`); + console.log(`As List: ${as_list}`); + } + } + + let axios_api = axios.create({ + baseURL: api_cfg['base_url'], + timeout: timeout, // in milliseconds; 60000 = 60 seconds + /* other custom settings */ + }); + axios_api.defaults.headers = api_cfg['headers']; + console.log(axios_api.defaults.headers); + + console.log('Additional headers:', headers); + // console.log(headers); + + console.log('Clean the headers. No _underscores_!') + let headers_cleaned = {}; + 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]}`); + } + } + headers = headers_cleaned; + console.log('Cleaned additional headers:', headers); + + console.log('URL params:'); + for (const prop in params) { + if (log_lvl) { + console.log(`${prop}: ${params[prop]}`); + } + if (params[prop] === null ) { + params[prop] = 'null'; + } + } + + 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, + { + headers: headers, + 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; + } + } + ) + .then(function (response) { + if (log_lvl) { + console.log('GET Response:', response); + } + if (!Array.isArray(response.data['data']) && as_list) { + if (log_lvl) { + console.log('Data result is a dictionary/object, not an array/list. Forcing return as an array/list'); + } + let return_data = []; + return_data.push(response.data['data']); + return return_data; + } else if (response.data['data']) { + let return_data = response.data['data']; + if (log_lvl) { + if (Array.isArray(return_data)) { + console.log(`Data result is an array/list. Array length: ${return_data.length}`); + } else { + console.log(`Data result is a dictionary/object, not an array/list.`); + } + } + return return_data; + } else { + let return_data = response.data; + if (log_lvl) { + if (Array.isArray(return_data)) { + console.log(`Not a standard response from Aether's API. Data result is an array/list. Array length: ${return_data.length}`); + } else { + console.log(`Not a standard response from Aether's API. Data result is a dictionary/object, not an array/list.`); + } + } + return return_data; + } + }) + .catch(function (error) { + if (log_lvl) { + console.log(`Base URL: ${api_cfg['base_url']} | Endpoint: ${endpoint}`); + console.log('Error Message:', error.message); // Is this needed here or below in the in the else portion??? + + if (error.response) { + // The request was made and the server responded with a status code that falls out of the range of 2xx + console.log('Error Response Data', error.response.data); + console.log('Error Response Status', error.response.status); + console.log('Error Response Headers', error.response.headers); + } else if (error.request) { + // The request was made but no response was received `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js + console.log('Error Request', error.request); + } else { + // Something happened in setting up the request that triggered an Error + console.log('Error Message', error.message); + } + } + if (log_lvl > 1) { + console.log('Error:', error); + console.log(error.config); + } + + if (error.response && error.response.status === 404) { + if (log_lvl) { + console.log('The response was a 404 not found "error". Returning null.'); + } + if (log_lvl > 1) { + console.log(error.response); + } + if (log_lvl > 2) { + console.log(error); + } + return null; // Returning null since there were no results + } + if (log_lvl) { + console.log('The response was an error. Returning false.'); + } + + return false; // Returning false since something may have gone wrong. This includes timeouts. Also more in line with what the API returns. + // return error; + }); + + if (log_lvl > 1) { + // console.log(`Response Data: ${response_data_promise}`); + console.log(`Response Data:`, response_data_promise); + // console.log(response_data_promise); + } + if (response_data_promise) { + // The most common and expected response. + // console.log('Returning result. This is generally expected.'); + return response_data_promise; + } else if (response_data_promise === null) { + // Less common, but expected response if no results were returned. + console.log('Returning null. This is expected if no results were found.'); + return response_data_promise; + } else if (response_data_promise === false) { + // Not common, but expected response if the request to the API had an issue. + console.log('Returning false. There may have been an issue with this request.'); + return response_data_promise; + } else { + // This generally should not happen. It likely means the query was bad or an API issue. + console.log('Returning unknown. This should not happen in most cases.'); + Promise.reject(new Error('fail')).then(resolved, rejected); + } + } else { + // console.log('Expecting a Blob to be returned...'); + + let response_data_promise = await axios_api.get( + endpoint, + { + params: params, + // 'paramsSerializer': params => parse_params(params), + responseType: 'blob', + onDownloadProgress: (progressEvent) => { + let percent_completed = Math.round( + (progressEvent.loaded * 100) / progressEvent.total + ); + console.log('GET Blob Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed); + + temp_get_blob_percent_completed = percent_completed; + } + } + ) + .then(function (response) { + if (log_lvl) { + console.log(response); + } + + const { data, headers } = response + console.log(headers); + + if (filename) { + } else if (headers['content-disposition']) { + filename = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1'); + } else { + filename = 'unknown_file.ext'; + } + + if (auto_download) { + const url = window.URL.createObjectURL(new Blob([response.data])); + const link = document.createElement('a'); + link.href = url; + // link.setAttribute('download', 'event_exhibit_tracking_export.xlsx'); //or any other extension + link.setAttribute('download', filename); //or any other extension + document.body.appendChild(link); + link.click(); + return true; + } else { + return response; + } + }); + + if (response_data_promise) { + // The most common and expected response. + // console.log('Returning result. This is generally expected.'); + // let test_blob = new Blob([response_data_promise.data]); + // console.log(test_blob); + // return test_blob; + // console.log(response_data_promise.blob()); + return response_data_promise; + } else { + // This generally should not happen. It likely means the query was bad or an API issue. + console.log('Returning unknown. This should not happen in most cases.'); + Promise.reject(new Error('fail')).then(resolved, rejected); + } + } +} + +export let patch_object = async function patch_object({api_cfg=null, endpoint='', params={}, data={}, return_meta=false, log_lvl=0}) { + console.log('*** patch_object() ***'); + + if (log_lvl) { + // console.log(api_cfg); + console.log(endpoint); + console.log(params); + if (log_lvl > 1) { + console.log(data); + } + // console.log(return_meta); + // console.log(as_list); + } + + let axios_api = axios.create({ + baseURL: api_cfg['base_url'], + /* other custom settings */ + }); + axios_api.defaults.headers = api_cfg['headers']; + + let response_data = await axios_api.patch(endpoint, data, { params: params }) + .then(function (response) { + console.log(response.data); + return response.data['data']; + //return response.data; + }) + .catch(function (error) { + if (error.response && error.response.status === 404) { + return null; // Returning null since there were no results + } + console.log(error); + return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. + // return error; + }); + + if (log_lvl > 1) { + console.log(response_data); + } + return response_data; +} + + +// Updated 2023-11-10 +export let post_object = async function post_object({api_cfg=null, endpoint='', params={}, data={}, form_data=null, return_meta=false, return_blob=false, filename=null, auto_download=false, log_lvl=0}) { + console.log('*** post_object() ***'); + + if (log_lvl) { + // console.log(api_cfg); + console.log('Endpoint:', endpoint); + console.log('Params:', params); + if (log_lvl > 1) { + console.log('Data:', data); + console.log(typeof data); + } + if (log_lvl > 2) { + console.log(`Return Meta: ${return_meta}`); + console.log(`Return Blob: ${return_blob}`); + console.log(`Filename: ${filename}`); + console.log(`Auto Download: ${auto_download}`); + } + // console.log(return_meta); + // console.log(as_list); + } + + let axios_api = axios.create({ + baseURL: api_cfg['base_url'], + /* other custom settings */ + }); + axios_api.defaults.headers = api_cfg['headers']; + console.log('Axios API', axios_api); + // console.log('Axios API POST', axios_api.post); + + // if (typeof data == 'FormData') { + if (form_data) { + axios_api.defaults.headers['content-type'] = 'multipart/form-data'; + data = form_data; + } else { + axios_api.defaults.headers['content-type'] = 'application/json'; + } + + + if (!return_blob) { + let response_data = await axios_api.post(endpoint, data, { params: params }) + .then(function (response) { + console.log('POST Response Data:', response.data); + if (response.data['data'].result === null) { + // This should mean that the request was successful, but a result of None/null was returned from Aether API. + // console.log('Returning null after POST'); + return null; + } else { + // This should mean that the request was successful, and a result with data was returned from Aether API. + // console.log('Returning data after POST'); + return response.data['data']; + } + //return response.data; + }) + .catch(function (error) { + if (error.response && error.response.status === 404) { + return null; // Returning null since there were no results + } + console.log(error); + return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. + // return error; + }); + + if (log_lvl > 1) { + console.log('Response Data:', response_data); + } + axios_api.defaults.headers['content-type'] = 'application/json'; + return response_data; + + } else { + // console.log('Expecting a Blob to be returned...'); + + let response_data_promise = await axios_api.post( + endpoint, + data, + { + params: params, + responseType: 'blob', + onDownloadProgress: (progressEvent) => { + let percent_completed = Math.round( + (progressEvent.loaded * 100) / progressEvent.total + ); + console.log('POST Blob Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed); + + temp_get_blob_percent_completed = percent_completed; + } + } + ) + .then(function (response) { + if (log_lvl) { + console.log(response); + } + + const { data, headers } = response + console.log(headers); + + if (filename) { + } else if (headers['content-disposition']) { + filename = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1'); + } else { + filename = 'unknown_file.ext'; + } + + if (auto_download) { + const url = window.URL.createObjectURL(new Blob([response.data])); + const link = document.createElement('a'); + link.href = url; + // link.setAttribute('download', 'event_exhibit_tracking_export.xlsx'); //or any other extension + link.setAttribute('download', filename); //or any other extension + document.body.appendChild(link); + link.click(); + return true; + } else { + return response; + } + }); + + if (response_data_promise) { + // The most common and expected response. + // console.log('Returning result. This is generally expected.'); + // let test_blob = new Blob([response_data_promise.data]); + // console.log(test_blob); + // return test_blob; + // console.log(response_data_promise.blob()); + return response_data_promise; + } else { + // This generally should not happen. It likely means the query was bad or an API issue. + console.log('Returning unknown. This should not happen in most cases.'); + Promise.reject(new Error('fail')).then(resolved, rejected); + } + } +} + + +// Updated 2023-12-01 +export let get_ae_obj_id_crud = async function get_ae_obj_id_crud({ + api_cfg, + 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={}, + 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 == '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_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); + } + + params['use_alt_table'] = use_alt_table; + params['use_alt_base'] = use_alt_base; + + if (log_lvl) { + console.log('Params:', params); + } + + let object_obj_get_promise = await get_object({ + api_cfg: api_cfg, + endpoint: endpoint, + headers: headers, + params: params, + 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 == '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_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 charcters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). + console.log('JSON Object:', 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 charcters 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, 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 + + 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 == '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_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`; + } 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; + } 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, 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 + + 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 == '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_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 (return_obj) { + params['return_obj'] = true; + } + + 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, field_name, field_value, fields={}, key, jwt=null, headers={}, params={}, data={}, method='delete', return_meta=false, log_lvl=0}) { + if (log_lvl) { + console.log('*** delete_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 + + 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 == '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_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={}, 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, 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_backup'] + + // data_store_obj_get_promise = await api.get_object({api_cfg: temp_api, endpoint: endpoint, headers: headers, params: params, 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); + } else if (data_type == 'json') { + // console.log(data_store_obj.json); + window.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; \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 93e2d2c9..814ee8f7 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,64 +1,163 @@ + - - - - - Skeleton - - - - Discord - - - Twitter - - - GitHub - - - - - - + + + + + + Æ Home + + + OSIT's Aether App + + Example Page + + Example Page + + + + + + + + + +
+ {@html $ae_loc.hub.ds['hub_site_footer']} +
+
+
+ diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 67c03533..189004ba 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,23 +1,15 @@ - +