getVersionPubspec method

Future<Map<String, dynamic>?> getVersionPubspec(
  1. String packageName,
  2. String version
)

Get the pubspec for a specific version of a package.

Extracts the pubspec field from the version info response. Returns null on failure.

Implementation

Future<Map<String, dynamic>?> getVersionPubspec(
  String packageName,
  String version,
) async {
  try {
    // First, try to extract from the full package info if already cached.
    // This avoids an extra HTTP request when the data is already available.
    final cacheKey = 'pub_package_$packageName';
    final cachedPackage = await _cache.get(cacheKey);
    if (cachedPackage != null) {
      final versions = cachedPackage['versions'] as List<dynamic>? ?? [];
      for (final v in versions) {
        final versionMap = v as Map<String, dynamic>;
        if (versionMap['version'] == version) {
          return versionMap['pubspec'] as Map<String, dynamic>?;
        }
      }
    }

    // Fall back to the version-specific endpoint.
    final versionInfo = await getVersionInfo(packageName, version);
    return versionInfo?['pubspec'] as Map<String, dynamic>?;
  } catch (e, stack) {
    Logger.error(
      'Error fetching pubspec for $packageName@$version',
      e,
      stack,
    );
    return null;
  }
}