Skip to content

Commit

Permalink
feat: optimize code (opensearch-project#40)
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe authored and Hailong-am committed Jul 18, 2023
1 parent 716c7c3 commit bf23c4d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
16 changes: 4 additions & 12 deletions src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ export class BasePath {
return this.basePath;
};

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

public prependWithoutWorkspacePath = (path: string): string => {
if (!this.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}`;
}
});
};
Expand Down
8 changes: 7 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,7 @@ import { AnonymousPathsService } from './anonymous_paths_service';
import { LoadingCountService } from './loading_count_service';
import { Fetch } from './fetch';
import { CoreService } from '../../types';
import { getWorkspaceIdFromUrl } from '../utils';

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

public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup {
const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion();
let workspaceBasePath = '';
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
workspaceBasePath = `/w/${workspaceId}`;
}
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath(),
injectedMetadata.getWorkspaceBasePath()
workspaceBasePath
);
const fetchService = new Fetch({ basePath, opensearchDashboardsVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });
Expand Down
5 changes: 0 additions & 5 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ export interface IBasePath {
* See {@link BasePath.get} for getting the basePath value for a specific request
*/
readonly serverBasePath: string;

/**
* Prepends `path` with the basePath.
*/
prependWithoutWorkspacePath: (url: string) => string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,6 @@ export class InjectedMetadataService {
getSurvey: () => {
return this.state.survey;
},

getWorkspaceBasePath: () => {
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
return `/w/${workspaceId}`;
}

return '';
},
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
AppMountParameters,
AppNavLinkStatus,
} from '../../../core/public';
import { WORKSPACE_APP_ID, PATHS } from '../common/constants';
import { WORKSPACE_APP_ID } from '../common/constants';
import { mountDropdownList } from './mount';
import { getWorkspaceIdFromUrl } from '../../../core/public/utils';

export class WorkspacesPlugin implements Plugin<{}, {}> {
private core?: CoreSetup;
private getWorkpsaceIdFromURL(): string | null {
private getWorkspaceIdFromURL(): string | null {
return getWorkspaceIdFromUrl(window.location.href);
}
private getPatchedUrl = (url: string, workspaceId: string) => {
Expand All @@ -40,9 +40,9 @@ export class WorkspacesPlugin implements Plugin<{}, {}> {
this.core = core;
this.core?.workspaces.setFormatUrlWithWorkspaceId((url, id) => this.getPatchedUrl(url, id));
/**
* Retrive workspace id from url
* Retrieve workspace id from url
*/
const workspaceId = this.getWorkpsaceIdFromURL();
const workspaceId = this.getWorkspaceIdFromURL();

if (workspaceId) {
const result = await core.workspaces.client.enterWorkspace(workspaceId);
Expand Down

0 comments on commit bf23c4d

Please sign in to comment.