2121
2222final class JsDelivrEsmResolver implements PackageResolverInterface
2323{
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 ' ;
2525 public const URL_PATTERN_DIST_CSS = 'https://cdn.jsdelivr.net/npm/%s@%s%s ' ;
2626 public const URL_PATTERN_DIST = self ::URL_PATTERN_DIST_CSS .'/+esm ' ;
2727 public const URL_PATTERN_ENTRYPOINT = 'https://data.jsdelivr.com/v1/packages/npm/%s@%s/entrypoints ' ;
@@ -32,9 +32,6 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
3232
3333 public function __construct (
3434 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
3835 ) {
3936 $ this ->httpClient = $ httpClient ?? HttpClient::create ();
4037 }
@@ -49,7 +46,6 @@ public function resolvePackages(array $packagesToRequire): array
4946 $ requiredPackages = [];
5047 foreach ($ packagesToRequire as $ options ) {
5148 $ packageSpecifier = trim ($ options ->packageModuleSpecifier , '/ ' );
52- $ constraint = $ options ->versionConstraint ?? '* ' ;
5349
5450 // avoid resolving the same package twice
5551 if (isset ($ resolvedPackages [$ packageSpecifier ])) {
@@ -58,7 +54,11 @@ public function resolvePackages(array $packagesToRequire): array
5854
5955 [$ packageName , $ filePath ] = ImportMapEntry::splitPackageNameAndFilePath ($ packageSpecifier );
6056
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 );
6262 $ requiredPackages [] = [$ options , $ response , $ packageName , $ filePath , /* resolved version */ null ];
6363 }
6464
@@ -72,7 +72,11 @@ public function resolvePackages(array $packagesToRequire): array
7272 }
7373
7474 $ 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 ;
7680 $ requiredPackages [$ i ][1 ] = $ this ->httpClient ->request ('GET ' , sprintf ($ pattern , $ packageName , $ version , $ filePath ));
7781 $ requiredPackages [$ i ][4 ] = $ version ;
7882
@@ -163,7 +167,7 @@ public function downloadPackages(array $importMapEntries, callable $progressCall
163167 throw new \InvalidArgumentException (sprintf ('The entry "%s" is not a remote package. ' , $ entry ->importName ));
164168 }
165169
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 ;
167171 $ url = sprintf ($ pattern , $ entry ->getPackageName (), $ entry ->version , $ entry ->getPackagePathString ());
168172
169173 $ responses [$ package ] = $ this ->httpClient ->request ('GET ' , $ url );
0 commit comments