diff --git a/src/client.ts b/src/client.ts index dc31d14..4482da5 100644 --- a/src/client.ts +++ b/src/client.ts @@ -98,8 +98,9 @@ export async function apiGet( params?: Record, ) { const client = createClient(); + // tRPC SuperJSON input uses the { json: ... } envelope. const query = params - ? `?input=${encodeURIComponent(JSON.stringify(params))}` + ? `?input=${encodeURIComponent(JSON.stringify({ json: params }))}` : ""; const response = await client.get(`/trpc/${endpoint}${query}`); return response.data?.result?.data?.json ?? response.data; diff --git a/tests/client.test.ts b/tests/client.test.ts index 849213f..3fbcfba 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -1,4 +1,17 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { apiGet } from "../src/client.js"; + +const getSpy = vi.hoisted(() => vi.fn()); + +vi.mock("axios", () => { + return { + default: { + create: () => ({ + get: getSpy, + }), + }, + }; +}); describe("readAuthConfig", () => { const originalEnv = { ...process.env }; @@ -48,6 +61,44 @@ describe("readAuthConfig", () => { }); }); +describe("apiGet", () => { + const originalEnv = { ...process.env }; + + beforeEach(() => { + process.env.DOKPLOY_URL = "https://test.dokploy.com"; + process.env.DOKPLOY_API_KEY = "test-key-123"; + getSpy.mockReset().mockResolvedValue({ data: {} }); + }); + + afterEach(() => { + process.env = { ...originalEnv }; + }); + + it("should wrap GET params in the SuperJSON { json: ... } input envelope", async () => { + await apiGet("compose.one", { composeId: "abc123", limit: 1 }); + + expect(getSpy).toHaveBeenCalledWith( + `/trpc/compose.one?input=${encodeURIComponent( + JSON.stringify({ json: { composeId: "abc123", limit: 1 } }), + )}`, + ); + }); + + it("should omit the input query string when params are undefined", async () => { + await apiGet("project.all"); + + expect(getSpy).toHaveBeenCalledWith("/trpc/project.all"); + }); + + it("should send an empty { json: {} } envelope when params are an empty object", async () => { + await apiGet("project.all", {}); + + expect(getSpy).toHaveBeenCalledWith( + `/trpc/project.all?input=${encodeURIComponent(JSON.stringify({ json: {} }))}`, + ); + }); +}); + describe("saveAuthConfig", () => { it("should write config with correct structure", async () => { const { saveAuthConfig } = await import("../src/client.js");