-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
40 lines (29 loc) · 811 Bytes
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const Router = require("koa-router");
const { createEnv, getEnv, deleteEnv, existsEnv } = require("./envsModel");
const router = new Router();
router.post("/envs", async(ctx) => {
const { id } = await createEnv();
const env = await getEnv(id);
ctx.body = {
...env,
};
});
const checkId = async (ctx, next) => {
if (!ctx.params.id.match(/^e[a-f0-9]{16}$/)) {
ctx.throw(400, "Invalid id");
}
if (!await existsEnv(ctx.params.id)) {
ctx.throw(404, "Env not found");
}
return next();
};
router.get("/envs/:id", checkId, async(ctx) => {
ctx.body = {
...await getEnv(ctx.params.id),
};
});
router.delete("/envs/:id", checkId, async(ctx) => {
await deleteEnv(ctx.params.id);
ctx.status = 204;
});
module.exports = router;