feat: add resolveWithBasePath function (#6421)

This commit is contained in:
wpxp123456
2026-01-06 14:13:24 +08:00
committed by GitHub
parent beb196f4cc
commit df2de99646
3 changed files with 42 additions and 1 deletions
+23 -1
View File
@@ -15,7 +15,7 @@
*/
import { describe, expect, it } from 'vitest';
import { isLegalUrl, normalizeUrl } from '../url';
import { isLegalUrl, normalizeUrl, resolveWithBasePath } from '../url';
describe('Test url utils', () => {
it('should return true on legal url', () => {
@@ -36,4 +36,26 @@ describe('Test url utils', () => {
expect(normalizeUrl('https://univer.ai')).toEqual('https://univer.ai');
expect(normalizeUrl('zhang@univer.ai')).toEqual('mailto://zhang@univer.ai');
});
it('should resolve url with base path', () => {
expect(resolveWithBasePath('/universer-api/strean/file/dowload?file_id-2008162633680711718', 'http://univer.examples.com.cn:9080/univer/')).toEqual(
'http://univer.examples.com.cn:9080/univer/universer-api/strean/file/dowload?file_id-2008162633680711718'
);
expect(resolveWithBasePath('universer-api/strean/file/dowload?file_id-2008162633680711718', 'http://univer.examples.com.cn:9080/univer/')).toEqual(
'http://univer.examples.com.cn:9080/univer/universer-api/strean/file/dowload?file_id-2008162633680711718'
);
expect(resolveWithBasePath('/universer-api/strean/file/dowload?file_id-2008162633680711718', 'http://univer.examples.com.cn:9080/univer')).toEqual(
'http://univer.examples.com.cn:9080/univer/universer-api/strean/file/dowload?file_id-2008162633680711718'
);
expect(resolveWithBasePath('https://univer.examples.com.cn/file/2007806045355209235/univer-New%20Sheet.xlsx', 'http://univer.examples.com.cn:9080/univer/')).toEqual(
'https://univer.examples.com.cn/file/2007806045355209235/univer-New%20Sheet.xlsx'
);
expect(resolveWithBasePath('/universer-api/strean/file/dowload?file_id-2008162633680711718', 'http://univer.examples.com.cn:9080/')).toEqual(
'http://univer.examples.com.cn:9080/universer-api/strean/file/dowload?file_id-2008162633680711718'
);
});
});
+18
View File
@@ -367,3 +367,21 @@ function isEmail(url: string) {
export function normalizeUrl(urlStr: string) {
return hasProtocol(urlStr) ? urlStr : isEmail(urlStr) ? `mailto://${urlStr}` : `https://${urlStr}`;
}
/**
* Resolve a URL with a base URL, ensuring the path from the base URL is preserved.
* @param {string} url - The URL to resolve.
* @param {string} baseURL - The base URL to use for resolution.
* @returns {string} - The resolved URL.
*/
export function resolveWithBasePath(url: string, baseURL: string): string {
try {
const base = new URL(baseURL);
const basePath = base.pathname.endsWith('/') ? base.pathname : `${base.pathname}/`;
const cleanedUrl = url.startsWith('/') ? url.substring(1) : url;
return new URL(cleanedUrl, base.origin + basePath).toString();
} catch (error) {
console.error('Error resolving URL with base URL:', error);
return url;
}
}
+1
View File
@@ -42,6 +42,7 @@ export { requestImmediateMacroTask } from './common/request-immediate-macro-task
export { type ISequenceExecuteResult, sequence, sequenceAsync } from './common/sequence';
export { mergeSets } from './common/set';
export { UnitModel, type UnitType, UniverInstanceType } from './common/unit';
export { resolveWithBasePath } from './common/url';
export * from './docs/data-model';
export { JSON1, JSONX } from './docs/data-model/json-x/json-x';
export type { JSONXActions, JSONXPath } from './docs/data-model/json-x/json-x';