Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Add APIs to support plugin state in request (#6303) #315

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
};
Loading