From 158ef760fc3c6b2ba0d2d121ac48be7678ffa356 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 25 Oct 2024 16:13:31 +0800 Subject: [PATCH] fix(api-client): supports silent mode (#5512) --- .../core/client/src/api-client/APIClient.ts | 18 ++++++++++++++++-- packages/core/sdk/src/APIClient.ts | 10 ++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/core/client/src/api-client/APIClient.ts b/packages/core/client/src/api-client/APIClient.ts index 9fa5b7e561..6299359f7a 100644 --- a/packages/core/client/src/api-client/APIClient.ts +++ b/packages/core/client/src/api-client/APIClient.ts @@ -61,6 +61,19 @@ export class APIClient extends APIClientSDK { /** 该值会在 AntdAppProvider 中被重新赋值 */ notification: any = notification; + cloneInstance() { + const api = new APIClient(this.options); + api.options = this.options; + api.services = this.services; + api.storage = this.storage; + api.app = this.app; + api.auth = this.auth; + api.storagePrefix = this.storagePrefix; + api.notification = this.notification; + api.axios = this.axios; + return api; + } + getHeaders() { const headers = super.getHeaders(); const appName = this.app.getName(); @@ -180,7 +193,8 @@ export class APIClient extends APIClientSDK { } silent() { - this.silence = true; - return this; + const api = this.cloneInstance(); + api.silence = true; + return api; } } diff --git a/packages/core/sdk/src/APIClient.ts b/packages/core/sdk/src/APIClient.ts index 36e7eac298..e475c145b8 100644 --- a/packages/core/sdk/src/APIClient.ts +++ b/packages/core/sdk/src/APIClient.ts @@ -275,6 +275,7 @@ interface ExtendedOptions { export type APIClientOptions = AxiosInstance | (AxiosRequestConfig & ExtendedOptions); export class APIClient { + options?: APIClientOptions; axios: AxiosInstance; auth: Auth; storage: Storage; @@ -297,11 +298,12 @@ export class APIClient { return headers; } - constructor(instance?: APIClientOptions) { - if (typeof instance === 'function') { - this.axios = instance; + constructor(options?: APIClientOptions) { + this.options = options; + if (typeof options === 'function') { + this.axios = options; } else { - const { authClass, storageType, storageClass, storagePrefix = 'NOCOBASE_', ...others } = instance || {}; + const { authClass, storageType, storageClass, storagePrefix = 'NOCOBASE_', ...others } = options || {}; this.storagePrefix = storagePrefix; this.axios = axios.create(others); this.initStorage(storageClass, storageType);