Repository navigation
refactor(ui): centralize proxy base URL resolution into tested resolver #29793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ryan-crabbe-berri
merged 2 commits into
litellm_internal_staging
from
litellm_centralize_api_base_url_resolution
Jun 5, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { resolveApiBase } from "./resolveApiBase"; | ||
|
|
||
| describe("resolveApiBase", () => { | ||
| describe("same-origin (no explicit base)", () => { | ||
| it("returns an empty base so requests stay relative", () => { | ||
| expect(resolveApiBase({})).toBe(""); | ||
| expect(resolveApiBase({ explicitBase: null, serverRootPath: "/" })).toBe(""); | ||
| expect(resolveApiBase({ explicitBase: "", serverRootPath: undefined })).toBe(""); | ||
| }); | ||
|
|
||
| it("prefixes a relative base with the server root path", () => { | ||
| expect(resolveApiBase({ serverRootPath: "/litellm" })).toBe("/litellm"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("explicit base (split-origin / dev)", () => { | ||
| it("uses the explicit base verbatim when root path is mounted at /", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/" })).toBe( | ||
| "http://localhost:4000", | ||
| ); | ||
| }); | ||
|
|
||
| it("appends the server root path to the explicit base", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/litellm" })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("dedup — base already includes the root path", () => { | ||
| it("does not double the root path", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000/litellm", serverRootPath: "/litellm" })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("normalization", () => { | ||
| it("trims a trailing slash from the explicit base before appending", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000/", serverRootPath: "/litellm" })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
|
|
||
| it("trims a trailing slash from the explicit base when there is no root path", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000/", serverRootPath: "/" })).toBe( | ||
| "http://localhost:4000", | ||
| ); | ||
| }); | ||
|
|
||
| it("adds a leading slash to a root path that lacks one", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "litellm" })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
|
|
||
| it("trims a trailing slash from the root path", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/litellm/" })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores whitespace around inputs", () => { | ||
| expect(resolveApiBase({ explicitBase: " http://localhost:4000 ", serverRootPath: " /litellm " })).toBe( | ||
| "http://localhost:4000/litellm", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("multi-segment root paths", () => { | ||
| it("appends a nested root path", () => { | ||
| expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/team/litellm" })).toBe( | ||
| "http://localhost:4000/team/litellm", | ||
| ); | ||
| }); | ||
|
|
||
| it("dedups a nested root path", () => { | ||
| expect( | ||
| resolveApiBase({ explicitBase: "http://localhost:4000/team/litellm", serverRootPath: "/team/litellm" }), | ||
| ).toBe("http://localhost:4000/team/litellm"); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| export interface ApiBaseInputs { | ||
| /** | ||
| * Absolute API origin to target instead of same-origin. Comes from the | ||
| * NEXT_PUBLIC_BASE_URL build env (dev / split-origin) or the proxy_base_url | ||
| * the backend reports in its UI config. Empty/unset means same-origin. | ||
| */ | ||
| explicitBase?: string | null; | ||
| /** | ||
| * The proxy's mount path when it sits under a sub-path (e.g. "/litellm" | ||
| * behind a reverse proxy). "/" or empty means mounted at the root. | ||
| */ | ||
| serverRootPath?: string | null; | ||
| } | ||
|
|
||
| const normalizeRootPath = (serverRootPath: string | null | undefined): string => { | ||
| const trimmed = (serverRootPath ?? "").trim(); | ||
| if (trimmed === "" || trimmed === "/") return ""; | ||
| const withLeadingSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`; | ||
| return withLeadingSlash.replace(/\/+$/, ""); | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve the single API base string that every request is prefixed with. | ||
| * | ||
| * Same-origin is represented as "" so requests stay relative and work behind | ||
| * any domain or reverse proxy without hardcoding an origin. An explicit base is | ||
| * used verbatim (trailing slash trimmed). The server root path is appended | ||
| * unless the base already ends with it. | ||
| */ | ||
| export const resolveApiBase = ({ explicitBase, serverRootPath }: ApiBaseInputs): string => { | ||
| const base = (explicitBase ?? "").trim().replace(/\/+$/, ""); | ||
| const rootPath = normalizeRootPath(serverRootPath); | ||
| if (rootPath === "" || base.endsWith(rootPath)) return base; | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| return `${base}${rootPath}`; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.