Files
SnapOtter/apps/api/src/routes/settings.ts
T
AshimandGitHub 97938bdc47 feat: API sync and documentation audit - 100% endpoint coverage (#94)
Code quality:
- Add Zod validation to 14 route handlers that used raw JSON.parse
  (favicon, find-duplicates, barcode-read, upscale, blur-faces,
  erase-object, colorize, enhance-faces, red-eye-removal,
  remove-background/effects, auth, api-keys, roles, teams,
  analytics, settings, user-files)
- Standardize error responses to safeParse + formatZodErrors pattern
- Replace unsafe `as` type casts with schema validation

OpenAPI spec (89 -> 115 operations):
- Add 14 missing tool endpoints (adjust-colors, sharpening,
  optimize-for-web, image-enhancement, noise-removal, red-eye-removal,
  restore-photo, passport-photo, colorize, enhance-faces, image-to-base64)
- Add 12 missing non-tool endpoints (analytics, features, audit-log,
  roles, admin-health)
- Add typed error schemas for 401/403/409 responses
- Add descriptions to all path parameters
- Bump version from 0.9.0 to 1.15.9

Documentation:
- Fix 8 incorrect env var defaults in configuration guide
- Add 15 undocumented env vars to configuration guide
- Fix tool ID mismatch (color-adjustments -> adjust-colors)
- Add 4 new API sections (Roles, Audit Log, Analytics, Features)
- Add image-enhancement to AI engine reference
- Update AI tool count from 13 to 14 across all docs
- Add 6 missing doc links to README
2026-04-23 20:26:58 +08:00

116 lines
3.6 KiB
TypeScript

/**
* Application settings routes (key-value store).
*
* GET /api/v1/settings — Get all settings as a key-value object
* PUT /api/v1/settings — Save settings (admin only)
* GET /api/v1/settings/:key — Get a specific setting
*/
import { eq } from "drizzle-orm";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";
import { db, schema } from "../db/index.js";
import { requirePermission } from "../permissions.js";
import { requireAuth } from "../plugins/auth.js";
const settingsBodySchema = z.record(z.string().min(1), z.unknown());
const HTML_TAG_PATTERN = /<[a-z/!][^>]*>/i;
export async function settingsRoutes(app: FastifyInstance): Promise<void> {
// GET /api/v1/settings — Get all settings as a key-value object
app.get("/api/v1/settings", async (request: FastifyRequest, reply: FastifyReply) => {
const user = requireAuth(request, reply);
if (!user) return;
const rows = db.select().from(schema.settings).all();
const settings: Record<string, string> = {};
for (const row of rows) {
settings[row.key] = row.value;
}
return reply.send({ settings });
});
// PUT /api/v1/settings — Save settings (admin only)
app.put("/api/v1/settings", async (request: FastifyRequest, reply: FastifyReply) => {
const admin = requirePermission("settings:write")(request, reply);
if (!admin) return;
const parsed = settingsBodySchema.safeParse(request.body);
if (!parsed.success) {
return reply.status(400).send({
error: "Request body must be a JSON object with key-value pairs",
code: "VALIDATION_ERROR",
});
}
const body = parsed.data;
// Pass 1: validate all entries before writing any
const entries: Array<{ key: string; strValue: string }> = [];
for (const [key, value] of Object.entries(body)) {
if (typeof key !== "string" || key.length === 0) continue;
const strValue = typeof value === "string" ? value : JSON.stringify(value);
if (HTML_TAG_PATTERN.test(key) || HTML_TAG_PATTERN.test(strValue)) {
return reply.status(400).send({
error: "Settings keys and values must not contain HTML tags",
code: "VALIDATION_ERROR",
});
}
entries.push({ key, strValue });
}
// Pass 2: write all entries now that all have passed validation
const now = new Date();
for (const { key, strValue } of entries) {
// Upsert: insert or update on conflict
const existing = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
if (existing) {
db.update(schema.settings)
.set({ value: strValue, updatedAt: now })
.where(eq(schema.settings.key, key))
.run();
} else {
db.insert(schema.settings).values({ key, value: strValue }).run();
}
}
return reply.send({ ok: true, updatedCount: entries.length });
});
// GET /api/v1/settings/:key — Get a specific setting
app.get(
"/api/v1/settings/:key",
async (request: FastifyRequest<{ Params: { key: string } }>, reply: FastifyReply) => {
const user = requireAuth(request, reply);
if (!user) return;
const { key } = request.params;
const row = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
if (!row) {
return reply.status(404).send({
error: `Setting "${key}" not found`,
code: "NOT_FOUND",
});
}
return reply.send({
key: row.key,
value: row.value,
updatedAt: row.updatedAt.toISOString(),
});
},
);
app.log.info("Settings routes registered");
}