From d1a1b7dc947063aef5f8375a6a1e03246b272c84 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 18 Aug 2021 17:29:55 +0200 Subject: Implemented caching for certain API endpoints, Added documentation & made backend-fixes --- packages/server/src/routes/api/v1/repo/index.ts | 35 +++++++++++-------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'packages/server/src/routes/api/v1/repo/index.ts') diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts index 4cd6c51..f8e01d3 100644 --- a/packages/server/src/routes/api/v1/repo/index.ts +++ b/packages/server/src/routes/api/v1/repo/index.ts @@ -1,15 +1,15 @@ -import { CoolFastifyRequest, Route } from "../../../../types/fastify"; -import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { CoolFastifyRequest, Route, FastifyPluginOptions } from "../../../../types/fastify"; +import { FastifyInstance, FastifyPluginCallback } from "fastify"; import { Repository } from "../../../../git/repository"; -import { Tag } from "../../../../git/tag"; import { BaseTreeEntry, BlobTreeEntry, TreeEntry } from "../../../../git/tree_entry"; import { basename } from "path"; import branches from "./branches"; import log from "./log"; import { verifyRepoName } from "../../util"; -import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "api"; +import { Tree as APITree, TreeEntry as APITreeEntry } from "api"; import { ServerError } from "../../../../git/error"; -import { commitMap } from "./map"; +import { getLogCommits, getTags } from "../data"; +import { sources } from "../../../../cache"; declare module "fastify" { interface FastifyRequest { @@ -48,20 +48,11 @@ async function treeEntryMap(entry: BaseTreeEntry) { }; } -async function tagMap(tag: Tag) { - const author = await tag.author(); - return { - name: tag.name, - author: { name: author.name, email: author.email }, - date: await tag.date() - }; -} - -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { +const repo: FastifyPluginCallback = (fastify, opts, done) => { addHooks(fastify, opts); - fastify.register(log); - fastify.register(branches); + fastify.register(log, { config: opts.config }); + fastify.register(branches, { config: opts.config }); fastify.route({ method: "GET", @@ -127,7 +118,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do const history = await tree_entry.history(Number(req.query.count)); - reply.send({ data: await Promise.all(history.map(commitMap)) }); + reply.send({ data: await getLogCommits(history) }); } }); @@ -137,10 +128,14 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do handler: async(req, reply) => { const tags = await req.repository.tags(); reply.send({ - data: await Promise.all(tags.map(tagMap)) + data: await (opts.config.cache + ? opts.config.cache.receive(sources.TagsSource, req.repository, tags) + : getTags(tags)) }); } }); done(); -} \ No newline at end of file +}; + +export default repo; \ No newline at end of file -- cgit v1.2.3-18-g5258