35 lines
754 B
TypeScript
35 lines
754 B
TypeScript
|
|
import type { Response } from 'express'
|
||
|
|
import type { ApiResponse } from '../../shared/types.js'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a successful API response
|
||
|
|
*/
|
||
|
|
export const successResponse = <T>(res: Response, data: T, statusCode: number = 200): void => {
|
||
|
|
const response: ApiResponse<T> = {
|
||
|
|
success: true,
|
||
|
|
data,
|
||
|
|
}
|
||
|
|
res.status(statusCode).json(response)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send an error API response
|
||
|
|
*/
|
||
|
|
export const errorResponse = (
|
||
|
|
res: Response,
|
||
|
|
statusCode: number,
|
||
|
|
code: string,
|
||
|
|
message: string,
|
||
|
|
details?: unknown
|
||
|
|
): void => {
|
||
|
|
const response: ApiResponse<null> = {
|
||
|
|
success: false,
|
||
|
|
error: {
|
||
|
|
code,
|
||
|
|
message,
|
||
|
|
details: process.env.NODE_ENV === 'production' ? undefined : details,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
res.status(statusCode).json(response)
|
||
|
|
}
|