21
21
22
22
final class JsDelivrEsmResolver implements PackageResolverInterface
23
23
{
24
- public const URL_PATTERN_VERSION = 'https://data.jsdelivr.com/v1/packages/npm/%s/resolved?specifier=%s ' ;
24
+ public const URL_PATTERN_VERSION = 'https://data.jsdelivr.com/v1/packages/npm/%s/resolved ' ;
25
25
public const URL_PATTERN_DIST_CSS = 'https://cdn.jsdelivr.net/npm/%s@%s%s ' ;
26
26
public const URL_PATTERN_DIST = self ::URL_PATTERN_DIST_CSS .'/+esm ' ;
27
27
public const URL_PATTERN_ENTRYPOINT = 'https://data.jsdelivr.com/v1/packages/npm/%s@%s/entrypoints ' ;
@@ -32,9 +32,6 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
32
32
33
33
public function __construct (
34
34
HttpClientInterface $ httpClient = null ,
35
- private readonly string $ versionUrlPattern = self ::URL_PATTERN_VERSION ,
36
- private readonly string $ distUrlPattern = self ::URL_PATTERN_DIST ,
37
- private readonly string $ distUrlCssPattern = self ::URL_PATTERN_DIST_CSS
38
35
) {
39
36
$ this ->httpClient = $ httpClient ?? HttpClient::create ();
40
37
}
@@ -49,7 +46,6 @@ public function resolvePackages(array $packagesToRequire): array
49
46
$ requiredPackages = [];
50
47
foreach ($ packagesToRequire as $ options ) {
51
48
$ packageSpecifier = trim ($ options ->packageModuleSpecifier , '/ ' );
52
- $ constraint = $ options ->versionConstraint ?? '* ' ;
53
49
54
50
// avoid resolving the same package twice
55
51
if (isset ($ resolvedPackages [$ packageSpecifier ])) {
@@ -58,7 +54,11 @@ public function resolvePackages(array $packagesToRequire): array
58
54
59
55
[$ packageName , $ filePath ] = ImportMapEntry::splitPackageNameAndFilePath ($ packageSpecifier );
60
56
61
- $ response = $ this ->httpClient ->request ('GET ' , sprintf ($ this ->versionUrlPattern , $ packageName , urlencode ($ constraint )));
57
+ $ versionUrl = sprintf (self ::URL_PATTERN_VERSION , $ packageName );
58
+ if (null !== $ options ->versionConstraint ) {
59
+ $ versionUrl .= '?specifier= ' .urlencode ($ options ->versionConstraint );
60
+ }
61
+ $ response = $ this ->httpClient ->request ('GET ' , $ versionUrl );
62
62
$ requiredPackages [] = [$ options , $ response , $ packageName , $ filePath , /* resolved version */ null ];
63
63
}
64
64
@@ -72,7 +72,11 @@ public function resolvePackages(array $packagesToRequire): array
72
72
}
73
73
74
74
$ version = $ response ->toArray ()['version ' ];
75
- $ pattern = str_ends_with ($ filePath , '.css ' ) ? $ this ->distUrlCssPattern : $ this ->distUrlPattern ;
75
+ if (null === $ version ) {
76
+ throw new RuntimeException (sprintf ('Unable to find the latest version for package "%s" - try specifying the version manually. ' , $ packageName ));
77
+ }
78
+
79
+ $ pattern = str_ends_with ($ filePath , '.css ' ) ? self ::URL_PATTERN_DIST_CSS : self ::URL_PATTERN_DIST ;
76
80
$ requiredPackages [$ i ][1 ] = $ this ->httpClient ->request ('GET ' , sprintf ($ pattern , $ packageName , $ version , $ filePath ));
77
81
$ requiredPackages [$ i ][4 ] = $ version ;
78
82
@@ -163,7 +167,7 @@ public function downloadPackages(array $importMapEntries, callable $progressCall
163
167
throw new \InvalidArgumentException (sprintf ('The entry "%s" is not a remote package. ' , $ entry ->importName ));
164
168
}
165
169
166
- $ pattern = ImportMapType::CSS === $ entry ->type ? $ this -> distUrlCssPattern : $ this -> distUrlPattern ;
170
+ $ pattern = ImportMapType::CSS === $ entry ->type ? self :: URL_PATTERN_DIST_CSS : self :: URL_PATTERN_DIST ;
167
171
$ url = sprintf ($ pattern , $ entry ->getPackageName (), $ entry ->version , $ entry ->getPackagePathString ());
168
172
169
173
$ responses [$ package ] = $ this ->httpClient ->request ('GET ' , $ url );
0 commit comments