upd: api jenna perangkat desa
Deskripsi: - api yg akan diakses oleh jenna perangkat desa - struktur api keys - migrasi database No Issues
This commit is contained in:
@@ -12,7 +12,7 @@ import "moment/locale/id";
|
||||
const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
.use(cors({
|
||||
origin: "*",
|
||||
methods: ["GET", "POST", "OPTIONS"],
|
||||
methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"]
|
||||
}))
|
||||
.use(swagger({
|
||||
@@ -1531,11 +1531,79 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
},
|
||||
}
|
||||
)
|
||||
;
|
||||
|
||||
;
|
||||
// ─── API KEY MANAGEMENT ──────────────────────────────────────────────────
|
||||
|
||||
.get("/api-keys", async ({ set }) => {
|
||||
try {
|
||||
const keys = await prisma.apiKey.findMany({ orderBy: { createdAt: "desc" } });
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan API keys",
|
||||
data: keys.map((k) => ({ ...k, key: k.key.slice(0, 8) + "••••••••" + k.key.slice(-4) })),
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return { success: false, message: "Gagal mendapatkan API keys" };
|
||||
}
|
||||
}, {
|
||||
detail: { summary: "List API Keys", tags: ["api-key"] },
|
||||
})
|
||||
|
||||
.post("/api-keys", async ({ body, set }) => {
|
||||
try {
|
||||
const rawKey = "ak_" + crypto.randomUUID().replace(/-/g, "");
|
||||
const key = await prisma.apiKey.create({ data: { name: body.name, key: rawKey } });
|
||||
return {
|
||||
success: true,
|
||||
message: "API key berhasil dibuat",
|
||||
data: { ...key, key: rawKey },
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return { success: false, message: "Gagal membuat API key" };
|
||||
}
|
||||
}, {
|
||||
body: t.Object({ name: t.String({ description: "Nama key" }) }),
|
||||
detail: { summary: "Buat API Key", tags: ["api-key"] },
|
||||
})
|
||||
|
||||
.patch("/api-keys/:id", async ({ params, body, set }) => {
|
||||
try {
|
||||
const key = await prisma.apiKey.update({
|
||||
where: { id: params.id },
|
||||
data: { isActive: body.isActive },
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "API key berhasil diupdate",
|
||||
data: { ...key, key: key.key.slice(0, 8) + "••••••••" + key.key.slice(-4) },
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return { success: false, message: "Gagal mengupdate API key" };
|
||||
}
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
body: t.Object({ isActive: t.Boolean({ description: "Status aktif" }) }),
|
||||
detail: { summary: "Toggle API Key", tags: ["api-key"] },
|
||||
})
|
||||
|
||||
.delete("/api-keys/:id", async ({ params, set }) => {
|
||||
try {
|
||||
await prisma.apiKey.delete({ where: { id: params.id } });
|
||||
return { success: true, message: "API key berhasil dihapus" };
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return { success: false, message: "Gagal menghapus API key" };
|
||||
}
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
detail: { summary: "Hapus API Key", tags: ["api-key"] },
|
||||
});
|
||||
|
||||
export const GET = MonitoringServer.handle;
|
||||
export const POST = MonitoringServer.handle;
|
||||
export const PATCH = MonitoringServer.handle;
|
||||
export const DELETE = MonitoringServer.handle;
|
||||
export const OPTIONS = MonitoringServer.handle;
|
||||
|
||||
Reference in New Issue
Block a user