Skip to content

Commit cbeb9a0

Browse files
authored
fix: show license from correct version
1 parent 5eab00c commit cbeb9a0

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

app/composables/npm/usePackage.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ function getTrustLevel(version: PackumentVersion): PublishTrustLevel {
1616
return 'none'
1717
}
1818

19+
function normalizeLicense(license?: PackumentLicense): string | undefined {
20+
if (license && typeof license === 'object' && 'type' in license) {
21+
return license.type
22+
}
23+
return typeof license === 'string' ? license : undefined
24+
}
25+
1926
/**
2027
* Transform a full Packument into a slimmed version for client-side use.
2128
* Reduces payload size by:
@@ -72,6 +79,7 @@ export function transformPackument(
7279
for (const v of includedVersions) {
7380
const version = pkg.versions[v]
7481
if (version) {
82+
const versionLicense = normalizeLicense(version.license)
7583
if (version.version === requestedVersion) {
7684
// Strip readme from each version, extract install scripts info
7785
const { readme: _readme, scripts, ...slimVersion } = version
@@ -80,25 +88,20 @@ export function transformPackument(
8088
const installScripts = scripts ? extractInstallScriptsInfo(scripts) : null
8189
versionData = {
8290
...slimVersion,
91+
license: versionLicense,
8392
installScripts: installScripts ?? undefined,
8493
}
8594
}
8695
const trustLevel = getTrustLevel(version)
8796
const hasProvenance = trustLevel !== 'none'
8897

89-
// Normalize license: some versions use { type: "MIT" } instead of "MIT"
90-
let versionLicense = version.license
91-
if (versionLicense && typeof versionLicense === 'object' && 'type' in versionLicense) {
92-
versionLicense = (versionLicense as { type: string }).type
93-
}
94-
9598
filteredVersions[v] = {
9699
hasProvenance,
97100
trustLevel,
98101
version: version.version,
99102
deprecated: version.deprecated,
100103
tags: version.tags as string[],
101-
license: typeof versionLicense === 'string' ? versionLicense : undefined,
104+
license: versionLicense,
102105
type: typeof version.type === 'string' ? version.type : undefined,
103106
}
104107
}
@@ -113,10 +116,7 @@ export function transformPackument(
113116
}
114117

115118
// Normalize license field
116-
let license = pkg.license
117-
if (license && typeof license === 'object' && 'type' in license) {
118-
license = license.type
119-
}
119+
const license = normalizeLicense(versionData?.license || pkg.license)
120120

121121
// Extract storybook field from the requested version (custom package.json field)
122122
const requestedPkgVersion = requestedVersion ? pkg.versions[requestedVersion] : null

shared/types/npm-registry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ export interface PackumentVersion extends PackumentVersionWithoutAttestations {
2323
dist: PackumentVersionWithoutAttestations['dist'] & { attestations?: NpmVersionAttestations }
2424
}
2525

26+
export type PackumentLicense = string | { type: string; url?: string }
27+
2628
export type Packument = Omit<PackumentWithoutLicenseObjects, 'license' | 'versions'> & {
2729
// Fix for license field being incorrectly typed in @npm/types
2830
// TODO: Remove this type override when @npm/types fixes the license field typing
29-
license?: string | { type: string; url?: string }
31+
license?: PackumentLicense
3032
versions: Record<string, PackumentVersion>
3133
}
3234

0 commit comments

Comments
 (0)