Skip to content

Commit

Permalink
feat: use request app to store request workspace id
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Apr 3, 2024
1 parent b7a6cb7 commit dd6cebf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 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: 14 additions & 17 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,23 @@ 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.app) {
rawRequest.app = {};
}

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,
};
};
8 changes: 0 additions & 8 deletions src/legacy/server/osd_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ declare module 'hapi' {
}
}

declare module '@hapi/hapi' {
interface PluginsStates {
workspace?: {
id?: string;
};
}
}

type OsdMixinFunc = (osdServer: OsdServer, server: Server, config: any) => Promise<any> | void;

export interface PluginsSetup {
Expand Down

0 comments on commit dd6cebf

Please sign in to comment.