forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add APIs to support plugin state in request
Signed-off-by: SuZhou-Joe <[email protected]>
- Loading branch information
1 parent
a2cead4
commit fb3f97e
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters