project-nomad/admin/app/helpers/api_response.ts
Claude def1a0733f
fix: standardize API error responses, fix parseInt radix, harden Docker service
- 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
2026-03-24 09:30:49 +00:00

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 })
}