From fc3fef28019cec891af69148b81a5d70e2c855de Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Fri, 5 Apr 2024 01:11:20 +0800 Subject: [PATCH] [Workspace] Add APIs to support plugin state in request (#6303) * feat: add APIs to support plugin state in request (#312) * feat: add APIs to support plugin state in request Signed-off-by: SuZhou-Joe * feat: add APIs to support plugin state in request Signed-off-by: SuZhou-Joe --------- Signed-off-by: SuZhou-Joe * feat: update CHANGELOG Signed-off-by: SuZhou-Joe * feat: update Signed-off-by: SuZhou-Joe * feat: use request app to store request workspace id Signed-off-by: SuZhou-Joe * feat: remove useless if Signed-off-by: SuZhou-Joe --------- Signed-off-by: SuZhou-Joe --- CHANGELOG.md | 1 + src/core/server/utils/index.ts | 1 + src/core/server/utils/workspace.test.ts | 19 +++++++++++++ src/core/server/utils/workspace.ts | 36 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 src/core/server/utils/workspace.test.ts create mode 100644 src/core/server/utils/workspace.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f5b9cf27969..75a1b492345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multiple Datasource] Fetch data source title for DataSourceView when only id is provided ([#6315](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6315) - [Workspace] Add permission control logic ([#6052](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6052)) - [Multiple Datasource] Pass selected data sources to plugin consumers when the multi-select component initially loads ([#6333](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6333)) +- [Workspace] Add APIs to support plugin state in request ([#6303](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6303)) ### 🐛 Bug Fixes diff --git a/src/core/server/utils/index.ts b/src/core/server/utils/index.ts index 42b01e72b0d..a20b8c4c4e5 100644 --- a/src/core/server/utils/index.ts +++ b/src/core/server/utils/index.ts @@ -33,3 +33,4 @@ export * from './from_root'; export * from './package_json'; export * from './streams'; export { getWorkspaceIdFromUrl, cleanWorkspaceId } from '../../utils'; +export { updateWorkspaceState, getWorkspaceState } from './workspace'; diff --git a/src/core/server/utils/workspace.test.ts b/src/core/server/utils/workspace.test.ts new file mode 100644 index 00000000000..7dfcff9e5d1 --- /dev/null +++ b/src/core/server/utils/workspace.test.ts @@ -0,0 +1,19 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { httpServerMock } from '../mocks'; +import { getWorkspaceState, updateWorkspaceState } from './workspace'; + +describe('updateWorkspaceState', () => { + it('update with payload', () => { + const requestMock = httpServerMock.createOpenSearchDashboardsRequest(); + updateWorkspaceState(requestMock, { + requestWorkspaceId: 'foo', + }); + expect(getWorkspaceState(requestMock)).toEqual({ + requestWorkspaceId: 'foo', + }); + }); +}); diff --git a/src/core/server/utils/workspace.ts b/src/core/server/utils/workspace.ts new file mode 100644 index 00000000000..2003e615d50 --- /dev/null +++ b/src/core/server/utils/workspace.ts @@ -0,0 +1,36 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { OpenSearchDashboardsRequest, ensureRawRequest } from '../http/router'; + +export interface WorkspaceState { + requestWorkspaceId?: string; +} + +/** + * This function will be used as a proxy + * because `ensureRequest` is only importable from core module. + * + * @param workspaceId string + * @returns void + */ +export const updateWorkspaceState = ( + request: OpenSearchDashboardsRequest, + payload: Partial +) => { + const rawRequest = ensureRawRequest(request); + + rawRequest.app = { + ...rawRequest.app, + ...payload, + }; +}; + +export const getWorkspaceState = (request: OpenSearchDashboardsRequest): WorkspaceState => { + const { requestWorkspaceId } = ensureRawRequest(request).app as WorkspaceState; + return { + requestWorkspaceId, + }; +};