aboutsummaryrefslogtreecommitdiffstats
path: root/index.ts
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-16 21:16:29 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-16 21:16:29 +0200
commit0a8c0885cb6751df116adaaa80c431c7c8e8941c (patch)
tree1d22cdad6d4deba5385537110e01110ff178a9af /index.ts
downloadsrht-git-feed-0a8c0885cb6751df116adaaa80c431c7c8e8941c.tar.xz
srht-git-feed-0a8c0885cb6751df116adaaa80c431c7c8e8941c.zip
Initial commit
Diffstat (limited to 'index.ts')
-rw-r--r--index.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/index.ts b/index.ts
new file mode 100644
index 0000000..a3bb69c
--- /dev/null
+++ b/index.ts
@@ -0,0 +1,70 @@
+import { serve } from "https://deno.land/std@0.93.0/http/server.ts";
+import "https://deno.land/x/dotenv/load.ts";
+const SERVER_PORT = Deno.env.get("SERVER_PORT") ?? "8080";
+const SERVER_HOST = Deno.env.get("SERVER_HOST") ?? "localhost";
+const API_URL = Deno.env.get("API_URL") ?? "https://git.sr.ht/query";
+const API_KEY = Deno.env.get("API_KEY");
+
+interface QueryResponse {
+ data: {
+ repositories: {
+ results: Repository[];
+ };
+ };
+}
+
+interface Repository {
+ id: string,
+ name: string,
+ description: string,
+ updated: string,
+ visibility: "PUBLIC" | "UNLISTED" | "PRIVATE"
+}
+
+window.onload = function() {
+ if(!API_KEY) {
+ throw new Error("API_KEY is empty");
+ }
+}
+
+async function getRepositories(): Promise<Repository[] | undefined> {
+ const response = await fetch(API_URL, {
+ method: "POST",
+ headers: {
+ "Authorization": "Bearer " + API_KEY,
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ query: `
+{
+ repositories {
+ results {
+ id,
+ name,
+ description,
+ updated,
+ visibility
+ }
+ }
+}
+ `
+ })
+ })
+
+ if (response.ok) {
+ const json = await response.json() as QueryResponse;
+ return json?.data.repositories.results?.filter(repo => repo.visibility === "PUBLIC") ?? [];
+ } else {
+ throw response;
+ }
+}
+
+const s = serve({ port: parseInt(SERVER_PORT), hostname: SERVER_HOST });
+console.log("http://"+SERVER_HOST+":" + SERVER_PORT);
+for await (const req of s) {
+ try {
+ req.respond({ body: JSON.stringify(await getRepositories()) });
+ } catch(err) {
+ req.respond({ body: JSON.stringify(err) });
+ }
+}