From c63e558f402cfad914031a58fdcf3d8e0f3d125d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 25 Jul 2021 12:32:59 +0200 Subject: Moved backend routes to a dedicated directory --- packages/server/src/routes/api/v1/repo/branches.ts | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/server/src/routes/api/v1/repo/branches.ts (limited to 'packages/server/src/routes/api/v1/repo/branches.ts') diff --git a/packages/server/src/routes/api/v1/repo/branches.ts b/packages/server/src/routes/api/v1/repo/branches.ts new file mode 100644 index 0000000..10ac736 --- /dev/null +++ b/packages/server/src/routes/api/v1/repo/branches.ts @@ -0,0 +1,48 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { Branch } from "../../../../git/branch"; +import { Route } from "../../../../types/fastify"; +import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "shared_types"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + fastify.route({ + method: "GET", + url: "/branches", + handler: async(req, reply) => { + const branches = await req.repository.branches(); + + reply.send({ + data: branches.map(branch => { + return { + id: branch.id, + name: branch.name + }; + }) + }); + } + }); + + fastify.route({ + method: "GET", + url: "/branches/:branch", + handler: async(req, reply) => { + const branch = await Branch.lookup(req.repository, req.params.branch); + + if(!branch) { + reply.code(404).send({ error: "Branch not found!" }); + return; + } + + const data: APIBranch = { + id: branch.id, + name: branch.name, + latest_commit: await branch.latestCommit() + }; + + reply.send({ + data: data + }); + } + }); + + done(); +} \ No newline at end of file -- cgit v1.2.3-18-g5258