diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-22 13:31:43 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-22 13:31:43 +0200 |
commit | 43faf0bfd5544f9338e1442ab5b1fcb3210867ac (patch) | |
tree | 98ef97249f6a927b1cfba41b8a3587fd211a68e1 /packages/server/src/api/v1 | |
parent | e59d40c2b662a570f679c133a7fe26d1b47edd89 (diff) |
Refactored the backend yet again & got eslint too work with typescript
Diffstat (limited to 'packages/server/src/api/v1')
-rw-r--r-- | packages/server/src/api/v1/index.ts | 18 | ||||
-rw-r--r-- | packages/server/src/api/v1/repo.ts | 108 | ||||
-rw-r--r-- | packages/server/src/api/v1/repo/branches.ts | 40 | ||||
-rw-r--r-- | packages/server/src/api/v1/repo/index.ts | 48 | ||||
-rw-r--r-- | packages/server/src/api/v1/repo/log.ts | 40 |
5 files changed, 137 insertions, 117 deletions
diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts index 31ab24f..00652a8 100644 --- a/packages/server/src/api/v1/index.ts +++ b/packages/server/src/api/v1/index.ts @@ -1,9 +1,10 @@ import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { verifyRepoName } from "../util"; import { GitAPI } from "../git"; +import { Route } from "../../fastify_types"; import repo from "./repo"; +import { verifyRepoName } from "../util"; -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) { +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { const git = new GitAPI(opts.config.settings.base_dir); fastify.setErrorHandler((err, req, reply) => { @@ -12,7 +13,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do }); fastify.setNotFoundHandler((req, reply) => { reply.code(404).send({ error: "Endpoint not found!" }); - }) + }); fastify.route({ method: "GET", @@ -31,23 +32,22 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do } }); - fastify.route({ + fastify.route<Route>({ method: "GET", url: "/repos/:repo", handler: async(req, reply) => { - const params: any = req.params; - const repo_verification = await verifyRepoName(opts.config.settings.base_dir, params.repo); + const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo); if(repo_verification.success === false && repo_verification.code) { reply.code(repo_verification.code).send({ error: repo_verification.message }); } - const desc = await git.getRepositoryFile(params.repo, "description"); + const desc = await git.getRepositoryFile(req.params.repo, "description"); - reply.send({ data: { name: params.repo, description: desc, has_readme: await git.doesReadmeExist(params.repo) } }); + reply.send({ data: { name: req.params.repo, description: desc, has_readme: await git.doesReadmeExist(req.params.repo) } }); } }); fastify.register(repo, { prefix: "/repos/:repo", config: { git: git, settings: opts.config.settings } }); done(); -};
\ No newline at end of file +}
\ No newline at end of file diff --git a/packages/server/src/api/v1/repo.ts b/packages/server/src/api/v1/repo.ts deleted file mode 100644 index 6ef2923..0000000 --- a/packages/server/src/api/v1/repo.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { verifySHA, verifyRepoName } from "../util"; - -export default function (fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) { - const git = opts.config.git; - - fastify.addHook("onRequest", async(req, reply) => { - const params: any = req.params; - const repo_verification = await verifyRepoName(opts.config.settings.base_dir, params.repo); - if(repo_verification.success === false && repo_verification.code) { - reply.code(repo_verification.code).send({ error: repo_verification.message }); - } - }); - - fastify.route({ - method: "GET", - url: "/log", - handler: async(req, reply) => { - const log = await git.getLog((<any>req.params).repo); - - if(log.length === 0) { - reply.code(500).send({ error: "Internal server error!" }); - return; - } - - reply.send({ data: log }); - } - }); - - fastify.route({ - method: "GET", - url: "/log/:commit", - handler: async(req, reply) => { - const params: any = req.params; - const commit_verification = await verifySHA(git, params.repo, params.commit); - if(commit_verification.success === false && commit_verification.code) { - reply.code(commit_verification.code).send({ error: commit_verification.message }); - } - - const commit = await git.getCommit(params.repo, params.commit); - - reply.send({ data: commit }); - } - }); - - fastify.route({ - method: "GET", - url: "/tree", - handler: async(req, reply) => { - const params: any = req.params; - const query: any = req.query; - - const tree_path = (query.length !== 0 && query.path) ? query.path : null; - - const tree = await git.getTree(params.repo, tree_path); - - if(!tree) { - reply.code(404).send({ error: "Path not found" }); - } - - reply.send({ data: tree }); - } - }); - - fastify.route({ - method: "GET", - url: "/branches", - handler: async(req, reply) => { - const params: any = req.params; - const branches = await git.getBranches(params.repo); - - reply.send({ data: branches }); - } - }); - - fastify.route({ - method: "GET", - url: "/branches/:branch", - handler: async(req, reply) => { - const params: any = req.params; - const branch_verification = await verifySHA(git, params.repo, params.branch); - if(branch_verification.success === false && branch_verification.code) { - reply.code(branch_verification.code).send({ error: branch_verification.message }); - } - - const branch = await git.getBranch(params.repo, params.branch); - - if(!branch) { - reply.code(404).send({ error: "Branch not found!" }); - return; - } - - reply.send({ data: branch }); - } - }); - - fastify.route({ - method: "GET", - url: "/tags", - handler: async(req, reply) => { - const params: any = req.params; - const refs = await git.getTags(params.repo); - reply.send({ data: refs }); - } - }); - - done(); -}
\ No newline at end of file diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/api/v1/repo/branches.ts new file mode 100644 index 0000000..fd8056a --- /dev/null +++ b/packages/server/src/api/v1/repo/branches.ts @@ -0,0 +1,40 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { GitAPI } from "../../git"; +import { Route } from "../../../fastify_types"; +import { verifySHA } from "../../util"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + const git: GitAPI = opts.config.git; + + fastify.route<Route>({ + method: "GET", + url: "/branches", + handler: async(req, reply) => { + const branches = await git.getBranches(req.params.repo); + + reply.send({ data: branches }); + } + }); + + fastify.route<Route>({ + method: "GET", + url: "/branches/:branch", + handler: async(req, reply) => { + const branch_verification = await verifySHA(git, req.params.repo, req.params.branch); + if(branch_verification.success === false && branch_verification.code) { + reply.code(branch_verification.code).send({ error: branch_verification.message }); + } + + const branch = await git.getBranch(req.params.repo, req.params.branch); + + if(!branch) { + reply.code(404).send({ error: "Branch not found!" }); + return; + } + + reply.send({ data: branch }); + } + }); + + done(); +}
\ No newline at end of file diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts new file mode 100644 index 0000000..86e5d10 --- /dev/null +++ b/packages/server/src/api/v1/repo/index.ts @@ -0,0 +1,48 @@ +import { FastifyInstance, FastifyPluginOptions, FastifyRequest } from "fastify"; +import { GitAPI } from "../../git"; +import { Route } from "../../../fastify_types"; +import branches from "./branches"; +import log from "./log"; +import { verifyRepoName } from "../../util"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + const git: GitAPI = opts.config.git; + + fastify.addHook("onRequest", async(req: FastifyRequest<Route>, reply) => { + const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo); + if(repo_verification.success === false && repo_verification.code) { + reply.code(repo_verification.code).send({ error: repo_verification.message }); + } + }); + + fastify.register(log, { config: { git: git } }); + fastify.register(branches, { config: { git: git } }); + + fastify.route<Route>({ + method: "GET", + url: "/tree", + handler: async(req, reply) => { + const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null; + + const tree = await git.getTree(req.params.repo, tree_path); + + if(!tree) { + reply.code(404).send({ error: "Path not found" }); + return; + } + + reply.send({ data: tree }); + } + }); + + fastify.route<Route>({ + method: "GET", + url: "/tags", + handler: async(req, reply) => { + const refs = await git.getTags(req.params.repo); + reply.send({ data: refs }); + } + }); + + done(); +}
\ No newline at end of file diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts new file mode 100644 index 0000000..f692b00 --- /dev/null +++ b/packages/server/src/api/v1/repo/log.ts @@ -0,0 +1,40 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { GitAPI } from "../../git"; +import { Route } from "../../../fastify_types"; +import { verifySHA } from "../../util"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + const git: GitAPI = opts.config.git; + + fastify.route<Route>({ + method: "GET", + url: "/log", + handler: async(req, reply) => { + const log = await git.getLog(req.params.repo); + + if(log.length === 0) { + reply.code(500).send({ error: "Internal server error!" }); + return; + } + + reply.send({ data: log }); + } + }); + + fastify.route<Route>({ + method: "GET", + url: "/log/:commit", + handler: async(req, reply) => { + const commit_verification = await verifySHA(git, req.params.repo, req.params.commit); + if(commit_verification.success === false && commit_verification.code) { + reply.code(commit_verification.code).send({ error: commit_verification.message }); + } + + const commit = await git.getCommit(req.params.repo, req.params.commit); + + reply.send({ data: commit }); + } + }); + + done(); +}
\ No newline at end of file |