UNPKG

@c0dez/github-repo-fetcher

Version:

🔍 A lightweight GitHub profile analyzer that fetches repositories, READMEs, and comprehensive user statistics with TypeScript support. Perfect for building GitHub analytics tools and portfolio sites.

98 lines (97 loc) 3.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHubRepoFetcher = void 0; const axios_1 = __importDefault(require("axios")); class GitHubRepoFetcher { constructor(token) { this.baseUrl = 'https://api.github.com'; this.token = token || ''; } get headers() { return { 'Accept': 'application/vnd.github.v3+json', ...(this.token && { 'Authorization': `token ${this.token}` }) }; } async getReposWithReadme(username) { try { const repos = await this.getRepositories(username); const reposWithReadme = await Promise.all(repos.map(async (repo) => { const readme = await this.getReadme(repo.full_name); return { ...repo, readme }; })); return reposWithReadme; } catch (error) { throw this.handleError(error); } } async getRepositories(username) { try { const response = await axios_1.default.get(`${this.baseUrl}/users/${username}/repos`, { headers: this.headers }); return response.data; } catch (error) { throw this.handleError(error); } } async getReadme(fullRepoName) { try { const response = await axios_1.default.get(`${this.baseUrl}/repos/${fullRepoName}/readme`, { headers: { ...this.headers, 'Accept': 'application/vnd.github.raw' } }); return response.data; } catch (error) { return undefined; } } async getUserStats(username) { try { // Get basic user info const userResponse = await axios_1.default.get(`${this.baseUrl}/users/${username}`, { headers: this.headers }); // Get all repositories const repos = await this.getRepositories(username); // Calculate additional stats const languages = {}; let totalStars = 0; for (const repo of repos) { // Get languages for each repo const langResponse = await axios_1.default.get(`${this.baseUrl}/repos/${repo.full_name}/languages`, { headers: this.headers }); Object.entries(langResponse.data).forEach(([lang, bytes]) => { languages[lang] = (languages[lang] || 0) + bytes; }); // Add stars totalStars += repo.stargazers_count || 0; } // Get contribution count for the last year const contributionsResponse = await axios_1.default.get(`${this.baseUrl}/users/${username}/contributions`, { headers: this.headers }).catch(() => ({ data: 0 })); // This endpoint might need different handling based on GitHub API version return { ...userResponse.data, languages, totalStars, contributions: contributionsResponse.data }; } catch (error) { throw this.handleError(error); } } handleError(error) { var _a, _b, _c; if (axios_1.default.isAxiosError(error)) { return { message: ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown GitHub API error', status: ((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) || 500 }; } return { message: 'Unknown error occurred', status: 500 }; } } exports.GitHubRepoFetcher = GitHubRepoFetcher;