loadAxiosConfig.js
1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const { config: baseConfig } = require("../../common/runtimeConfig");
const { getAxiosConfigKey } = require("./resolveApiName");
function pickAxiosBlock(targetConfig, apiName = "dj") {
const configKey = getAxiosConfigKey(apiName);
if (!configKey) {
throw new Error(`Unknown apiName for axios config: ${apiName}`);
}
const block = targetConfig?.[configKey];
if (!block?.baseURL || !block?.appkey || !block?.secret) {
throw new Error(`Invalid ROP config: missing ${configKey}.baseURL/appkey/secret`);
}
return {
baseURL: block.baseURL,
appkey: block.appkey,
secret: block.secret,
configKey,
};
}
/**
* 从代码内的当前环境读取 axios 配置。
* 保留异步返回值,避免影响现有 Node/脚本调用方。
* @param {{ apiName?: string }} options
*/
async function loadAxiosConfig(options = {}) {
const apiName = typeof options === "object" && options.apiName
? options.apiName
: "dj";
const { baseURL, appkey, secret, configKey } = pickAxiosBlock(baseConfig, apiName);
return {
baseURL,
appkey,
secret,
env: baseConfig.env,
apiName,
configKey,
};
}
module.exports = {
loadAxiosConfig,
pickAxiosBlock,
};