Created the key.ts router

This commit is contained in:
Mathias Wagner 2023-01-22 18:04:27 +01:00
parent e75a2cc38c
commit d53a9ed23f
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

31
src/routes/v1/key.ts Normal file
View File

@ -0,0 +1,31 @@
import { Request, Response, Router } from "express";
import { createKey, deleteKey, listKeys } from "@controller/key";
import { validateSchema } from "@utils/error";
import { keyCreationValidation } from "./validations/key";
const app: Router = Router();
app.get("/:projectId/list", async (req: Request, res: Response) => {
const keys = await listKeys(String(req.user?._id), req.params.projectId);
if ("code" in keys) return res.json(keys);
res.json(keys);
});
app.put("/:projectId", async (req: Request, res: Response) => {
if (validateSchema(res, keyCreationValidation, req.body)) return;
const key = await createKey(String(req.user?._id), req.params.projectId, req.body);
if ("code" in key) return res.json(key);
res.json({ message: "Your key has been successfully created", token: key.token });
});
app.delete("/:projectId/:keyId", async (req: Request, res: Response) => {
const keyError = await deleteKey(String(req.user?._id), req.params.projectId, req.params.keyId);
if (keyError) return res.json(keyError);
res.json({message: "The provided key has been successfully deleted"});
});
export default app;