@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
40 lines (39 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendsConfig = extendsConfig;
exports.prepareConfig = prepareConfig;
const lodash_1 = require("lodash");
const helper_1 = require("../helper");
function extendsConfig(config, newConfig) {
const mergedConfig = (0, lodash_1.cloneDeep)(config);
return (0, lodash_1.mergeWith)(mergedConfig, newConfig, (srcValue, newValue, key) => {
if (key !== 'handleApi') {
return newValue ?? srcValue; // fallback to default merge behavior
}
// handleApi is a special case, we need to merge the functions
if (typeof newValue === 'function' && typeof srcValue === 'function') {
// chain the functions
return (...args) => {
const result = srcValue(...args);
// null or undefined return is a valid value for filtering
// if plugin return undefined or null, we should break the chain
if (!result) {
return result;
}
return newValue(result);
};
}
return newValue ?? srcValue; // fallback to default merge behavior
});
}
async function prepareConfig(config) {
let _config = (0, lodash_1.cloneDeep)(config);
const plugins = _config.plugins || [];
const pluginDriver = new helper_1.PluginDriver(plugins);
// plugin: handle config hook
_config = await pluginDriver.hookSeq('config', [_config], (result, args) => {
return result ? [result] : args;
}) ?? _config;
return _config;
}
exports.default = prepareConfig;