Files
ts-scripts/libraries/deno-github-mod.ts

42 lines
1.2 KiB
TypeScript

import {
fetchWithTimoutAndAutoProxy,
} from "https://global.hatter.ink/script/get/@0/deno-fetch-auto-proxy-mod.ts";
export class SshKey {
algorithm: string;
material: string;
description?: string;
constructor(
algorithm: string,
material: string,
description: string | undefined,
) {
this.algorithm = algorithm;
this.material = material;
this.description = description;
}
static parseSshKey(key: string): SshKey {
const keyParts = key.split(/\s+/);
if (keyParts.length < 2 || keyParts.length > 3) {
throw `Bad SSH key format ${key}`;
}
return new SshKey(
keyParts[0],
keyParts[1],
(keyParts.length > 2) ? keyParts[2] : undefined,
);
}
}
export async function fetchKeys(
username: string,
timeout?: number,
): Promise<Array<SshKey>> {
const url = `https://github.com/${username}.keys`;
const response = await fetchWithTimoutAndAutoProxy(url, timeout);
const responseText: string = await response.text();
return responseText.trim().split("\n").map((k) => SshKey.parseSshKey(k));
}