mirror of
https://github.com/SkyfallWasTaken/dinopkg.git
synced 2025-05-01 01:39:29 +00:00
Add basic registry client
This commit is contained in:
parent
6a4a73426a
commit
32845b9006
3 changed files with 740 additions and 4 deletions
10
crates/dinopkg-npm-registry/Cargo.toml
Normal file
10
crates/dinopkg-npm-registry/Cargo.toml
Normal 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" }
|
32
crates/dinopkg-npm-registry/src/lib.rs
Normal file
32
crates/dinopkg-npm-registry/src/lib.rs
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue