diff --git a/CHANGELOG.md b/CHANGELOG.md index f5b9cf279694..75a1b4923452 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 42b01e72b0d1..a20b8c4c4e5b 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 000000000000..7dfcff9e5d18 --- /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 000000000000..2003e615d501 --- /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, + }; +};