Skip to content

Commit 77f8f81

Browse files
authored
fix: normalise package licence field (#944)
1 parent c9d9c9d commit 77f8f81

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

app/composables/npm/usePackage.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ function transformPackument(pkg: Packument, requestedVersion?: string | null): S
6969
if (pkg.time[v]) filteredTime[v] = pkg.time[v]
7070
}
7171

72+
// Normalize license field
73+
let license = pkg.license
74+
if (license && typeof license === 'object' && 'type' in license) {
75+
license = license.type
76+
}
77+
7278
return {
7379
'_id': pkg._id,
7480
'_rev': pkg._rev,
@@ -78,7 +84,7 @@ function transformPackument(pkg: Packument, requestedVersion?: string | null): S
7884
'time': filteredTime,
7985
'maintainers': pkg.maintainers,
8086
'author': pkg.author,
81-
'license': pkg.license,
87+
'license': license,
8288
'homepage': pkg.homepage,
8389
'keywords': pkg.keywords,
8490
'repository': pkg.repository,

app/composables/usePackageComparison.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ export function usePackageComparison(packageNames: MaybeRefOrGetter<string[]>) {
152152
severity: vulnsSeverity,
153153
},
154154
metadata: {
155-
license: pkgData.license,
155+
license:
156+
typeof pkgData.license === 'object' && 'type' in pkgData.license
157+
? pkgData.license.type
158+
: pkgData.license,
156159
// Use version-specific publish time, NOT time.modified (which can be
157160
// updated by metadata changes like maintainer additions)
158161
lastUpdated: pkgData.time?.[latestVersion],

shared/types/npm-registry.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
* @see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md
77
*/
88

9-
import type { PackumentVersion } from '@npm/types'
9+
import type { Packument as PackumentWithoutLicenseObjects, PackumentVersion } from '@npm/types'
1010
import type { ReadmeResponse } from './readme'
1111

1212
// Re-export official npm types for packument/manifest
13-
export type {
14-
Packument,
15-
PackumentVersion,
16-
Manifest,
17-
ManifestVersion,
18-
PackageJSON,
19-
} from '@npm/types'
13+
export type { PackumentVersion, Manifest, ManifestVersion, PackageJSON } from '@npm/types'
14+
15+
// TODO: Remove this type override when @npm/types fixes the license field typing
16+
export type Packument = Omit<PackumentWithoutLicenseObjects, 'license'> & {
17+
// Fix for license field being incorrectly typed in @npm/types
18+
license?: string | { type: string; url?: string }
19+
}
2020

2121
/** Install scripts info (preinstall, install, postinstall) */
2222
export interface InstallScriptsInfo {

0 commit comments

Comments
 (0)