mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 17:53:59 +00:00
api logging
This commit is contained in:
@@ -5,6 +5,7 @@ import getElectron from './getElectron';
|
|||||||
// import socket from './socket';
|
// import socket from './socket';
|
||||||
|
|
||||||
let eventSource;
|
let eventSource;
|
||||||
|
let apiLogging = false;
|
||||||
// let cacheCleanerRegistered;
|
// let cacheCleanerRegistered;
|
||||||
|
|
||||||
function wantEventSource() {
|
function wantEventSource() {
|
||||||
@@ -15,10 +16,18 @@ function wantEventSource() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function apiCall(route: string, args: {} = undefined) {
|
export async function apiCall(route: string, args: {} = undefined) {
|
||||||
|
if (apiLogging) {
|
||||||
|
console.log('>>> API CALL', route, args);
|
||||||
|
}
|
||||||
|
|
||||||
const electron = getElectron();
|
const electron = getElectron();
|
||||||
if (electron) {
|
if (electron) {
|
||||||
// console.log('CALLING API', route.replace('/', '-'), JSON.stringify(args == null ? null : args));
|
|
||||||
const resp = await electron.invoke(route.replace('/', '-'), args);
|
const resp = await electron.invoke(route.replace('/', '-'), args);
|
||||||
|
|
||||||
|
if (apiLogging) {
|
||||||
|
console.log('<<< API RESPONSE', route, args, resp);
|
||||||
|
}
|
||||||
|
|
||||||
return resp;
|
return resp;
|
||||||
} else {
|
} else {
|
||||||
const resp = await fetch(`${resolveApi()}/${route}`, {
|
const resp = await fetch(`${resolveApi()}/${route}`, {
|
||||||
@@ -30,7 +39,14 @@ export async function apiCall(route: string, args: {} = undefined) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify(args),
|
body: JSON.stringify(args),
|
||||||
});
|
});
|
||||||
return resp.json();
|
|
||||||
|
const json = await resp.json();
|
||||||
|
|
||||||
|
if (apiLogging) {
|
||||||
|
console.log('<<< API RESPONSE', route, args, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +57,9 @@ export function apiOn(event: string, handler: Function) {
|
|||||||
if (electron) {
|
if (electron) {
|
||||||
if (!apiHandlers.has(handler)) {
|
if (!apiHandlers.has(handler)) {
|
||||||
const handlerProxy = (e, data) => {
|
const handlerProxy = (e, data) => {
|
||||||
|
if (apiLogging) {
|
||||||
|
console.log('@@@ API EVENT', event, data);
|
||||||
|
}
|
||||||
handler(data);
|
handler(data);
|
||||||
};
|
};
|
||||||
apiHandlers.set(handler, handlerProxy);
|
apiHandlers.set(handler, handlerProxy);
|
||||||
@@ -51,8 +70,12 @@ export function apiOn(event: string, handler: Function) {
|
|||||||
wantEventSource();
|
wantEventSource();
|
||||||
if (!apiHandlers.has(handler)) {
|
if (!apiHandlers.has(handler)) {
|
||||||
const handlerProxy = e => {
|
const handlerProxy = e => {
|
||||||
// console.log('RECEIVED', e.type, JSON.parse(e.data));
|
const json = JSON.parse(e.data);
|
||||||
handler(JSON.parse(e.data));
|
if (apiLogging) {
|
||||||
|
console.log('@@@ API EVENT', event, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(json);
|
||||||
};
|
};
|
||||||
apiHandlers.set(handler, handlerProxy);
|
apiHandlers.set(handler, handlerProxy);
|
||||||
}
|
}
|
||||||
@@ -87,3 +110,15 @@ export function useApiCall(route, args, defaultValue) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function enableApiLog() {
|
||||||
|
apiLogging = true;
|
||||||
|
console.log('API loggin enabled');
|
||||||
|
}
|
||||||
|
function disableApiLog() {
|
||||||
|
apiLogging = false;
|
||||||
|
console.log('API loggin disabled');
|
||||||
|
}
|
||||||
|
|
||||||
|
window['enableApiLog'] = enableApiLog;
|
||||||
|
window['disableApiLog'] = disableApiLog;
|
||||||
|
|||||||
Reference in New Issue
Block a user