import _axiosInstance from './axios';

const trans = (apiObj) => {
    const obj = apiObj;
    Object.keys(obj).forEach((apiKey) => {
        const data = {
            ...obj[apiKey]
        };
        obj[apiKey] = (payload) => _axiosInstance({
            method: data.method,
            url: data.url,
            ...payload,
        });
    });
    return obj;
};

const apiAll = require.context('@/api', true, /-api\.js$/);
const moduleApis = {};
// -转驼峰
function toCamelCase(str) {
    const pattern = /-([a-z])/g;
    return str.replace(pattern, (all, letter) => letter.toUpperCase());
}
apiAll.keys().map((key) => {
    // 截取文件名
    const suffixIndex = toCamelCase(key.match(/.*\/.*\/(.*\..*)/)[1]).indexOf('.js');
    const name = toCamelCase(key.match(/.*\/.*\/(.*\..*)/)[1]).substring(0, suffixIndex);
    moduleApis[name] = apiAll(key).default;
    // console.log('moduleApis ', moduleApis)
    return trans(moduleApis[name]);
});
export default moduleApis;
// console.log(moduleApis);
