Skip to content

Commit ec3b9b2

Browse files
committed
refactor(upgrade-deps): share npm registry fetch helper
getLatestNpmVersion and getNpmDependencyRange duplicated the same registry fetch + ok-check + json block; extract fetchNpmLatest so the registry URL and error wording live in one place.
1 parent 9d18d66 commit ec3b9b2

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

.github/scripts/upgrade-deps.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type LatestTagOptions = {
2828

2929
type NpmLatestResponse = {
3030
version?: unknown;
31+
dependencies?: Record<string, string>;
3132
};
3233

3334
type 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.
151156
async 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

Comments
 (0)