Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 9 additions & 60 deletions api/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,66 +802,15 @@ pub enum ApiUpdateScopeRequest {
Description(Option<String>),
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStats {
pub newest: Vec<ApiStatsPackage>,
pub updated: Vec<ApiStatsPackageVersion>,
pub featured: Vec<ApiStatsPackage>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStatsPackage {
pub scope: ScopeName,
pub name: PackageName,
}

impl From<StatsPackage> for ApiStatsPackage {
fn from(p: StatsPackage) -> Self {
Self {
scope: p.scope,
name: p.name,
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStatsPackageVersion {
pub scope: ScopeName,
pub package: PackageName,
pub version: Version,
}

impl From<StatsPackageVersion> for ApiStatsPackageVersion {
fn from(v: StatsPackageVersion) -> Self {
Self {
scope: v.scope,
package: v.name,
version: v.version,
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiMetrics {
pub packages: usize,
pub packages_1d: usize,
pub packages_7d: usize,
pub packages_30d: usize,

pub users: usize,
pub users_1d: usize,
pub users_7d: usize,
pub users_30d: usize,

pub package_versions: usize,
pub package_versions_1d: usize,
pub package_versions_7d: usize,
pub package_versions_30d: usize,
}
// `ApiStats`, `ApiStatsPackage`, `ApiStatsPackageVersion`, and `ApiMetrics` now
// live in the shared, wasm-safe `jsr_types` crate so the workers-rs front serves
// byte-identical JSON for the ported `GET /api/stats` and `GET /api/metrics`
// endpoints. Re-exported here so existing `crate::api::types::*` paths keep
// working.
pub use jsr_types::api::ApiMetrics;
pub use jsr_types::api::ApiStats;
pub use jsr_types::api::ApiStatsPackage;
pub use jsr_types::api::ApiStatsPackageVersion;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
80 changes: 80 additions & 0 deletions crates/jsr_types/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.

//! Shared API wire types.
//!
//! These are the JSON response shapes served on `api.jsr.io`. They live in the
//! shared crate so both the Cloud Run compute service and the workers-rs front
//! serialize byte-identical responses (JSON parity), and so the Worker can
//! build them without depending on the native `api` crate. Only the wire types
//! that the Worker actually produces are moved here as their endpoints migrate;
//! the rest stay in `api/src/api/types.rs` for now.

use serde::Deserialize;
use serde::Serialize;

use crate::ids::PackageName;
use crate::ids::ScopeName;
use crate::ids::Version;
use crate::models::StatsPackage;
use crate::models::StatsPackageVersion;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStats {
pub newest: Vec<ApiStatsPackage>,
pub updated: Vec<ApiStatsPackageVersion>,
pub featured: Vec<ApiStatsPackage>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStatsPackage {
pub scope: ScopeName,
pub name: PackageName,
}

impl From<StatsPackage> for ApiStatsPackage {
fn from(p: StatsPackage) -> Self {
Self {
scope: p.scope,
name: p.name,
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiStatsPackageVersion {
pub scope: ScopeName,
pub package: PackageName,
pub version: Version,
}

impl From<StatsPackageVersion> for ApiStatsPackageVersion {
fn from(v: StatsPackageVersion) -> Self {
Self {
scope: v.scope,
package: v.name,
version: v.version,
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiMetrics {
pub packages: usize,
pub packages_1d: usize,
pub packages_7d: usize,
pub packages_30d: usize,

pub users: usize,
pub users_1d: usize,
pub users_7d: usize,
pub users_30d: usize,

pub package_versions: usize,
pub package_versions_1d: usize,
pub package_versions_7d: usize,
pub package_versions_30d: usize,
}
1 change: 1 addition & 0 deletions crates/jsr_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
//! too, but are gated behind the default-off `sqlx` feature so wasm builds
//! never compile them.

pub mod api;
pub mod ids;
pub mod models;
Loading
Loading