Initial import NDC_1C
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ensureDir = ensureDir;
|
||||
exports.readJsonFile = readJsonFile;
|
||||
exports.writeJsonFile = writeJsonFile;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
function ensureDir(path) {
|
||||
if (!fs_1.default.existsSync(path)) {
|
||||
fs_1.default.mkdirSync(path, { recursive: true });
|
||||
}
|
||||
}
|
||||
function readJsonFile(path, fallback) {
|
||||
try {
|
||||
const raw = fs_1.default.readFileSync(path, "utf-8");
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
function writeJsonFile(path, value) {
|
||||
fs_1.default.writeFileSync(path, JSON.stringify(value, null, 2), "utf-8");
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ApiError = void 0;
|
||||
exports.ok = ok;
|
||||
exports.created = created;
|
||||
exports.errorMiddleware = errorMiddleware;
|
||||
class ApiError extends Error {
|
||||
code;
|
||||
status;
|
||||
details;
|
||||
constructor(code, message, status = 400, details) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
exports.ApiError = ApiError;
|
||||
function ok(res, payload) {
|
||||
return res.status(200).json(payload);
|
||||
}
|
||||
function created(res, payload) {
|
||||
return res.status(201).json(payload);
|
||||
}
|
||||
function errorMiddleware(err, _req, res, _next) {
|
||||
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
|
||||
}
|
||||
});
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.logJson = logJson;
|
||||
const REDACT_KEYS = new Set(["apiKey", "authorization", "Authorization", "openai_api_key", "OPENAI_API_KEY"]);
|
||||
function redactObject(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(redactObject);
|
||||
}
|
||||
if (value !== null && typeof value === "object") {
|
||||
const source = value;
|
||||
const out = {};
|
||||
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;
|
||||
}
|
||||
function logJson(entry) {
|
||||
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