source refactor
This commit is contained in:
parent
0d3d254dee
commit
c1935fb360
7 changed files with 137 additions and 139 deletions
36
src/lib/client.ts
Normal file
36
src/lib/client.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts";
|
||||
import { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
|
||||
|
||||
export class ApiClient {
|
||||
url;
|
||||
token;
|
||||
constructor(hostname: string, token: string) {
|
||||
this.url = `https://${hostname}`;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
async fetch(path: string) {
|
||||
console.log(crayon.lightBlack("\tfetching", crayon.bold(path)));
|
||||
const headers = new Headers([["PRIVATE-TOKEN", this.token]]);
|
||||
const result = await fetch(this.url + "/" + path, { headers });
|
||||
const parsed = await result.json();
|
||||
return parsed as unknown;
|
||||
}
|
||||
|
||||
async fetch_parse<T>(path: string, parser: z.ZodType<T>) {
|
||||
const unkn = await this.fetch(path);
|
||||
return parser.parse(unkn);
|
||||
}
|
||||
|
||||
async fetch_parse_paginated<T>(path: string, parser: z.ZodType<T>) {
|
||||
const result = [] as T[];
|
||||
let page = 1;
|
||||
while (true) {
|
||||
const fetched = await this.fetch_parse(path + "?per_page=100&page=" + page, parser.array());
|
||||
result.push(...fetched);
|
||||
if (fetch.length < 100) break;
|
||||
page += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
20
src/lib/crypto.ts
Normal file
20
src/lib/crypto.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { encodeBase64 } from "https://deno.land/std@0.222.1/encoding/base64.ts";
|
||||
|
||||
export async function is_relevant(text: string) {
|
||||
return (await hash(text)) === relevant_hash;
|
||||
}
|
||||
|
||||
async function hash(namespace: string) {
|
||||
const buffer = new TextEncoder().encode(namespace);
|
||||
const hash = await crypto.subtle.digest("SHA-512", buffer);
|
||||
const base64 = encodeBase64(hash);
|
||||
return base64;
|
||||
}
|
||||
|
||||
const relevant_hash = "Qa4xy8yRPnBsQf8U2UUE7tse2u4Fi/Aqxbt" +
|
||||
"6aq56m5ofcmCkTi8PbgMBF1OtHC6qkXDF2tz2qKprYCIAMzTSQQ==";
|
||||
Deno.test("encode_namespace", async () => {
|
||||
const namespace = "CHANGEME";
|
||||
const base64 = await hash(namespace);
|
||||
console.log({ namespace, base64 });
|
||||
});
|
17
src/lib/parser.ts
Normal file
17
src/lib/parser.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
|
||||
|
||||
export type MergeRequest = z.infer<typeof merge_request_parser>;
|
||||
export const merge_request_parser = z.object({
|
||||
iid: z.number(),
|
||||
project_id: z.number(),
|
||||
reviewers: z.array(z.object({
|
||||
username: z.string(),
|
||||
})),
|
||||
});
|
||||
|
||||
export const note_parser = z.object({
|
||||
body: z.string(),
|
||||
author: z.object({
|
||||
username: z.string(),
|
||||
}),
|
||||
});
|
10
src/lib/utils.ts
Normal file
10
src/lib/utils.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
export function sum(numbers: Iterable<number>) {
|
||||
let result = 0;
|
||||
for (const item of numbers) result += item;
|
||||
return result;
|
||||
}
|
||||
export async function parallel<I, O>(inputs: I[], operation: (item: I) => Promise<O>) {
|
||||
const promises = [] as Promise<O>[];
|
||||
for (const input of inputs) promises.push(operation(input));
|
||||
return await Promise.all(promises);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue