Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export async function apiGet(
params?: Record<string, unknown>,
) {
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;
Expand Down
51 changes: 51 additions & 0 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down Expand Up @@ -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");
Expand Down