Skip to content

Commit

Permalink
[Workspace] Add APIs to support plugin state in request (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#6303) (opensearch-project#315)

* feat: add APIs to support plugin state in request (opensearch-project#312)

* feat: add APIs to support plugin state in request



* feat: add APIs to support plugin state in request



---------



* feat: update CHANGELOG



* feat: update



* feat: use request app to store request workspace id



* feat: remove useless if



---------

Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe authored Apr 7, 2024
1 parent ee245d7 commit 0f34d69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/core/server/utils/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe('updateWorkspaceState', () => {
it('update with payload', () => {
const requestMock = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(requestMock, {
id: 'foo',
requestWorkspaceId: 'foo',
});
expect(getWorkspaceState(requestMock)).toEqual({
id: 'foo',
requestWorkspaceId: 'foo',
});
});
});
31 changes: 12 additions & 19 deletions src/core/server/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

/**
* This file is using {@link PluginsStates} to store workspace info into request.
* The best practice would be using {@link Server.register} to register plugins into the hapi server
* but OSD is wrappering the hapi server and the hapi server instance is hidden as internal implementation.
*/
import { PluginsStates } from '@hapi/hapi';
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.
Expand All @@ -20,24 +18,19 @@ import { OpenSearchDashboardsRequest, ensureRawRequest } from '../http/router';
*/
export const updateWorkspaceState = (
request: OpenSearchDashboardsRequest,
payload: Partial<PluginsStates['workspace']>
payload: Partial<WorkspaceState>
) => {
const rawRequest = ensureRawRequest(request);

if (!rawRequest.plugins) {
rawRequest.plugins = {};
}

if (!rawRequest.plugins.workspace) {
rawRequest.plugins.workspace = {};
}

rawRequest.plugins.workspace = {
...rawRequest.plugins.workspace,
rawRequest.app = {
...rawRequest.app,
...payload,
};
};

export const getWorkspaceState = (request: OpenSearchDashboardsRequest) => {
return ensureRawRequest(request).plugins?.workspace;
export const getWorkspaceState = (request: OpenSearchDashboardsRequest): WorkspaceState => {
const { requestWorkspaceId } = ensureRawRequest(request).app as WorkspaceState;
return {
requestWorkspaceId,
};
};

0 comments on commit 0f34d69

Please sign in to comment.