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

Feature/move utils #7

Open
wants to merge 25 commits into
base: backport/backport-212-to-workspace
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
43acbfb
feat: enable workspace id in basePath
SuZhou-Joe Oct 9, 2023
3c128d1
feat: add unit test
SuZhou-Joe Oct 10, 2023
c5a1a64
Register Advance Settings, Data Source management,Index Pattern manag…
raintygao Oct 10, 2023
8e1310b
feat: remove useless test object id
SuZhou-Joe Oct 10, 2023
1e82266
feat: add unit test
SuZhou-Joe Oct 10, 2023
b3d9a00
feat: add unit test
SuZhou-Joe Oct 10, 2023
a758fa8
feat: update snapshot
SuZhou-Joe Oct 10, 2023
2ca6444
remove unnecessary workspace enabled flag from core workspace module …
ruanyl Oct 10, 2023
2cb7047
feat: move formatUrlWithWorkspaceId to core/public/utils
SuZhou-Joe Oct 10, 2023
2499584
feat: remove useless variable
SuZhou-Joe Oct 11, 2023
cfbe94d
feat: remove useless code
SuZhou-Joe Oct 11, 2023
20a64a5
Merge branch 'workspace' into feature/move-utils
SuZhou-Joe Oct 12, 2023
4db94fc
[Workspace]Add workspace id in basePath (#212)
SuZhou-Joe Oct 12, 2023
2653ab4
feat: optimization
SuZhou-Joe Oct 11, 2023
1eb48dd
feat: optimization
SuZhou-Joe Oct 11, 2023
a1d00ea
feat: move workspace/utils to core
SuZhou-Joe Oct 11, 2023
2da5246
feat: update comment
SuZhou-Joe Oct 12, 2023
2b6764a
feat: optimize code
SuZhou-Joe Oct 12, 2023
bf64805
feat: update unit test
SuZhou-Joe Oct 12, 2023
6ea3891
feat: optimization
SuZhou-Joe Oct 12, 2023
d0ac2ec
feat: add space under license
SuZhou-Joe Oct 12, 2023
640c456
fix: unit test
SuZhou-Joe Oct 12, 2023
a69e05e
Merge branch 'backport/backport-212-to-workspace' into feature/move-u…
SuZhou-Joe Oct 13, 2023
adc6029
feat: some sync
SuZhou-Joe Oct 13, 2023
b1bd984
Merge branch 'backport/backport-212-to-workspace' into feature/move-u…
SuZhou-Joe Oct 13, 2023
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
32 changes: 32 additions & 0 deletions src/core/public/http/base_path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,36 @@ describe('BasePath', () => {
expect(new BasePath('/foo/bar', '/foo').serverBasePath).toEqual('/foo');
});
});

describe('workspaceBasePath', () => {
it('get path with workspace', () => {
expect(new BasePath('/foo/bar', '/foo/bar', '/workspace').get()).toEqual(
'/foo/bar/workspace'
);
});

it('getBasePath with workspace provided', () => {
expect(new BasePath('/foo/bar', '/foo/bar', '/workspace').getBasePath()).toEqual('/foo/bar');
});

it('prepend with workspace provided', () => {
expect(new BasePath('/foo/bar', '/foo/bar', '/workspace').prepend('/prepend')).toEqual(
'/foo/bar/workspace/prepend'
);
});

it('prepend with workspace provided but calls without workspace', () => {
expect(
new BasePath('/foo/bar', '/foo/bar', '/workspace').prepend('/prepend', {
withoutWorkspace: true,
})
).toEqual('/foo/bar/prepend');
});

it('remove with workspace provided', () => {
expect(
new BasePath('/foo/bar', '/foo/bar', '/workspace').remove('/foo/bar/workspace/remove')
).toEqual('/remove');
});
});
});
10 changes: 5 additions & 5 deletions src/core/public/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type HttpSetupMock = jest.Mocked<HttpSetup> & {
anonymousPaths: jest.Mocked<HttpSetup['anonymousPaths']>;
};

const createServiceMock = ({ basePath = '' } = {}): HttpSetupMock => ({
const createServiceMock = ({ basePath = '', workspaceBasePath = '' } = {}): HttpSetupMock => ({
fetch: jest.fn(),
get: jest.fn(),
head: jest.fn(),
Expand All @@ -48,7 +48,7 @@ const createServiceMock = ({ basePath = '' } = {}): HttpSetupMock => ({
patch: jest.fn(),
delete: jest.fn(),
options: jest.fn(),
basePath: new BasePath(basePath),
basePath: new BasePath(basePath, undefined, workspaceBasePath),
anonymousPaths: {
register: jest.fn(),
isAnonymous: jest.fn(),
Expand All @@ -58,14 +58,14 @@ const createServiceMock = ({ basePath = '' } = {}): HttpSetupMock => ({
intercept: jest.fn(),
});

const createMock = ({ basePath = '' } = {}) => {
const createMock = ({ basePath = '', workspaceBasePath = '' } = {}) => {
const mocked: jest.Mocked<PublicMethodsOf<HttpService>> = {
setup: jest.fn(),
start: jest.fn(),
stop: jest.fn(),
};
mocked.setup.mockReturnValue(createServiceMock({ basePath }));
mocked.start.mockReturnValue(createServiceMock({ basePath }));
mocked.setup.mockReturnValue(createServiceMock({ basePath, workspaceBasePath }));
mocked.start.mockReturnValue(createServiceMock({ basePath, workspaceBasePath }));
return mocked;
};

Expand Down
26 changes: 26 additions & 0 deletions src/core/public/http/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ describe('#setup()', () => {
// We don't verify that this Observable comes from Fetch#getLoadingCount$() to avoid complex mocking
expect(loadingServiceSetup.addLoadingCountSource).toHaveBeenCalledWith(expect.any(Observable));
});

it('setup basePath without workspaceId provided in window.location.href', () => {
const injectedMetadata = injectedMetadataServiceMock.createSetupContract();
const fatalErrors = fatalErrorsServiceMock.createSetupContract();
const httpService = new HttpService();
const setupResult = httpService.setup({ fatalErrors, injectedMetadata });
expect(setupResult.basePath.get()).toEqual('');
});

it('setup basePath with workspaceId provided in window.location.href', () => {
const windowSpy = jest.spyOn(window, 'window', 'get');
windowSpy.mockImplementation(
() =>
({
location: {
href: 'http://localhost/w/workspaceId/app',
},
} as any)
);
const injectedMetadata = injectedMetadataServiceMock.createSetupContract();
const fatalErrors = fatalErrorsServiceMock.createSetupContract();
const httpService = new HttpService();
const setupResult = httpService.setup({ fatalErrors, injectedMetadata });
expect(setupResult.basePath.get()).toEqual('/w/workspaceId');
windowSpy.mockRestore();
});
});

describe('#stop()', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,3 @@ export {
MANAGEMENT_WORKSPACE_ID,
WORKSPACE_TYPE,
} from '../utils';

export { getWorkspaceIdFromUrl } from './utils';
5 changes: 3 additions & 2 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
export { shareWeakReplay } from './share_weak_replay';
export { Sha256 } from './crypto';
export { MountWrapper, mountReactNode } from './mount';
export { getWorkspaceIdFromUrl } from './workspace';
export {
WORKSPACE_PATH_PREFIX,
WORKSPACE_TYPE,
formatUrlWithWorkspaceId,
getWorkspaceIdFromUrl,
PUBLIC_WORKSPACE_ID,
MANAGEMENT_WORKSPACE_ID,
WORKSPACE_TYPE,
} from '../../utils';
15 changes: 0 additions & 15 deletions src/core/public/utils/workspace.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/core/public/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export { WorkspacesStart, WorkspacesService, WorkspacesSetup } from './workspaces_service';
1 change: 1 addition & 0 deletions src/core/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from './crypto';
export * from './from_root';
export * from './package_json';
export * from './streams';
export { getWorkspaceIdFromUrl, cleanWorkspaceId } from '../../utils';
1 change: 1 addition & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ export {
WORKSPACE_TYPE,
PERSONAL_WORKSPACE_ID_PREFIX,
} from './constants';
export { getWorkspaceIdFromUrl, formatUrlWithWorkspaceId, cleanWorkspaceId } from './workspace';
32 changes: 32 additions & 0 deletions src/core/utils/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { getWorkspaceIdFromUrl, formatUrlWithWorkspaceId } from './workspace';
import { httpServiceMock } from '../public/mocks';

describe('#getWorkspaceIdFromUrl', () => {
it('return workspace when there is a match', () => {
expect(getWorkspaceIdFromUrl('http://localhost/w/foo')).toEqual('foo');
});

it('return empty when there is not a match', () => {
expect(getWorkspaceIdFromUrl('http://localhost/w2/foo')).toEqual('');
});
});

describe('#formatUrlWithWorkspaceId', () => {
const basePathWithoutWorkspaceBasePath = httpServiceMock.createSetupContract().basePath;
it('return url with workspace prefix when format with a id provided', () => {
expect(
formatUrlWithWorkspaceId('/app/dashboard', 'foo', basePathWithoutWorkspaceBasePath)
).toEqual('http://localhost/w/foo/app/dashboard');
});

it('return url without workspace prefix when format without a id', () => {
expect(
formatUrlWithWorkspaceId('/w/foo/app/dashboard', '', basePathWithoutWorkspaceBasePath)
).toEqual('http://localhost/app/dashboard');
});
});
42 changes: 42 additions & 0 deletions src/core/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { WORKSPACE_PATH_PREFIX } from './constants';
import { IBasePath } from '../public';

export const getWorkspaceIdFromUrl = (url: string): string => {
const regexp = /\/w\/([^\/]*)/;
const urlObject = new URL(url);
const matchedResult = urlObject.pathname.match(regexp);
if (matchedResult) {
return matchedResult[1];
}

return '';
};

export const cleanWorkspaceId = (path: string) => {
return path.replace(/^\/w\/([^\/]*)/, '');
};

export const formatUrlWithWorkspaceId = (url: string, workspaceId: string, basePath: IBasePath) => {
const newUrl = new URL(url, window.location.href);
/**
* Patch workspace id into path
*/
newUrl.pathname = basePath.remove(newUrl.pathname);

if (workspaceId) {
newUrl.pathname = `${WORKSPACE_PATH_PREFIX}/${workspaceId}${newUrl.pathname}`;
} else {
newUrl.pathname = cleanWorkspaceId(newUrl.pathname);
}

newUrl.pathname = basePath.prepend(newUrl.pathname, {
withoutWorkspace: true,
});

return newUrl.toString();
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1075,10 +1075,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const hideImport = workspaceEnabled && !workspaceId;

return (
<EuiPageContent
horizontalPosition="center"
style={this.props.fullWidth ? {} : { maxWidth: '75%', marginTop: '40px' }}
>
<EuiPageContent horizontalPosition="center">
{this.renderFlyout()}
{this.renderRelationships()}
{this.renderDeleteConfirmModal()}
Expand Down
Loading
Loading