Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: add resolveWithBasePath function (#6421)

wpxp123456df2de99646b0
diff --git a/packages/core/src/common/__tests__/url.spec.ts b/packages/core/src/common/__tests__/url.spec.ts
index 61b2477bf6..0fee2a53f5 100644
--- a/packages/core/src/common/__tests__/url.spec.ts
+++ b/packages/core/src/common/__tests__/url.spec.ts
@@ -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('[email protected]')).toEqual('mailto://[email protected]');
});
+
+ 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'
+ );
+ });
});
diff --git a/packages/core/src/common/url.ts b/packages/core/src/common/url.ts
index 5ed084b2a8..838d8b887d 100644
--- a/packages/core/src/common/url.ts
+++ b/packages/core/src/common/url.ts
@@ -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;
+ }
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index cbe8247d1d..b4cb661e17 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -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';