Skip to content

Commit

Permalink
feat: some changes to add core workspace util
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Mar 21, 2024
1 parent 4bf9eb4 commit 216ac3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/core/server/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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
*/
import { Server } from '@hapi/hapi';
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: PluginsStates['workspace']
) => {
if (!payload) {
return undefined;
}

const rawRequest = ensureRawRequest(request);

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 216ac3a

Please sign in to comment.