Add basic registry client

This commit is contained in:
SkyfallWasTaken 2024-07-29 14:50:13 +01:00
parent 6a4a73426a
commit 32845b9006
3 changed files with 740 additions and 4 deletions

View file

@ -0,0 +1,10 @@
[package]
name = "dinopkg-npm-registry"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12.5", features = ["json"] }
serde = { version = "1.0.204", features = ["derive"] }
thiserror = "1.0.63"
dinopkg-package-json = { path = "../dinopkg-package-json" }

View file

@ -0,0 +1,32 @@
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use dinopkg_package_json::PackageJson;
const NPM_REGISTRY_ROOT_URL: &str = "https://registry.npmjs.org";
#[derive(Serialize, Deserialize)]
pub struct PackageInfo {
/// The name of the package, for example `discord.js`.
name: String,
/// A map of versions to their respective version info.
///
/// The key is the version string (e.g. `0.1.0`), and the value is the version's `package.json` info.
versions: HashMap<String, PackageJson>,
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("network error: {0}")]
NetworkError(#[from] reqwest::Error),
}
impl PackageInfo {
pub async fn get_package_info(package_name: &str, client: &reqwest::Client) -> Result<PackageInfo, Error> {
let url = format!("{NPM_REGISTRY_ROOT_URL}/{package_name}");
let response = client.get(&url).send().await?;
let package_info = response.json::<PackageInfo>().await?;
Ok(package_info)
}
}