import Foundation

class Response<T>: Decodable where T: Decodable {
    let success: Bool
    let data: T?
    let error: String?
    
    enum CodingKeys: String, CodingKey {
        case success
        case data
        case error
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        success = try container.decode(Bool.self, forKey: .success)
        if success {
            data = try container.decode(T.self, forKey: .data)
            error = nil
        } else {
            error = try container.decode(String.self, forKey: .error)
            data = nil
        }
    }
}
