refactor: clean up imports and improve function formatting

- Reordered and grouped import statements for better readability in client.ts, index.ts, and auth.ts.
- Updated the readAuthConfig function to prioritize DOKPLOY_API_KEY over DOKPLOY_AUTH_TOKEN.
- Enhanced formatting of apiPost and apiGet functions for improved clarity and consistency.
This commit is contained in:
Mauricio Siu
2026-04-15 18:41:20 -06:00
parent 5b02a22e21
commit 70f542990d
3 changed files with 36 additions and 13 deletions

View File

@@ -1,8 +1,8 @@
import axios, { type AxiosInstance } from "axios";
import chalk from "chalk";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import axios, { type AxiosInstance } from "axios";
import chalk from "chalk";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -14,7 +14,8 @@ export interface AuthConfig {
}
export function readAuthConfig(): AuthConfig {
const envToken = process.env.DOKPLOY_AUTH_TOKEN;
const envToken =
process.env.DOKPLOY_API_KEY ?? process.env.DOKPLOY_AUTH_TOKEN;
const envUrl = process.env.DOKPLOY_URL;
if (envToken && envUrl) {
@@ -60,13 +61,22 @@ export function createClient(): AxiosInstance {
});
}
export async function apiPost(endpoint: string, data?: Record<string, unknown>) {
export async function apiPost(
endpoint: string,
data?: Record<string, unknown>,
) {
const client = createClient();
const response = await client.post(`/trpc/${endpoint}`, data ? { json: data } : undefined);
const response = await client.post(
`/trpc/${endpoint}`,
data ? { json: data } : undefined,
);
return response.data?.result?.data?.json ?? response.data;
}
export async function apiGet(endpoint: string, params?: Record<string, unknown>) {
export async function apiGet(
endpoint: string,
params?: Record<string, unknown>,
) {
const client = createClient();
const query = params
? `?input=${encodeURIComponent(JSON.stringify(params))}`

View File

@@ -1,14 +1,20 @@
import type { Command } from "commander";
import chalk from "chalk";
import axios from "axios";
import chalk from "chalk";
import type { Command } from "commander";
import { saveAuthConfig } from "../client.js";
export function registerAuthCommand(program: Command) {
program
.command("auth")
.description("Authenticate with your Dokploy server")
.requiredOption("-u, --url <url>", "Server URL (e.g., https://panel.dokploy.com)")
.requiredOption("-t, --token <token>", "API key from your Dokploy dashboard")
.requiredOption(
"-u, --url <url>",
"Server URL (e.g., https://panel.dokploy.com)",
)
.requiredOption(
"-t, --token <token>",
"API key from your Dokploy dashboard",
)
.action(async (opts: { url: string; token: string }) => {
const url = opts.url.replace(/\/+$/, "");

View File

@@ -1,16 +1,23 @@
#!/usr/bin/env node
import { program } from "commander";
import chalk from "chalk";
import { program } from "commander";
import { registerAuthCommand } from "./commands/auth.js";
import { registerGeneratedCommands } from "./generated/commands.js";
const pkg = { name: "dokploy", version: "0.3.0", description: "Dokploy CLI - Manage your Dokploy server" };
const pkg = {
name: "dokploy",
version: "0.3.0",
description: "Dokploy CLI - Manage your Dokploy server",
};
program
.name(pkg.name)
.version(pkg.version)
.description(pkg.description);
.description(pkg.description)
.action(() => {
program.help();
});
registerAuthCommand(program);
registerGeneratedCommands(program);