-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathgithub.ts
More file actions
67 lines (61 loc) · 1.72 KB
/
github.ts
File metadata and controls
67 lines (61 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const API = "https://api.github.com";
export interface OrgSearchItem {
login: string;
id: number;
avatar_url: string;
html_url: string;
type: string;
}
export interface OrgSearchResult {
total_count: number;
incomplete_results: boolean;
items: OrgSearchItem[];
}
export interface GitHubOrg {
login: string;
id: number;
node_id: string;
url: string;
avatar_url: string;
html_url: string;
name: string | null;
company: string | null;
blog: string | null;
location: string | null;
email: string | null;
twitter_username: string | null;
description: string | null;
public_repos: number;
public_gists: number;
followers: number;
following: number;
created_at: string;
type: string;
}
async function parseError(res: Response): Promise<string> {
try {
const body = await res.json();
if (body && typeof body.message === "string") return body.message;
} catch {
/* ignore */
}
return res.statusText || `Request failed (${res.status})`;
}
export async function searchOrganizations(
query: string,
): Promise<OrgSearchResult> {
const q = `${query.trim()} in:login type:org`.replace(/\s+/g, " ");
const url = `${API}/search/users?q=${encodeURIComponent(q)}&per_page=20`;
const res = await fetch(url, {
headers: { Accept: "application/vnd.github+json" },
});
if (!res.ok) throw new Error(await parseError(res));
return res.json() as Promise<OrgSearchResult>;
}
export async function fetchOrganization(login: string): Promise<GitHubOrg> {
const res = await fetch(`${API}/orgs/${encodeURIComponent(login.trim())}`, {
headers: { Accept: "application/vnd.github+json" },
});
if (!res.ok) throw new Error(await parseError(res));
return res.json() as Promise<GitHubOrg>;
}