fix(client): get api url (#4161)

* fix(client): get api url

* fix: trim /
This commit is contained in:
chenos 2024-04-25 16:30:46 +08:00 committed by GitHub
parent 333fe8d8f4
commit b25db239a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 7 deletions

View File

@ -168,20 +168,24 @@ export class Application {
}
getPublicPath() {
return this.options.publicPath || '/';
let publicPath = this.options.publicPath || '/';
if (!publicPath.endsWith('/')) {
publicPath += '/';
}
return publicPath;
}
getApiUrl(pathname = '') {
let baseURL = this.apiClient.axios['defaults']['baseURL'];
if (!baseURL.startsWith('http://') || !baseURL.startsWith('https://')) {
if (!baseURL.startsWith('http://') && !baseURL.startsWith('https://')) {
const { protocol, host } = window.location;
baseURL = `${protocol}//${host}/`;
baseURL = `${protocol}//${host}${baseURL}`;
}
return baseURL + pathname;
return baseURL.replace(/\/$/g, '') + '/' + pathname.replace(/^\//g, '');
}
getRouteUrl(pathname: string) {
return this.getPublicPath().replace(/\/$/g, '') + pathname;
return this.getPublicPath() + pathname.replace(/^\//g, '');
}
getCollectionManager(dataSource?: string) {

View File

@ -33,6 +33,47 @@ describe('Application', () => {
expect(Object.keys(app.components).length).toBeGreaterThan(1);
});
describe('getApiUrl', () => {
it('api path', () => {
const app = new Application({
apiClient: {
baseURL: '/api/',
},
});
const { protocol, host } = window.location;
const baseURL = `${protocol}//${host}/api/`;
expect(app.getApiUrl()).toBe(baseURL);
});
it('api url', () => {
const app = new Application({
apiClient: {
baseURL: 'http://localhost:13000/foo/api/',
},
});
expect(app.getApiUrl()).toBe('http://localhost:13000/foo/api/');
});
it('api url', () => {
const app = new Application({
apiClient: {
baseURL: 'https://123.1.2.3:13000/foo/api/',
},
});
expect(app.getApiUrl()).toBe('https://123.1.2.3:13000/foo/api/');
});
it('api url', () => {
const app = new Application({
apiClient: {
baseURL: 'https://123.1.2.3:13000/foo/api',
},
});
expect(app.getApiUrl('/test/bar')).toBe('https://123.1.2.3:13000/foo/api/test/bar');
expect(app.getApiUrl('test/bar')).toBe('https://123.1.2.3:13000/foo/api/test/bar');
});
});
describe('publicPath', () => {
it('default', () => {
const app = new Application({});
@ -42,14 +83,16 @@ describe('Application', () => {
it('custom', () => {
const app = new Application({ publicPath: '/admin' });
expect(app.getPublicPath()).toBe('/admin');
expect(app.getPublicPath()).toBe('/admin/');
expect(app.getRouteUrl('/test')).toBe('/admin/test');
expect(app.getRouteUrl('test')).toBe('/admin/test');
});
it('custom end with /', () => {
const app = new Application({ publicPath: '/admin/' });
expect(app.getPublicPath()).toBe('/admin/');
expect(app.getRouteUrl('/test')).toBe('/admin/test');
expect(app.getRouteUrl('/test/foo')).toBe('/admin/test/foo');
expect(app.getRouteUrl('test/foo/')).toBe('/admin/test/foo/');
});
});