@cosmneo/onion-lasagna 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backend/core/global.cjs +283 -0
- package/dist/backend/core/global.cjs.map +1 -0
- package/dist/backend/core/global.d.cts +294 -0
- package/dist/backend/core/global.d.ts +294 -0
- package/dist/backend/core/global.js +39 -0
- package/dist/backend/core/global.js.map +1 -0
- package/dist/backend/core/onion-layers.cjs +2302 -0
- package/dist/backend/core/onion-layers.cjs.map +1 -0
- package/dist/backend/core/onion-layers.d.cts +1675 -0
- package/dist/backend/core/onion-layers.d.ts +1675 -0
- package/dist/backend/core/onion-layers.js +1158 -0
- package/dist/backend/core/onion-layers.js.map +1 -0
- package/dist/backend/core/presentation.cjs +573 -0
- package/dist/backend/core/presentation.cjs.map +1 -0
- package/dist/backend/core/presentation.d.cts +5 -0
- package/dist/backend/core/presentation.d.ts +5 -0
- package/dist/backend/core/presentation.js +28 -0
- package/dist/backend/core/presentation.js.map +1 -0
- package/dist/backend/core/validators/arktype.cjs +947 -0
- package/dist/backend/core/validators/arktype.cjs.map +1 -0
- package/dist/backend/core/validators/arktype.d.cts +188 -0
- package/dist/backend/core/validators/arktype.d.ts +188 -0
- package/dist/backend/core/validators/arktype.js +287 -0
- package/dist/backend/core/validators/arktype.js.map +1 -0
- package/dist/backend/core/validators/typebox.cjs +939 -0
- package/dist/backend/core/validators/typebox.cjs.map +1 -0
- package/dist/backend/core/validators/typebox.d.cts +189 -0
- package/dist/backend/core/validators/typebox.d.ts +189 -0
- package/dist/backend/core/validators/typebox.js +281 -0
- package/dist/backend/core/validators/typebox.js.map +1 -0
- package/dist/backend/core/validators/valibot.cjs +942 -0
- package/dist/backend/core/validators/valibot.cjs.map +1 -0
- package/dist/backend/core/validators/valibot.d.cts +160 -0
- package/dist/backend/core/validators/valibot.d.ts +160 -0
- package/dist/backend/core/validators/valibot.js +294 -0
- package/dist/backend/core/validators/valibot.js.map +1 -0
- package/dist/backend/core/validators/zod.cjs +934 -0
- package/dist/backend/core/validators/zod.cjs.map +1 -0
- package/dist/backend/core/validators/zod.d.cts +188 -0
- package/dist/backend/core/validators/zod.d.ts +188 -0
- package/dist/backend/core/validators/zod.js +278 -0
- package/dist/backend/core/validators/zod.js.map +1 -0
- package/dist/backend/frameworks/elysia.cjs +715 -0
- package/dist/backend/frameworks/elysia.cjs.map +1 -0
- package/dist/backend/frameworks/elysia.d.cts +208 -0
- package/dist/backend/frameworks/elysia.d.ts +208 -0
- package/dist/backend/frameworks/elysia.js +251 -0
- package/dist/backend/frameworks/elysia.js.map +1 -0
- package/dist/backend/frameworks/fastify.cjs +677 -0
- package/dist/backend/frameworks/fastify.cjs.map +1 -0
- package/dist/backend/frameworks/fastify.d.cts +201 -0
- package/dist/backend/frameworks/fastify.d.ts +201 -0
- package/dist/backend/frameworks/fastify.js +213 -0
- package/dist/backend/frameworks/fastify.js.map +1 -0
- package/dist/backend/frameworks/hono.cjs +715 -0
- package/dist/backend/frameworks/hono.cjs.map +1 -0
- package/dist/backend/frameworks/hono.d.cts +163 -0
- package/dist/backend/frameworks/hono.d.ts +163 -0
- package/dist/backend/frameworks/hono.js +249 -0
- package/dist/backend/frameworks/hono.js.map +1 -0
- package/dist/backend/frameworks/nestjs.cjs +260 -0
- package/dist/backend/frameworks/nestjs.cjs.map +1 -0
- package/dist/backend/frameworks/nestjs.d.cts +168 -0
- package/dist/backend/frameworks/nestjs.d.ts +168 -0
- package/dist/backend/frameworks/nestjs.js +193 -0
- package/dist/backend/frameworks/nestjs.js.map +1 -0
- package/dist/base-dto.class-D7W9iqoU.d.cts +146 -0
- package/dist/base-dto.class-D7W9iqoU.d.ts +146 -0
- package/dist/base-uuid-v7.vo-BPGEIWLM.d.ts +799 -0
- package/dist/base-uuid-v7.vo-BjqKX44G.d.cts +799 -0
- package/dist/chunk-74IKUOSE.js +116 -0
- package/dist/chunk-74IKUOSE.js.map +1 -0
- package/dist/chunk-BKZOLGQW.js +29 -0
- package/dist/chunk-BKZOLGQW.js.map +1 -0
- package/dist/chunk-CGZBV6BD.js +54 -0
- package/dist/chunk-CGZBV6BD.js.map +1 -0
- package/dist/chunk-DDAHJZVK.js +258 -0
- package/dist/chunk-DDAHJZVK.js.map +1 -0
- package/dist/chunk-MQD5GXMT.js +171 -0
- package/dist/chunk-MQD5GXMT.js.map +1 -0
- package/dist/chunk-OKFXZHBC.js +43 -0
- package/dist/chunk-OKFXZHBC.js.map +1 -0
- package/dist/chunk-RLLWYFPI.js +168 -0
- package/dist/chunk-RLLWYFPI.js.map +1 -0
- package/dist/chunk-VCHFXT5W.js +425 -0
- package/dist/chunk-VCHFXT5W.js.map +1 -0
- package/dist/chunk-ZWLYNGO3.js +40 -0
- package/dist/chunk-ZWLYNGO3.js.map +1 -0
- package/dist/http-response-BAhi8lF4.d.cts +124 -0
- package/dist/http-response-BAhi8lF4.d.ts +124 -0
- package/dist/index-DingXh7B.d.cts +1187 -0
- package/dist/index-tOH7XBa3.d.ts +1187 -0
- package/dist/routing.type-DB4pt-d9.d.ts +184 -0
- package/dist/routing.type-DF2BIL7x.d.cts +184 -0
- package/dist/validation-error.type-kD4_qNZ9.d.cts +199 -0
- package/dist/validation-error.type-kD4_qNZ9.d.ts +199 -0
- package/package.json +191 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/backend/frameworks/fastify/index.ts","../../../src/backend/frameworks/fastify/routing.ts","../../../src/backend/core/global/exceptions/coded-error.error.ts","../../../src/backend/core/global/exceptions/error-codes.const.ts","../../../src/backend/core/global/exceptions/object-validation.error.ts","../../../src/backend/core/onion-layers/domain/exceptions/domain.error.ts","../../../src/backend/core/onion-layers/app/exceptions/use-case.error.ts","../../../src/backend/core/onion-layers/app/exceptions/not-found.error.ts","../../../src/backend/core/onion-layers/app/exceptions/conflict.error.ts","../../../src/backend/core/onion-layers/app/exceptions/unprocessable.error.ts","../../../src/backend/core/onion-layers/infra/exceptions/infra.error.ts","../../../src/backend/core/onion-layers/presentation/exceptions/controller.error.ts","../../../src/backend/core/onion-layers/presentation/exceptions/access-denied.error.ts","../../../src/backend/core/onion-layers/presentation/exceptions/invalid-request.error.ts","../../../src/backend/frameworks/fastify/map-error-to-response.ts","../../../src/backend/frameworks/fastify/error-handler.ts"],"sourcesContent":["export {\n registerFastifyRoutes,\n type HttpController,\n type FastifyMiddleware,\n type RouteInputOrArray,\n type RegisterRoutesOptions,\n} from './routing';\nexport {\n mapErrorToResponse,\n type ErrorResponseBody,\n type MappedErrorResponse,\n} from './map-error-to-response';\nexport { onionErrorHandler } from './error-handler';\n","import type {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n RouteHandlerMethod,\n HTTPMethods,\n} from 'fastify';\nimport type { Controller } from '../../core/onion-layers/presentation/interfaces/types/controller.type';\nimport type { HttpRequest } from '../../core/onion-layers/presentation/interfaces/types/http/http-request';\nimport type { HttpResponse } from '../../core/onion-layers/presentation/interfaces/types/http/http-response';\nimport type { RouteInput } from '../../core/onion-layers/presentation/routing';\n\n/**\n * Controller that works with HttpRequest/HttpResponse.\n */\nexport type HttpController = Controller<HttpRequest, HttpResponse>;\n\n/**\n * Fastify preHandler middleware type.\n */\nexport type FastifyMiddleware = (\n request: FastifyRequest,\n reply: FastifyReply,\n) => Promise<void> | void;\n\n/**\n * Converts `{param}` to Fastify's `:param` format.\n */\nfunction toFastifyPath(path: string): string {\n return path.replace(/\\{([^}]+)\\}/g, ':$1');\n}\n\n/**\n * Extracts HttpRequest from Fastify request.\n */\nfunction extractRequest(request: FastifyRequest): HttpRequest {\n const headers: Record<string, string> = {};\n for (const [key, value] of Object.entries(request.headers)) {\n if (typeof value === 'string') {\n headers[key.toLowerCase()] = value;\n } else if (Array.isArray(value)) {\n headers[key.toLowerCase()] = value.join(', ');\n }\n }\n\n const queryParams: Record<string, string | string[]> = {};\n const query = request.query as Record<string, string | string[] | undefined>;\n for (const [key, value] of Object.entries(query)) {\n if (value != null) {\n queryParams[key] = value;\n }\n }\n\n return {\n body: request.body,\n headers,\n queryParams,\n pathParams: request.params as Record<string, string>,\n };\n}\n\n/**\n * Sends HttpResponse via Fastify reply.\n */\nfunction sendResponse(reply: FastifyReply, response: HttpResponse): FastifyReply {\n if (response.headers) {\n for (const [key, value] of Object.entries(response.headers)) {\n if (value != null) {\n reply.header(key, String(value));\n }\n }\n }\n\n reply.status(response.statusCode);\n\n if (response.body === undefined || response.body === null) {\n return reply.send();\n }\n\n return reply.send(response.body);\n}\n\n/**\n * Route input type that accepts either a single route or an array of routes.\n */\nexport type RouteInputOrArray = RouteInput | RouteInput[];\n\n/**\n * Options for registering routes.\n */\nexport interface RegisterRoutesOptions {\n /**\n * Prefix to apply to all routes in this registration.\n *\n * @example\n * ```typescript\n * registerFastifyRoutes(app, userRoutes, {\n * prefix: '/api/v1',\n * });\n * // Routes will be: /api/v1/users, /api/v1/users/:id, etc.\n * ```\n */\n prefix?: string;\n\n /**\n * Middlewares (preHandler hooks) to apply to all routes in this registration.\n * These run before the controller handler.\n *\n * @example\n * ```typescript\n * registerFastifyRoutes(app, protectedRoutes, {\n * middlewares: [\n * async (request, reply) => {\n * if (!request.headers.authorization) {\n * reply.status(401).send({ message: 'Unauthorized' });\n * }\n * },\n * ],\n * });\n * ```\n */\n middlewares?: FastifyMiddleware[];\n}\n\n/**\n * Registers routes onto a Fastify instance.\n *\n * Accepts either a single route or an array of routes. Can be called multiple\n * times to register routes from different domains/modules.\n *\n * @param app - The Fastify instance (passed by reference)\n * @param routes - A single route or an array of routes to register\n * @param options - Optional configuration including prefix\n *\n * @example Single route\n * ```typescript\n * const app = Fastify();\n *\n * registerFastifyRoutes(app, {\n * metadata: { path: '/health', method: 'GET' },\n * controller: healthController,\n * });\n * ```\n *\n * @example Multiple routes\n * ```typescript\n * const app = Fastify();\n *\n * registerFastifyRoutes(app, [\n * { metadata: { path: '/users', method: 'POST' }, controller: createUserController },\n * { metadata: { path: '/users/{id}', method: 'GET' }, controller: getUserController },\n * { metadata: { path: '/users/{id}', method: 'DELETE' }, controller: deleteUserController },\n * ]);\n * ```\n *\n * @example With prefix\n * ```typescript\n * registerFastifyRoutes(app, userRoutes, {\n * prefix: '/api/v1',\n * });\n * ```\n *\n * @example With middlewares\n * ```typescript\n * registerFastifyRoutes(app, protectedRoutes, {\n * middlewares: [\n * async (request, reply) => {\n * if (!request.headers.authorization) {\n * reply.status(401).send({ message: 'Unauthorized' });\n * }\n * },\n * ],\n * });\n * ```\n *\n * @example Registering from multiple domains\n * ```typescript\n * const app = Fastify();\n *\n * // Public routes\n * registerFastifyRoutes(app, publicRoutes);\n *\n * // API routes with prefix\n * registerFastifyRoutes(app, userRoutes, { prefix: '/api' });\n * registerFastifyRoutes(app, orderRoutes, { prefix: '/api' });\n *\n * await app.listen({ port: 3000 });\n * ```\n */\nexport function registerFastifyRoutes(\n app: FastifyInstance,\n routes: RouteInputOrArray,\n options?: RegisterRoutesOptions,\n): void {\n const routeArray = Array.isArray(routes) ? routes : [routes];\n const prefix = options?.prefix ?? '';\n const middlewares = options?.middlewares ?? [];\n\n for (const { metadata, controller, requestDtoFactory } of routeArray) {\n const path = prefix + toFastifyPath(metadata.path);\n const method = metadata.method.toUpperCase() as HTTPMethods;\n\n const handler: RouteHandlerMethod = async (request, reply) => {\n const rawRequest = extractRequest(request);\n const requestDto = requestDtoFactory(rawRequest);\n const responseDto = await controller.execute(requestDto);\n return sendResponse(reply, responseDto.data);\n };\n\n app.route({\n method,\n url: path,\n preHandler: middlewares,\n handler,\n });\n }\n}\n","import type { ErrorCode } from './error-codes.const';\n\n/**\n * Base error class for all application errors with a machine-readable code.\n *\n * Abstract class that extends the native `Error` with:\n * - A `code` property for programmatic error handling\n * - Optional `cause` for error chaining (ES2022 compatible)\n * - A `fromError` static factory pattern for error transformation\n *\n * **Why abstract:** Prevents non-declarative error usage. All errors must\n * be explicitly defined as subclasses to ensure consistent error taxonomy.\n *\n * @example Subclass implementation\n * ```typescript\n * class DbError extends InfraError {\n * static override fromError(cause: unknown): DbError {\n * return new DbError({\n * message: cause instanceof Error ? cause.message : 'Database error',\n * cause,\n * });\n * }\n * }\n * ```\n *\n * @example Usage with wrapErrorAsync\n * ```typescript\n * await wrapErrorAsync(\n * () => this.db.query(...),\n * DbError.fromError,\n * );\n * ```\n */\nexport abstract class CodedError extends Error {\n /** Machine-readable error code for programmatic handling. */\n public readonly code: ErrorCode | string;\n\n /**\n * Creates a new CodedError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable error message\n * @param options.code - Machine-readable error code from ErrorCodes registry or custom string\n * @param options.cause - Optional underlying error that caused this error\n */\n constructor({\n message,\n code,\n cause,\n }: {\n message: string;\n code: ErrorCode | string;\n cause?: unknown;\n }) {\n super(message);\n this.name = this.constructor.name;\n this.code = code;\n if (cause !== undefined) {\n Object.defineProperty(this, 'cause', {\n value: cause,\n writable: false,\n enumerable: false,\n configurable: true,\n });\n }\n }\n\n /**\n * Factory method to create a typed error from a caught error.\n *\n * Subclasses should override this to provide proper error transformation.\n * Designed for use with {@link wrapErrorAsync} and {@link wrapError}.\n *\n * @param _cause - The original caught error\n * @returns A new CodedError instance\n * @throws {Error} If not overridden by subclass\n *\n * @example\n * ```typescript\n * class NotFoundError extends UseCaseError {\n * static override fromError(cause: unknown): NotFoundError {\n * return new NotFoundError({\n * message: 'Resource not found',\n * cause,\n * });\n * }\n * }\n * ```\n */\n static fromError(_cause: unknown): CodedError {\n throw new Error(`${this.name}.fromError() must be implemented by subclass`);\n }\n}\n","/**\n * Centralized registry of all error codes used across the application.\n *\n * Error codes are grouped by architectural layer to maintain clear boundaries\n * and make it easy to identify where an error originated.\n *\n * @example Using error codes in custom errors\n * ```typescript\n * import { ErrorCodes } from '@cosmneo/onion-lasagna/backend/core/global';\n *\n * throw new NotFoundError({\n * message: 'User not found',\n * code: ErrorCodes.App.NOT_FOUND,\n * });\n * ```\n *\n * @example Checking error codes programmatically\n * ```typescript\n * if (error.code === ErrorCodes.App.NOT_FOUND) {\n * // Handle not found case\n * }\n * ```\n */\nexport const ErrorCodes = {\n /**\n * Domain layer error codes.\n * Used for business rule violations and invariant failures.\n */\n Domain: {\n /** Generic domain error */\n DOMAIN_ERROR: 'DOMAIN_ERROR',\n /** Business invariant was violated */\n INVARIANT_VIOLATION: 'INVARIANT_VIOLATION',\n /** Aggregate was partially loaded (missing required relations) */\n PARTIAL_LOAD: 'PARTIAL_LOAD',\n },\n\n /**\n * Application layer (use case) error codes.\n * Used for orchestration failures and business operation errors.\n */\n App: {\n /** Generic use case error */\n USE_CASE_ERROR: 'USE_CASE_ERROR',\n /** Requested resource was not found */\n NOT_FOUND: 'NOT_FOUND',\n /** Resource state conflict (e.g., duplicate, already exists) */\n CONFLICT: 'CONFLICT',\n /** Request is valid but cannot be processed due to business rules */\n UNPROCESSABLE: 'UNPROCESSABLE',\n },\n\n /**\n * Infrastructure layer error codes.\n * Used for data access, external services, and I/O failures.\n */\n Infra: {\n /** Generic infrastructure error */\n INFRA_ERROR: 'INFRA_ERROR',\n /** Database operation failed */\n DB_ERROR: 'DB_ERROR',\n /** Network connectivity or communication error */\n NETWORK_ERROR: 'NETWORK_ERROR',\n /** Operation timed out */\n TIMEOUT_ERROR: 'TIMEOUT_ERROR',\n /** External/third-party service error */\n EXTERNAL_SERVICE_ERROR: 'EXTERNAL_SERVICE_ERROR',\n },\n\n /**\n * Presentation layer error codes.\n * Used for controller, request handling, and authorization errors.\n */\n Presentation: {\n /** Generic controller error */\n CONTROLLER_ERROR: 'CONTROLLER_ERROR',\n /** Request denied due to authorization failure */\n ACCESS_DENIED: 'ACCESS_DENIED',\n /** Request validation failed (malformed input) */\n INVALID_REQUEST: 'INVALID_REQUEST',\n },\n\n /**\n * Global/cross-cutting error codes.\n * Used for validation and other cross-layer concerns.\n */\n Global: {\n /** Object/schema validation failed */\n OBJECT_VALIDATION_ERROR: 'OBJECT_VALIDATION_ERROR',\n },\n} as const;\n\n/**\n * Type representing all possible domain error codes.\n */\nexport type DomainErrorCode = (typeof ErrorCodes.Domain)[keyof typeof ErrorCodes.Domain];\n\n/**\n * Type representing all possible application error codes.\n */\nexport type AppErrorCode = (typeof ErrorCodes.App)[keyof typeof ErrorCodes.App];\n\n/**\n * Type representing all possible infrastructure error codes.\n */\nexport type InfraErrorCode = (typeof ErrorCodes.Infra)[keyof typeof ErrorCodes.Infra];\n\n/**\n * Type representing all possible presentation error codes.\n */\nexport type PresentationErrorCode =\n (typeof ErrorCodes.Presentation)[keyof typeof ErrorCodes.Presentation];\n\n/**\n * Type representing all possible global error codes.\n */\nexport type GlobalErrorCode = (typeof ErrorCodes.Global)[keyof typeof ErrorCodes.Global];\n\n/**\n * Union type of all error codes across all layers.\n *\n * Use this when you need to accept any valid error code.\n *\n * @example\n * ```typescript\n * function logError(code: ErrorCode, message: string) {\n * console.error(`[${code}] ${message}`);\n * }\n * ```\n */\nexport type ErrorCode =\n | DomainErrorCode\n | AppErrorCode\n | InfraErrorCode\n | PresentationErrorCode\n | GlobalErrorCode;\n","import { CodedError } from './coded-error.error';\nimport { ErrorCodes, type GlobalErrorCode } from './error-codes.const';\nimport type { ValidationError } from '../interfaces/types/validation-error.type';\n\n/**\n * Error thrown when object validation fails.\n *\n * Contains structured validation errors with field paths and messages,\n * making it easy to report specific validation failures to clients.\n * Thrown by all validator implementations (Zod, ArkType, TypeBox, Valibot).\n *\n * **Flow:**\n * 1. Validator throws `ObjectValidationError` with field-level errors\n * 2. Controller catches and converts to {@link InvalidRequestError}\n * 3. HTTP layer maps to 400 Bad Request with error details\n *\n * @example\n * ```typescript\n * try {\n * const dto = CreateUserDto.create(invalidData);\n * } catch (error) {\n * if (error instanceof ObjectValidationError) {\n * console.log(error.validationErrors);\n * // [\n * // { field: 'email', message: 'Invalid email format' },\n * // { field: 'age', message: 'Must be a positive number' }\n * // ]\n * }\n * }\n * ```\n */\nexport class ObjectValidationError extends CodedError {\n /**\n * Array of field-level validation errors.\n *\n * Each entry contains:\n * - `field`: Dot-notation path to the invalid field (e.g., 'user.email')\n * - `message`: Human-readable validation failure message\n */\n validationErrors: ValidationError[];\n\n /**\n * Creates a new ObjectValidationError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable summary message\n * @param options.code - Machine-readable error code (default: 'OBJECT_VALIDATION_ERROR')\n * @param options.cause - Optional underlying error from validation library\n * @param options.validationErrors - Array of field-level validation errors\n */\n constructor({\n message,\n code = ErrorCodes.Global.OBJECT_VALIDATION_ERROR,\n cause,\n validationErrors,\n }: {\n message: string;\n code?: GlobalErrorCode | string;\n cause?: unknown;\n validationErrors: ValidationError[];\n }) {\n super({ message, code, cause });\n this.validationErrors = validationErrors;\n }\n\n /**\n * Creates an ObjectValidationError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new ObjectValidationError instance with the cause attached\n */\n static override fromError(cause: unknown): ObjectValidationError {\n return new ObjectValidationError({\n message: cause instanceof Error ? cause.message : 'Validation failed',\n cause,\n validationErrors: [],\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport { ErrorCodes, type DomainErrorCode } from '../../../global/exceptions/error-codes.const';\n\n/**\n * Base error class for domain layer failures.\n *\n * Domain errors represent violations of business rules, invariants,\n * or aggregate consistency. They originate from the core domain logic\n * and should be caught and handled by the application layer.\n *\n * **When to throw:**\n * - Business rule violations (e.g., \"Cannot withdraw more than balance\")\n * - Invariant violations (e.g., \"Email format invalid\")\n * - Aggregate consistency failures\n *\n * **Child classes:**\n * - {@link InvariantViolationError} - Value object or entity invariant failures\n * - {@link PartialLoadError} - Incomplete aggregate reconstitution\n *\n * @example\n * ```typescript\n * if (account.balance < amount) {\n * throw new DomainError({\n * message: 'Insufficient funds for withdrawal',\n * code: 'INSUFFICIENT_FUNDS',\n * });\n * }\n * ```\n */\nexport class DomainError extends CodedError {\n /**\n * Creates a new DomainError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable error description\n * @param options.code - Machine-readable error code (default: 'DOMAIN_ERROR')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.Domain.DOMAIN_ERROR,\n cause,\n }: {\n message: string;\n code?: DomainErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates a DomainError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new DomainError instance with the cause attached\n */\n static override fromError(cause: unknown): DomainError {\n return new DomainError({\n message: cause instanceof Error ? cause.message : 'Domain error',\n cause,\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport { ErrorCodes, type AppErrorCode } from '../../../global/exceptions/error-codes.const';\n\n/**\n * Base error class for application layer (use case) failures.\n *\n * Use case errors represent failures in the application's business logic\n * orchestration, such as resource conflicts, missing entities, or\n * unprocessable requests. They bridge domain errors to the presentation layer.\n *\n * **When to throw:**\n * - Resource not found (e.g., \"User with ID X not found\")\n * - Conflict states (e.g., \"Email already registered\")\n * - Unprocessable business operations\n *\n * **Child classes:**\n * - {@link ConflictError} - Resource state conflicts (HTTP 409)\n * - {@link NotFoundError} - Resource not found (HTTP 404)\n * - {@link UnprocessableError} - Valid but unprocessable request (HTTP 422)\n *\n * @example\n * ```typescript\n * const user = await this.userRepo.findById(id);\n * if (!user) {\n * throw new NotFoundError({\n * message: `User with ID ${id} not found`,\n * code: 'USER_NOT_FOUND',\n * });\n * }\n * ```\n */\nexport class UseCaseError extends CodedError {\n /**\n * Creates a new UseCaseError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable error description\n * @param options.code - Machine-readable error code (default: 'USE_CASE_ERROR')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.App.USE_CASE_ERROR,\n cause,\n }: {\n message: string;\n code?: AppErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates a UseCaseError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new UseCaseError instance with the cause attached\n */\n static override fromError(cause: unknown): UseCaseError {\n return new UseCaseError({\n message: cause instanceof Error ? cause.message : 'Use case error',\n cause,\n });\n }\n}\n","import { ErrorCodes, type AppErrorCode } from '../../../global/exceptions/error-codes.const';\nimport { UseCaseError } from './use-case.error';\n\n/**\n * Error thrown when a requested resource does not exist.\n *\n * Indicates that the entity or resource referenced by the request\n * could not be found in the system.\n *\n * **When to throw:**\n * - Entity lookup by ID returns null\n * - Referenced resource doesn't exist\n * - Parent entity for a child operation not found\n *\n * @example\n * ```typescript\n * const user = await this.userRepo.findById(userId);\n * if (!user) {\n * throw new NotFoundError({\n * message: `User with ID ${userId} not found`,\n * code: 'USER_NOT_FOUND',\n * });\n * }\n * ```\n *\n * @extends UseCaseError\n */\nexport class NotFoundError extends UseCaseError {\n /**\n * Creates a new NotFoundError instance.\n *\n * @param options - Error configuration\n * @param options.message - Description of what was not found\n * @param options.code - Machine-readable error code (default: 'NOT_FOUND')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.App.NOT_FOUND,\n cause,\n }: {\n message: string;\n code?: AppErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates a NotFoundError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new NotFoundError instance with the cause attached\n */\n static override fromError(cause: unknown): NotFoundError {\n return new NotFoundError({\n message: cause instanceof Error ? cause.message : 'Resource not found',\n cause,\n });\n }\n}\n","import { ErrorCodes, type AppErrorCode } from '../../../global/exceptions/error-codes.const';\nimport { UseCaseError } from './use-case.error';\n\n/**\n * Error thrown when an operation conflicts with existing state.\n *\n * Indicates that the requested operation cannot be completed because\n * it would violate uniqueness constraints or cause state conflicts.\n *\n * **When to throw:**\n * - Duplicate unique field (e.g., email already registered)\n * - Optimistic locking conflict (stale version)\n * - Concurrent modification detected\n *\n * @example\n * ```typescript\n * const existing = await this.userRepo.findByEmail(email);\n * if (existing) {\n * throw new ConflictError({\n * message: 'Email already registered',\n * code: 'EMAIL_ALREADY_EXISTS',\n * });\n * }\n * ```\n *\n * @extends UseCaseError\n */\nexport class ConflictError extends UseCaseError {\n /**\n * Creates a new ConflictError instance.\n *\n * @param options - Error configuration\n * @param options.message - Description of the conflict\n * @param options.code - Machine-readable error code (default: 'CONFLICT')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.App.CONFLICT,\n cause,\n }: {\n message: string;\n code?: AppErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates a ConflictError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new ConflictError instance with the cause attached\n */\n static override fromError(cause: unknown): ConflictError {\n return new ConflictError({\n message: cause instanceof Error ? cause.message : 'Conflict error',\n cause,\n });\n }\n}\n","import { ErrorCodes, type AppErrorCode } from '../../../global/exceptions/error-codes.const';\nimport { UseCaseError } from './use-case.error';\n\n/**\n * Error thrown when a request is valid but cannot be processed.\n *\n * The request is syntactically correct and passes validation, but\n * business logic prevents the operation from being completed.\n *\n * **When to throw:**\n * - Business rule prevents operation (e.g., insufficient balance)\n * - Invalid state transition (e.g., canceling a completed order)\n * - Preconditions not met for the operation\n *\n * @example\n * ```typescript\n * if (account.balance < amount) {\n * throw new UnprocessableError({\n * message: 'Insufficient balance for withdrawal',\n * code: 'INSUFFICIENT_BALANCE',\n * });\n * }\n * ```\n *\n * @extends UseCaseError\n */\nexport class UnprocessableError extends UseCaseError {\n /**\n * Creates a new UnprocessableError instance.\n *\n * @param options - Error configuration\n * @param options.message - Description of why the request cannot be processed\n * @param options.code - Machine-readable error code (default: 'UNPROCESSABLE')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.App.UNPROCESSABLE,\n cause,\n }: {\n message: string;\n code?: AppErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates an UnprocessableError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new UnprocessableError instance with the cause attached\n */\n static override fromError(cause: unknown): UnprocessableError {\n return new UnprocessableError({\n message: cause instanceof Error ? cause.message : 'Unprocessable request',\n cause,\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport { ErrorCodes, type InfraErrorCode } from '../../../global/exceptions/error-codes.const';\n\n/**\n * Base error class for infrastructure layer failures.\n *\n * Infrastructure errors represent failures in external dependencies\n * such as databases, network services, file systems, or third-party APIs.\n * They are automatically created by {@link BaseOutboundAdapter} when\n * repository or gateway methods fail.\n *\n * **When to throw:**\n * - Database connection or query failures\n * - Network timeouts or connection errors\n * - External API failures\n * - File system errors\n *\n * **Child classes:**\n * - {@link DbError} - Database operation failures\n * - {@link NetworkError} - Network connectivity issues\n * - {@link TimeoutError} - Operation timeout\n * - {@link ExternalServiceError} - Third-party service failures\n *\n * @example\n * ```typescript\n * // In a repository extending BaseOutboundAdapter\n * protected override createInfraError(error: unknown, methodName: string): InfraError {\n * return new DbError({\n * message: `Database error in ${methodName}`,\n * cause: error,\n * });\n * }\n * ```\n */\nexport class InfraError extends CodedError {\n /**\n * Creates a new InfraError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable error description\n * @param options.code - Machine-readable error code (default: 'INFRA_ERROR')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.Infra.INFRA_ERROR,\n cause,\n }: {\n message: string;\n code?: InfraErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates an InfraError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new InfraError instance with the cause attached\n */\n static override fromError(cause: unknown): InfraError {\n return new InfraError({\n message: cause instanceof Error ? cause.message : 'Infrastructure error',\n cause,\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport {\n ErrorCodes,\n type PresentationErrorCode,\n} from '../../../global/exceptions/error-codes.const';\n\n/**\n * Base error class for presentation layer (controller) failures.\n *\n * Controller errors represent failures in request handling,\n * such as access control violations or malformed requests.\n * They are the outermost error layer and typically map to HTTP responses.\n *\n * **When to throw:**\n * - Access control failures (unauthorized/forbidden)\n * - Request validation failures\n * - Unexpected controller execution errors\n *\n * **Child classes:**\n * - {@link AccessDeniedError} - Authorization failures (HTTP 403)\n * - {@link InvalidRequestError} - Request validation failures (HTTP 400)\n *\n * @example\n * ```typescript\n * // Thrown automatically by BaseController for unexpected errors\n * throw new ControllerError({\n * message: 'Controller execution failed',\n * cause: originalError,\n * });\n * ```\n */\nexport class ControllerError extends CodedError {\n /**\n * Creates a new ControllerError instance.\n *\n * @param options - Error configuration\n * @param options.message - Human-readable error description\n * @param options.code - Machine-readable error code (default: 'CONTROLLER_ERROR')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.Presentation.CONTROLLER_ERROR,\n cause,\n }: {\n message: string;\n code?: PresentationErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates a ControllerError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new ControllerError instance with the cause attached\n */\n static override fromError(cause: unknown): ControllerError {\n return new ControllerError({\n message: cause instanceof Error ? cause.message : 'Controller error',\n cause,\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport {\n ErrorCodes,\n type PresentationErrorCode,\n} from '../../../global/exceptions/error-codes.const';\n\n/**\n * Error thrown when access to a resource is denied.\n *\n * Indicates that the requester does not have permission to perform\n * the requested operation. Thrown by {@link GuardedController} when\n * an access guard returns `isAllowed: false`.\n *\n * **When thrown:**\n * - Access guard denies the request\n * - User lacks required permissions\n * - Resource ownership check fails\n *\n * @example GuardedController usage\n * ```typescript\n * const controller = GuardedController.create({\n * accessGuard: (req) => ({\n * isAllowed: req.user?.role === 'admin',\n * reason: 'Admin access required',\n * }),\n * // ... other config\n * });\n * ```\n *\n * @example Manual usage\n * ```typescript\n * if (!user.canAccess(resource)) {\n * throw new AccessDeniedError({\n * message: 'You do not have access to this resource',\n * code: 'RESOURCE_ACCESS_DENIED',\n * });\n * }\n * ```\n *\n * @extends CodedError\n */\nexport class AccessDeniedError extends CodedError {\n /**\n * Creates a new AccessDeniedError instance.\n *\n * @param options - Error configuration\n * @param options.message - Description of why access was denied\n * @param options.code - Machine-readable error code (default: 'ACCESS_DENIED')\n * @param options.cause - Optional underlying error\n */\n constructor({\n message,\n code = ErrorCodes.Presentation.ACCESS_DENIED,\n cause,\n }: {\n message: string;\n code?: PresentationErrorCode | string;\n cause?: unknown;\n }) {\n super({ message, code, cause });\n }\n\n /**\n * Creates an AccessDeniedError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new AccessDeniedError instance with the cause attached\n */\n static override fromError(cause: unknown): AccessDeniedError {\n return new AccessDeniedError({\n message: cause instanceof Error ? cause.message : 'Access denied',\n cause,\n });\n }\n}\n","import { CodedError } from '../../../global/exceptions/coded-error.error';\nimport {\n ErrorCodes,\n type PresentationErrorCode,\n} from '../../../global/exceptions/error-codes.const';\nimport type { ValidationError } from '../../../global/interfaces/types/validation-error.type';\n\n/**\n * Error thrown when request validation fails at the controller level.\n *\n * Contains structured validation errors with field paths and messages,\n * converted from {@link ObjectValidationError} by {@link BaseController}.\n * Provides detailed feedback about which fields failed validation.\n *\n * **When thrown:**\n * - Request DTO validation fails\n * - Malformed request data\n * - Missing required fields\n *\n * @example\n * ```typescript\n * // Automatically thrown by BaseController when DTO validation fails\n * // The validationErrors array contains field-level details:\n * // [\n * // { field: 'email', message: 'Invalid email format' },\n * // { field: 'age', message: 'Must be a positive number' }\n * // ]\n * ```\n *\n * @example Manual usage\n * ```typescript\n * throw new InvalidRequestError({\n * message: 'Request validation failed',\n * validationErrors: [\n * { field: 'username', message: 'Username is required' },\n * ],\n * });\n * ```\n *\n * @extends CodedError\n */\nexport class InvalidRequestError extends CodedError {\n /**\n * Array of field-level validation errors.\n *\n * Each entry contains:\n * - `field`: Dot-notation path to the invalid field\n * - `message`: Human-readable validation failure message\n */\n readonly validationErrors: ValidationError[];\n\n /**\n * Creates a new InvalidRequestError instance.\n *\n * @param options - Error configuration\n * @param options.message - Summary of the validation failure\n * @param options.code - Machine-readable error code (default: 'INVALID_REQUEST')\n * @param options.cause - Optional underlying error\n * @param options.validationErrors - Array of field-level validation errors\n */\n constructor({\n message,\n code = ErrorCodes.Presentation.INVALID_REQUEST,\n cause,\n validationErrors,\n }: {\n message: string;\n code?: PresentationErrorCode | string;\n cause?: unknown;\n validationErrors: ValidationError[];\n }) {\n super({ message, code, cause });\n this.validationErrors = validationErrors;\n }\n\n /**\n * Creates an InvalidRequestError from a caught error.\n *\n * @param cause - The original caught error\n * @returns A new InvalidRequestError instance with the cause attached\n */\n static override fromError(cause: unknown): InvalidRequestError {\n return new InvalidRequestError({\n message: cause instanceof Error ? cause.message : 'Invalid request',\n cause,\n validationErrors: [],\n });\n }\n}\n","import { CodedError } from '../../core/global/exceptions/coded-error.error';\nimport { ObjectValidationError } from '../../core/global/exceptions/object-validation.error';\nimport { DomainError } from '../../core/onion-layers/domain/exceptions/domain.error';\nimport { UseCaseError } from '../../core/onion-layers/app/exceptions/use-case.error';\nimport { NotFoundError } from '../../core/onion-layers/app/exceptions/not-found.error';\nimport { ConflictError } from '../../core/onion-layers/app/exceptions/conflict.error';\nimport { UnprocessableError } from '../../core/onion-layers/app/exceptions/unprocessable.error';\nimport { InfraError } from '../../core/onion-layers/infra/exceptions/infra.error';\nimport { ControllerError } from '../../core/onion-layers/presentation/exceptions/controller.error';\nimport { AccessDeniedError } from '../../core/onion-layers/presentation/exceptions/access-denied.error';\nimport { InvalidRequestError } from '../../core/onion-layers/presentation/exceptions/invalid-request.error';\n\n/**\n * Error item for field-level validation errors.\n */\ninterface ErrorItem {\n item: string;\n message: string;\n}\n\n/**\n * Standard error response body.\n */\nexport interface ErrorResponseBody {\n message: string;\n errorCode: string;\n errorItems?: ErrorItem[];\n}\n\n/**\n * Mapped error response with status code and body.\n */\nexport interface MappedErrorResponse {\n statusCode: number;\n body: ErrorResponseBody;\n}\n\n/**\n * Maps our error hierarchy to HTTP status codes and response bodies.\n *\n * Mapping strategy:\n * - `ObjectValidationError` → 400 Bad Request\n * - `InvalidRequestError` → 400 Bad Request\n * - `AccessDeniedError` → 403 Forbidden\n * - `NotFoundError` → 404 Not Found\n * - `ConflictError` → 409 Conflict\n * - `UnprocessableError` → 422 Unprocessable Entity\n * - `UseCaseError` (other) → 400 Bad Request\n * - `DomainError` → 500 Internal Server Error (masked)\n * - `InfraError` → 500 Internal Server Error (masked)\n * - `ControllerError` → 500 Internal Server Error (masked)\n * - Unknown → 500 Internal Server Error (masked)\n *\n * **Security Note:** Domain and infrastructure errors are masked to avoid\n * leaking internal implementation details.\n *\n * @param error - The error to map\n * @returns Mapped error response with status code and body\n */\nexport function mapErrorToResponse(error: unknown): MappedErrorResponse {\n // Validation errors → 400 Bad Request\n if (error instanceof ObjectValidationError) {\n return {\n statusCode: 400,\n body: {\n message: error.message,\n errorCode: error.code,\n errorItems: error.validationErrors.map((e) => ({\n item: e.field,\n message: e.message,\n })),\n },\n };\n }\n\n if (error instanceof InvalidRequestError) {\n return {\n statusCode: 400,\n body: {\n message: error.message,\n errorCode: error.code,\n errorItems: error.validationErrors.map((e) => ({\n item: e.field,\n message: e.message,\n })),\n },\n };\n }\n\n // Access control → 403 Forbidden\n if (error instanceof AccessDeniedError) {\n return {\n statusCode: 403,\n body: {\n message: error.message,\n errorCode: error.code,\n },\n };\n }\n\n // Use case errors → specific HTTP status codes\n if (error instanceof NotFoundError) {\n return {\n statusCode: 404,\n body: {\n message: error.message,\n errorCode: error.code,\n },\n };\n }\n\n if (error instanceof ConflictError) {\n return {\n statusCode: 409,\n body: {\n message: error.message,\n errorCode: error.code,\n },\n };\n }\n\n if (error instanceof UnprocessableError) {\n return {\n statusCode: 422,\n body: {\n message: error.message,\n errorCode: error.code,\n },\n };\n }\n\n // Other use case errors → 400 Bad Request\n if (error instanceof UseCaseError) {\n return {\n statusCode: 400,\n body: {\n message: error.message,\n errorCode: error.code,\n },\n };\n }\n\n // Domain errors → 500 Internal Server Error (masked)\n if (error instanceof DomainError) {\n return {\n statusCode: 500,\n body: {\n message: 'An unexpected error occurred',\n errorCode: 'INTERNAL_ERROR',\n },\n };\n }\n\n // Infrastructure errors → 500 Internal Server Error (masked)\n if (error instanceof InfraError) {\n return {\n statusCode: 500,\n body: {\n message: 'An unexpected error occurred',\n errorCode: 'INTERNAL_ERROR',\n },\n };\n }\n\n // Controller errors → 500 Internal Server Error (masked)\n if (error instanceof ControllerError) {\n return {\n statusCode: 500,\n body: {\n message: 'An unexpected error occurred',\n errorCode: 'INTERNAL_ERROR',\n },\n };\n }\n\n // CodedError (catch-all for known errors) → 500 (masked)\n if (error instanceof CodedError) {\n return {\n statusCode: 500,\n body: {\n message: 'An unexpected error occurred',\n errorCode: 'INTERNAL_ERROR',\n },\n };\n }\n\n // Unknown errors → 500 Internal Server Error (masked)\n return {\n statusCode: 500,\n body: {\n message: 'An unexpected error occurred',\n errorCode: 'INTERNAL_ERROR',\n },\n };\n}\n","import type { FastifyError, FastifyReply, FastifyRequest } from 'fastify';\nimport { mapErrorToResponse } from './map-error-to-response';\n\n/**\n * Fastify error handler that maps domain errors to HTTP responses.\n *\n * Apply this to your Fastify app using `setErrorHandler()` to automatically\n * convert domain/use-case errors to appropriate HTTP responses.\n *\n * @example\n * ```typescript\n * import Fastify from 'fastify';\n * import { registerFastifyRoutes, onionErrorHandler } from '@cosmneo/onion-lasagna/backend/frameworks/fastify';\n *\n * const app = Fastify();\n *\n * // Apply error handler\n * app.setErrorHandler(onionErrorHandler);\n *\n * // Register routes\n * registerFastifyRoutes(app, routes);\n *\n * await app.listen({ port: 3000 });\n * ```\n *\n * @example With custom error logging\n * ```typescript\n * app.setErrorHandler((error, request, reply) => {\n * // Log the error\n * request.log.error(error);\n *\n * // Use the onion error handler\n * return onionErrorHandler(error, request, reply);\n * });\n * ```\n */\nexport function onionErrorHandler(\n error: FastifyError | Error,\n _request: FastifyRequest,\n reply: FastifyReply,\n): FastifyReply {\n const { statusCode, body } = mapErrorToResponse(error);\n\n return reply.status(statusCode).send(body);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;A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AGA,MAAI,iBAAiB,cAAc;AACjC,WAAO;AAAA,MACL,YAAY;AAAA,MACZ,MAAM;AAAA,QACJ,SAAS,MAAM;AAAA,QACf,WAAW,MAAM;AAAA,MACnB;AAAA,IACF;AAAA,EACF;AAGA,MAAI,iBAAiB,aAAa;AAChC,WAAO;AAAA,MACL,YAAY;AAAA,MACZ,MAAM;AAAA,QACJ,SAAS;AAAA,QACT,WAAW;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAGA,MAAI,iBAAiB,YAAY;AAC/B,WAAO;AAAA,MACL,YAAY;AAAA,MACZ,MAAM;AAAA,QACJ,SAAS;AAAA,QACT,WAAW;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAGA,MAAI,iBAAiB,iBAAiB;AACpC,WAAO;AAAA,MACL,YAAY;AAAA,MACZ,MAAM;AAAA,QACJ,SAAS;AAAA,QACT,WAAW;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAGA,MAAI,iBAAiB,YAAY;AAC/B,WAAO;AAAA,MACL,YAAY;AAAA,MACZ,MAAM;AAAA,QACJ,SAAS;AAAA,QACT,WAAW;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAGA,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,WAAW;AAAA,IACb;AAAA,EACF;AACF;;;AC9JO,SAAS,kBACd,OACA,UACA,OACc;AACd,QAAM,EAAE,YAAY,KAAK,IAAI,mBAAmB,KAAK;AAErD,SAAO,MAAM,OAAO,UAAU,EAAE,KAAK,IAAI;AAC3C;","names":[]}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify';
|
|
2
|
+
import { C as Controller, H as HttpRequest, a as HttpResponse } from '../../http-response-BAhi8lF4.cjs';
|
|
3
|
+
import { R as RouteInput } from '../../routing.type-DF2BIL7x.cjs';
|
|
4
|
+
import '../../base-dto.class-D7W9iqoU.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Controller that works with HttpRequest/HttpResponse.
|
|
8
|
+
*/
|
|
9
|
+
type HttpController = Controller<HttpRequest, HttpResponse>;
|
|
10
|
+
/**
|
|
11
|
+
* Fastify preHandler middleware type.
|
|
12
|
+
*/
|
|
13
|
+
type FastifyMiddleware = (request: FastifyRequest, reply: FastifyReply) => Promise<void> | void;
|
|
14
|
+
/**
|
|
15
|
+
* Route input type that accepts either a single route or an array of routes.
|
|
16
|
+
*/
|
|
17
|
+
type RouteInputOrArray = RouteInput | RouteInput[];
|
|
18
|
+
/**
|
|
19
|
+
* Options for registering routes.
|
|
20
|
+
*/
|
|
21
|
+
interface RegisterRoutesOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Prefix to apply to all routes in this registration.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* registerFastifyRoutes(app, userRoutes, {
|
|
28
|
+
* prefix: '/api/v1',
|
|
29
|
+
* });
|
|
30
|
+
* // Routes will be: /api/v1/users, /api/v1/users/:id, etc.
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
prefix?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Middlewares (preHandler hooks) to apply to all routes in this registration.
|
|
36
|
+
* These run before the controller handler.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* registerFastifyRoutes(app, protectedRoutes, {
|
|
41
|
+
* middlewares: [
|
|
42
|
+
* async (request, reply) => {
|
|
43
|
+
* if (!request.headers.authorization) {
|
|
44
|
+
* reply.status(401).send({ message: 'Unauthorized' });
|
|
45
|
+
* }
|
|
46
|
+
* },
|
|
47
|
+
* ],
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
middlewares?: FastifyMiddleware[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Registers routes onto a Fastify instance.
|
|
55
|
+
*
|
|
56
|
+
* Accepts either a single route or an array of routes. Can be called multiple
|
|
57
|
+
* times to register routes from different domains/modules.
|
|
58
|
+
*
|
|
59
|
+
* @param app - The Fastify instance (passed by reference)
|
|
60
|
+
* @param routes - A single route or an array of routes to register
|
|
61
|
+
* @param options - Optional configuration including prefix
|
|
62
|
+
*
|
|
63
|
+
* @example Single route
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const app = Fastify();
|
|
66
|
+
*
|
|
67
|
+
* registerFastifyRoutes(app, {
|
|
68
|
+
* metadata: { path: '/health', method: 'GET' },
|
|
69
|
+
* controller: healthController,
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example Multiple routes
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const app = Fastify();
|
|
76
|
+
*
|
|
77
|
+
* registerFastifyRoutes(app, [
|
|
78
|
+
* { metadata: { path: '/users', method: 'POST' }, controller: createUserController },
|
|
79
|
+
* { metadata: { path: '/users/{id}', method: 'GET' }, controller: getUserController },
|
|
80
|
+
* { metadata: { path: '/users/{id}', method: 'DELETE' }, controller: deleteUserController },
|
|
81
|
+
* ]);
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @example With prefix
|
|
85
|
+
* ```typescript
|
|
86
|
+
* registerFastifyRoutes(app, userRoutes, {
|
|
87
|
+
* prefix: '/api/v1',
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example With middlewares
|
|
92
|
+
* ```typescript
|
|
93
|
+
* registerFastifyRoutes(app, protectedRoutes, {
|
|
94
|
+
* middlewares: [
|
|
95
|
+
* async (request, reply) => {
|
|
96
|
+
* if (!request.headers.authorization) {
|
|
97
|
+
* reply.status(401).send({ message: 'Unauthorized' });
|
|
98
|
+
* }
|
|
99
|
+
* },
|
|
100
|
+
* ],
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @example Registering from multiple domains
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const app = Fastify();
|
|
107
|
+
*
|
|
108
|
+
* // Public routes
|
|
109
|
+
* registerFastifyRoutes(app, publicRoutes);
|
|
110
|
+
*
|
|
111
|
+
* // API routes with prefix
|
|
112
|
+
* registerFastifyRoutes(app, userRoutes, { prefix: '/api' });
|
|
113
|
+
* registerFastifyRoutes(app, orderRoutes, { prefix: '/api' });
|
|
114
|
+
*
|
|
115
|
+
* await app.listen({ port: 3000 });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function registerFastifyRoutes(app: FastifyInstance, routes: RouteInputOrArray, options?: RegisterRoutesOptions): void;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Error item for field-level validation errors.
|
|
122
|
+
*/
|
|
123
|
+
interface ErrorItem {
|
|
124
|
+
item: string;
|
|
125
|
+
message: string;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Standard error response body.
|
|
129
|
+
*/
|
|
130
|
+
interface ErrorResponseBody {
|
|
131
|
+
message: string;
|
|
132
|
+
errorCode: string;
|
|
133
|
+
errorItems?: ErrorItem[];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Mapped error response with status code and body.
|
|
137
|
+
*/
|
|
138
|
+
interface MappedErrorResponse {
|
|
139
|
+
statusCode: number;
|
|
140
|
+
body: ErrorResponseBody;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Maps our error hierarchy to HTTP status codes and response bodies.
|
|
144
|
+
*
|
|
145
|
+
* Mapping strategy:
|
|
146
|
+
* - `ObjectValidationError` → 400 Bad Request
|
|
147
|
+
* - `InvalidRequestError` → 400 Bad Request
|
|
148
|
+
* - `AccessDeniedError` → 403 Forbidden
|
|
149
|
+
* - `NotFoundError` → 404 Not Found
|
|
150
|
+
* - `ConflictError` → 409 Conflict
|
|
151
|
+
* - `UnprocessableError` → 422 Unprocessable Entity
|
|
152
|
+
* - `UseCaseError` (other) → 400 Bad Request
|
|
153
|
+
* - `DomainError` → 500 Internal Server Error (masked)
|
|
154
|
+
* - `InfraError` → 500 Internal Server Error (masked)
|
|
155
|
+
* - `ControllerError` → 500 Internal Server Error (masked)
|
|
156
|
+
* - Unknown → 500 Internal Server Error (masked)
|
|
157
|
+
*
|
|
158
|
+
* **Security Note:** Domain and infrastructure errors are masked to avoid
|
|
159
|
+
* leaking internal implementation details.
|
|
160
|
+
*
|
|
161
|
+
* @param error - The error to map
|
|
162
|
+
* @returns Mapped error response with status code and body
|
|
163
|
+
*/
|
|
164
|
+
declare function mapErrorToResponse(error: unknown): MappedErrorResponse;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Fastify error handler that maps domain errors to HTTP responses.
|
|
168
|
+
*
|
|
169
|
+
* Apply this to your Fastify app using `setErrorHandler()` to automatically
|
|
170
|
+
* convert domain/use-case errors to appropriate HTTP responses.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import Fastify from 'fastify';
|
|
175
|
+
* import { registerFastifyRoutes, onionErrorHandler } from '@cosmneo/onion-lasagna/backend/frameworks/fastify';
|
|
176
|
+
*
|
|
177
|
+
* const app = Fastify();
|
|
178
|
+
*
|
|
179
|
+
* // Apply error handler
|
|
180
|
+
* app.setErrorHandler(onionErrorHandler);
|
|
181
|
+
*
|
|
182
|
+
* // Register routes
|
|
183
|
+
* registerFastifyRoutes(app, routes);
|
|
184
|
+
*
|
|
185
|
+
* await app.listen({ port: 3000 });
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @example With custom error logging
|
|
189
|
+
* ```typescript
|
|
190
|
+
* app.setErrorHandler((error, request, reply) => {
|
|
191
|
+
* // Log the error
|
|
192
|
+
* request.log.error(error);
|
|
193
|
+
*
|
|
194
|
+
* // Use the onion error handler
|
|
195
|
+
* return onionErrorHandler(error, request, reply);
|
|
196
|
+
* });
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
declare function onionErrorHandler(error: FastifyError | Error, _request: FastifyRequest, reply: FastifyReply): FastifyReply;
|
|
200
|
+
|
|
201
|
+
export { type ErrorResponseBody, type FastifyMiddleware, type HttpController, type MappedErrorResponse, type RegisterRoutesOptions, type RouteInputOrArray, mapErrorToResponse, onionErrorHandler, registerFastifyRoutes };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify';
|
|
2
|
+
import { C as Controller, H as HttpRequest, a as HttpResponse } from '../../http-response-BAhi8lF4.js';
|
|
3
|
+
import { R as RouteInput } from '../../routing.type-DB4pt-d9.js';
|
|
4
|
+
import '../../base-dto.class-D7W9iqoU.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Controller that works with HttpRequest/HttpResponse.
|
|
8
|
+
*/
|
|
9
|
+
type HttpController = Controller<HttpRequest, HttpResponse>;
|
|
10
|
+
/**
|
|
11
|
+
* Fastify preHandler middleware type.
|
|
12
|
+
*/
|
|
13
|
+
type FastifyMiddleware = (request: FastifyRequest, reply: FastifyReply) => Promise<void> | void;
|
|
14
|
+
/**
|
|
15
|
+
* Route input type that accepts either a single route or an array of routes.
|
|
16
|
+
*/
|
|
17
|
+
type RouteInputOrArray = RouteInput | RouteInput[];
|
|
18
|
+
/**
|
|
19
|
+
* Options for registering routes.
|
|
20
|
+
*/
|
|
21
|
+
interface RegisterRoutesOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Prefix to apply to all routes in this registration.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* registerFastifyRoutes(app, userRoutes, {
|
|
28
|
+
* prefix: '/api/v1',
|
|
29
|
+
* });
|
|
30
|
+
* // Routes will be: /api/v1/users, /api/v1/users/:id, etc.
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
prefix?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Middlewares (preHandler hooks) to apply to all routes in this registration.
|
|
36
|
+
* These run before the controller handler.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* registerFastifyRoutes(app, protectedRoutes, {
|
|
41
|
+
* middlewares: [
|
|
42
|
+
* async (request, reply) => {
|
|
43
|
+
* if (!request.headers.authorization) {
|
|
44
|
+
* reply.status(401).send({ message: 'Unauthorized' });
|
|
45
|
+
* }
|
|
46
|
+
* },
|
|
47
|
+
* ],
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
middlewares?: FastifyMiddleware[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Registers routes onto a Fastify instance.
|
|
55
|
+
*
|
|
56
|
+
* Accepts either a single route or an array of routes. Can be called multiple
|
|
57
|
+
* times to register routes from different domains/modules.
|
|
58
|
+
*
|
|
59
|
+
* @param app - The Fastify instance (passed by reference)
|
|
60
|
+
* @param routes - A single route or an array of routes to register
|
|
61
|
+
* @param options - Optional configuration including prefix
|
|
62
|
+
*
|
|
63
|
+
* @example Single route
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const app = Fastify();
|
|
66
|
+
*
|
|
67
|
+
* registerFastifyRoutes(app, {
|
|
68
|
+
* metadata: { path: '/health', method: 'GET' },
|
|
69
|
+
* controller: healthController,
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example Multiple routes
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const app = Fastify();
|
|
76
|
+
*
|
|
77
|
+
* registerFastifyRoutes(app, [
|
|
78
|
+
* { metadata: { path: '/users', method: 'POST' }, controller: createUserController },
|
|
79
|
+
* { metadata: { path: '/users/{id}', method: 'GET' }, controller: getUserController },
|
|
80
|
+
* { metadata: { path: '/users/{id}', method: 'DELETE' }, controller: deleteUserController },
|
|
81
|
+
* ]);
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @example With prefix
|
|
85
|
+
* ```typescript
|
|
86
|
+
* registerFastifyRoutes(app, userRoutes, {
|
|
87
|
+
* prefix: '/api/v1',
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example With middlewares
|
|
92
|
+
* ```typescript
|
|
93
|
+
* registerFastifyRoutes(app, protectedRoutes, {
|
|
94
|
+
* middlewares: [
|
|
95
|
+
* async (request, reply) => {
|
|
96
|
+
* if (!request.headers.authorization) {
|
|
97
|
+
* reply.status(401).send({ message: 'Unauthorized' });
|
|
98
|
+
* }
|
|
99
|
+
* },
|
|
100
|
+
* ],
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @example Registering from multiple domains
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const app = Fastify();
|
|
107
|
+
*
|
|
108
|
+
* // Public routes
|
|
109
|
+
* registerFastifyRoutes(app, publicRoutes);
|
|
110
|
+
*
|
|
111
|
+
* // API routes with prefix
|
|
112
|
+
* registerFastifyRoutes(app, userRoutes, { prefix: '/api' });
|
|
113
|
+
* registerFastifyRoutes(app, orderRoutes, { prefix: '/api' });
|
|
114
|
+
*
|
|
115
|
+
* await app.listen({ port: 3000 });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function registerFastifyRoutes(app: FastifyInstance, routes: RouteInputOrArray, options?: RegisterRoutesOptions): void;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Error item for field-level validation errors.
|
|
122
|
+
*/
|
|
123
|
+
interface ErrorItem {
|
|
124
|
+
item: string;
|
|
125
|
+
message: string;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Standard error response body.
|
|
129
|
+
*/
|
|
130
|
+
interface ErrorResponseBody {
|
|
131
|
+
message: string;
|
|
132
|
+
errorCode: string;
|
|
133
|
+
errorItems?: ErrorItem[];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Mapped error response with status code and body.
|
|
137
|
+
*/
|
|
138
|
+
interface MappedErrorResponse {
|
|
139
|
+
statusCode: number;
|
|
140
|
+
body: ErrorResponseBody;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Maps our error hierarchy to HTTP status codes and response bodies.
|
|
144
|
+
*
|
|
145
|
+
* Mapping strategy:
|
|
146
|
+
* - `ObjectValidationError` → 400 Bad Request
|
|
147
|
+
* - `InvalidRequestError` → 400 Bad Request
|
|
148
|
+
* - `AccessDeniedError` → 403 Forbidden
|
|
149
|
+
* - `NotFoundError` → 404 Not Found
|
|
150
|
+
* - `ConflictError` → 409 Conflict
|
|
151
|
+
* - `UnprocessableError` → 422 Unprocessable Entity
|
|
152
|
+
* - `UseCaseError` (other) → 400 Bad Request
|
|
153
|
+
* - `DomainError` → 500 Internal Server Error (masked)
|
|
154
|
+
* - `InfraError` → 500 Internal Server Error (masked)
|
|
155
|
+
* - `ControllerError` → 500 Internal Server Error (masked)
|
|
156
|
+
* - Unknown → 500 Internal Server Error (masked)
|
|
157
|
+
*
|
|
158
|
+
* **Security Note:** Domain and infrastructure errors are masked to avoid
|
|
159
|
+
* leaking internal implementation details.
|
|
160
|
+
*
|
|
161
|
+
* @param error - The error to map
|
|
162
|
+
* @returns Mapped error response with status code and body
|
|
163
|
+
*/
|
|
164
|
+
declare function mapErrorToResponse(error: unknown): MappedErrorResponse;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Fastify error handler that maps domain errors to HTTP responses.
|
|
168
|
+
*
|
|
169
|
+
* Apply this to your Fastify app using `setErrorHandler()` to automatically
|
|
170
|
+
* convert domain/use-case errors to appropriate HTTP responses.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import Fastify from 'fastify';
|
|
175
|
+
* import { registerFastifyRoutes, onionErrorHandler } from '@cosmneo/onion-lasagna/backend/frameworks/fastify';
|
|
176
|
+
*
|
|
177
|
+
* const app = Fastify();
|
|
178
|
+
*
|
|
179
|
+
* // Apply error handler
|
|
180
|
+
* app.setErrorHandler(onionErrorHandler);
|
|
181
|
+
*
|
|
182
|
+
* // Register routes
|
|
183
|
+
* registerFastifyRoutes(app, routes);
|
|
184
|
+
*
|
|
185
|
+
* await app.listen({ port: 3000 });
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @example With custom error logging
|
|
189
|
+
* ```typescript
|
|
190
|
+
* app.setErrorHandler((error, request, reply) => {
|
|
191
|
+
* // Log the error
|
|
192
|
+
* request.log.error(error);
|
|
193
|
+
*
|
|
194
|
+
* // Use the onion error handler
|
|
195
|
+
* return onionErrorHandler(error, request, reply);
|
|
196
|
+
* });
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
declare function onionErrorHandler(error: FastifyError | Error, _request: FastifyRequest, reply: FastifyReply): FastifyReply;
|
|
200
|
+
|
|
201
|
+
export { type ErrorResponseBody, type FastifyMiddleware, type HttpController, type MappedErrorResponse, type RegisterRoutesOptions, type RouteInputOrArray, mapErrorToResponse, onionErrorHandler, registerFastifyRoutes };
|