🏗️ refactor response handling to use ResponseUtils and improve code consistency
This commit is contained in:
parent
b5fd0c6747
commit
20a8208240
@ -1,11 +1,11 @@
|
||||
import BaseController from "./BaseController";
|
||||
import ServerService from "../service/ServerService";
|
||||
import { success } from "../utils/response";
|
||||
import PathUtils from "../utils/PathUtils";
|
||||
import fs from "fs";
|
||||
import FrpcProcessService from "../service/FrpcProcessService";
|
||||
import SystemService from "../service/SystemService";
|
||||
import moment from "moment";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
|
||||
class ConfigController extends BaseController {
|
||||
private readonly _serverService: ServerService;
|
||||
@ -25,19 +25,19 @@ class ConfigController extends BaseController {
|
||||
|
||||
saveConfig(req: ControllerParam) {
|
||||
this._serverService.saveServerConfig(req.args).then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
});
|
||||
}
|
||||
|
||||
getServerConfig(req: ControllerParam) {
|
||||
this._serverService.getServerConfig().then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
openAppData(req: ControllerParam) {
|
||||
this._systemService.openLocalPath(PathUtils.getAppData()).then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ class ConfigController extends BaseController {
|
||||
force: true
|
||||
});
|
||||
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
}
|
||||
|
||||
exportConfig(req: ControllerParam) {
|
||||
@ -75,7 +75,7 @@ class ConfigController extends BaseController {
|
||||
"YYYYMMDDhhmmss"
|
||||
)}.toml`;
|
||||
this._serverService.genTomlConfig(path).then(() => {
|
||||
req.event.reply(req.channel, success(path));
|
||||
req.event.reply(req.channel, ResponseUtils.success(path));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseController from "./BaseController";
|
||||
import FrpcProcessService from "../service/FrpcProcessService";
|
||||
import { success } from "../utils/response";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
|
||||
class LaunchController extends BaseController {
|
||||
private readonly _frpcProcessService: FrpcProcessService;
|
||||
@ -14,7 +14,7 @@ class LaunchController extends BaseController {
|
||||
this._frpcProcessService
|
||||
.startFrpcProcess()
|
||||
.then(r => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err, "1");
|
||||
@ -23,13 +23,13 @@ class LaunchController extends BaseController {
|
||||
|
||||
terminate(req: ControllerParam) {
|
||||
this._frpcProcessService.stopFrpcProcess().then(r => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
});
|
||||
}
|
||||
|
||||
getStatus(req: ControllerParam) {
|
||||
const running = this._frpcProcessService.isRunning();
|
||||
req.event.reply(req.channel, success(running));
|
||||
req.event.reply(req.channel, ResponseUtils.success(running));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseController from "./BaseController";
|
||||
import LogService from "../service/LogService";
|
||||
import { fail, success } from "../utils/response";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
|
||||
class LogController extends BaseController {
|
||||
private readonly _logService: LogService;
|
||||
@ -12,23 +12,23 @@ class LogController extends BaseController {
|
||||
|
||||
getFrpLogContent(req: ControllerParam) {
|
||||
this._logService.getFrpLogContent().then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
// watchFrpcLogContent(req: ControllerRequest) {
|
||||
// this._logService.watchFrpcLog().then(data => {
|
||||
// console.log('reply watch', data);
|
||||
// req.event.reply(req.reply, this.success(data));
|
||||
// req.event.reply(req.reply, this.ResponseUtils.success(data));
|
||||
// });
|
||||
// }
|
||||
|
||||
openFrpcLogFile(req: ControllerParam) {
|
||||
this._logService.openFrpcLogFile().then(data => {
|
||||
if (data) {
|
||||
success(null);
|
||||
ResponseUtils.success(null);
|
||||
} else {
|
||||
fail();
|
||||
ResponseUtils.fail();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseController from "./BaseController";
|
||||
import ProxyService from "../service/ProxyService";
|
||||
import { success } from "../utils/response";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
import ProxyRepository from "../repository/ProxyRepository";
|
||||
|
||||
class ProxyController extends BaseController {
|
||||
@ -15,37 +15,37 @@ class ProxyController extends BaseController {
|
||||
|
||||
createProxy(req: ControllerParam) {
|
||||
this._proxyService.insertProxy(req.args).then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
modifyProxy(req: ControllerParam) {
|
||||
this._proxyService.updateProxy(req.args).then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
getAllProxies(req: ControllerParam) {
|
||||
this._proxyDao.findAll().then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
deleteProxy(req: ControllerParam) {
|
||||
this._proxyService.deleteProxy(req.args).then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
modifyProxyStatus(req: ControllerParam) {
|
||||
this._proxyDao.updateProxyStatus(req.args.id, req.args.status).then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
});
|
||||
}
|
||||
|
||||
getLocalPorts(req: ControllerParam) {
|
||||
this._proxyService.getLocalPorts().then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import SystemService from "../service/SystemService";
|
||||
import { fail, success } from "../utils/response";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
import PathUtils from "../utils/PathUtils";
|
||||
|
||||
class SystemController {
|
||||
@ -13,22 +13,22 @@ class SystemController {
|
||||
this._systemService
|
||||
.openUrl(req.args.url)
|
||||
.then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
})
|
||||
.catch(err => {
|
||||
req.event.reply(req.channel, fail());
|
||||
req.event.reply(req.channel, ResponseUtils.fail());
|
||||
});
|
||||
}
|
||||
|
||||
relaunchApp(req: ControllerParam) {
|
||||
this._systemService.relaunch().then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
});
|
||||
}
|
||||
|
||||
openAppData(req: ControllerParam) {
|
||||
this._systemService.openLocalPath(PathUtils.getAppData()).then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseController from "./BaseController";
|
||||
import VersionService from "../service/VersionService";
|
||||
import { fail, success } from "../utils/response";
|
||||
import ResponseUtils from "../utils/ResponseUtils";
|
||||
import VersionRepository from "../repository/VersionRepository";
|
||||
|
||||
class VersionController extends BaseController {
|
||||
@ -17,18 +17,18 @@ class VersionController extends BaseController {
|
||||
this._versionService
|
||||
.getFrpVersionsByGitHub()
|
||||
.then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
})
|
||||
.catch(() => {
|
||||
this._versionService.getFrpVersionByLocalJson().then(localData => {
|
||||
req.event.reply(req.channel, success(localData));
|
||||
req.event.reply(req.channel, ResponseUtils.success(localData));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getDownloadedVersions(req: ControllerParam) {
|
||||
this._versionDao.findAll().then(data => {
|
||||
req.event.reply(req.channel, success(data));
|
||||
req.event.reply(req.channel, ResponseUtils.success(data));
|
||||
});
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ class VersionController extends BaseController {
|
||||
.downloadFrpVersion(req.args.githubReleaseId, progress => {
|
||||
req.event.reply(
|
||||
req.channel,
|
||||
success({
|
||||
ResponseUtils.success({
|
||||
percent: progress.percent,
|
||||
githubReleaseId: req.args.githubReleaseId,
|
||||
completed: progress.percent >= 1
|
||||
@ -56,22 +56,22 @@ class VersionController extends BaseController {
|
||||
this._versionService
|
||||
.deleteFrpVersion(req.args.githubReleaseId)
|
||||
.then(() => {
|
||||
req.event.reply(req.channel, success());
|
||||
req.event.reply(req.channel, ResponseUtils.success());
|
||||
})
|
||||
.catch(err => {
|
||||
req.event.reply(req.channel, fail());
|
||||
req.event.reply(req.channel, ResponseUtils.fail());
|
||||
});
|
||||
}
|
||||
|
||||
importLocalFrpcVersion(req: ControllerParam) {
|
||||
this._versionService
|
||||
.importLocalFrpcVersion(req.win)
|
||||
.then(data => {
|
||||
req.event.reply(req.channel, success());
|
||||
})
|
||||
.catch(err => {
|
||||
req.event.reply(req.channel, fail());
|
||||
});
|
||||
// this._versionService
|
||||
// .importLocalFrpcVersion(req.win)
|
||||
// .then(data => {
|
||||
// req.event.reply(req.channel, ResponseUtils.success());
|
||||
// })
|
||||
// .catch(err => {
|
||||
// req.event.reply(req.channel, ResponseUtils.fail());
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ import VersionRepository from "../repository/VersionRepository";
|
||||
import PathUtils from "../utils/PathUtils";
|
||||
import GlobalConstant from "../core/GlobalConstant";
|
||||
import { app, BrowserWindow, Notification } from "electron";
|
||||
import { success } from "../utils/response";
|
||||
import { success } from "../utils/ResponseUtils";
|
||||
import treeKill from "tree-kill";
|
||||
import BeanFactory from "../core/BeanFactory";
|
||||
|
||||
@ -94,7 +94,7 @@ class FrpcProcessService {
|
||||
const win: BrowserWindow = BeanFactory.getBean("win");
|
||||
win.webContents.send(
|
||||
listenerParam.channel,
|
||||
success(running)
|
||||
ResponseUtils.success(running)
|
||||
);
|
||||
}, GlobalConstant.FRPC_PROCESS_STATUS_CHECK_INTERVAL);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import fs from "fs";
|
||||
import { success } from "../utils/response";
|
||||
import { success } from "../utils/ResponseUtils";
|
||||
import PathUtils from "../utils/PathUtils";
|
||||
import SystemService from "./SystemService";
|
||||
import BeanFactory from "../core/BeanFactory";
|
||||
@ -40,7 +40,7 @@ class LogService {
|
||||
const win: BrowserWindow = BeanFactory.getBean("win");
|
||||
win.webContents.send(
|
||||
listenerParam.channel,
|
||||
success(true)
|
||||
ResponseUtils.success(true)
|
||||
);
|
||||
} else {
|
||||
}
|
||||
|
@ -1,90 +0,0 @@
|
||||
import Datastore from "nedb";
|
||||
import path from "path";
|
||||
import { app } from "electron";
|
||||
|
||||
import { logInfo, logError, LogModule, logDebug } from "../utils/log";
|
||||
import { maskSensitiveData } from "../utils/desensitize";
|
||||
|
||||
const configDB = new Datastore({
|
||||
autoload: true,
|
||||
filename: path.join(app.getPath("userData"), "config.db")
|
||||
});
|
||||
/**
|
||||
* 保存
|
||||
*/
|
||||
export const saveConfig = (
|
||||
document: FrpConfig,
|
||||
cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void
|
||||
) => {
|
||||
document["_id"] = "1";
|
||||
logDebug(
|
||||
LogModule.DB,
|
||||
`Saving configuration to the database. ${JSON.stringify(
|
||||
maskSensitiveData(document, [
|
||||
"serverAddr",
|
||||
"serverPort",
|
||||
"authToken",
|
||||
"user",
|
||||
"metaToken"
|
||||
])
|
||||
)}`
|
||||
);
|
||||
configDB.update(
|
||||
{ _id: "1" },
|
||||
document,
|
||||
{ upsert: true },
|
||||
(err, numberOfUpdated, upsert) => {
|
||||
if (err) {
|
||||
logError(
|
||||
LogModule.DB,
|
||||
`Error saving configuration: ${err.message}`
|
||||
);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Configuration saved successfully. Updated: ${numberOfUpdated}, Upsert: ${upsert}`
|
||||
); // 添加成功日志
|
||||
}
|
||||
if (cb) cb(err, numberOfUpdated, upsert);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 查找
|
||||
* @param cb
|
||||
*/
|
||||
export const getConfig = (
|
||||
cb: (err: Error | null, document: FrpConfig) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, "Retrieving configuration from the database."); // 添加信息日志
|
||||
configDB.findOne({ _id: "1" }, (err, document) => {
|
||||
if (err) {
|
||||
logError(
|
||||
LogModule.DB,
|
||||
`Error retrieving configuration: ${err.message}`
|
||||
); // 添加错误日志
|
||||
} else {
|
||||
logInfo(LogModule.DB, "Configuration retrieved successfully."); // 添加成功日志
|
||||
}
|
||||
cb(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
export const clearConfig = (cb?: (err: Error | null, n: number) => void) => {
|
||||
logInfo(LogModule.DB, "Clearing all configurations from the database."); // 添加信息日志
|
||||
configDB.remove({}, { multi: true }, (err, n) => {
|
||||
if (err) {
|
||||
logError(
|
||||
LogModule.DB,
|
||||
`Error clearing configurations: ${err.message}`
|
||||
); // 添加错误日志
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Successfully cleared configurations. Number of documents removed: ${n}`
|
||||
); // 添加成功日志
|
||||
}
|
||||
if (cb) cb(err, n);
|
||||
});
|
||||
};
|
@ -1,143 +0,0 @@
|
||||
import Datastore from "nedb";
|
||||
import path from "path";
|
||||
import { app } from "electron";
|
||||
|
||||
import { logInfo, logError, LogModule, logDebug } from "../utils/log";
|
||||
|
||||
const proxyDB = new Datastore({
|
||||
autoload: true,
|
||||
filename: path.join(app.getPath("userData"), "proxy.db")
|
||||
});
|
||||
|
||||
export const insertProxy = (
|
||||
proxy: Proxy,
|
||||
cb?: (err: Error | null, document: Proxy) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Inserting proxy: ${JSON.stringify(proxy)}`);
|
||||
proxyDB.insert(proxy, (err, document) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error inserting proxy: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxy inserted successfully: ${JSON.stringify(document)}`
|
||||
);
|
||||
}
|
||||
if (cb) cb(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteProxyById = (
|
||||
_id: string,
|
||||
cb?: (err: Error | null, n: number) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Deleting proxy with ID: ${_id}`);
|
||||
proxyDB.remove({ _id: _id }, (err, n) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error deleting proxy: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxy deleted successfully. Number of documents removed: ${n}`
|
||||
);
|
||||
}
|
||||
if (cb) cb(err, n);
|
||||
});
|
||||
};
|
||||
|
||||
export const updateProxyById = (
|
||||
proxy: Proxy,
|
||||
cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Updating proxy: ${JSON.stringify(proxy)}`);
|
||||
proxyDB.update(
|
||||
{ _id: proxy._id },
|
||||
proxy,
|
||||
{},
|
||||
(err, numberOfUpdated, upsert) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error updating proxy: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxy updated successfully. Updated: ${numberOfUpdated}, Upsert: ${upsert}`
|
||||
);
|
||||
}
|
||||
if (cb) cb(err, numberOfUpdated, upsert);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const listProxy = (
|
||||
callback: (err: Error | null, documents: Proxy[]) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Listing all proxies`);
|
||||
proxyDB.find({}, (err, documents) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error listing proxies: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxies listed successfully. Count: ${documents.length}`
|
||||
);
|
||||
}
|
||||
callback(err, documents);
|
||||
});
|
||||
};
|
||||
|
||||
export const getProxyById = (
|
||||
id: string,
|
||||
callback: (err: Error | null, document: Proxy) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Getting proxy by ID: ${id}`);
|
||||
proxyDB.findOne({ _id: id }, (err, document) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error getting proxy by ID: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxy retrieved successfully: ${JSON.stringify(document)}`
|
||||
);
|
||||
}
|
||||
callback(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
export const clearProxy = (cb?: (err: Error | null, n: number) => void) => {
|
||||
logInfo(LogModule.DB, `Clearing all proxies`);
|
||||
proxyDB.remove({}, { multi: true }, (err, n) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error clearing proxies: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxies cleared successfully. Number of documents removed: ${n}`
|
||||
);
|
||||
}
|
||||
if (cb) cb(err, n);
|
||||
});
|
||||
};
|
||||
|
||||
export const updateProxyStatus = (
|
||||
id: string,
|
||||
st: boolean,
|
||||
cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Updating proxy status for ID: ${id} to ${st}`);
|
||||
proxyDB.update(
|
||||
{ _id: id },
|
||||
{ $set: { status: st } },
|
||||
{},
|
||||
(err, numberOfUpdated, upsert) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error updating proxy status: ${err.message}`);
|
||||
} else {
|
||||
logInfo(
|
||||
LogModule.DB,
|
||||
`Proxy status updated successfully. Updated: ${numberOfUpdated}, Upsert: ${upsert}`
|
||||
);
|
||||
}
|
||||
if (cb) cb(err, numberOfUpdated, upsert);
|
||||
}
|
||||
);
|
||||
};
|
@ -1,90 +0,0 @@
|
||||
import Datastore from "nedb";
|
||||
import path from "path";
|
||||
import { app } from "electron";
|
||||
|
||||
import { logInfo, logError, LogModule, logDebug } from "../utils/log";
|
||||
|
||||
const versionDB = new Datastore({
|
||||
autoload: true,
|
||||
filename: path.join(app.getPath("userData"), "version.db")
|
||||
});
|
||||
|
||||
/**
|
||||
* Insert version
|
||||
* @param version
|
||||
* @param cb
|
||||
*/
|
||||
export const insertVersion = (
|
||||
version: FrpVersion,
|
||||
cb?: (err: Error | null, document: any) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Inserting version: ${JSON.stringify(version)}`);
|
||||
versionDB.insert(version, (err, document) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error inserting version: ${err.message}`);
|
||||
} else {
|
||||
logInfo(LogModule.DB, `Version inserted successfully: ${JSON.stringify(document)}`);
|
||||
}
|
||||
if (cb) cb(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* List versions
|
||||
* @param cb
|
||||
*/
|
||||
export const listVersion = (
|
||||
callback: (err: Error | null, documents: FrpVersion[]) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, "Listing all versions.");
|
||||
versionDB.find({}, (err, documents) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error listing versions: ${err.message}`);
|
||||
} else {
|
||||
logInfo(LogModule.DB, `Successfully listed versions: ${documents.length} found.`);
|
||||
}
|
||||
callback(err, documents);
|
||||
});
|
||||
};
|
||||
|
||||
export const getVersionById = (
|
||||
id: number,
|
||||
callback: (err: Error | null, document: FrpVersion) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Retrieving version by ID: ${id}`);
|
||||
versionDB.findOne({ id: id }, (err, document) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error retrieving version by ID: ${err.message}`);
|
||||
} else {
|
||||
logInfo(LogModule.DB, `Version retrieved successfully: ${JSON.stringify(document)}`);
|
||||
}
|
||||
callback(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteVersionById = (
|
||||
id: string,
|
||||
callback: (err: Error | null, document: any) => void
|
||||
) => {
|
||||
logInfo(LogModule.DB, `Deleting version: ${id}`);
|
||||
versionDB.remove({ id: id }, (err, document) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error deleting version: ${err.message}`);
|
||||
} else {
|
||||
logInfo(LogModule.DB, `Version deleted successfully: ${id}`);
|
||||
}
|
||||
callback(err, document);
|
||||
});
|
||||
};
|
||||
|
||||
export const clearVersion = (cb?: (err: Error | null, n: number) => void) => {
|
||||
logInfo(LogModule.DB, "Clearing all versions from the database.");
|
||||
versionDB.remove({}, { multi: true }, (err, n) => {
|
||||
if (err) {
|
||||
logError(LogModule.DB, `Error clearing versions: ${err.message}`);
|
||||
} else {
|
||||
logInfo(LogModule.DB, `Successfully cleared versions. Number of documents removed: ${n}`);
|
||||
}
|
||||
if (cb) cb(err, n);
|
||||
});
|
||||
};
|
21
electron/utils/ResponseUtils.ts
Normal file
21
electron/utils/ResponseUtils.ts
Normal file
@ -0,0 +1,21 @@
|
||||
class ResponseUtils {
|
||||
public static success<T>(data?: any, message?: string) {
|
||||
const resp: ApiResponse<T> = {
|
||||
success: true,
|
||||
data: data,
|
||||
message: message || "successful."
|
||||
};
|
||||
return resp;
|
||||
}
|
||||
|
||||
public static fail(message?: string) {
|
||||
const resp: ApiResponse<any> = {
|
||||
success: false,
|
||||
data: null,
|
||||
message: message || "internal error."
|
||||
};
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
export default ResponseUtils;
|
@ -1,12 +0,0 @@
|
||||
export const maskSensitiveData = (
|
||||
obj: Record<string, any>,
|
||||
keysToMask: string[]
|
||||
) => {
|
||||
const maskedObj = JSON.parse(JSON.stringify(obj));
|
||||
keysToMask.forEach(key => {
|
||||
if (maskedObj.hasOwnProperty(key)) {
|
||||
maskedObj[key] = "***";
|
||||
}
|
||||
});
|
||||
return maskedObj;
|
||||
};
|
@ -1,30 +0,0 @@
|
||||
import log from "electron-log";
|
||||
// 定义模块枚举
|
||||
export enum LogModule {
|
||||
APP = "app",
|
||||
FRP_CLIENT = "frpc client",
|
||||
GITHUB = "github",
|
||||
DB = "db"
|
||||
}
|
||||
|
||||
export const initLog = () => {
|
||||
log.transports.file.level = "debug";
|
||||
log.transports.console.level = "debug";
|
||||
};
|
||||
|
||||
// 自定义日志输出函数,记录到指定业务模块
|
||||
export const logInfo = (module: LogModule, message: string) => {
|
||||
log.info(`[${module}] ${message}`);
|
||||
};
|
||||
|
||||
export const logError = (module: LogModule, message: string) => {
|
||||
log.error(`[${module}] ${message}`);
|
||||
};
|
||||
|
||||
export const logDebug = (module: LogModule, message: string) => {
|
||||
log.debug(`[${module}] ${message}`);
|
||||
};
|
||||
|
||||
export const logWarn = (module: LogModule, message: string) => {
|
||||
log.warn(`[${module}] ${message}`);
|
||||
};
|
@ -1,26 +0,0 @@
|
||||
export function success<T>(data?: any, message?: string) {
|
||||
const resp: ApiResponse<T> = {
|
||||
success: true,
|
||||
data: data,
|
||||
message: message || "successful."
|
||||
};
|
||||
return resp;
|
||||
}
|
||||
|
||||
// export function success(message?: string) {
|
||||
// const resp: ApiResponse<void> = {
|
||||
// success: true,
|
||||
// data: null,
|
||||
// message: message || "successful."
|
||||
// };
|
||||
// return resp;
|
||||
// }
|
||||
|
||||
export function fail(message?: string) {
|
||||
const resp: ApiResponse<any> = {
|
||||
success: false,
|
||||
data: null,
|
||||
message: message || "internal error."
|
||||
};
|
||||
return resp;
|
||||
}
|
Loading…
Reference in New Issue
Block a user