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)

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

* feat: add APIs to support plugin state in request

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add APIs to support plugin state in request

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: use request app to store request workspace id

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: remove useless if

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Apr 6, 2024
1 parent 55d9b82 commit 12c99d0
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 12c99d0

Please sign in to comment.