Initial import NDC_1C
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import fs from "fs";
|
||||
|
||||
export function ensureDir(path: string): void {
|
||||
if (!fs.existsSync(path)) {
|
||||
fs.mkdirSync(path, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function readJsonFile<T>(path: string, fallback: T): T {
|
||||
try {
|
||||
const raw = fs.readFileSync(path, "utf-8");
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeJsonFile(path: string, value: unknown): void {
|
||||
fs.writeFileSync(path, JSON.stringify(value, null, 2), "utf-8");
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
|
||||
export class ApiError extends Error {
|
||||
public readonly code: string;
|
||||
public readonly status: number;
|
||||
public readonly details?: unknown;
|
||||
|
||||
constructor(code: string, message: string, status = 400, details?: unknown) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
export function ok<T>(res: Response, payload: T): Response<T> {
|
||||
return res.status(200).json(payload);
|
||||
}
|
||||
|
||||
export function created<T>(res: Response, payload: T): Response<T> {
|
||||
return res.status(201).json(payload);
|
||||
}
|
||||
|
||||
export function errorMiddleware(err: unknown, _req: Request, res: Response, _next: NextFunction): void {
|
||||
if (err instanceof ApiError) {
|
||||
res.status(err.status).json({
|
||||
ok: false,
|
||||
error: {
|
||||
code: err.code,
|
||||
message: err.message,
|
||||
details: err.details ?? null
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const fallback = err instanceof Error ? err.message : "Unknown error";
|
||||
res.status(500).json({
|
||||
ok: false,
|
||||
error: {
|
||||
code: "INTERNAL_ERROR",
|
||||
message: fallback,
|
||||
details: null
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
export interface JsonLogEntry {
|
||||
timestamp: string;
|
||||
level: "info" | "warn" | "error";
|
||||
service: string;
|
||||
message: string;
|
||||
sessionId?: string;
|
||||
runId?: string;
|
||||
taskId?: string;
|
||||
eventType?: string;
|
||||
details?: unknown;
|
||||
}
|
||||
|
||||
const REDACT_KEYS = new Set(["apiKey", "authorization", "Authorization", "openai_api_key", "OPENAI_API_KEY"]);
|
||||
|
||||
function redactObject(value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(redactObject);
|
||||
}
|
||||
if (value !== null && typeof value === "object") {
|
||||
const source = value as Record<string, unknown>;
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [key, field] of Object.entries(source)) {
|
||||
if (REDACT_KEYS.has(key)) {
|
||||
out[key] = "***REDACTED***";
|
||||
} else {
|
||||
out[key] = redactObject(field);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function logJson(entry: JsonLogEntry): void {
|
||||
const safe = {
|
||||
...entry,
|
||||
details: redactObject(entry.details)
|
||||
};
|
||||
// Structured JSON logs for diagnostics/trace aggregation.
|
||||
process.stdout.write(JSON.stringify(safe) + "\n");
|
||||
}
|
||||
Reference in New Issue
Block a user