From 18012cd33bc4eccad0b0cf6f82baf179fbda343b Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Wed, 7 Dec 2022 11:44:53 +0800 Subject: [PATCH] [release-2.0] feat: api client requests support configuring the mute parameter to hide exception toast (#752) This is an automated cherry-pick of #744 /assign ruibaby ```release-note None ``` --- env.d.ts | 8 +++++++ src/composables/use-setting-form.ts | 3 +++ .../themes/components/ThemeListModal.vue | 22 ++++++++++++++----- .../system/plugins/composables/use-plugin.ts | 22 ++++++++++++++----- src/stores/system-states.ts | 18 +++++++-------- src/stores/theme.ts | 21 ++++++++++++------ src/utils/api-client.ts | 6 +++++ 7 files changed, 71 insertions(+), 29 deletions(-) diff --git a/env.d.ts b/env.d.ts index accae153..f5c19f22 100644 --- a/env.d.ts +++ b/env.d.ts @@ -6,6 +6,8 @@ import type { CoreMenuGroupId } from "@halo-dev/console-shared"; import "vue-router"; +import "axios"; + declare module "*.vue" { import type { DefineComponent } from "vue"; // eslint-disable-next-line @@ -28,3 +30,9 @@ declare module "vue-router" { }; } } + +declare module "axios" { + export interface AxiosRequestConfig { + mute?: boolean; + } +} diff --git a/src/composables/use-setting-form.ts b/src/composables/use-setting-form.ts index 8263de15..8254c29e 100644 --- a/src/composables/use-setting-form.ts +++ b/src/composables/use-setting-form.ts @@ -91,6 +91,9 @@ export function useSettingForm( const response = await apiClient.extension.configMap.getv1alpha1ConfigMap( { name: configMapName.value, + }, + { + mute: true, } ); diff --git a/src/modules/interface/themes/components/ThemeListModal.vue b/src/modules/interface/themes/components/ThemeListModal.vue index d3c92d07..f6df9555 100644 --- a/src/modules/interface/themes/components/ThemeListModal.vue +++ b/src/modules/interface/themes/components/ThemeListModal.vue @@ -126,15 +126,25 @@ const handleUninstall = async (theme: Theme, deleteExtensions?: boolean) => { const { settingName, configMapName } = theme.spec; if (settingName) { - await apiClient.extension.setting.deletev1alpha1Setting({ - name: settingName, - }); + await apiClient.extension.setting.deletev1alpha1Setting( + { + name: settingName, + }, + { + mute: true, + } + ); } if (configMapName) { - await apiClient.extension.configMap.deletev1alpha1ConfigMap({ - name: configMapName, - }); + await apiClient.extension.configMap.deletev1alpha1ConfigMap( + { + name: configMapName, + }, + { + mute: true, + } + ); } } catch (e) { console.error("Failed to uninstall theme", e); diff --git a/src/modules/system/plugins/composables/use-plugin.ts b/src/modules/system/plugins/composables/use-plugin.ts index b49f002e..ba5316d0 100644 --- a/src/modules/system/plugins/composables/use-plugin.ts +++ b/src/modules/system/plugins/composables/use-plugin.ts @@ -84,15 +84,25 @@ export function usePluginLifeCycle( const { settingName, configMapName } = plugin.value.spec; if (settingName) { - await apiClient.extension.setting.deletev1alpha1Setting({ - name: settingName, - }); + await apiClient.extension.setting.deletev1alpha1Setting( + { + name: settingName, + }, + { + mute: true, + } + ); } if (configMapName) { - await apiClient.extension.configMap.deletev1alpha1ConfigMap({ - name: configMapName, - }); + await apiClient.extension.configMap.deletev1alpha1ConfigMap( + { + name: configMapName, + }, + { + mute: true, + } + ); } } catch (e) { console.error("Failed to uninstall plugin", e); diff --git a/src/stores/system-states.ts b/src/stores/system-states.ts index 8cada689..32b0fb19 100644 --- a/src/stores/system-states.ts +++ b/src/stores/system-states.ts @@ -1,6 +1,5 @@ import { defineStore } from "pinia"; -import axios from "axios"; -import type { ConfigMap } from "@halo-dev/api-client"; +import { apiClient } from "@/utils/api-client"; interface SystemState { isSetup: boolean; @@ -20,14 +19,13 @@ export const useSystemStatesStore = defineStore({ actions: { async fetchSystemStates() { try { - const { data } = await axios.get( - `${ - import.meta.env.VITE_API_URL - }/api/v1alpha1/configmaps/system-states`, - { - withCredentials: true, - } - ); + const { data } = + await apiClient.extension.configMap.getv1alpha1ConfigMap( + { + name: "system-states", + }, + { mute: true } + ); if (data.data) { this.states = JSON.parse(data.data["states"]); diff --git a/src/stores/theme.ts b/src/stores/theme.ts index 27847d25..a58e012e 100644 --- a/src/stores/theme.ts +++ b/src/stores/theme.ts @@ -14,21 +14,28 @@ export const useThemeStore = defineStore("theme", { async fetchActivatedTheme() { try { const { data } = - await apiClient.extension.configMap.getv1alpha1ConfigMap({ - name: "system", - }); + await apiClient.extension.configMap.getv1alpha1ConfigMap( + { + name: "system", + }, + { mute: true } + ); if (!data.data?.theme) { - // Todo: show error return; } const themeConfig = JSON.parse(data.data.theme); const { data: themeData } = - await apiClient.extension.theme.getthemeHaloRunV1alpha1Theme({ - name: themeConfig.active, - }); + await apiClient.extension.theme.getthemeHaloRunV1alpha1Theme( + { + name: themeConfig.active, + }, + { + mute: true, + } + ); this.activatedTheme = themeData; } catch (e) { diff --git a/src/utils/api-client.ts b/src/utils/api-client.ts index a14e697e..202fc9ef 100644 --- a/src/utils/api-client.ts +++ b/src/utils/api-client.ts @@ -73,6 +73,12 @@ axiosInstance.interceptors.response.use( const { title } = errorResponse.data; + // Don't show error toast + // see https://github.com/halo-dev/halo/issues/2836 + if (errorResponse.config.mute) { + return Promise.reject(error); + } + if (status === 400) { Toast.error(`请求参数错误:${title}`); } else if (status === 401) {