From e55b57afd6136328cb303282bd04bf07555a2996 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 19 Apr 2024 00:04:49 +0000 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 (cherry picked from commit fc3fef28019cec891af69148b81a5d70e2c855de) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- src/core/server/utils/index.ts | 1 + src/core/server/utils/workspace.test.ts | 19 +++++++++++++ src/core/server/utils/workspace.ts | 36 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/core/server/utils/workspace.test.ts create mode 100644 src/core/server/utils/workspace.ts 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, + }; +};