mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-03-29 04:59:26 +02:00
- Create api_response.ts helper for consistent { success, error/data } format
- Add radix parameter (10) to all parseInt calls across controllers and services
- Fix race condition in DockerService by making in-memory guard atomic
- Fix container command splitting to handle quoted arguments properly
- Stop leaking internal error.message to API responses; log details server-side
https://claude.ai/code/session_01JFvpTYgm8GiE4vJ4cJKsFx
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import logger from '@adonisjs/core/services/logger'
|
|
|
|
/**
|
|
* Standardized API response helpers.
|
|
*
|
|
* Success responses follow the shape `{ success: true, data?: ..., message?: ... }`.
|
|
* Error responses follow the shape `{ success: false, error: '...' }`.
|
|
*
|
|
* Internal error details are logged server-side and never leaked to the client.
|
|
*/
|
|
|
|
export function apiSuccess(data?: Record<string, unknown> | unknown[] | string) {
|
|
if (typeof data === 'string') {
|
|
return { success: true, message: data }
|
|
}
|
|
return { success: true, ...(data && typeof data === 'object' ? (Array.isArray(data) ? { data } : data) : {}) }
|
|
}
|
|
|
|
export function apiError(
|
|
response: HttpContext['response'],
|
|
status: number,
|
|
clientMessage: string,
|
|
internalError?: unknown
|
|
) {
|
|
if (internalError) {
|
|
const detail = internalError instanceof Error ? internalError.message : String(internalError)
|
|
logger.error(`[API] ${clientMessage}: ${detail}`)
|
|
}
|
|
return response.status(status).send({ success: false, error: clientMessage })
|
|
}
|