Skip to content

Commit 4e998b5

Browse files
Seann0824sean0824
andauthored
feat: enhance getBaseUrl function to validate and construct base URL (#49)
- Updated the getBaseUrl function to parse the provided URL and extract the protocol, hostname, and port. - Added error handling to throw a descriptive error message for invalid URL formats. Co-authored-by: sean0824 <[email protected]>
1 parent 7c7f2e1 commit 4e998b5

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/utils/api.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,30 @@ const getCommonHeader = () => ({
1919
process.env.MG_MCP_TOKEN || process.env.MASTERGO_API_TOKEN || parseToken(),
2020
});
2121

22-
const getBaseUrl = () => process.env.API_BASE_URL || parseUrl();
22+
const getBaseUrl = () => {
23+
const url = process.env.API_BASE_URL || parseUrl();
24+
try {
25+
// 解析URL
26+
const urlObj = new URL(url);
27+
28+
// 提取域名和协议
29+
const protocol = urlObj.protocol;
30+
const hostname = urlObj.hostname;
31+
const port = urlObj.port;
32+
33+
// 构建基础URL
34+
let baseUrl = `${protocol}//${hostname}`;
35+
if (port) {
36+
baseUrl += `:${port}`;
37+
}
38+
39+
return baseUrl;
40+
} catch {
41+
throw new Error(
42+
`无效的URL格式: ${url}。请提供正确的URL格式,例如: https://mastergo.com`
43+
);
44+
}
45+
};
2346

2447
const extractComponentDocumentLinks = (dsl: DslResponse): string[] => {
2548
const documentLinks = new Set<string>();

0 commit comments

Comments
 (0)