aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/v1/repo/branches.ts
blob: 84ca003ea5320c000178fcfc23f040ee1a2df047 (plain)
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
41
42
43
44
import { FastifyPluginCallback } from "fastify";
import { sources } from "cache";
import { Branch } from "git/branch";
import { FastifyPluginOptions, Route } from "types/fastify";
import { getBranch, getBranches } from "../data";

const branches: FastifyPluginCallback<FastifyPluginOptions> = (fastify, opts, done) => {
	fastify.route<Route>({
		method: "GET",
		url: "/branches",
		handler: async(req, reply) => {
			const branches = await req.repository.branches();

			reply.send({
				data: opts.config.cache
					? await opts.config.cache.receive(sources.BranchesSource, req.repository, branches)
					: getBranches(branches)
			});
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/branches/:branch",
		schema: {
			params: {
				branch: { type: "string" }
			}
		},
		handler: async(req, reply) => {
			const branch = await Branch.lookup(req.repository, req.params.branch);

			reply.send({
				data: await (opts.config.cache
					? opts.config.cache.receive(sources.BranchSource, req.repository, branch)
					: getBranch(branch))
			});
		}
	});

	done();
};

export default branches;