@@ -28,6 +28,7 @@ type LatestTagOptions = {
2828
2929type NpmLatestResponse = {
3030 version ?: unknown ;
31+ dependencies ?: Record < string , string > ;
3132} ;
3233
3334type UpstreamVersions = {
@@ -132,14 +133,18 @@ async function getLatestTag(
132133}
133134
134135// ============ npm Registry ============
135- async function getLatestNpmVersion ( packageName : string ) : Promise < string > {
136+ async function fetchNpmLatest ( packageName : string ) : Promise < NpmLatestResponse > {
136137 const res = await fetch ( `https://registry.npmjs.org/${ packageName } /latest` ) ;
137138 if ( ! res . ok ) {
138139 throw new Error (
139- `Failed to fetch npm version for ${ packageName } : ${ res . status } ${ res . statusText } ` ,
140+ `Failed to fetch npm metadata for ${ packageName } : ${ res . status } ${ res . statusText } ` ,
140141 ) ;
141142 }
142- const data = ( await res . json ( ) ) as NpmLatestResponse ;
143+ return ( await res . json ( ) ) as NpmLatestResponse ;
144+ }
145+
146+ async function getLatestNpmVersion ( packageName : string ) : Promise < string > {
147+ const data = await fetchNpmLatest ( packageName ) ;
143148 if ( typeof data . version !== 'string' ) {
144149 throw new Error ( `Invalid npm response for ${ packageName } : missing version field` ) ;
145150 }
@@ -149,13 +154,7 @@ async function getLatestNpmVersion(packageName: string): Promise<string> {
149154// Read a dependency range from the latest published version of `packageName`,
150155// e.g. the `lightningcss` range that the bundled `@tsdown/css` depends on.
151156async function getNpmDependencyRange ( packageName : string , dependencyName : string ) : Promise < string > {
152- const res = await fetch ( `https://registry.npmjs.org/${ packageName } /latest` ) ;
153- if ( ! res . ok ) {
154- throw new Error (
155- `Failed to fetch npm metadata for ${ packageName } : ${ res . status } ${ res . statusText } ` ,
156- ) ;
157- }
158- const data = ( await res . json ( ) ) as { dependencies ?: Record < string , string > } ;
157+ const data = await fetchNpmLatest ( packageName ) ;
159158 const range = data . dependencies ?. [ dependencyName ] ;
160159 if ( typeof range !== 'string' ) {
161160 throw new Error (
0 commit comments