Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(test): extract resolveApiBaseUrl and add libs unit tests

Tin2476481f97f6
diff --git a/packages/libs/src/api-url.test.ts b/packages/libs/src/api-url.test.ts
new file mode 100644
index 000000000..b98d1e320
--- /dev/null
+++ b/packages/libs/src/api-url.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it } from "vitest";
+import { resolveApiBaseUrl } from "./api-url";
+
+describe("resolveApiBaseUrl", () => {
+ it("appends /api when the base has no api suffix", () => {
+ expect(resolveApiBaseUrl(undefined)).toBe("http://localhost:1337/api");
+ expect(resolveApiBaseUrl("http://localhost:1337")).toBe(
+ "http://localhost:1337/api",
+ );
+ });
+
+ it("returns the URL unchanged when it already ends with /api", () => {
+ expect(resolveApiBaseUrl("http://localhost:1337/api")).toBe(
+ "http://localhost:1337/api",
+ );
+ });
+});
diff --git a/packages/libs/src/api-url.ts b/packages/libs/src/api-url.ts
new file mode 100644
index 000000000..d872c2c97
--- /dev/null
+++ b/packages/libs/src/api-url.ts
@@ -0,0 +1,8 @@
+/**
+ * Resolves the Hono client base URL from `VITE_API_URL` (or default).
+ * If the value already ends with `/api`, it is returned as-is; otherwise `/api` is appended.
+ */
+export function resolveApiBaseUrl(viteApiUrl: string | undefined): string {
+ const baseUrl = viteApiUrl || "http://localhost:1337";
+ return baseUrl.endsWith("/api") ? baseUrl : `${baseUrl}/api`;
+}
diff --git a/packages/libs/src/hono.ts b/packages/libs/src/hono.ts
index 8bea8331a..5ee8d92ae 100644
--- a/packages/libs/src/hono.ts
+++ b/packages/libs/src/hono.ts
@@ -2,9 +2,9 @@
import type { AppType } from "@kaneo/api";
import { hc } from "hono/client";
+import { resolveApiBaseUrl } from "./api-url";
-const baseUrl = import.meta.env.VITE_API_URL || "http://localhost:1337";
-const apiUrl = baseUrl.endsWith("/api") ? baseUrl : `${baseUrl}/api`;
+const apiUrl = resolveApiBaseUrl(import.meta.env.VITE_API_URL);
export const client = hc<AppType>(apiUrl, {
fetch: (input: RequestInfo | URL, init?: RequestInit) => {
diff --git a/packages/libs/src/index.ts b/packages/libs/src/index.ts
index 48445ab70..496d10b5c 100644
--- a/packages/libs/src/index.ts
+++ b/packages/libs/src/index.ts
@@ -1 +1,2 @@
+export { resolveApiBaseUrl } from "./api-url";
export { client } from "./hono";
diff --git a/packages/libs/vitest.config.ts b/packages/libs/vitest.config.ts
new file mode 100644
index 000000000..cfbd4c3fc
--- /dev/null
+++ b/packages/libs/vitest.config.ts
@@ -0,0 +1,8 @@
+import { defineConfig } from "vitest/config";
+
+export default defineConfig({
+ test: {
+ environment: "node",
+ include: ["src/**/*.test.ts"],
+ },
+});