Skip to content

Commit

Permalink
feat: add APIs to support plugin state in request (opensearch-project…
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
SuZhou-Joe committed Apr 2, 2024
1 parent 8810f08 commit 710a0d4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
19 changes: 19 additions & 0 deletions src/core/server/utils/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -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, {
id: 'foo',
});
expect(getWorkspaceState(requestMock)).toEqual({
id: 'foo',
});
});
});
43 changes: 43 additions & 0 deletions src/core/server/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright OpenSearch Contributors
* 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';

/**
* 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<PluginsStates['workspace']>
) => {
const rawRequest = ensureRawRequest(request);

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

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

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

export const getWorkspaceState = (request: OpenSearchDashboardsRequest) => {
return ensureRawRequest(request).plugins?.workspace;
};
8 changes: 8 additions & 0 deletions src/legacy/server/osd_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ 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 710a0d4

Please sign in to comment.