From 697578d1fd79c3b72e2a327d78226f1f4fa61edc Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 23 Dec 2024 15:56:38 +0000 Subject: [PATCH] chore(sdf): Migrate list_modules to v2 endpoint --- app/web/src/store/module.store.ts | 8 ++-- lib/sdf-server/src/service/module.rs | 2 - .../src/service/module/list_modules.rs | 46 ------------------- lib/sdf-server/src/service/v2/module.rs | 4 ++ lib/sdf-server/src/service/v2/module/list.rs | 35 ++++++++++++++ lib/si-frontend-types-rs/src/lib.rs | 3 +- lib/si-frontend-types-rs/src/module.rs | 7 +++ 7 files changed, 52 insertions(+), 53 deletions(-) delete mode 100644 lib/sdf-server/src/service/module/list_modules.rs create mode 100644 lib/sdf-server/src/service/v2/module/list.rs diff --git a/app/web/src/store/module.store.ts b/app/web/src/store/module.store.ts index ffad031331..320bb7769f 100644 --- a/app/web/src/store/module.store.ts +++ b/app/web/src/store/module.store.ts @@ -252,15 +252,15 @@ export const useModuleStore = () => { }, async LOAD_LOCAL_MODULES() { - return new ApiRequest<{ modules: LocalModuleSummary[] }>({ - url: "/module/list_modules", - params: { ...getVisibilityParams() }, + return new ApiRequest({ + method: "get", + url: API_PREFIX, onSuccess: (response) => { // TODO: remove this // the backend currently needs the full tar file name // but we want the actual name in the module metadata // easier to strip off temporarily but we'll need to change what the backend is storing - const modulesWithNamesFixed = _.map(response.modules, (m) => ({ + const modulesWithNamesFixed = _.map(response, (m) => ({ ...m, name: m.name.replace(/-\d\d\d\d-\d\d-\d\d\.sipkg/, ""), })); diff --git a/lib/sdf-server/src/service/module.rs b/lib/sdf-server/src/service/module.rs index 1189a0f8d7..7de2737f42 100644 --- a/lib/sdf-server/src/service/module.rs +++ b/lib/sdf-server/src/service/module.rs @@ -37,7 +37,6 @@ pub mod get_module; pub mod import_workspace_vote; pub mod install_module; mod install_workspace; -pub mod list_modules; pub mod reject_module; pub mod remote_module_spec; @@ -254,7 +253,6 @@ pub fn routes() -> Router { "/install_workspace", post(install_workspace::install_workspace), ) - .route("/list_modules", get(list_modules::list_modules)) .route( "/remote_module_spec", get(remote_module_spec::remote_module_spec), diff --git a/lib/sdf-server/src/service/module/list_modules.rs b/lib/sdf-server/src/service/module/list_modules.rs deleted file mode 100644 index c8fff381dd..0000000000 --- a/lib/sdf-server/src/service/module/list_modules.rs +++ /dev/null @@ -1,46 +0,0 @@ -use axum::{extract::Query, Json}; -use dal::{module::Module, Visibility}; -use serde::{Deserialize, Serialize}; - -use super::ModuleResult; -use crate::extract::{AccessBuilder, HandlerContext}; - -#[derive(Deserialize, Serialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct ModuleListRequest { - #[serde(flatten)] - pub visibility: Visibility, -} - -#[derive(Deserialize, Serialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct ModuleListResponse { - pub modules: Vec, -} - -#[derive(Deserialize, Serialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct ModuleView { - name: String, - hash: String, -} - -pub async fn list_modules( - HandlerContext(builder): HandlerContext, - AccessBuilder(request_ctx): AccessBuilder, - Query(request): Query, -) -> ModuleResult> { - let ctx = builder.build(request_ctx.build(request.visibility)).await?; - - let installed_modules = Module::list_installed(&ctx).await?; - - let modules: Vec = installed_modules - .iter() - .map(|module| ModuleView { - name: module.name().to_owned(), - hash: module.root_hash().to_string(), - }) - .collect(); - - Ok(Json(ModuleListResponse { modules })) -} diff --git a/lib/sdf-server/src/service/v2/module.rs b/lib/sdf-server/src/service/v2/module.rs index bcda934822..e6295aa133 100644 --- a/lib/sdf-server/src/service/v2/module.rs +++ b/lib/sdf-server/src/service/v2/module.rs @@ -11,8 +11,11 @@ use thiserror::Error; use crate::{service::ApiError, AppState}; mod contribute; +mod list; mod sync; +pub type ModuleAPIResult = Result; + #[remain::sorted] #[derive(Debug, Error)] pub enum ModulesAPIError { @@ -57,4 +60,5 @@ pub fn v2_routes() -> Router { Router::new() .route("/contribute", post(contribute::contribute)) .route("/sync", get(sync::sync)) + .route("/", get(list::list)) } diff --git a/lib/sdf-server/src/service/v2/module/list.rs b/lib/sdf-server/src/service/v2/module/list.rs new file mode 100644 index 0000000000..10e8b9fb9b --- /dev/null +++ b/lib/sdf-server/src/service/v2/module/list.rs @@ -0,0 +1,35 @@ +use axum::{ + extract::{Host, OriginalUri, Path}, + Json, +}; +use dal::{module::Module, ChangeSetId, WorkspacePk}; +use si_frontend_types::ModuleSummary; + +use super::ModuleAPIResult; +use crate::extract::{AccessBuilder, HandlerContext, PosthogClient, RawAccessToken}; + +pub async fn list( + HandlerContext(builder): HandlerContext, + AccessBuilder(access_builder): AccessBuilder, + RawAccessToken(_raw_access_token): RawAccessToken, + PosthogClient(_posthog_client): PosthogClient, + OriginalUri(_original_uri): OriginalUri, + Host(_host_name): Host, + Path((_workspace_pk, change_set_id)): Path<(WorkspacePk, ChangeSetId)>, +) -> ModuleAPIResult>> { + let ctx = builder + .build(access_builder.build(change_set_id.into())) + .await?; + + let installed_modules = Module::list_installed(&ctx).await?; + + let modules: Vec = installed_modules + .iter() + .map(|module| ModuleSummary { + name: module.name().to_owned(), + hash: module.root_hash().to_string(), + }) + .collect(); + + Ok(Json(modules)) +} diff --git a/lib/si-frontend-types-rs/src/lib.rs b/lib/si-frontend-types-rs/src/lib.rs index eaeeb6e6a7..e33d298e60 100644 --- a/lib/si-frontend-types-rs/src/lib.rs +++ b/lib/si-frontend-types-rs/src/lib.rs @@ -20,7 +20,8 @@ pub use crate::func::{ FuncSummary, LeafInputLocation, }; pub use crate::module::{ - BuiltinModules, LatestModule, ModuleContributeRequest, ModuleDetails, SyncedModules, + BuiltinModules, LatestModule, ModuleContributeRequest, ModuleDetails, ModuleSummary, + SyncedModules, }; pub use crate::schema_variant::{ ComponentType, InputSocket, OutputSocket, Prop, PropKind, SchemaVariant, UninstalledVariant, diff --git a/lib/si-frontend-types-rs/src/module.rs b/lib/si-frontend-types-rs/src/module.rs index cfd8ac1fd9..099a66bc18 100644 --- a/lib/si-frontend-types-rs/src/module.rs +++ b/lib/si-frontend-types-rs/src/module.rs @@ -28,3 +28,10 @@ pub struct ModuleContributeRequest { pub version: String, pub schema_variant_id: SchemaVariantId, } + +#[derive(Clone, Debug, Deserialize, Eq, Serialize, PartialEq, Default)] +#[serde(rename_all = "camelCase")] +pub struct ModuleSummary { + pub name: String, + pub hash: String, +}