aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/util.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-25 12:32:59 +0200
committerHampusM <hampus@hampusmat.com>2021-07-25 12:32:59 +0200
commitc63e558f402cfad914031a58fdcf3d8e0f3d125d (patch)
treebf080e4c23310f5a5a1d14f15bc3e575c0671625 /packages/server/src/routes/api/util.ts
parenta5afb39803e70a6117965760f50615aaba82f84a (diff)
Moved backend routes to a dedicated directory
Diffstat (limited to 'packages/server/src/routes/api/util.ts')
-rw-r--r--packages/server/src/routes/api/util.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/server/src/routes/api/util.ts b/packages/server/src/routes/api/util.ts
new file mode 100644
index 0000000..2a06393
--- /dev/null
+++ b/packages/server/src/routes/api/util.ts
@@ -0,0 +1,42 @@
+import { Commit } from "../../git/commit";
+import { Repository } from "../../git/repository";
+
+type VerificationResultType = "SUCCESS" | "NOT_FOUND" | "INVALID";
+
+export class VerificationResult {
+ constructor(result: VerificationResultType, subject?: string) {
+ this.success = result === "SUCCESS";
+
+ if(result !== "SUCCESS") {
+ const verification_error_types = {
+ NOT_FOUND: { code: 404, message: `${String(subject?.substr(0, 1).toUpperCase()) + subject?.substr(1)} not found!` },
+ INVALID: { code: 403, message: `Invalid ${subject}` }
+ };
+
+ this.message = verification_error_types[result].message;
+ this.code = verification_error_types[result].code;
+ }
+ }
+
+ success: boolean;
+ code: number | null = null;
+ message: string | null = null;
+}
+
+export function verifyRepoName(repo_name: string): boolean {
+ return /^[a-zA-Z0-9.\-_]+$/u.test(repo_name);
+}
+
+export async function verifySHA(repository: Repository, sha: string): Promise<VerificationResult> {
+ if(!(/^[a-fA-F0-9]+$/u).test(sha)) {
+ return new VerificationResult("INVALID", "sha");
+ }
+
+ const object_exists = await Commit.lookupExists(repository, sha);
+
+ if(!object_exists) {
+ return new VerificationResult("NOT_FOUND", "object");
+ }
+
+ return new VerificationResult("SUCCESS");
+} \ No newline at end of file