Skip to content

Commit 1eb3a98

Browse files
author
Quy Ngoc Nguyen
committed
fix(usage): a CLIProxyAPI hub served under a path keeps its path
The management URL was built with an absolute path, `new URL("/v0/management/...", hubUrl)`, which replaces whatever path the hub URL carries. A hub behind a reverse proxy at https://tools.example/cliproxy was therefore asked at https://tools.example/v0/management/... and every read failed. The URL is now resolved relative to the hub URL with a trailing slash, so a root URL behaves as before and a prefixed one keeps its prefix.
1 parent d10621d commit 1eb3a98

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎apps/server/src/usage/cliproxyApi.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function fixture(
7373
request,
7474
Response.json({}, { status: options.cooldownStatus ?? 200 }),
7575
);
76-
expect(path).toBe("/v0/management/api-call");
76+
expect(path).toMatch(/\/v0\/management\/api-call$/);
7777
expect(body?.header?.Authorization).toBe("Bearer $TOKEN$");
7878
const upstream = options.upstream?.(body!) ?? {
7979
status: 200,
@@ -145,6 +145,18 @@ describe("CLIProxyAPI built-in management API", () => {
145145
}),
146146
);
147147

148+
it.effect("keeps the path of a hub served under a prefix", () =>
149+
Effect.gen(function* () {
150+
const test = fixture();
151+
const api = yield* test.api;
152+
yield* api.readAccounts({ ...config, url: "https://tools.test/cliproxy" });
153+
expect(test.requests.length).toBeGreaterThan(0);
154+
expect(
155+
test.requests.every((request) => request.path.startsWith("/cliproxy/v0/management/")),
156+
).toBe(true);
157+
}),
158+
);
159+
148160
it.effect("keeps usage when the credits endpoint fails", () =>
149161
Effect.gen(function* () {
150162
const test = fixture({

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export const makeCliproxyApi = Effect.gen(function* () {
131131
body?: unknown,
132132
) {
133133
const url = yield* Effect.try({
134-
try: () => new URL(`/v0/management/${path}`, config.url).toString(),
134+
try: () =>
135+
new URL(
136+
`v0/management/${path}`,
137+
config.url.endsWith("/") ? config.url : `${config.url}/`,
138+
).toString(),
135139
catch: () => new UsageLimitSourceError({ detail: "The hub URL is not valid." }),
136140
});
137141
const request = (

0 commit comments

Comments
 (0)