import axios, { AxiosInstance, AxiosRequestConfig } from "axios";

{{> ./httpClient_common}}
    
	
export const GLOBAL_AXIOS_CONFIG = {
	default: {} as AxiosRequestConfig,
}

export class BaseApi {
    private _client: AxiosInstance;
    /** You can set this to whatever default axios config */

    constructor(axiosInstance?: AxiosInstance) {
        this._client = axiosInstance || axios.create({
            baseURL: BASE_PATH,
            ...GLOBAL_AXIOS_CONFIG.default,
        });
    }

    /** Assert required */
    protected r(params: any, fieldNames: string[]) {
        for (const fieldName of fieldNames) {
            const value = params[fieldName];
            if (value === null || value === undefined) {
                throw new Error(`field: ${fieldName} required`);
            }
        }
    }

    protected async ajax(method: AjaxMethod, localVarPath: string, queryParameters: any, bodyDict: any): Promise<any> {
        const response = await this._client.request({
            method,
            url: localVarPath,
            params: queryParameters,
            data: bodyDict
        });
        return response.data;
    }
}
