Initial project structure and core modules

Add environment example, update .gitignore, and switch license to AGPL v3. Introduce error handling classes, ESLint config, and main modules for database, logging, certificate management, and utility functions. Add authentication routes, schemas, and service layer for a modular REST API. Update README and set up repository structure for further development.
This commit is contained in:
2025-12-23 15:59:43 +01:00
parent 4e822b4868
commit 0b1662d8ca
32 changed files with 4656 additions and 88 deletions

24
errors/DefaultError.js Normal file
View File

@@ -0,0 +1,24 @@
class DefaultError extends Error {
constructor(code, message, error) {
super(message)
this.code = code
this.error = error
this.message = message || "Internal Server Error"
this.isOperational = true
Error.captureStackTrace(this, this.constructor)
}
serialize() {
const response = {
code: this.code,
message: this.message,
}
if (this.error) {
response.error = this.error
}
return response
}
}
module.exports = DefaultError

53
errors/ValidationError.js Normal file
View File

@@ -0,0 +1,53 @@
const path = require("node:path")
const DefaultError = require("./DefaultError")
const Logger = require("../modules/logger")
const logger = Logger.createLogger(path.join(__dirname, ".."))
class ValidationError extends DefaultError {
constructor(zodResult, config = {}, context = {}) {
const formattedErrors = zodResult.error.issues.map(e => ({
field: e.path.join("."),
message: e.message
}))
const message = config.message || "Validation failed"
const statusCode = config.code || 400
super(statusCode, message, { errors: formattedErrors })
this.config = config
this.formattedErrors = formattedErrors
this.context = context
this.logError()
}
logError() {
const { method, path, ip } = this.context
if (method && path) {
logger.warn(
`Validation failed for ${method} ${path} (${this.config.errorFormat || "Standard"}) ` +
`<IP:${ip || "Unknown"}>`,
["WEB", "yellow"]
)
}
}
serialize() {
if (this.config.errorFormat === "YggdrasilError") {
return {
error: this.config.errorName || "IllegalArgumentException",
errorMessage: this.message,
cause: JSON.stringify(this.formattedErrors)
}
}
return {
code: this.code,
message: this.message,
errors: this.formattedErrors
}
}
}
module.exports = ValidationError

32
errors/YggdrasilError.js Normal file
View File

@@ -0,0 +1,32 @@
class YggdrasilError extends Error {
/**
* @param {number} statusCode - Le code HTTP (ex: 403, 415)
* @param {string} error - Description courte (ex: "ForbiddenOperationException" ou "Unsupported Media Type")
* @param {string} errorMessage - Description longue pour l'utilisateur
* @param {string} [cause] - Cause optionnelle de l'erreur
*/
constructor(statusCode, error, errorMessage, cause) {
super(errorMessage)
this.statusCode = statusCode
this.error = error
this.errorMessage = errorMessage
this.cause = cause
this.isOperational = true
Error.captureStackTrace(this, this.constructor)
}
serialize() {
const response = {
error: this.error,
errorMessage: this.errorMessage
}
if (this.cause) {
response.cause = this.cause
}
return response
}
}
module.exports = YggdrasilError

9
errors/errors.js Normal file
View File

@@ -0,0 +1,9 @@
const DefaultError = require("./DefaultError")
const YggdrasilError = require("./YggdrasilError")
const ValidationError = require("./ValidationError")
module.exports = {
DefaultError,
YggdrasilError,
ValidationError
}