feat(apihits): so i can see if what end points are being used and when and how often
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user