Add configurable internal API URL

This commit is contained in:
Mike Cao committed 2026-05-09 01:06:52 -07:00
1 parent fa182d0947
commit 2fd319f910
6 files changed
+124 -4

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
type ApiUrlOptions = {
apiUrl?: string;
basePath?: string;
};
function trimTrailingSlash(value: string) {
return value.replace(/\/+$/, '');
}
function trimLeadingSlash(value: string) {
return value.replace(/^\/+/, '');
}
function joinPath(base: string, path: string) {
if (!base) {
return `/${trimLeadingSlash(path)}`;
}
return `${trimTrailingSlash(base)}/${trimLeadingSlash(path)}`;
}
function isAbsoluteUrl(url: string) {
return /^https?:\/\//i.test(url);
}
export function getApiUrl(url: string, options: ApiUrlOptions = {}) {
if (isAbsoluteUrl(url)) {
return url;
}
const { apiUrl = process.env.apiUrl || '', basePath = process.env.basePath || '' } = options;
const baseUrl = apiUrl
? isAbsoluteUrl(apiUrl)
? apiUrl
: joinPath(basePath, apiUrl)
: joinPath(basePath, '/api');
return joinPath(baseUrl, url);
}