|
| 1 | +import axios from 'axios' |
| 2 | + |
| 3 | +import { |
| 4 | + NuGetFetchError, |
| 5 | + NuGetRegistrationIndex, |
| 6 | + NuGetRegistrationPage, |
| 7 | + NuGetSearchItem, |
| 8 | +} from './types' |
| 9 | + |
| 10 | +const SERVICE_INDEX_URL = 'https://api.nuget.org/v3/index.json' |
| 11 | + |
| 12 | +interface ServiceIndexResource { |
| 13 | + '@id': string |
| 14 | + '@type': string |
| 15 | +} |
| 16 | + |
| 17 | +interface ServiceIndex { |
| 18 | + resources: ServiceIndexResource[] |
| 19 | +} |
| 20 | + |
| 21 | +interface ResolvedEndpoints { |
| 22 | + searchBaseUrl: string |
| 23 | + registrationBaseUrl: string |
| 24 | +} |
| 25 | + |
| 26 | +let cachedEndpoints: ResolvedEndpoints | null = null |
| 27 | + |
| 28 | +async function resolveEndpoints(): Promise<ResolvedEndpoints> { |
| 29 | + if (cachedEndpoints) return cachedEndpoints |
| 30 | + |
| 31 | + const resp = await axios.get<ServiceIndex>(SERVICE_INDEX_URL, { |
| 32 | + timeout: 10000, |
| 33 | + }) |
| 34 | + |
| 35 | + const resources = resp.data.resources |
| 36 | + |
| 37 | + const findFirst = (...types: string[]): string | undefined => { |
| 38 | + for (const t of types) { |
| 39 | + const found = resources.find((r) => r['@type'] === t) |
| 40 | + if (found) return found['@id'] |
| 41 | + } |
| 42 | + return undefined |
| 43 | + } |
| 44 | + |
| 45 | + const searchBaseUrl = findFirst('SearchQueryService/3.5.0', 'SearchQueryService') |
| 46 | + const registrationBaseUrl = findFirst( |
| 47 | + 'RegistrationsBaseUrl/3.6.0', |
| 48 | + 'RegistrationsBaseUrl/3.5.0', |
| 49 | + 'RegistrationsBaseUrl', |
| 50 | + ) |
| 51 | + |
| 52 | + if (!searchBaseUrl || !registrationBaseUrl) { |
| 53 | + throw new Error('NuGet service index missing required endpoints') |
| 54 | + } |
| 55 | + |
| 56 | + cachedEndpoints = { searchBaseUrl, registrationBaseUrl } |
| 57 | + return cachedEndpoints |
| 58 | +} |
| 59 | + |
| 60 | +function classifyError(err: unknown): NuGetFetchError | null { |
| 61 | + if (!axios.isAxiosError(err)) return null |
| 62 | + const status = err.response?.status |
| 63 | + if (status === 404) return { kind: 'NOT_FOUND', status, message: err.message } |
| 64 | + if (status === 429) return { kind: 'RATE_LIMIT', status, message: err.message } |
| 65 | + return null |
| 66 | +} |
| 67 | + |
| 68 | +export async function fetchSearch(packageId: string): Promise<NuGetSearchItem | NuGetFetchError> { |
| 69 | + const { searchBaseUrl } = await resolveEndpoints() |
| 70 | + const lowerPackageId = packageId.toLowerCase() |
| 71 | + |
| 72 | + try { |
| 73 | + const resp = await axios.get<{ totalHits: number; data: NuGetSearchItem[] }>(searchBaseUrl, { |
| 74 | + params: { |
| 75 | + q: `packageid:${packageId}`, |
| 76 | + prerelease: 'true', |
| 77 | + semVerLevel: '2.0.0', |
| 78 | + take: 20, |
| 79 | + }, |
| 80 | + headers: { |
| 81 | + 'Accept-Encoding': 'gzip', |
| 82 | + }, |
| 83 | + timeout: 15000, |
| 84 | + }) |
| 85 | + |
| 86 | + const match = resp.data.data.find((item) => item.id.toLowerCase() === lowerPackageId) |
| 87 | + if (!match) { |
| 88 | + return { kind: 'NOT_FOUND', message: `Package ${packageId} not found in search results` } |
| 89 | + } |
| 90 | + return match |
| 91 | + } catch (err) { |
| 92 | + const classified = classifyError(err) |
| 93 | + if (classified) return classified |
| 94 | + throw err |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +async function fetchRegistrationPage( |
| 99 | + pageId: string, |
| 100 | + maxAttempts = 2, |
| 101 | +): Promise<NuGetRegistrationPage> { |
| 102 | + for (let attempt = 1; ; attempt++) { |
| 103 | + try { |
| 104 | + const resp = await axios.get<NuGetRegistrationPage>(pageId, { |
| 105 | + headers: { 'Accept-Encoding': 'gzip' }, |
| 106 | + timeout: 15000, |
| 107 | + }) |
| 108 | + return resp.data |
| 109 | + } catch (err) { |
| 110 | + if (attempt >= maxAttempts) throw err |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export async function fetchRegistration( |
| 116 | + packageId: string, |
| 117 | +): Promise<NuGetRegistrationIndex | NuGetFetchError> { |
| 118 | + const { registrationBaseUrl } = await resolveEndpoints() |
| 119 | + const lowerId = packageId.toLowerCase() |
| 120 | + |
| 121 | + try { |
| 122 | + const resp = await axios.get<NuGetRegistrationIndex>( |
| 123 | + `${registrationBaseUrl}${lowerId}/index.json`, |
| 124 | + { |
| 125 | + headers: { 'Accept-Encoding': 'gzip' }, |
| 126 | + timeout: 15000, |
| 127 | + }, |
| 128 | + ) |
| 129 | + |
| 130 | + const index = resp.data |
| 131 | + |
| 132 | + for (let i = 0; i < index.items.length; i++) { |
| 133 | + const page = index.items[i] |
| 134 | + if (!page.items) { |
| 135 | + const fullPage = await fetchRegistrationPage(page['@id']) |
| 136 | + index.items[i] = { ...page, items: fullPage.items ?? [] } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return index |
| 141 | + } catch (err) { |
| 142 | + const classified = classifyError(err) |
| 143 | + if (classified) return classified |
| 144 | + throw err |
| 145 | + } |
| 146 | +} |
0 commit comments