
//#region src/deploy/dokploy-api.ts
var DokployApiError = class extends Error {
	constructor(message, status, statusText, issues) {
		super(message);
		this.status = status;
		this.statusText = statusText;
		this.issues = issues;
		this.name = "DokployApiError";
	}
};
/**
* Dokploy API client
*/
var DokployApi = class {
	baseUrl;
	token;
	constructor(options) {
		this.baseUrl = options.baseUrl.replace(/\/$/, "");
		this.token = options.token;
	}
	/**
	* Make a GET request to the Dokploy API
	*/
	async get(endpoint) {
		return this.request("GET", endpoint);
	}
	/**
	* Make a POST request to the Dokploy API
	*/
	async post(endpoint, body) {
		return this.request("POST", endpoint, body);
	}
	/**
	* Make a request to the Dokploy API
	*/
	async request(method, endpoint, body) {
		const url = `${this.baseUrl}/api/${endpoint}`;
		const response = await fetch(url, {
			method,
			headers: {
				"Content-Type": "application/json",
				"x-api-key": this.token
			},
			body: body ? JSON.stringify(body) : void 0
		});
		if (!response.ok) {
			let errorMessage = `Dokploy API error: ${response.status} ${response.statusText}`;
			let issues;
			try {
				const errorBody = await response.json();
				if (errorBody.message) errorMessage = `Dokploy API error: ${errorBody.message}`;
				if (errorBody.issues?.length) {
					issues = errorBody.issues;
					errorMessage += `\n  Issues: ${errorBody.issues.map((i) => i.message).join(", ")}`;
				}
			} catch {}
			throw new DokployApiError(errorMessage, response.status, response.statusText, issues);
		}
		const text = await response.text();
		if (!text || text.trim() === "") return void 0;
		return JSON.parse(text);
	}
	/**
	* Validate the API token by making a test request
	*/
	async validateToken() {
		try {
			await this.get("project.all");
			return true;
		} catch {
			return false;
		}
	}
	/**
	* List all projects
	*/
	async listProjects() {
		return this.get("project.all");
	}
	/**
	* Get a single project by ID
	*/
	async getProject(projectId) {
		return this.get(`project.one?projectId=${projectId}`);
	}
	/**
	* Create a new project
	*/
	async createProject(name, description) {
		return this.post("project.create", {
			name,
			description: description ?? `Created by gkm CLI`
		});
	}
	/**
	* Delete a project and all its resources
	*/
	async deleteProject(projectId) {
		await this.post("project.remove", { projectId });
	}
	/**
	* Create an environment in a project
	*/
	async createEnvironment(projectId, name, description) {
		return this.post("environment.create", {
			projectId,
			name,
			description: description ?? `${name} environment`
		});
	}
	/**
	* List all applications in a project
	*/
	async listApplications(projectId) {
		try {
			return await this.get(`application.all?projectId=${projectId}`);
		} catch {
			return [];
		}
	}
	/**
	* Find an application by name in a project
	*/
	async findApplicationByName(projectId, name) {
		const applications = await this.listApplications(projectId);
		const normalizedName = name.toLowerCase().replace(/[^a-z0-9-]/g, "-");
		return applications.find((app) => app.name === name || app.appName === normalizedName);
	}
	/**
	* Create a new application
	*/
	async createApplication(name, projectId, environmentId) {
		return this.post("application.create", {
			name,
			projectId,
			environmentId,
			appName: name.toLowerCase().replace(/[^a-z0-9-]/g, "-")
		});
	}
	/**
	* Find or create an application by name
	*/
	async findOrCreateApplication(name, projectId, environmentId) {
		const existing = await this.findApplicationByName(projectId, name);
		if (existing) return {
			application: existing,
			created: false
		};
		const application = await this.createApplication(name, projectId, environmentId);
		return {
			application,
			created: true
		};
	}
	/**
	* Get an application by ID
	*/
	async getApplication(applicationId) {
		try {
			return await this.get(`application.one?applicationId=${applicationId}`);
		} catch {
			return null;
		}
	}
	/**
	* Update an application
	*/
	async updateApplication(applicationId, updates) {
		await this.post("application.update", {
			applicationId,
			...updates
		});
	}
	/**
	* Save environment variables for an application
	*/
	async saveApplicationEnv(applicationId, env) {
		await this.post("application.saveEnvironment", {
			applicationId,
			env
		});
	}
	/**
	* Configure application to use Docker provider (pull from registry)
	*
	* For private registries, either:
	* - Use `registryId` if the registry is configured in Dokploy
	* - Or provide `username`, `password`, and `registryUrl` directly
	*/
	async saveDockerProvider(applicationId, dockerImage, options) {
		await this.post("application.saveDockerProvider", {
			applicationId,
			dockerImage,
			...options
		});
	}
	/**
	* Deploy an application
	*/
	async deployApplication(applicationId) {
		await this.post("application.deploy", { applicationId });
	}
	/**
	* Delete an application
	*/
	async deleteApplication(applicationId) {
		await this.post("application.remove", { applicationId });
	}
	/**
	* List all registries
	*/
	async listRegistries() {
		return this.get("registry.all");
	}
	/**
	* Create a new registry
	*/
	async createRegistry(registryName, registryUrl, username, password, options) {
		return this.post("registry.create", {
			registryName,
			registryUrl,
			username,
			password,
			imagePrefix: options?.imagePrefix
		});
	}
	/**
	* Get a registry by ID
	*/
	async getRegistry(registryId) {
		return this.get(`registry.one?registryId=${registryId}`);
	}
	/**
	* Update a registry
	*/
	async updateRegistry(registryId, updates) {
		await this.post("registry.update", {
			registryId,
			...updates
		});
	}
	/**
	* Delete a registry
	*/
	async deleteRegistry(registryId) {
		await this.post("registry.remove", { registryId });
	}
	/**
	* List all Postgres databases in a project
	*/
	async listPostgres(projectId) {
		try {
			return await this.get(`postgres.all?projectId=${projectId}`);
		} catch {
			return [];
		}
	}
	/**
	* Find a Postgres database by name in a project
	*/
	async findPostgresByName(projectId, name) {
		const databases = await this.listPostgres(projectId);
		const normalizedName = name.toLowerCase().replace(/[^a-z0-9-]/g, "-");
		return databases.find((db) => db.name === name || db.appName === normalizedName);
	}
	/**
	* Create a new Postgres database
	*/
	async createPostgres(name, projectId, environmentId, options) {
		return this.post("postgres.create", {
			name,
			projectId,
			environmentId,
			appName: options?.appName ?? name.toLowerCase().replace(/[^a-z0-9-]/g, "-"),
			databaseName: options?.databaseName,
			databaseUser: options?.databaseUser ?? "postgres",
			databasePassword: options?.databasePassword,
			dockerImage: options?.dockerImage ?? "postgres:18",
			description: options?.description ?? `Postgres database for ${name}`
		});
	}
	/**
	* Find or create a Postgres database by name
	*/
	async findOrCreatePostgres(name, projectId, environmentId, options) {
		const existing = await this.findPostgresByName(projectId, name);
		if (existing) return {
			postgres: existing,
			created: false
		};
		const postgres = await this.createPostgres(name, projectId, environmentId, options);
		return {
			postgres,
			created: true
		};
	}
	/**
	* Get a Postgres database by ID
	*/
	async getPostgres(postgresId) {
		return this.get(`postgres.one?postgresId=${postgresId}`);
	}
	/**
	* Deploy a Postgres database
	*/
	async deployPostgres(postgresId) {
		await this.post("postgres.deploy", { postgresId });
	}
	/**
	* Save environment variables for Postgres
	*/
	async savePostgresEnv(postgresId, env) {
		await this.post("postgres.saveEnvironment", {
			postgresId,
			env
		});
	}
	/**
	* Set external port for Postgres (for external access)
	*/
	async savePostgresExternalPort(postgresId, externalPort) {
		await this.post("postgres.saveExternalPort", {
			postgresId,
			externalPort
		});
	}
	/**
	* Update Postgres configuration
	*/
	async updatePostgres(postgresId, updates) {
		await this.post("postgres.update", {
			postgresId,
			...updates
		});
	}
	/**
	* Delete a Postgres database
	*/
	async deletePostgres(postgresId) {
		await this.post("postgres.remove", { postgresId });
	}
	/**
	* List all Redis instances in a project
	*/
	async listRedis(projectId) {
		try {
			return await this.get(`redis.all?projectId=${projectId}`);
		} catch {
			return [];
		}
	}
	/**
	* Find a Redis instance by name in a project
	*/
	async findRedisByName(projectId, name) {
		const instances = await this.listRedis(projectId);
		const normalizedName = name.toLowerCase().replace(/[^a-z0-9-]/g, "-");
		return instances.find((redis) => redis.name === name || redis.appName === normalizedName);
	}
	/**
	* Create a new Redis instance
	*/
	async createRedis(name, projectId, environmentId, options) {
		return this.post("redis.create", {
			name,
			projectId,
			environmentId,
			appName: options?.appName ?? name.toLowerCase().replace(/[^a-z0-9-]/g, "-"),
			databasePassword: options?.databasePassword,
			dockerImage: options?.dockerImage ?? "redis:8",
			description: options?.description ?? `Redis instance for ${name}`
		});
	}
	/**
	* Find or create a Redis instance by name
	*/
	async findOrCreateRedis(name, projectId, environmentId, options) {
		const existing = await this.findRedisByName(projectId, name);
		if (existing) return {
			redis: existing,
			created: false
		};
		const redis = await this.createRedis(name, projectId, environmentId, options);
		return {
			redis,
			created: true
		};
	}
	/**
	* Get a Redis instance by ID
	*/
	async getRedis(redisId) {
		return this.get(`redis.one?redisId=${redisId}`);
	}
	/**
	* Deploy a Redis instance
	*/
	async deployRedis(redisId) {
		await this.post("redis.deploy", { redisId });
	}
	/**
	* Save environment variables for Redis
	*/
	async saveRedisEnv(redisId, env) {
		await this.post("redis.saveEnvironment", {
			redisId,
			env
		});
	}
	/**
	* Set external port for Redis (for external access)
	*/
	async saveRedisExternalPort(redisId, externalPort) {
		await this.post("redis.saveExternalPort", {
			redisId,
			externalPort
		});
	}
	/**
	* Update Redis configuration
	*/
	async updateRedis(redisId, updates) {
		await this.post("redis.update", {
			redisId,
			...updates
		});
	}
	/**
	* Delete a Redis instance
	*/
	async deleteRedis(redisId) {
		await this.post("redis.remove", { redisId });
	}
	/**
	* Create a new domain for an application
	*/
	async createDomain(options) {
		return this.post("domain.create", options);
	}
	/**
	* Update an existing domain
	*/
	async updateDomain(domainId, updates) {
		await this.post("domain.update", {
			domainId,
			...updates
		});
	}
	/**
	* Delete a domain
	*/
	async deleteDomain(domainId) {
		await this.post("domain.delete", { domainId });
	}
	/**
	* Get a domain by ID
	*/
	async getDomain(domainId) {
		return this.get(`domain.one?domainId=${domainId}`);
	}
	/**
	* Get all domains for an application
	*/
	async getDomainsByApplicationId(applicationId) {
		return this.get(`domain.byApplicationId?applicationId=${applicationId}`);
	}
	/**
	* Validate a domain and trigger SSL certificate generation
	*
	* This should be called after DNS records are created and propagated.
	* It triggers Let's Encrypt certificate generation for HTTPS domains.
	*
	* @param domain - The domain hostname to validate (e.g., 'api.example.com')
	*/
	async validateDomain(domain) {
		return this.post("domain.validateDomain", { domain });
	}
	/**
	* Auto-generate a domain name for an application
	*/
	async generateDomain(appName, serverId) {
		return this.post("domain.generateDomain", {
			appName,
			serverId
		});
	}
	/**
	* List all backup destinations
	*/
	async listDestinations() {
		return this.get("destination.all");
	}
	/**
	* Get a destination by ID
	*/
	async getDestination(destinationId) {
		return this.get(`destination.one?destinationId=${destinationId}`);
	}
	/**
	* Create a new S3 backup destination
	*/
	async createDestination(options) {
		return this.post("destination.create", { ...options });
	}
	/**
	* Find a destination by name
	*/
	async findDestinationByName(name) {
		const destinations = await this.listDestinations();
		return destinations.find((d) => d.name === name);
	}
	/**
	* Find or create a destination by name
	*/
	async findOrCreateDestination(name, options) {
		const existing = await this.findDestinationByName(name);
		if (existing) return {
			destination: existing,
			created: false
		};
		const destination = await this.createDestination({
			name,
			...options
		});
		return {
			destination,
			created: true
		};
	}
	/**
	* Update a destination
	*/
	async updateDestination(destinationId, updates) {
		await this.post("destination.update", {
			destinationId,
			...updates
		});
	}
	/**
	* Delete a destination
	*/
	async deleteDestination(destinationId) {
		await this.post("destination.remove", { destinationId });
	}
	/**
	* Test connection to a destination
	*/
	async testDestinationConnection(destinationId) {
		return this.post("destination.testConnection", { destinationId });
	}
	/**
	* Create a backup schedule for postgres
	*/
	async createPostgresBackup(options) {
		return this.post("backup.create", {
			...options,
			databaseType: "postgres"
		});
	}
	/**
	* List backups for a postgres database
	*/
	async listPostgresBackups(postgresId) {
		return this.get(`backup.all?postgresId=${postgresId}&databaseType=postgres`);
	}
	/**
	* Get a backup by ID
	*/
	async getBackup(backupId) {
		return this.get(`backup.one?backupId=${backupId}`);
	}
	/**
	* Update a backup schedule
	*/
	async updateBackup(backupId, updates) {
		await this.post("backup.update", {
			backupId,
			...updates
		});
	}
	/**
	* Delete a backup schedule
	*/
	async deleteBackup(backupId) {
		await this.post("backup.remove", { backupId });
	}
	/**
	* Trigger a manual backup run
	*/
	async runBackupManually(backupId) {
		await this.post("backup.manualBackup", { backupId });
	}
};

//#endregion
Object.defineProperty(exports, 'DokployApi', {
  enumerable: true,
  get: function () {
    return DokployApi;
  }
});
Object.defineProperty(exports, 'DokployApiError', {
  enumerable: true,
  get: function () {
    return DokployApiError;
  }
});
//# sourceMappingURL=dokploy-api-Dvyq-LsM.cjs.map