Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/composables/npm/usePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
}
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion shared/types/npm-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackumentWithoutLicenseObjects, 'license' | 'versions'> & {
// 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<string, PackumentVersion>
}

Expand Down
Loading