import { response } from "express";

export interface ApiError {
    code: string;
    message: string;
}

function isApiError(value: unknown): value is ApiError {
    return (
        value !== null &&
        typeof value === "object" &&
        "code" in value &&
        "message" in value
    );
}

export class ApiResponse<T> {
    data: T;

    constructor(data: T) {
        this.data = data;
    }

    getAnswer(): { response: T } | { error: ApiError } {
        if (isApiError(this.data)) {
            return {
                error: this.data
            };
        }
        return {
            response: this.data
        };
    }
}