forked from pinks/eris
1
0
Fork 0
nyx/api/paramsRoute.ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-10-05 09:00:51 +00:00
import { deepMerge } from "std/collections/deep_merge.ts";
2023-10-15 19:13:38 +00:00
import { info } from "std/log/mod.ts";
2023-10-08 21:23:54 +00:00
import { createEndpoint, createMethodFilter } from "t_rest/server";
2023-10-05 09:00:51 +00:00
import { configSchema, getConfig, setConfig } from "../app/config.ts";
2023-10-17 13:03:14 +00:00
import { withUser } from "./withUser.ts";
2023-10-05 09:00:51 +00:00
2023-10-08 21:23:54 +00:00
export const paramsRoute = createMethodFilter({
GET: createEndpoint(
2023-10-05 09:00:51 +00:00
{ query: null, body: null },
async () => {
const config = await getConfig();
2023-10-08 21:23:54 +00:00
return { status: 200, body: { type: "application/json", data: config.defaultParams } };
2023-10-05 09:00:51 +00:00
},
),
2023-10-08 21:23:54 +00:00
PATCH: createEndpoint(
2023-10-05 09:00:51 +00:00
{
query: { sessionId: { type: "string" } },
body: {
type: "application/json",
schema: configSchema.properties.defaultParams,
},
},
async ({ query, body }) => {
2023-10-17 13:03:14 +00:00
return withUser(query, async (chat) => {
const config = await getConfig();
info(`User ${chat.username} updated default params: ${JSON.stringify(body.data)}`);
const defaultParams = deepMerge(config.defaultParams ?? {}, body.data);
await setConfig({ defaultParams });
return { status: 200, body: { type: "application/json", data: config.defaultParams } };
}, { admin: true });
2023-10-05 09:00:51 +00:00
},
),
2023-10-08 21:23:54 +00:00
});