TypeScript
fix(usage): a CLIProxyAPI hub served under a path keeps its path (#19336)
Quy Ngoc Nguyendef17b215f5a
diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.tsindex 459363e5fe8..7d46eb6642b 100644--- a/apps/server/src/usage/cliproxyApi.test.ts+++ b/apps/server/src/usage/cliproxyApi.test.ts@@ -73,7 +73,7 @@ function fixture(request,Response.json({}, { status: options.cooldownStatus ?? 200 }),);- expect(path).toBe("/v0/management/api-call");+ expect(path).toMatch(/\/v0\/management\/api-call$/);expect(body?.header?.Authorization).toBe("Bearer $TOKEN$");const upstream = options.upstream?.(body!) ?? {status: 200,@@ -145,6 +145,18 @@ describe("CLIProxyAPI built-in management API", () => {}),);+ it.effect("keeps the path of a hub served under a prefix", () =>+ Effect.gen(function* () {+ const test = fixture();+ const api = yield* test.api;+ yield* api.readAccounts({ ...config, url: "https://tools.test/cliproxy" });+ expect(test.requests.length).toBeGreaterThan(0);+ expect(+ test.requests.every((request) => request.path.startsWith("/cliproxy/v0/management/")),+ ).toBe(true);+ }),+ );+it.effect("keeps usage when the credits endpoint fails", () =>Effect.gen(function* () {const test = fixture({diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.tsindex b6404fce42a..1436318280b 100644--- a/apps/server/src/usage/cliproxyApi.ts+++ b/apps/server/src/usage/cliproxyApi.ts@@ -130,7 +130,11 @@ export const makeCliproxyApi = Effect.gen(function* () {body?: unknown,) {const url = yield* Effect.try({- try: () => new URL(`/v0/management/${path}`, config.url).toString(),+ try: () =>+ new URL(+ `v0/management/${path}`,+ config.url.endsWith("/") ? config.url : `${config.url}/`,+ ).toString(),catch: () => new UsageLimitSourceError({ detail: "The hub URL is not valid." }),});const request = (