feat(apihits): so i can see if what end points are being used and when and how often
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import type {User} from "../../../../types/users.js";
|
||||
import {verify} from "hono/jwt";
|
||||
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
|
||||
import {addModule} from "../../controller/module/addModule.js";
|
||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import type { User } from "../../../../types/users.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||
import { addModule } from "../../controller/module/addModule.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the response schema
|
||||
const responseSchema = z.object({
|
||||
message: z.string().optional(),
|
||||
module_id: z.string().openapi({example: "6c922c6c-7de3-4ec4-acb0-f068abdc"}).optional(),
|
||||
name: z.string().openapi({example: "Production"}).optional(),
|
||||
active: z.boolean().openapi({example: true}).optional(),
|
||||
roles: z.string().openapi({example: `["viewer","technician"]`}).optional(),
|
||||
module_id: z
|
||||
.string()
|
||||
.openapi({ example: "6c922c6c-7de3-4ec4-acb0-f068abdc" })
|
||||
.optional(),
|
||||
name: z.string().openapi({ example: "Production" }).optional(),
|
||||
active: z.boolean().openapi({ example: true }).optional(),
|
||||
roles: z
|
||||
.string()
|
||||
.openapi({ example: `["viewer","technician"]` })
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
@@ -29,7 +36,7 @@ const app = new OpenAPIHono();
|
||||
// });
|
||||
|
||||
const AddModule = z.object({
|
||||
name: z.string().openapi({example: "production"}),
|
||||
name: z.string().openapi({ example: "production" }),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -42,21 +49,26 @@ app.openapi(
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {schema: AddModule},
|
||||
"application/json": { schema: AddModule },
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {schema: responseSchema},
|
||||
"application/json": { schema: responseSchema },
|
||||
},
|
||||
description: "Response message",
|
||||
},
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -64,7 +76,12 @@ app.openapi(
|
||||
401: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Unauthenticated" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Unauthorized",
|
||||
@@ -72,7 +89,12 @@ app.openapi(
|
||||
500: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -92,16 +114,26 @@ app.openapi(
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
user = payload.user as User;
|
||||
} catch (error) {
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
return c.json({ message: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await c?.req.json();
|
||||
apiHit(c, { endpoint: `/modules`, lastBody: data });
|
||||
await addModule(data, user.user_id ?? "");
|
||||
return c.json({success: true, message: "New setting was added"}, 200);
|
||||
return c.json(
|
||||
{ success: true, message: "New setting was added" },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json({message: "Please make sure you are not missing your data.", error}, 400);
|
||||
return c.json(
|
||||
{
|
||||
message: "Please make sure you are not missing your data.",
|
||||
error,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { modules } from "../../../../../database/schema/modules.js";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { desc } from "drizzle-orm";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
@@ -45,6 +46,7 @@ app.openapi(
|
||||
}),
|
||||
async (c) => {
|
||||
//console.log("system modules");
|
||||
apiHit(c, { endpoint: `/getmodules`, action: "just modules" });
|
||||
let module: any = [];
|
||||
try {
|
||||
module = await db.select().from(modules).orderBy(modules.name); // .where(eq(modules.active, true));
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { modules } from "../../../../../database/schema/modules.js";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { subModules } from "../../../../../database/schema/subModules.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
@@ -45,6 +46,7 @@ app.openapi(
|
||||
}),
|
||||
async (c) => {
|
||||
//console.log("system modules");
|
||||
apiHit(c, { endpoint: `/submodules` });
|
||||
let module: any = [];
|
||||
try {
|
||||
module = await db
|
||||
|
||||
@@ -1,31 +1,38 @@
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import type {User} from "../../../../types/users.js";
|
||||
import {verify} from "hono/jwt";
|
||||
import {updateModule} from "../../controller/module/updateModule.js";
|
||||
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
|
||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import type { User } from "../../../../types/users.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import { updateModule } from "../../controller/module/updateModule.js";
|
||||
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the response schema
|
||||
const responseSchema = z.object({
|
||||
message: z.string().optional(),
|
||||
module_id: z.string().openapi({example: "6c922c6c-7de3-4ec4-acb0-f068abdc"}).optional(),
|
||||
name: z.string().openapi({example: "Production"}).optional(),
|
||||
active: z.boolean().openapi({example: true}).optional(),
|
||||
roles: z.string().openapi({example: `["viewer","technician"]`}).optional(),
|
||||
module_id: z
|
||||
.string()
|
||||
.openapi({ example: "6c922c6c-7de3-4ec4-acb0-f068abdc" })
|
||||
.optional(),
|
||||
name: z.string().openapi({ example: "Production" }).optional(),
|
||||
active: z.boolean().openapi({ example: true }).optional(),
|
||||
roles: z
|
||||
.string()
|
||||
.openapi({ example: `["viewer","technician"]` })
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
id: z
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "id",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "id",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -41,14 +48,19 @@ app.openapi(
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {schema: responseSchema},
|
||||
"application/json": { schema: responseSchema },
|
||||
},
|
||||
description: "Response message",
|
||||
},
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -56,7 +68,12 @@ app.openapi(
|
||||
401: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Unauthenticated" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Unauthorized",
|
||||
@@ -64,7 +81,12 @@ app.openapi(
|
||||
500: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -72,7 +94,7 @@ app.openapi(
|
||||
},
|
||||
}),
|
||||
async (c) => {
|
||||
const {id} = c.req.valid("param");
|
||||
const { id } = c.req.valid("param");
|
||||
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
const authHeader = c.req.header("Authorization");
|
||||
@@ -84,16 +106,30 @@ app.openapi(
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
user = payload.user as User;
|
||||
} catch (error) {
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
return c.json({ message: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await c?.req.json();
|
||||
apiHit(c, {
|
||||
endpoint: `/modules/${id}`,
|
||||
action: "just update modules",
|
||||
lastBody: data,
|
||||
});
|
||||
await updateModule(data, id ?? "");
|
||||
return c.json({success: true, message: "New setting was added"}, 200);
|
||||
return c.json(
|
||||
{ success: true, message: "New setting was added" },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json({message: "Please make sure you are not missing your data.", error}, 400);
|
||||
return c.json(
|
||||
{
|
||||
message: "Please make sure you are not missing your data.",
|
||||
error,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
|
||||
@@ -4,6 +4,7 @@ import { verify } from "hono/jwt";
|
||||
|
||||
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||
import { updateSubModule } from "../../controller/module/updateSubModule.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the response schema
|
||||
const responseSchema = z.object({
|
||||
@@ -112,6 +113,11 @@ app.openapi(
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await c?.req.json();
|
||||
apiHit(c, {
|
||||
endpoint: `/submodules/${id}`,
|
||||
action: "just updates submodules",
|
||||
lastBody: data,
|
||||
});
|
||||
await updateSubModule(data, id ?? "");
|
||||
return c.json({ success: true, message: "Module Updated" }, 200);
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {db} from "../../../../../database/dbclient.js";
|
||||
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
|
||||
import {serverData} from "../../../../../database/schema/serverData.js";
|
||||
import {eq} from "drizzle-orm";
|
||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||
import { serverData } from "../../../../../database/schema/serverData.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
@@ -15,10 +16,16 @@ const requestSchema = z.object({
|
||||
// Define the response schema
|
||||
const responseSchema = z.object({
|
||||
message: z.string().optional(),
|
||||
module_id: z.string().openapi({example: "6c922c6c-7de3-4ec4-acb0-f068abdc"}).optional(),
|
||||
name: z.string().openapi({example: "Production"}).optional(),
|
||||
active: z.boolean().openapi({example: true}).optional(),
|
||||
roles: z.string().openapi({example: `["viewer","technician"]`}).optional(),
|
||||
module_id: z
|
||||
.string()
|
||||
.openapi({ example: "6c922c6c-7de3-4ec4-acb0-f068abdc" })
|
||||
.optional(),
|
||||
name: z.string().openapi({ example: "Production" }).optional(),
|
||||
active: z.boolean().openapi({ example: true }).optional(),
|
||||
roles: z
|
||||
.string()
|
||||
.openapi({ example: `["viewer","technician"]` })
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
@@ -33,7 +40,7 @@ app.openapi(
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {schema: responseSchema},
|
||||
"application/json": { schema: responseSchema },
|
||||
},
|
||||
description: "Response message",
|
||||
},
|
||||
@@ -41,16 +48,22 @@ app.openapi(
|
||||
}),
|
||||
async (c) => {
|
||||
//console.log("system modules");
|
||||
apiHit(c, { endpoint: `/servers` });
|
||||
let servers: any = [];
|
||||
try {
|
||||
servers = await db.select().from(serverData).where(eq(serverData.active, true));
|
||||
servers = await db
|
||||
.select()
|
||||
.from(serverData)
|
||||
.where(eq(serverData.active, true));
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
servers = [];
|
||||
}
|
||||
|
||||
// sort the servers by there name
|
||||
servers = servers.sort((a: any, b: any) => a.sName.localeCompare(b.sName));
|
||||
servers = servers.sort((a: any, b: any) =>
|
||||
a.sName.localeCompare(b.sName)
|
||||
);
|
||||
|
||||
// Return response with the received data
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import hasCorrectRole from "../../../auth/middleware/roleCheck.js";
|
||||
import { serviceControl } from "../../controller/server/serviceControl.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
@@ -33,7 +34,7 @@ app.openapi(
|
||||
}),
|
||||
async (c) => {
|
||||
const { data, error } = await tryCatch(c.req.json());
|
||||
|
||||
//apiHit(c, { endpoint: `/serviceprocess`, lastBody: data });
|
||||
if (error) {
|
||||
return c.json({
|
||||
success: false,
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import {addSetting} from "../../controller/settings/addSetting.js";
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { addSetting } from "../../controller/settings/addSetting.js";
|
||||
|
||||
import {verify} from "hono/jwt";
|
||||
import type {User} from "../../../../types/users.js";
|
||||
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
|
||||
import {responses} from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import type { User } from "../../../../types/users.js";
|
||||
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const AddSetting = z.object({
|
||||
name: z.string().openapi({example: "server"}),
|
||||
value: z.string().openapi({example: "localhost"}),
|
||||
description: z.string().openapi({example: "The server we are going to connect to"}),
|
||||
roles: z.string().openapi({example: "admin"}),
|
||||
module: z.string().openapi({example: "production"}),
|
||||
name: z.string().openapi({ example: "server" }),
|
||||
value: z.string().openapi({ example: "localhost" }),
|
||||
description: z
|
||||
.string()
|
||||
.openapi({ example: "The server we are going to connect to" }),
|
||||
roles: z.string().openapi({ example: "admin" }),
|
||||
module: z.string().openapi({ example: "production" }),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -26,7 +29,7 @@ app.openapi(
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {schema: AddSetting},
|
||||
"application/json": { schema: AddSetting },
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -43,16 +46,26 @@ app.openapi(
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
user = payload.user as User;
|
||||
} catch (error) {
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
return c.json({ message: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await c?.req.json();
|
||||
apiHit(c, { endpoint: `/addsettings`, lastBody: data });
|
||||
await addSetting(data, user.user_id ?? "");
|
||||
return c.json({success: true, message: "New setting was added"}, 200);
|
||||
return c.json(
|
||||
{ success: true, message: "New setting was added" },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json({message: "Please make sure you are not missing your data.", error}, 400);
|
||||
return c.json(
|
||||
{
|
||||
message: "Please make sure you are not missing your data.",
|
||||
error,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
|
||||
import type {User} from "../../../../types/users.js";
|
||||
import {verify} from "hono/jwt";
|
||||
import {getSettings} from "../../controller/settings/getSettings.js";
|
||||
import type { User } from "../../../../types/users.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import { getSettings } from "../../controller/settings/getSettings.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
@@ -17,8 +18,8 @@ app.openapi(
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: true}),
|
||||
message: z.string().openapi({example: "Starter"}),
|
||||
success: z.boolean().openapi({ example: true }),
|
||||
message: z.string().openapi({ example: "Starter" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
@@ -27,7 +28,12 @@ app.openapi(
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -35,7 +41,12 @@ app.openapi(
|
||||
401: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Unauthenticated" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Unauthorized",
|
||||
@@ -43,7 +54,12 @@ app.openapi(
|
||||
500: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -52,13 +68,19 @@ app.openapi(
|
||||
}),
|
||||
async (c) => {
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
|
||||
apiHit(c, { endpoint: `/settings` });
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await getSettings();
|
||||
return c.json({success: true, message: "All Current Settings", data}, 200);
|
||||
return c.json(
|
||||
{ success: true, message: "All Current Settings", data },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json({message: "There was an error getting the settings.", error}, 400);
|
||||
return c.json(
|
||||
{ message: "There was an error getting the settings.", error },
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import type {User} from "../../../../types/users.js";
|
||||
import {verify} from "hono/jwt";
|
||||
import {updateSetting} from "../../controller/settings/updateSetting.js";
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import type { User } from "../../../../types/users.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import { updateSetting } from "../../controller/settings/updateSetting.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const UpdateSetting = z.object({
|
||||
name: z.string().openapi({example: "server"}),
|
||||
value: z.string().openapi({example: "localhost"}),
|
||||
name: z.string().openapi({ example: "server" }),
|
||||
value: z.string().openapi({ example: "localhost" }),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -19,7 +20,7 @@ app.openapi(
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {schema: UpdateSetting},
|
||||
"application/json": { schema: UpdateSetting },
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -28,8 +29,8 @@ app.openapi(
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: true}),
|
||||
message: z.string().openapi({example: "Starter"}),
|
||||
success: z.boolean().openapi({ example: true }),
|
||||
message: z.string().openapi({ example: "Starter" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
@@ -38,7 +39,12 @@ app.openapi(
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -46,7 +52,12 @@ app.openapi(
|
||||
401: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Unauthenticated" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Unauthorized",
|
||||
@@ -54,7 +65,12 @@ app.openapi(
|
||||
500: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
schema: z.object({
|
||||
message: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Internal Server error" }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
@@ -66,11 +82,17 @@ app.openapi(
|
||||
const authHeader = c.req.header("Authorization");
|
||||
|
||||
if (authHeader?.includes("Basic")) {
|
||||
return c.json({message: "You are a Basic user! Please login to get a token"}, 401);
|
||||
return c.json(
|
||||
{
|
||||
message:
|
||||
"You are a Basic user! Please login to get a token",
|
||||
},
|
||||
401
|
||||
);
|
||||
}
|
||||
|
||||
if (!authHeader) {
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
return c.json({ message: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const token = authHeader?.split("Bearer ")[1] || "";
|
||||
@@ -80,16 +102,27 @@ app.openapi(
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
user = payload.user as User;
|
||||
} catch (error) {
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
return c.json({ message: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// now pass all the data over to update the user info
|
||||
try {
|
||||
const data = await c?.req.json();
|
||||
apiHit(c, { endpoint: `/updatesettings`, lastBody: data });
|
||||
await updateSetting(data, user.user_id ?? "");
|
||||
return c.json({success: true, message: "The Setting was just updated", data}, 200);
|
||||
return c.json(
|
||||
{
|
||||
success: true,
|
||||
message: "The Setting was just updated",
|
||||
data,
|
||||
},
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json({message: "There was an error updating the settings.", error}, 400);
|
||||
return c.json(
|
||||
{ message: "There was an error updating the settings.", error },
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
processAllServers,
|
||||
updateServer,
|
||||
} from "../../../../scripts/updateServers.js";
|
||||
import { apiHit } from "../../../../globalUtils/apiHits.js";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
@@ -80,7 +81,7 @@ app.openapi(
|
||||
async (c) => {
|
||||
const { server } = c.req.valid("param");
|
||||
const body = await c.req.json();
|
||||
|
||||
apiHit(c, { endpoint: `/update/${server}`, lastBody: body });
|
||||
// fire off the update we wont make this way
|
||||
if (body.all) {
|
||||
const update = await processAllServers(body.devDir);
|
||||
|
||||
Reference in New Issue
Block a user