created config api
This commit is contained in:
@@ -31,6 +31,7 @@ const userRoutes = require("./electron/api/user/user.routes");
|
|||||||
const listRoutes = require("./electron/api/list/list.routes");
|
const listRoutes = require("./electron/api/list/list.routes");
|
||||||
const anilistRoute = require("./electron/api/anilist/anilist");
|
const anilistRoute = require("./electron/api/anilist/anilist");
|
||||||
const localRoutes = require("./electron/api/local/local.routes");
|
const localRoutes = require("./electron/api/local/local.routes");
|
||||||
|
const configRoutes = require("./electron/api/config/config.routes");
|
||||||
|
|
||||||
fastify.addHook("preHandler", async (request) => {
|
fastify.addHook("preHandler", async (request) => {
|
||||||
const auth = request.headers.authorization;
|
const auth = request.headers.authorization;
|
||||||
@@ -73,6 +74,7 @@ fastify.register(userRoutes, { prefix: "/api" });
|
|||||||
fastify.register(anilistRoute, { prefix: "/api" });
|
fastify.register(anilistRoute, { prefix: "/api" });
|
||||||
fastify.register(listRoutes, { prefix: "/api" });
|
fastify.register(listRoutes, { prefix: "/api" });
|
||||||
fastify.register(localRoutes, { prefix: "/api" });
|
fastify.register(localRoutes, { prefix: "/api" });
|
||||||
|
fastify.register(configRoutes, { prefix: "/api" });
|
||||||
|
|
||||||
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
||||||
|
|
||||||
|
|||||||
43
desktop/src/api/config/config.controller.ts
Normal file
43
desktop/src/api/config/config.controller.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import {FastifyReply, FastifyRequest} from 'fastify';
|
||||||
|
import {getConfig, setConfig} from '../../shared/config';
|
||||||
|
|
||||||
|
export async function getFullConfig(req: FastifyRequest, reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
return getConfig();
|
||||||
|
} catch (err) {
|
||||||
|
return { error: "Error loading config" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getConfigSection(req: FastifyRequest<{ Params: { section: string } }>, reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
const { section } = req.params;
|
||||||
|
const config = getConfig();
|
||||||
|
|
||||||
|
if (config[section] === undefined) {
|
||||||
|
return { error: "Section not found" };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { [section]: config[section] };
|
||||||
|
} catch (err) {
|
||||||
|
return { error: "Error loading config section" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateConfig(req: FastifyRequest<{ Body: any }>, reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
return setConfig(req.body);
|
||||||
|
} catch (err) {
|
||||||
|
return { error: "Error updating config" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateConfigSection(req: FastifyRequest<{ Params: { section: string }, Body: any }>, reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
const { section } = req.params;
|
||||||
|
const updatedConfig = setConfig({ [section]: req.body });
|
||||||
|
return { [section]: updatedConfig[section] };
|
||||||
|
} catch (err) {
|
||||||
|
return { error: "Error updating config section" };
|
||||||
|
}
|
||||||
|
}
|
||||||
11
desktop/src/api/config/config.routes.ts
Normal file
11
desktop/src/api/config/config.routes.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { FastifyInstance } from 'fastify';
|
||||||
|
import * as controller from './config.controller';
|
||||||
|
|
||||||
|
async function configRoutes(fastify: FastifyInstance) {
|
||||||
|
fastify.get('/config', controller.getFullConfig);
|
||||||
|
fastify.get('/config/:section', controller.getConfigSection);
|
||||||
|
fastify.post('/config', controller.updateConfig);
|
||||||
|
fastify.post('/config/:section', controller.updateConfigSection);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default configRoutes;
|
||||||
Reference in New Issue
Block a user