diff --git a/app/composables/npm/usePackage.ts b/app/composables/npm/usePackage.ts index 747de4992e..7864f1b6f5 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) return undefined + if (typeof license === 'string') return license + if (typeof license.type === 'string') return license.type + return 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(requestedVersion ? 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 }