Merge PR #5 from XlebyllleK/master

chore: update api.ts
This commit is contained in:
Eshan Roy
2025-01-11 22:27:57 +05:30
committed by GitHub

View File

@@ -1,30 +1,46 @@
import { Package } from '../types'; import {
Package,
Repository
} from '../types';
// Define the mirrors from which packages will be fetched // Define the mirrors from which packages will be fetched
const MIRRORS = [ const MIRRORS: Record<string, {
'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-core/refs/heads/master/packages.txt', url: string;
'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-extra/refs/heads/master/packages.txt' repository: Repository;
]; }> = {
/**
* Сейчас данный код будет удобным вариантом,
* чтобы убрать убирать пустые (неиспользуемые) репозитории из главной страницы
*/
'core': {
url: 'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-core/refs/heads/master/packages.txt',
repository: ('core' as Repository)
},
'extra': {
url: 'https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-extra/refs/heads/master/packages.txt',
repository: ('extra' as Repository)
}
}
// Fetch data from a single mirror (Core or Extra repository) // Fetch data from a single mirror (Core or Extra repository)
async function fetchFromMirror(url: string): Promise<Package[]> { async function fetchFromMirror(url: string, repository: Repository): Promise<Package[]> {
const response = await fetch(url); const response = await fetch(url);
const text = await response.text(); const text = await response.text();
// Determine the repository name (core or extra) from the URL
const repository = url.includes('snigdhaos-core') ? 'core' : 'extra';
// Parse the text file content and return a list of packages // Parse the text file content and return a list of packages
return text return text.split('\n').filter(Boolean).map((line) => {
.split('\n') // Split by line const [
.filter(Boolean) // Remove empty lines name,
.map((line) => { version,
const [name, version, ...descParts] = line.split(' '); ...description
] = line.split(' ');
return { return {
name, name,
version, version,
description: descParts.join(' '), description: description.join(' '),
repository, // Attach the repository name to each package repository
}; };
}); });
} }
@@ -34,20 +50,19 @@ export async function fetchPackages(): Promise<Package[]> {
let packages: Package[] = []; let packages: Package[] = [];
// Try fetching from each mirror and accumulate the results // Try fetching from each mirror and accumulate the results
for (const mirror of MIRRORS) { for (const key in MIRRORS) {
const mirror = MIRRORS[key];
try { try {
const result = await fetchFromMirror(mirror); const result = await fetchFromMirror(mirror.url, mirror.repository);
packages = packages.concat(result); // Append the packages from this mirror packages = packages.concat(result); // Append the packages from this mirror
} catch (error) { } catch (error) {
console.warn(`Failed to fetch from mirror ${mirror}:`, error); console.warn(`Failed to fetch from mirror ${mirror.url}:`, error);
continue; // Continue to the next mirror if this one fails continue; // Continue to the next mirror if this one fails
} }
} }
// If no successful fetch, throw an error // If no successful fetch, throw an error
if (packages.length === 0) { if (packages.length === 0) throw new Error('All mirrors failed to respond');
throw new Error('All mirrors failed to respond');
}
return packages; return packages;
} }