From 1f0e59f81cb85740e387be7d4f34f54b656feec9 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 30 Apr 2026 10:48:38 +0100 Subject: [PATCH 1/2] fix: show license from correct version --- app/composables/npm/usePackage.ts | 22 +++++++++++----------- shared/types/npm-registry.ts | 4 +++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/composables/npm/usePackage.ts b/app/composables/npm/usePackage.ts index 747de4992e..beff780699 100644 --- a/app/composables/npm/usePackage.ts +++ b/app/composables/npm/usePackage.ts @@ -16,6 +16,13 @@ function getTrustLevel(version: PackumentVersion): PublishTrustLevel { return 'none' } +function normalizeLicense(license?: PackumentLicense): string | undefined { + if (license && typeof license === 'object' && 'type' in license) { + return license.type + } + return typeof license === 'string' ? license : undefined +} + /** * Transform a full Packument into a slimmed version for client-side use. * Reduces payload size by: @@ -72,6 +79,7 @@ export function transformPackument( for (const v of includedVersions) { const version = pkg.versions[v] if (version) { + const versionLicense = normalizeLicense(version.license) if (version.version === requestedVersion) { // Strip readme from each version, extract install scripts info const { readme: _readme, scripts, ...slimVersion } = version @@ -80,25 +88,20 @@ export function transformPackument( const installScripts = scripts ? extractInstallScriptsInfo(scripts) : null versionData = { ...slimVersion, + license: versionLicense, installScripts: installScripts ?? undefined, } } const trustLevel = getTrustLevel(version) const hasProvenance = trustLevel !== 'none' - // Normalize license: some versions use { type: "MIT" } instead of "MIT" - let versionLicense = version.license - if (versionLicense && typeof versionLicense === 'object' && 'type' in versionLicense) { - versionLicense = (versionLicense as { type: string }).type - } - filteredVersions[v] = { hasProvenance, trustLevel, version: version.version, deprecated: version.deprecated, tags: version.tags as string[], - license: typeof versionLicense === 'string' ? versionLicense : undefined, + license: versionLicense, type: typeof version.type === 'string' ? version.type : undefined, } } @@ -113,10 +116,7 @@ export function transformPackument( } // Normalize license field - let license = pkg.license - if (license && typeof license === 'object' && 'type' in license) { - license = license.type - } + const license = normalizeLicense(versionData?.license || pkg.license) // Extract storybook field from the requested version (custom package.json field) const requestedPkgVersion = requestedVersion ? pkg.versions[requestedVersion] : null diff --git a/shared/types/npm-registry.ts b/shared/types/npm-registry.ts index 6ffe94aded..2c568e2503 100644 --- a/shared/types/npm-registry.ts +++ b/shared/types/npm-registry.ts @@ -23,10 +23,12 @@ export interface PackumentVersion extends PackumentVersionWithoutAttestations { dist: PackumentVersionWithoutAttestations['dist'] & { attestations?: NpmVersionAttestations } } +export type PackumentLicense = string | { type: string; url?: string } + export type Packument = Omit & { // Fix for license field being incorrectly typed in @npm/types // TODO: Remove this type override when @npm/types fixes the license field typing - license?: string | { type: string; url?: string } + license?: PackumentLicense versions: Record } From cb6d9d4a4efd3ae5fd861ea01fbe9e08175c2a68 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 30 Apr 2026 12:02:28 +0100 Subject: [PATCH 2/2] chore: refactor `normalizeLicense` --- app/composables/npm/usePackage.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/composables/npm/usePackage.ts b/app/composables/npm/usePackage.ts index beff780699..7864f1b6f5 100644 --- a/app/composables/npm/usePackage.ts +++ b/app/composables/npm/usePackage.ts @@ -17,10 +17,10 @@ function getTrustLevel(version: PackumentVersion): PublishTrustLevel { } function normalizeLicense(license?: PackumentLicense): string | undefined { - if (license && typeof license === 'object' && 'type' in license) { - return license.type - } - return typeof license === 'string' ? license : undefined + if (!license) return undefined + if (typeof license === 'string') return license + if (typeof license.type === 'string') return license.type + return undefined } /** @@ -116,7 +116,7 @@ export function transformPackument( } // Normalize license field - const license = normalizeLicense(versionData?.license || pkg.license) + const license = normalizeLicense(requestedVersion ? versionData?.license : pkg.license) // Extract storybook field from the requested version (custom package.json field) const requestedPkgVersion = requestedVersion ? pkg.versions[requestedVersion] : null