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,53 +1,68 @@
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 // Parse the text file content and return a list of packages
const repository = url.includes('snigdhaos-core') ? 'core' : 'extra'; return text.split('\n').filter(Boolean).map((line) => {
const [
name,
version,
...description
] = line.split(' ');
// Parse the text file content and return a list of packages return {
return text name,
.split('\n') // Split by line version,
.filter(Boolean) // Remove empty lines description: description.join(' '),
.map((line) => { repository
const [name, version, ...descParts] = line.split(' '); };
return {
name,
version,
description: descParts.join(' '),
repository, // Attach the repository name to each package
};
}); });
} }
// Fetch packages from all mirrors and combine them // Fetch packages from all mirrors and combine them
export async function fetchPackages(): Promise<Package[]> { 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) {
try { const mirror = MIRRORS[key];
const result = await fetchFromMirror(mirror);
packages = packages.concat(result); // Append the packages from this mirror try {
} catch (error) { const result = await fetchFromMirror(mirror.url, mirror.repository);
console.warn(`Failed to fetch from mirror ${mirror}:`, error); packages = packages.concat(result); // Append the packages from this mirror
continue; // Continue to the next mirror if this one fails } catch (error) {
console.warn(`Failed to fetch from mirror ${mirror.url}:`, error);
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;
} }