mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 12:16:01 +00:00
apiCall POC
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -66,6 +61,7 @@
|
||||
import { buildFileFormats, buildQuickExports } from './fileformats';
|
||||
import { buildThemes } from './themes';
|
||||
import dbgateTools from 'dbgate-tools';
|
||||
import { apiCall } from '../utility/api';
|
||||
|
||||
let pluginsDict = {};
|
||||
const installedPlugins = useInstalledPlugins();
|
||||
@@ -87,5 +83,4 @@
|
||||
.filter(x => x.content);
|
||||
|
||||
$: $extensions = buildExtensions(plugins);
|
||||
|
||||
</script>
|
||||
|
||||
18
packages/web/src/utility/api.ts
Normal file
18
packages/web/src/utility/api.ts
Normal file
@@ -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) {}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user