Skip to content

Commit 2d8b4f2

Browse files
committed
refactor: add exported PackageURL types
Add PackageURLComponentValue and PackageURLObject as exported reusable types. Update toJSON() and toObject() return types to use PackageURLObject instead of Record<string, any>. Remove unused index signature from PackageURL class.
1 parent b851db0 commit 2d8b4f2

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/package-url.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ import type {
4848
import type { Result } from './result.js'
4949
import type { DownloadUrl, RepositoryUrl } from './url-converter.js'
5050

51+
/**
52+
* Type representing the possible values for PackageURL component properties.
53+
* Used for index signature to allow dynamic property access.
54+
*/
55+
export type PackageURLComponentValue = string | QualifiersObject | undefined
56+
57+
/**
58+
* Type representing a plain object representation of a PackageURL.
59+
* Contains all package URL components as properties.
60+
*/
61+
export type PackageURLObject = {
62+
type?: string
63+
namespace?: string
64+
name?: string
65+
version?: string
66+
qualifiers?: QualifiersObject
67+
subpath?: string
68+
}
69+
5170
// Pattern to match URLs with schemes other than "pkg".
5271
const OTHER_SCHEME_PATTERN = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//
5372

@@ -68,9 +87,7 @@ class PackageURL {
6887
qualifiers?: QualifiersObject | undefined
6988
subpath?: string | undefined
7089
type?: string | undefined
71-
version?: string | undefined;
72-
73-
[key: string]: any
90+
version?: string | undefined
7491

7592
constructor(
7693
rawType: unknown,
@@ -161,7 +178,7 @@ class PackageURL {
161178
/**
162179
* Convert PackageURL to object for JSON.stringify compatibility.
163180
*/
164-
toJSON(): Record<string, any> {
181+
toJSON(): PackageURLObject {
165182
return this.toObject()
166183
}
167184

@@ -175,8 +192,8 @@ class PackageURL {
175192
/**
176193
* Convert PackageURL to a plain object representation.
177194
*/
178-
toObject(): Record<string, any> {
179-
const result: Record<string, any> = {}
195+
toObject(): PackageURLObject {
196+
const result: PackageURLObject = {}
180197
if (this.type !== undefined) {
181198
result['type'] = this.type
182199
}

0 commit comments

Comments
 (0)