( id: string, isVariable?: boolean, )
| 30 | }; |
| 31 | |
| 32 | export const getAvailableVersions = async ( |
| 33 | id: string, |
| 34 | isVariable?: boolean, |
| 35 | ): Promise<string[]> => { |
| 36 | const url = isVariable |
| 37 | ? `https://data.jsdelivr.com/v1/packages/npm/@fontsource-variable/${id}` |
| 38 | : `https://data.jsdelivr.com/v1/packages/npm/@fontsource/${id}`; |
| 39 | |
| 40 | const resp = await fetch(url); |
| 41 | |
| 42 | if (!resp.ok) { |
| 43 | if (resp.status === 404) { |
| 44 | throw new StatusError(404, `Not Found. ${id} does not exist.`); |
| 45 | } |
| 46 | |
| 47 | // Log error with logpush |
| 48 | console.log(await resp.text()); |
| 49 | throw new StatusError( |
| 50 | 500, |
| 51 | 'Internal Server Error. Unable to fetch versions.', |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | const list = (await resp.json()) as JSDelivrAPIVersion; |
| 56 | if (!list?.versions || list.versions.length === 0) { |
| 57 | throw new StatusError(404, `Not Found. No versions found for ${id}.`); |
| 58 | } |
| 59 | |
| 60 | const versions = list.versions.map((version) => version.version); |
| 61 | |
| 62 | return sortSemverList(versions); |
| 63 | }; |
no test coverage detected