import http from "@ohos:net.http";
import type { HttpConverter } from './HttpConverter';
/**
 * @ProjectName : HarmonyUtilSdk
 * @FileName : JsonConverter
 * @Author : yic
 * @Time : 2024/6/20 15:57
 * @Description : 使用JSON解析
 */
export class JsonConverter implements HttpConverter {
    convert<R>(response: http.HttpResponse): R {
        if (response.responseCode >= 200 && response.responseCode <= 299) {
            if (response.resultType === http.HttpDataType.STRING) {
                return JSON.parse(response.result as string) as R;
            }
            else if (response.resultType === http.HttpDataType.OBJECT) {
                return response.result as R;
            }
            else {
                throw new Error('Invalid data');
            }
        }
        else if (response.responseCode >= 400 && response.responseCode <= 499) {
            throw new Error('Invalid data params');
        }
        else if (response.responseCode >= 500) {
            throw new Error('Server error');
        }
        else {
            throw new Error('Other error');
        }
    }
}
