aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-06 15:37:35 +0200
committerHampusM <hampus@hampusmat.com>2021-07-06 15:37:35 +0200
commit7b48039aa475b8c0b52b019f10fad66c7842d08b (patch)
treec3b0c1666ce74f0fb0b0f43a924a476b6164934a
parent10f0154f1f46881123ef3418beb0eced48bfb4b9 (diff)
API uses shared types
-rw-r--r--packages/server/package.json1
-rw-r--r--packages/server/src/api/v1/index.ts24
-rw-r--r--packages/server/src/api/v1/repo/branches.ts15
-rw-r--r--packages/server/src/api/v1/repo/index.ts23
-rw-r--r--packages/server/src/api/v1/repo/log.ts31
-rw-r--r--packages/server/src/git/commit.ts6
-rw-r--r--packages/shared_types/src/repository.d.ts2
7 files changed, 59 insertions, 43 deletions
diff --git a/packages/server/package.json b/packages/server/package.json
index 568f395..6f5d47a 100644
--- a/packages/server/package.json
+++ b/packages/server/package.json
@@ -23,6 +23,7 @@
"@types/nodegit": "^0.27.2",
"@types/tar-stream": "^2.2.0",
"@types/whatwg-url": "^8.2.0",
+ "shared_types": "workspace:shared_types",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.30.0",
diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts
index a6ab918..c6b9187 100644
--- a/packages/server/src/api/v1/index.ts
+++ b/packages/server/src/api/v1/index.ts
@@ -3,6 +3,7 @@ import { Repository } from "../../git/repository";
import { Route } from "../../fastify_types";
import repo from "./repo";
import { verifyRepoName } from "../util";
+import { Info as APIInfo, RepositorySummary as APIRepositorySummary, Repository as APIRepository } from "shared_types";
function setHandlers(fastify: FastifyInstance): void {
fastify.setErrorHandler((err, req, reply) => {
@@ -28,7 +29,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do
reply.send({
data: await Promise.all(repos.map(async repository => {
- return {
+ return <APIRepositorySummary>{
name: repository.name.short,
description: repository.description,
last_updated: (await repository.latestCommit()).date
@@ -50,13 +51,13 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do
const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo);
- reply.send({
- data: {
- name: repository.name.short,
- description: repository.description,
- has_readme: await (await repository.tree()).findExists("README.md")
- }
- });
+ const data: APIRepository = {
+ name: repository.name.short,
+ description: repository.description,
+ has_readme: await (await repository.tree()).findExists("README.md")
+ };
+
+ reply.send({ data: data });
}
});
done();
@@ -69,7 +70,12 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/info",
handler: (req, reply) => {
- reply.send({ data: { title: opts.config.settings.title, about: opts.config.settings.about } });
+ const data: APIInfo = {
+ title: opts.config.settings.title,
+ about: opts.config.settings.about
+ };
+
+ reply.send({ data: data });
}
});
diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/api/v1/repo/branches.ts
index fe962aa..b280a4a 100644
--- a/packages/server/src/api/v1/repo/branches.ts
+++ b/packages/server/src/api/v1/repo/branches.ts
@@ -1,6 +1,7 @@
import { FastifyInstance, FastifyPluginOptions } from "fastify";
import { Branch } from "../../../git/branch";
import { Route } from "../../../fastify_types";
+import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "shared_types";
export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void {
fastify.route<Route>({
@@ -11,7 +12,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
reply.send({
data: branches.map(branch => {
- return {
+ return <APIBranchSummary>{
id: branch.id,
name: branch.name
};
@@ -31,12 +32,14 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
return;
}
+ const data: APIBranch = {
+ id: branch.id,
+ name: branch.name,
+ latest_commit: await branch.latestCommit()
+ };
+
reply.send({
- data: {
- id: branch.id,
- name: branch.name,
- latest_commit: await branch.latestCommit()
- }
+ data: data
});
}
});
diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts
index 51413d6..8fc9ac1 100644
--- a/packages/server/src/api/v1/repo/index.ts
+++ b/packages/server/src/api/v1/repo/index.ts
@@ -8,6 +8,7 @@ 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 "shared_types";
declare module "fastify" {
interface FastifyRequest {
@@ -32,7 +33,7 @@ function addHooks(fastify: FastifyInstance, opts: FastifyPluginOptions): void {
}
async function treeEntryMap(entry: TreeEntry) {
const latest_commit = await entry.latestCommit();
- return {
+ return <APITreeEntry>{
name: basename(entry.path),
type: entry.type,
latest_commit: {
@@ -45,7 +46,7 @@ async function treeEntryMap(entry: TreeEntry) {
async function tagMap(tag: Tag) {
const author = await tag.author();
- return {
+ return <APITag>{
name: tag.name,
author: { name: author.name, email: author.email },
date: await tag.date()
@@ -65,6 +66,9 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
const tree = await (await req.repository).tree();
const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null;
+
+ let data: APITree;
+
if(tree_path) {
const tree_found = await tree.find(tree_path);
@@ -73,16 +77,15 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
return;
}
- reply.send({
- data: tree_found instanceof Blob
- ? { type: "blob", content: await tree_found.content() }
- : { type: "tree", content: await Promise.all(tree_found.entries().map(treeEntryMap)) }
- });
-
- return;
+ data = tree_found instanceof Blob
+ ? { type: "blob", content: await tree_found.content() }
+ : { type: "tree", content: await Promise.all(tree_found.entries().map(treeEntryMap)) };
+ }
+ else {
+ data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) };
}
- reply.send({ data: { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) } });
+ reply.send({ data: data });
}
});
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts
index aa6fa1a..8157696 100644
--- a/packages/server/src/api/v1/repo/log.ts
+++ b/packages/server/src/api/v1/repo/log.ts
@@ -3,10 +3,11 @@ import { Commit } from "../../../git/commit";
import { Patch } from "../../../git/patch";
import { Route } from "../../../fastify_types";
import { verifySHA } from "../../util";
+import { LogCommit as APILogCommit, Patch as APIPatch, Commit as APICommit } from "shared_types";
async function commitMap(commit: Commit) {
const stats = await commit.stats();
- return {
+ return <APILogCommit>{
id: commit.id,
author: {
name: commit.author.name,
@@ -21,7 +22,7 @@ async function commitMap(commit: Commit) {
}
async function patchMap(patch: Patch, index: number) {
- return {
+ return <APIPatch>{
additions: patch.additions,
deletions: patch.deletions,
from: patch.from,
@@ -57,19 +58,21 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
const stats = await commit.stats();
+ const data: APICommit = {
+ message: commit.message,
+ author: {
+ name: commit.author.name,
+ email: commit.author.email
+ },
+ date: commit.date,
+ insertions: stats.insertions,
+ deletions: stats.deletions,
+ files_changed: stats.files_changed,
+ diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap))
+ };
+
reply.send({
- data: {
- message: commit.message,
- author: {
- name: commit.author.name,
- email: commit.author.email
- },
- date: commit.date,
- insertions: stats.insertions,
- deletions: stats.deletions,
- files_changed: stats.files_changed,
- diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap))
- }
+ data: data
});
}
});
diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts
index 5d86eb4..4b3e44b 100644
--- a/packages/server/src/git/commit.ts
+++ b/packages/server/src/git/commit.ts
@@ -5,9 +5,9 @@ import { Repository } from "./repository";
import { Tree } from "./tree";
export type CommitSummary = {
- id: string | null,
- message: string | null,
- date: number | null
+ id: string,
+ message: string,
+ date: number
}
type DiffStats = {
diff --git a/packages/shared_types/src/repository.d.ts b/packages/shared_types/src/repository.d.ts
index f07c173..cb4d246 100644
--- a/packages/shared_types/src/repository.d.ts
+++ b/packages/shared_types/src/repository.d.ts
@@ -1,6 +1,6 @@
interface RepositoryBase {
name: string,
- description: string
+ description: string | null
}
export interface RepositorySummary extends RepositoryBase {