Initial commit
This commit is contained in:
34
api/utils/response.ts
Normal file
34
api/utils/response.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user