diff options
author | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
commit | d1a1b7dc947063aef5f8375a6a1e03246b272c84 (patch) | |
tree | f5cb9bd6d4b5463d9d022026ac6fea87cb6ebe02 /packages/server/src/routes/api/v1/data.ts | |
parent | 6ed078de30a7bf35deace728857d1d293d59eb15 (diff) |
Implemented caching for certain API endpoints, Added documentation & made backend-fixes
Diffstat (limited to 'packages/server/src/routes/api/v1/data.ts')
-rw-r--r-- | packages/server/src/routes/api/v1/data.ts | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/packages/server/src/routes/api/v1/data.ts b/packages/server/src/routes/api/v1/data.ts new file mode 100644 index 0000000..97f07ff --- /dev/null +++ b/packages/server/src/routes/api/v1/data.ts @@ -0,0 +1,119 @@ +import { + LogCommit, + Patch as APIPatch, + Commit as APICommit, Tag as APITag, + RepositorySummary as APIRepositorySummary, + Repository as APIRepository, + BranchSummary, + Branch as APIBranch +} from "api"; +import { Branch, Commit, Patch, Repository, Tag } from "../../../git"; + +export async function getLogCommits(commits: Commit[]): Promise<LogCommit[]> { + return Promise.all(commits.map(async(commit: Commit) => { + const stats = await commit.stats(); + + const is_signed = await commit.isSigned(); + + return <LogCommit>{ + id: commit.id, + author: { + name: commit.author().name, + email: commit.author().email, + fingerprint: await commit.author().fingerprint().catch(() => null) + }, + isSigned: is_signed, + signatureVerified: is_signed ? await commit.verifySignature().catch(() => false) : null, + message: commit.message, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed + }; + })); +} + +export async function getCommit(commit: Commit): Promise<APICommit> { + const stats = await commit.stats(); + + const is_signed = await commit.isSigned(); + + const patches = await (await commit.diff()).patches().catch(() => null); + + return { + message: commit.message, + author: { + name: commit.author().name, + email: commit.author().email, + fingerprint: await commit.author().fingerprint().catch(() => null) + }, + isSigned: is_signed, + signatureVerified: is_signed ? await commit.verifySignature().catch(() => false) : null, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed, + too_large: Boolean(!patches), + diff: patches + ? await Promise.all(patches.map(async(patch: Patch) => { + return <APIPatch>{ + additions: patch.additions, + deletions: patch.deletions, + from: patch.from, + to: patch.to, + too_large: await patch.isTooLarge(), + hunks: await patch.getHunks().catch(() => null) + }; + })) + : null + }; +} + +export function getTags(tags: Tag[]): Promise<APITag[]> { + return Promise.all(tags.map(async(tag: Tag) => { + const author = await tag.author(); + return <APITag>{ + name: tag.name, + author: { + name: author.name, + email: author.email + }, + date: await tag.date() + }; + })); +} + +export function getRepositories(repositories: Repository[]): Promise<APIRepositorySummary[]> { + return Promise.all(repositories.map(async repository => { + return <APIRepositorySummary>{ + name: repository.name.short, + description: await repository.description(), + last_updated: (await repository.head()).date + }; + })); +} + +export async function getRepository(repository: Repository): Promise<APIRepository> { + return <APIRepository>{ + name: repository.name.short, + description: await repository.description(), + has_readme: await (await repository.tree()).findExists("README.md") + }; +} + +export function getBranches(branches: Branch[]): BranchSummary[] { + return branches.map(branch => { + return <BranchSummary>{ + id: branch.id, + name: branch.name + }; + }); +} + +export async function getBranch(branch: Branch): Promise<APIBranch> { + return { + id: branch.id, + name: branch.name, + latest_commit: await branch.latestCommit() + }; +}
\ No newline at end of file |