katya / src /utils /responseWrapper.ts
esubtelnik's picture
first commit
d76f93d
raw
history blame contribute delete
561 Bytes
export interface ErrorResponse {
message: string;
code?: string;
statusCode?: number;
}
export interface ApiResponse<T> {
data?: T;
successful: boolean;
error?: ErrorResponse;
}
export function wrapResponse<T>(data?: T): ApiResponse<T> {
return { data, successful: true };
}
export function wrapError<T>(
message: string,
statusCode: number = 500,
code?: string
): ApiResponse<T> {
return {
data: undefined,
successful: false,
error: { message, statusCode, code },
};
}