aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/v1/repo/branches.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-18 17:29:55 +0200
committerHampusM <hampus@hampusmat.com>2021-08-18 17:29:55 +0200
commitd1a1b7dc947063aef5f8375a6a1e03246b272c84 (patch)
treef5cb9bd6d4b5463d9d022026ac6fea87cb6ebe02 /packages/server/src/routes/api/v1/repo/branches.ts
parent6ed078de30a7bf35deace728857d1d293d59eb15 (diff)
Implemented caching for certain API endpoints, Added documentation & made backend-fixes
Diffstat (limited to 'packages/server/src/routes/api/v1/repo/branches.ts')
-rw-r--r--packages/server/src/routes/api/v1/repo/branches.ts37
1 files changed, 14 insertions, 23 deletions
diff --git a/packages/server/src/routes/api/v1/repo/branches.ts b/packages/server/src/routes/api/v1/repo/branches.ts
index 99f0327..f709f4d 100644
--- a/packages/server/src/routes/api/v1/repo/branches.ts
+++ b/packages/server/src/routes/api/v1/repo/branches.ts
@@ -1,9 +1,10 @@
-import { FastifyInstance, FastifyPluginOptions } from "fastify";
+import { FastifyPluginCallback } from "fastify";
+import { sources } from "../../../../cache";
import { Branch } from "../../../../git/branch";
-import { Route } from "../../../../types/fastify";
-import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "api";
+import { FastifyPluginOptions, Route } from "../../../../types/fastify";
+import { getBranch, getBranches } from "../data";
-export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void {
+const branches: FastifyPluginCallback<FastifyPluginOptions> = (fastify, opts, done) => {
fastify.route<Route>({
method: "GET",
url: "/branches",
@@ -11,12 +12,9 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
const branches = await req.repository.branches();
reply.send({
- data: branches.map(branch => {
- return <APIBranchSummary>{
- id: branch.id,
- name: branch.name
- };
- })
+ data: opts.config.cache
+ ? await opts.config.cache.receive(sources.BranchesSource, req.repository, branches)
+ : getBranches(branches)
});
}
});
@@ -32,22 +30,15 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
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
+ data: await (opts.config.cache
+ ? opts.config.cache.receive(sources.BranchSource, req.repository, branch)
+ : getBranch(branch))
});
}
});
done();
-} \ No newline at end of file
+};
+
+export default branches; \ No newline at end of file