chore: env doğrulama, Vitest, Prettier ve editör ayarları
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# BÜTÇEM ortam değişkenleri — kopyala: cp .env.example .env.local
|
||||||
|
# Gerçek değerler yalnızca .env.local (lokal) ve Dokploy ortam değişkenlerinde (canlı) tutulur.
|
||||||
|
|
||||||
|
# Lokal: docker compose ile tek düğümlü replica set (transaction için zorunlu)
|
||||||
|
MONGODB_URI="mongodb://localhost:27019/butcem?replicaSet=rs0&directConnection=true"
|
||||||
|
|
||||||
|
# Üret: openssl rand -base64 32
|
||||||
|
BETTER_AUTH_SECRET=""
|
||||||
|
# Lokal: http://localhost:3000 — Canlı: https://butcem.ta1sph.website
|
||||||
|
BETTER_AUTH_URL="http://localhost:3000"
|
||||||
|
|
||||||
|
# Faz 4 seed'i için ilk admin hesabı
|
||||||
|
SEED_ADMIN_EMAIL="admin@butcem.local"
|
||||||
|
SEED_ADMIN_PASSWORD=""
|
||||||
|
|
||||||
|
# Ek dosyalar (canlıda Docker volume: /app/uploads)
|
||||||
|
UPLOAD_DIR="./uploads"
|
||||||
|
MAX_UPLOAD_MB="10"
|
||||||
|
|
||||||
|
# Build sırasında Dockerfile tarafından doldurulur; lokalde boş bırakılabilir
|
||||||
|
APP_VERSION=""
|
||||||
|
APP_COMMIT=""
|
||||||
+8
-1
@@ -30,8 +30,9 @@ yarn-debug.log*
|
|||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
.pnpm-debug.log*
|
.pnpm-debug.log*
|
||||||
|
|
||||||
# env files (can opt-in for committing if needed)
|
# env files (only the example is committed)
|
||||||
.env*
|
.env*
|
||||||
|
!.env.example
|
||||||
|
|
||||||
# vercel
|
# vercel
|
||||||
.vercel
|
.vercel
|
||||||
@@ -39,3 +40,9 @@ yarn-error.log*
|
|||||||
# typescript
|
# typescript
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
|
|
||||||
|
# user uploads (never committed; backed up separately)
|
||||||
|
/uploads/
|
||||||
|
|
||||||
|
# local tooling
|
||||||
|
/scratch/
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
.next
|
||||||
|
node_modules
|
||||||
|
package-lock.json
|
||||||
|
coverage
|
||||||
|
uploads
|
||||||
|
next-env.d.ts
|
||||||
|
docs/PROMPT.md
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"semi": true,
|
||||||
|
"singleQuote": false,
|
||||||
|
"trailingComma": "all",
|
||||||
|
"printWidth": 100,
|
||||||
|
"plugins": ["prettier-plugin-tailwindcss"],
|
||||||
|
"tailwindStylesheet": "./src/app/globals.css",
|
||||||
|
"tailwindFunctions": ["cn", "cva"]
|
||||||
|
}
|
||||||
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"dbaeumer.vscode-eslint",
|
||||||
|
"esbenp.prettier-vscode",
|
||||||
|
"bradlc.vscode-tailwindcss",
|
||||||
|
"vitest.explorer",
|
||||||
|
"mongodb.mongodb-vscode",
|
||||||
|
"editorconfig.editorconfig"
|
||||||
|
]
|
||||||
|
}
|
||||||
Vendored
+14
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.fixAll.eslint": "explicit"
|
||||||
|
},
|
||||||
|
"typescript.tsdk": "node_modules/typescript/lib",
|
||||||
|
"typescript.enablePromptUseWorkspaceTsdk": true,
|
||||||
|
"files.eol": "\n",
|
||||||
|
"tailwindCSS.experimental.classRegex": [["cn\\(([^)]*)\\)", "[\"'`]([^\"'`]*)[\"'`]"]],
|
||||||
|
"files.associations": {
|
||||||
|
"*.css": "tailwindcss"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Seed betiği — Faz 4'te doldurulacak (admin, örnek hane, aile üyeleri ve alan verisi).
|
||||||
|
* Çalıştırma: npm run seed | Sıfırlayıp doldurma: npm run seed:reset
|
||||||
|
*/
|
||||||
|
if (process.env.NODE_ENV === "production" && process.argv.includes("--reset")) {
|
||||||
|
console.error("seed:reset production ortamında çalıştırılamaz.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Seed henüz uygulanmadı (Faz 4).");
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { parseServerEnv } from "./env";
|
||||||
|
|
||||||
|
const valid = {
|
||||||
|
MONGODB_URI: "mongodb://localhost:27019/butcem?replicaSet=rs0&directConnection=true",
|
||||||
|
BETTER_AUTH_SECRET: "x".repeat(32),
|
||||||
|
BETTER_AUTH_URL: "http://localhost:3000",
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("parseServerEnv", () => {
|
||||||
|
it("geçerli değerleri kabul eder ve varsayılanları uygular", () => {
|
||||||
|
const env = parseServerEnv(valid);
|
||||||
|
expect(env.UPLOAD_DIR).toBe("./uploads");
|
||||||
|
expect(env.MAX_UPLOAD_MB).toBe(10);
|
||||||
|
expect(env.NODE_ENV).toBe("development");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("MAX_UPLOAD_MB string'ini sayıya çevirir", () => {
|
||||||
|
expect(parseServerEnv({ ...valid, MAX_UPLOAD_MB: "25" }).MAX_UPLOAD_MB).toBe(25);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("boş string'i tanımsız sayar", () => {
|
||||||
|
const env = parseServerEnv({ ...valid, SEED_ADMIN_EMAIL: "", APP_VERSION: "" });
|
||||||
|
expect(env.SEED_ADMIN_EMAIL).toBeUndefined();
|
||||||
|
expect(env.APP_VERSION).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("eksik ve hatalı değişkenleri tek hata mesajında listeler", () => {
|
||||||
|
expect(() =>
|
||||||
|
parseServerEnv({ MONGODB_URI: "postgres://x", BETTER_AUTH_SECRET: "kisa" }),
|
||||||
|
).toThrowError(/MONGODB_URI[\s\S]*BETTER_AUTH_SECRET[\s\S]*BETTER_AUTH_URL/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sunucu ortam değişkenleri şeması.
|
||||||
|
*
|
||||||
|
* Doğrulama tembel (lazy) yapılır: `next build` sırasında (ör. Docker build aşamasında)
|
||||||
|
* sırlar henüz tanımlı olmadığı için modül yüklenirken değil, ilk `env()` çağrısında çalışır.
|
||||||
|
* Sunucu ilk isteği işlerken eksik/hatalı bir değişken varsa açık bir hata ile durur.
|
||||||
|
*/
|
||||||
|
export const serverEnvSchema = z.object({
|
||||||
|
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
||||||
|
MONGODB_URI: z
|
||||||
|
.string()
|
||||||
|
.min(1, "MONGODB_URI zorunlu")
|
||||||
|
.refine((v) => v.startsWith("mongodb://") || v.startsWith("mongodb+srv://"), {
|
||||||
|
message: "MONGODB_URI mongodb:// veya mongodb+srv:// ile başlamalı",
|
||||||
|
}),
|
||||||
|
BETTER_AUTH_SECRET: z.string().min(32, "BETTER_AUTH_SECRET en az 32 karakter olmalı"),
|
||||||
|
BETTER_AUTH_URL: z.url("BETTER_AUTH_URL geçerli bir URL olmalı"),
|
||||||
|
SEED_ADMIN_EMAIL: z.email().optional(),
|
||||||
|
SEED_ADMIN_PASSWORD: z.string().min(8).optional(),
|
||||||
|
UPLOAD_DIR: z.string().min(1).default("./uploads"),
|
||||||
|
MAX_UPLOAD_MB: z.coerce.number().int().positive().max(100).default(10),
|
||||||
|
APP_VERSION: z.string().optional(),
|
||||||
|
APP_COMMIT: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ServerEnv = z.infer<typeof serverEnvSchema>;
|
||||||
|
|
||||||
|
/** Boş string'leri tanımsız say (`.env` dosyalarında `KEY=""` yaygın). */
|
||||||
|
function normalize(source: Record<string, string | undefined>) {
|
||||||
|
return Object.fromEntries(Object.entries(source).map(([k, v]) => [k, v === "" ? undefined : v]));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseServerEnv(source: Record<string, string | undefined>): ServerEnv {
|
||||||
|
const result = serverEnvSchema.safeParse(normalize(source));
|
||||||
|
if (!result.success) {
|
||||||
|
const issues = result.error.issues
|
||||||
|
.map((i) => ` - ${i.path.join(".") || "(kök)"}: ${i.message}`)
|
||||||
|
.join("\n");
|
||||||
|
throw new Error(`Geçersiz ortam değişkenleri:\n${issues}`);
|
||||||
|
}
|
||||||
|
return result.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cached: ServerEnv | undefined;
|
||||||
|
|
||||||
|
/** Doğrulanmış sunucu ortam değişkenleri. Yalnızca sunucu kodunda kullan. */
|
||||||
|
export function env(): ServerEnv {
|
||||||
|
cached ??= parseServerEnv(process.env);
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
resolve: {
|
||||||
|
alias: { "@": fileURLToPath(new URL("./src", import.meta.url)) },
|
||||||
|
},
|
||||||
|
test: {
|
||||||
|
environment: "node",
|
||||||
|
include: ["src/**/*.test.ts", "scripts/**/*.test.ts"],
|
||||||
|
// "server-only" paketi test ortamında hata fırlatmasın.
|
||||||
|
server: { deps: { inline: ["server-only"] } },
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user