Skip to content
TypeScript

fix(usage): a CLIProxy hub saved as its management.html panel URL shows accounts again (#25579)

Jolymmiles24fbb5d4c83d
diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts
index 4a76d32c15d..b9f357522bb 100644
--- a/apps/server/src/usage/cliproxyApi.test.ts
+++ b/apps/server/src/usage/cliproxyApi.test.ts
@@ -264,11 +264,21 @@ describe("CLIProxyAPI built-in management API", () => {
}),
);
- it.effect("keeps the path of a hub served under a prefix", () =>
+ it.effect.each([
+ "https://tools.test/cliproxy",
+ "https://tools.test/cliproxy/",
+ "https://tools.test/cliproxy/management.html",
+ "https://tools.test/cliproxy/management.html/",
+ "https://tools.test/cliproxy/MANAGEMENT.HTML",
+ "https://tools.test/cliproxy/management.html?tab=accounts",
+ "https://tools.test/cliproxy/management.html#accounts",
+ "https://tools.test/cliproxy/management.html/?tab=accounts",
+ "https://tools.test/cliproxy/management.html/#accounts",
+ ])("keeps the path of a hub served under a prefix: %s", (url) =>
Effect.gen(function* () {
const test = fixture();
const api = yield* test.api;
- yield* api.readAccounts({ ...config, url: "https://tools.test/cliproxy" });
+ yield* api.readAccounts({ ...config, url });
expect(test.requests.length).toBeGreaterThan(0);
expect(
test.requests.every((request) => request.path.startsWith("/cliproxy/v0/management/")),
@@ -276,6 +286,20 @@ describe("CLIProxyAPI built-in management API", () => {
}),
);
+ it.effect("preserves a hub path that only ends with the panel filename", () =>
+ Effect.gen(function* () {
+ const test = fixture();
+ const api = yield* test.api;
+ yield* api.readAccounts({ ...config, url: "https://tools.test/team-management.html" });
+ expect(test.requests.length).toBeGreaterThan(0);
+ expect(
+ test.requests.every((request) =>
+ request.path.startsWith("/team-management.html/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.ts
index 4fb59d041c3..87ec1e05b8f 100644
--- a/apps/server/src/usage/cliproxyApi.ts
+++ b/apps/server/src/usage/cliproxyApi.ts
@@ -184,11 +184,12 @@ export const makeCliproxyApi = Effect.gen(function* () {
body?: unknown,
) {
const url = yield* Effect.try({
- try: () =>
- new URL(
- `v0/management/${path}`,
- hub.url.endsWith("/") ? hub.url : `${hub.url}/`,
- ).toString(),
+ try: () => {
+ const base = new URL(hub.url);
+ // Users often paste the hub's web panel URL, which ends in management.html.
+ base.pathname = base.pathname.replace(/(\/management\.html)?\/?$/i, "/");
+ return new URL(`v0/management/${path}`, base).toString();
+ },
catch: () => new UsageLimitSourceError({ detail: "The hub URL is not valid." }),
});
const request = (