From 5108c8fe7c735d53520a29f82eafb0d73fdacd0d Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Tue, 15 Oct 2024 18:07:55 +0800 Subject: [PATCH] feat(client): allows to store client information into session storage (#5424) * feat(client): allows to store client information into session storage * fix: test error --------- Co-authored-by: chenos --- packages/core/app/client/.umirc.ts | 11 +++++----- packages/core/app/client/src/pages/index.tsx | 3 +++ packages/core/cli/src/util.js | 2 ++ packages/core/devtools/umiConfig.js | 3 ++- packages/core/sdk/src/APIClient.ts | 21 +++++++++++++------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/core/app/client/.umirc.ts b/packages/core/app/client/.umirc.ts index 084f591a95..e3c887a459 100644 --- a/packages/core/app/client/.umirc.ts +++ b/packages/core/app/client/.umirc.ts @@ -18,19 +18,20 @@ export default defineConfig({ devtool: process.env.NODE_ENV === 'development' ? 'source-map' : false, favicons: [`${appPublicPath}favicon/favicon.ico`], metas: [{ name: 'viewport', content: 'initial-scale=0.1' }], - links: [ - { rel: 'stylesheet', href: `${appPublicPath}global.css` }, - ], + links: [{ rel: 'stylesheet', href: `${appPublicPath}global.css` }], headScripts: [ { src: `${appPublicPath}browser-checker.js`, }, { - content: isDevCmd ? '' : ` + content: isDevCmd + ? '' + : ` window['__webpack_public_path__'] = '{{env.APP_PUBLIC_PATH}}'; window['__nocobase_public_path__'] = '{{env.APP_PUBLIC_PATH}}'; window['__nocobase_api_base_url__'] = '{{env.API_BASE_URL}}'; window['__nocobase_api_client_storage_prefix__'] = '{{env.API_CLIENT_STORAGE_PREFIX}}'; + window['__nocobase_api_client_storage_type__'] = '{{env.API_CLIENT_STORAGE_TYPE}}'; window['__nocobase_ws_url__'] = '{{env.WS_URL}}'; window['__nocobase_ws_path__'] = '{{env.WS_PATH}}'; `, @@ -66,7 +67,7 @@ export default defineConfig({ target: ['chrome80', 'es2020'], }, codeSplitting: { - jsStrategy: 'depPerChunk' + jsStrategy: 'depPerChunk', }, chainWebpack(config, { env }) { if (env === 'production') { diff --git a/packages/core/app/client/src/pages/index.tsx b/packages/core/app/client/src/pages/index.tsx index 2b7b273228..2a27f6d896 100644 --- a/packages/core/app/client/src/pages/index.tsx +++ b/packages/core/app/client/src/pages/index.tsx @@ -13,6 +13,9 @@ import devDynamicImport from '../.plugins/index'; export const app = new Application({ apiClient: { + storageType: + // @ts-ignore + window['__nocobase_api_client_storage_type__'] || process.env.API_CLIENT_STORAGE_TYPE || 'localStorage', // @ts-ignore storagePrefix: // @ts-ignore diff --git a/packages/core/cli/src/util.js b/packages/core/cli/src/util.js index 66c834b047..4d58ecb894 100644 --- a/packages/core/cli/src/util.js +++ b/packages/core/cli/src/util.js @@ -291,6 +291,7 @@ function buildIndexHtml(force = false) { const data = fs.readFileSync(tpl, 'utf-8'); const replacedData = data .replace(/\{\{env.APP_PUBLIC_PATH\}\}/g, process.env.APP_PUBLIC_PATH) + .replace(/\{\{env.API_CLIENT_STORAGE_TYPE\}\}/g, process.env.API_CLIENT_STORAGE_TYPE) .replace(/\{\{env.API_CLIENT_STORAGE_PREFIX\}\}/g, process.env.API_CLIENT_STORAGE_PREFIX) .replace(/\{\{env.API_BASE_URL\}\}/g, process.env.API_BASE_URL || process.env.API_BASE_PATH) .replace(/\{\{env.WS_URL\}\}/g, process.env.WEBSOCKET_URL || '') @@ -327,6 +328,7 @@ exports.initEnv = function initEnv() { APP_PORT: 13000, API_BASE_PATH: '/api/', API_CLIENT_STORAGE_PREFIX: 'NOCOBASE_', + API_CLIENT_STORAGE_TYPE: 'localStorage', DB_DIALECT: 'sqlite', DB_STORAGE: 'storage/db/nocobase.sqlite', // DB_TIMEZONE: '+00:00', diff --git a/packages/core/devtools/umiConfig.js b/packages/core/devtools/umiConfig.js index bdccac8e8c..f0a4f5fd2b 100644 --- a/packages/core/devtools/umiConfig.js +++ b/packages/core/devtools/umiConfig.js @@ -8,7 +8,7 @@ const path = require('path'); console.log('VERSION: ', packageJson.version); function getUmiConfig() { - const { APP_PORT, API_BASE_URL, API_CLIENT_STORAGE_PREFIX, APP_PUBLIC_PATH } = process.env; + const { APP_PORT, API_BASE_URL, API_CLIENT_STORAGE_TYPE, API_CLIENT_STORAGE_PREFIX, APP_PUBLIC_PATH } = process.env; const API_BASE_PATH = process.env.API_BASE_PATH || '/api/'; const PROXY_TARGET_URL = process.env.PROXY_TARGET_URL || `http://127.0.0.1:${APP_PORT}`; const LOCAL_STORAGE_BASE_URL = 'storage/uploads/'; @@ -41,6 +41,7 @@ function getUmiConfig() { 'process.env.WS_PATH': process.env.WS_PATH, 'process.env.API_BASE_URL': API_BASE_URL || API_BASE_PATH, 'process.env.API_CLIENT_STORAGE_PREFIX': API_CLIENT_STORAGE_PREFIX, + 'process.env.API_CLIENT_STORAGE_TYPE': API_CLIENT_STORAGE_TYPE, 'process.env.APP_ENV': process.env.APP_ENV, 'process.env.VERSION': packageJson.version, 'process.env.WEBSOCKET_URL': process.env.WEBSOCKET_URL, diff --git a/packages/core/sdk/src/APIClient.ts b/packages/core/sdk/src/APIClient.ts index e898770781..f110816aae 100644 --- a/packages/core/sdk/src/APIClient.ts +++ b/packages/core/sdk/src/APIClient.ts @@ -267,6 +267,7 @@ export class MemoryStorage extends Storage { interface ExtendedOptions { authClass?: any; + storageType?: 'localStorage' | 'sessionStorage' | 'memory'; storageClass?: any; storagePrefix?: string; } @@ -300,10 +301,10 @@ export class APIClient { if (typeof instance === 'function') { this.axios = instance; } else { - const { authClass, storageClass, storagePrefix = 'NOCOBASE_', ...others } = instance || {}; + const { authClass, storageType, storageClass, storagePrefix = 'NOCOBASE_', ...others } = instance || {}; this.storagePrefix = storagePrefix; this.axios = axios.create(others); - this.initStorage(storageClass); + this.initStorage(storageClass, storageType); if (authClass) { this.auth = new authClass(this); } @@ -317,14 +318,20 @@ export class APIClient { this.interceptors(); } - private initStorage(storage?: any) { + private initStorage(storage?: any, storageType = 'localStorage') { if (storage) { this.storage = new storage(this); - } else if (typeof localStorage !== 'undefined') { - this.storage = localStorage; - } else { - this.storage = new MemoryStorage(); + return; } + if (storageType === 'localStorage' && typeof localStorage !== 'undefined') { + this.storage = localStorage; + return; + } + if (storageType === 'sessionStorage' && typeof sessionStorage !== 'undefined') { + this.storage = sessionStorage; + return; + } + this.storage = new MemoryStorage(); } interceptors() {