refactor: use schema constants

This commit is contained in:
Nicolas Meienberger
2025-10-23 19:25:12 +02:00
parent 8b1438ea62
commit 4ae738ce41
14 changed files with 661 additions and 35 deletions

View File

@@ -1,14 +1,19 @@
import { repositoryConfigSchema } from "@ironmount/schemas/restic";
import {
COMPRESSION_MODES,
REPOSITORY_BACKENDS,
REPOSITORY_STATUS,
repositoryConfigSchema,
} from "@ironmount/schemas/restic";
import { type } from "arktype";
import { describeRoute, resolver } from "hono-openapi";
const repositorySchema = type({
id: "string",
name: "string",
backend: type.enumerated("local", "sftp", "s3"),
type: type.valueOf(REPOSITORY_BACKENDS),
config: repositoryConfigSchema,
compressionMode: type.enumerated("off", "auto", "fastest", "better", "max").or("null"),
status: type.enumerated("healthy", "error", "unknown").or("null"),
compressionMode: type.valueOf(COMPRESSION_MODES).or("null"),
status: type.valueOf(REPOSITORY_STATUS).or("null"),
lastChecked: "number | null",
lastError: "string | null",
createdAt: "number",
@@ -46,8 +51,8 @@ export const listRepositoriesDto = describeRoute({
*/
export const createRepositoryBody = type({
name: "string",
compressionMode: type.valueOf(COMPRESSION_MODES).optional(),
config: repositoryConfigSchema,
"compressionMode?": type.enumerated("off", "auto", "fastest", "better", "max"),
});
export type CreateRepositoryBody = typeof createRepositoryBody.infer;

View File

@@ -1,5 +1,5 @@
import crypto from "node:crypto";
import type { CompressionMode, RepositoryConfig } from "@ironmount/schemas";
import type { CompressionMode, RepositoryConfig } from "@ironmount/schemas/restic";
import { eq } from "drizzle-orm";
import { ConflictError, InternalServerError, NotFoundError } from "http-errors-enhanced";
import slugify from "slugify";
@@ -15,14 +15,16 @@ const listRepositories = async () => {
};
const encryptConfig = async (config: RepositoryConfig): Promise<RepositoryConfig> => {
const encryptedConfig = { ...config };
const encryptedConfig: Record<string, string> = { ...config };
switch (config.backend) {
case "s3":
encryptedConfig.accessKeyId = await cryptoUtils.encrypt(config.accessKeyId);
encryptedConfig.secretAccessKey = await cryptoUtils.encrypt(config.secretAccessKey);
break;
}
return encryptedConfig;
return encryptedConfig as RepositoryConfig;
};
const createRepository = async (name: string, config: RepositoryConfig, compressionMode?: CompressionMode) => {
@@ -45,7 +47,7 @@ const createRepository = async (name: string, config: RepositoryConfig, compress
.values({
id,
name: slug,
backend: config.backend,
type: config.backend,
config: encryptedConfig,
compressionMode: compressionMode ?? "auto",
status: "unknown",