Skip to content

Commit

Permalink
[Workspace] Add workspace id in basePath (#6060)
Browse files Browse the repository at this point in the history
* [Workspace]Add workspace id in basePath (#212)

* feat: enable workspace id in basePath

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add unit test

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: remove useless test object id

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add unit test

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add unit test

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: update snapshot

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: move formatUrlWithWorkspaceId to core/public/utils

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: remove useless variable

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: remove useless variable

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimization

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimization

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimization

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: move workspace/utils to core

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: move workspace/utils to core

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: update comment

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimize code

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: update unit test

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimization

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add space under license

Signed-off-by: SuZhou-Joe <[email protected]>

* fix: unit test

Signed-off-by: SuZhou-Joe <[email protected]>

---------

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add CHANGELOG

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add feature flag check

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: make the pr smaller

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimize with a more strict check

Signed-off-by: SuZhou-Joe <[email protected]>

* fix: unit test error

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: remove useless code

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: add a unit test case

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: better merge

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: rename the workspaceBasePath to clientBasePath

Signed-off-by: SuZhou-Joe <[email protected]>

* fix: snapshot

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: rename withoutWorkspace to withoutClientBasePath

Signed-off-by: SuZhou-Joe <[email protected]>

* Revert "feat: add feature flag check"

This reverts commit 64b3645.

Signed-off-by: SuZhou-Joe <[email protected]>

* Revert "fix: unit test error"

This reverts commit 80bed72.

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimize comment and test cases description

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: optimize comment

Signed-off-by: SuZhou-Joe <[email protected]>

---------

Signed-off-by: SuZhou-Joe <[email protected]>
(cherry picked from commit 3073926)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
github-actions[bot] committed Apr 12, 2024
1 parent 730e640 commit 889d14a
Show file tree
Hide file tree
Showing 25 changed files with 334 additions and 23 deletions.

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

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

47 changes: 47 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,51 @@ describe('BasePath', () => {
expect(new BasePath('/foo/bar', '/foo').serverBasePath).toEqual('/foo');
});
});

describe('clientBasePath', () => {
it('get with clientBasePath provided when construct', () => {
expect(new BasePath('/foo/bar', '/foo/bar', '/client_base_path').get()).toEqual(
'/foo/bar/client_base_path'
);
});

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

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

it('construct with clientBasePath provided but calls prepend with withoutClientBasePath is true', () => {
expect(
new BasePath('/foo/bar', '/foo/bar', '/client_base_path').prepend('/prepend', {
withoutClientBasePath: true,
})
).toEqual('/foo/bar/prepend');
});

it('remove with clientBasePath provided when construct', () => {
expect(
new BasePath('/foo/bar', '/foo/bar', '/client_base_path').remove(
'/foo/bar/client_base_path/remove'
)
).toEqual('/remove');
});

it('construct with clientBasePath provided but calls remove with withoutClientBasePath is true', () => {
expect(
new BasePath('/foo/bar', '/foo/bar', '/client_base_path').remove(
'/foo/bar/client_base_path/remove',
{
withoutClientBasePath: true,
}
)
).toEqual('/client_base_path/remove');
});
});
});
28 changes: 19 additions & 9 deletions src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,47 @@
*/

import { modifyUrl } from '@osd/std';
import type { PrependOptions } from './types';

export class BasePath {
constructor(
private readonly basePath: string = '',
public readonly serverBasePath: string = basePath
public readonly serverBasePath: string = basePath,
private readonly clientBasePath: string = ''
) {}

public get = () => {
return `${this.basePath}${this.clientBasePath}`;
};

public getBasePath = () => {
return this.basePath;
};

public prepend = (path: string): string => {
if (!this.basePath) return path;
public prepend = (path: string, prependOptions?: PrependOptions): string => {
const { withoutClientBasePath } = prependOptions || {};
const basePath = withoutClientBasePath ? this.basePath : this.get();
if (!basePath) return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.basePath}${parts.pathname}`;
parts.pathname = `${basePath}${parts.pathname}`;
}
});
};

public remove = (path: string): string => {
if (!this.basePath) {
public remove = (path: string, prependOptions?: PrependOptions): string => {
const { withoutClientBasePath } = prependOptions || {};
const basePath = withoutClientBasePath ? this.basePath : this.get();
if (!basePath) {
return path;
}

if (path === this.basePath) {
if (path === basePath) {
return '/';
}

if (path.startsWith(`${this.basePath}/`)) {
return path.slice(this.basePath.length);
if (path.startsWith(`${basePath}/`)) {
return path.slice(basePath.length);
}

return path;
Expand Down
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 = '', clientBasePath = '' } = {}): 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, clientBasePath),
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 = '', clientBasePath = '' } = {}) => {
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, clientBasePath }));
mocked.start.mockReturnValue(createServiceMock({ basePath, clientBasePath }));
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
10 changes: 9 additions & 1 deletion src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { AnonymousPathsService } from './anonymous_paths_service';
import { LoadingCountService } from './loading_count_service';
import { Fetch } from './fetch';
import { CoreService } from '../../types';
import { getWorkspaceIdFromUrl } from '../utils';
import { WORKSPACE_PATH_PREFIX } from '../../utils/constants';

interface HttpDeps {
injectedMetadata: InjectedMetadataSetup;
Expand All @@ -50,9 +52,15 @@ export class HttpService implements CoreService<HttpSetup, HttpStart> {

public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup {
const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion();
let clientBasePath = '';
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
clientBasePath = `${WORKSPACE_PATH_PREFIX}/${workspaceId}`;
}
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath()
injectedMetadata.getServerBasePath(),
clientBasePath
);
const fetchService = new Fetch({ basePath, opensearchDashboardsVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });
Expand Down
28 changes: 23 additions & 5 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,43 @@ export interface HttpSetup {
*/
export type HttpStart = HttpSetup;

/**
* prepend options
*
* withoutClientBasePath option will prepend a relative url with serverBasePath only.
* For now, clientBasePath is consist of:
* workspacePath, which has the pattern of /w/{workspaceId}.
*
* In the future, clientBasePath may have other parts but keep `withoutClientBasePath` for now to not over-design the interface,
*/
export interface PrependOptions {
withoutClientBasePath?: boolean;
}

/**
* APIs for manipulating the basePath on URL segments.
* @public
*/
export interface IBasePath {
/**
* Gets the `basePath` string.
* Gets the `basePath + clientBasePath` string.
*/
get: () => string;

/**
* Prepends `path` with the basePath.
* Gets the `basePath` string
*/
getBasePath: () => string;

/**
* Prepends `path` with the basePath + clientBasePath.
*/
prepend: (url: string) => string;
prepend: (url: string, prependOptions?: PrependOptions) => string;

/**
* Removes the prepended basePath from the `path`.
* Removes the prepended basePath + clientBasePath from the `path`.
*/
remove: (url: string) => string;
remove: (url: string, prependOptions?: PrependOptions) => string;

/**
* Returns the server's root basePath as configured, without any namespace prefix.
Expand Down
4 changes: 3 additions & 1 deletion src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,6 @@ export {

export { __osdBootstrap__ } from './osd_bootstrap';

export { WorkspacesStart, WorkspacesSetup } from './workspace';
export { WorkspacesStart, WorkspacesSetup, WorkspacesService } from './workspace';

export { WORKSPACE_TYPE } from '../utils';
6 changes: 6 additions & 0 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
export { shareWeakReplay } from './share_weak_replay';
export { Sha256 } from './crypto';
export { MountWrapper, mountReactNode } from './mount';
export {
WORKSPACE_PATH_PREFIX,
WORKSPACE_TYPE,
formatUrlWithWorkspaceId,
getWorkspaceIdFromUrl,
} from '../../utils';
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';
Loading

0 comments on commit 889d14a

Please sign in to comment.