init
This commit is contained in:
35
src/github_static_fetch.ts
Normal file
35
src/github_static_fetch.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import colors from './colors.json';
|
||||
|
||||
export interface Repository {
|
||||
color: string;
|
||||
description: string;
|
||||
language: string;
|
||||
}
|
||||
|
||||
export const fetchGithubStaticData = async (owner: string, repo: string, token: string = ""): Promise<Repository> => {
|
||||
const empty_data: Repository = {
|
||||
color: "#fff",
|
||||
description: "",
|
||||
language: "Other",
|
||||
}
|
||||
try {
|
||||
var headers: RequestInit
|
||||
if (token != "") {
|
||||
headers = {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
}
|
||||
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`, headers);
|
||||
const data: Repository = await response.json();
|
||||
if (!response.ok) {
|
||||
return empty_data;
|
||||
}
|
||||
data.color = colors[data.language].color
|
||||
return data;
|
||||
} catch {
|
||||
return empty_data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user