diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index fc175c466..a132032bd 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -197,7 +197,7 @@ module.exports = { async get({ conid }) { if (portalConnections) return portalConnections.find(x => x._id == conid); const res = await this.datastore.find({ _id: conid }); - return res[0]; + return res[0] || null; }, newSqliteDatabase_meta: true, diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index ecd4dcb9a..06ee9fd21 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -250,7 +250,7 @@ module.exports = { serverVersion_meta: true, async serverVersion({ conid, database }) { const opened = await this.ensureOpened(conid, database); - return opened.serverVersion; + return opened.serverVersion || null; }, sqlPreview_meta: true, @@ -276,7 +276,10 @@ module.exports = { generateDeploySql_meta: true, async generateDeploySql({ conid, database, archiveFolder }) { const opened = await this.ensureOpened(conid, database); - const res = await this.sendRequest(opened, { msgtype: 'generateDeploySql', modelFolder: resolveArchiveFolder(archiveFolder) }); + const res = await this.sendRequest(opened, { + msgtype: 'generateDeploySql', + modelFolder: resolveArchiveFolder(archiveFolder), + }); return res; // const connection = await connections.get({ conid }); @@ -285,7 +288,7 @@ module.exports = { // analysedStructure: await this.structure({ conid, database }), // modelFolder: resolveArchiveFolder(archiveFolder), // }); - + // const deployedModel = generateDbPairingId(await importDbModel(path.join(archivedir(), archiveFolder))); // const currentModel = generateDbPairingId(await this.structure({ conid, database })); // const currentModelPaired = matchPairedObjects(deployedModel, currentModel); diff --git a/packages/web/src/App.svelte b/packages/web/src/App.svelte index 2b92d48be..af67e62ed 100644 --- a/packages/web/src/App.svelte +++ b/packages/web/src/App.svelte @@ -15,6 +15,7 @@ import { shouldWaitForElectronInitialize } from './utility/getElectron'; import { subscribeConnectionPingers } from './utility/connectionsPinger'; import { subscribePermissionCompiler } from './utility/hasPermission'; + import { apiCall } from './utility/api'; let loadedApi = false; @@ -27,10 +28,10 @@ try { // console.log('************** LOADING API'); - const settings = await axiosInstance().get('config/get-settings'); - const connections = await axiosInstance().get('connections/list'); - const config = await axiosInstance().get('config/get'); - loadedApi = settings?.data && connections?.data && config?.data; + const settings = await apiCall('config/get-settings'); + const connections = await apiCall('connections/list'); + const config = await apiCall('config/get'); + loadedApi = settings && connections && config; if (loadedApi) { subscribeApiDependendStores(); diff --git a/packages/web/src/plugins/PluginsProvider.svelte b/packages/web/src/plugins/PluginsProvider.svelte index 684811f3a..2e85d4d45 100644 --- a/packages/web/src/plugins/PluginsProvider.svelte +++ b/packages/web/src/plugins/PluginsProvider.svelte @@ -14,14 +14,10 @@ loaded: false, loadingPackageName: installed.name, }); - const resp = await axiosInstance().request({ - method: 'get', - url: 'plugins/script', - params: { - packageName: installed.name, - }, + const resp = await apiCall('plugins/script', { + packageName: installed.name, }); - const module = eval(`${resp.data}; plugin`); + const module = eval(`${resp}; plugin`); console.log('Loaded plugin', module); const moduleContent = module.__esModule ? module.default : module; if (moduleContent.initialize) moduleContent.initialize(dbgateEnv); @@ -55,7 +51,6 @@ }; return extensions; } - diff --git a/packages/web/src/utility/api.ts b/packages/web/src/utility/api.ts new file mode 100644 index 000000000..c842b1c09 --- /dev/null +++ b/packages/web/src/utility/api.ts @@ -0,0 +1,18 @@ +import resolveApi, { resolveApiHeaders } from './resolveApi'; + +export async function apiCall(route: string, args: {} = undefined) { + const resp = await fetch(`${resolveApi()}/${route}`, { + method: 'POST', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json', + ...resolveApiHeaders(), + }, + body: JSON.stringify(args), + }); + return resp.json(); +} + +export function apiOn(event: string, hander: Function) {} + +export function apiOff(event: string, hander: Function) {} diff --git a/packages/web/src/utility/metadataLoaders.ts b/packages/web/src/utility/metadataLoaders.ts index df35c5f1d..29586c989 100644 --- a/packages/web/src/utility/metadataLoaders.ts +++ b/packages/web/src/utility/metadataLoaders.ts @@ -9,6 +9,7 @@ import { DatabaseInfo } from 'dbgate-types'; import { derived } from 'svelte/store'; import { extendDatabaseInfo } from 'dbgate-tools'; import { setLocalStorage } from '../utility/storageCache'; +import { apiCall } from './api'; const databaseInfoLoader = ({ conid, database }) => ({ url: 'database-connections/structure', @@ -143,12 +144,8 @@ async function getCore(loader, args) { const key = stableStringify({ url, ...params }); async function doLoad() { - const resp = await axiosInstance().request({ - method: 'get', - url, - params, - }); - const res = (transform || (x => x))(resp.data); + const resp = await apiCall(url, params); + const res = (transform || (x => x))(resp); if (onLoaded) onLoaded(res); return res; } @@ -169,12 +166,8 @@ function useCore(loader, args) { subscribe: onChange => { async function handleReload() { async function doLoad() { - const resp = await axiosInstance().request({ - method: 'get', - params, - url, - }); - const res = (transform || (x => x))(resp.data); + const resp = await apiCall(url, params); + const res = (transform || (x => x))(resp); if (onLoaded) onLoaded(res); return res; } @@ -189,7 +182,7 @@ function useCore(loader, args) { cacheSet(cacheKey, res, reloadTrigger); onChange(res); } catch (err) { - console.error('Error when using cached promise', err); + console.error(`Error when using cached promise ${url}`, err); cacheClean(cacheKey); const res = await doLoad(); cacheSet(cacheKey, res, reloadTrigger);