loadAxiosConfig.js
3.01 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require("fs");
const http = require("http");
const https = require("https");
const path = require("path");
const { getAxiosConfigKey } = require("./resolveApiName");
function tryParseJson(raw) {
if (typeof raw !== "string" || !raw.trim()) {
return null;
}
try {
return JSON.parse(raw);
} catch (error) {
return null;
}
}
function parseConfigCenterResponse(response) {
if (!response || typeof response !== "object") {
return null;
}
let payload = response;
if (response.code == "200" && response.data) {
payload = typeof response.data === "string"
? tryParseJson(response.data)
: response.data;
}
if (
payload
&& typeof payload === "object"
&& payload.axios?.baseURL
&& payload.axios?.appkey
) {
return payload;
}
return null;
}
const DEFAULT_CONFIG_URL = process.env.VUE_APP_CONFIG_CENTER_URL
|| "https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config";
const DEVELOP_CONFIG_PATH = path.join(__dirname, "../../common/baseConfig.develop.json");
function fetchJson(url) {
return new Promise((resolve, reject) => {
const lib = url.startsWith("https") ? https : http;
lib.get(url, (res) => {
let raw = "";
res.on("data", (chunk) => { raw += chunk; });
res.on("end", () => {
try {
resolve(JSON.parse(raw));
} catch (error) {
reject(new Error(`JSON parse failed: ${raw.slice(0, 200)}`));
}
});
}).on("error", reject);
});
}
function loadDevelopFallback() {
return JSON.parse(fs.readFileSync(DEVELOP_CONFIG_PATH, "utf8"));
}
function pickAxiosBlock(baseConfig, apiName = "dj") {
const configKey = getAxiosConfigKey(apiName);
if (!configKey) {
throw new Error(`Unknown apiName for axios config: ${apiName}`);
}
const block = baseConfig?.[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 配置:优先配置中心,失败回退 baseConfig.develop.json
* @param {string|{ configUrl?: string, apiName?: string }} options
*/
async function loadAxiosConfig(options) {
let configUrl = DEFAULT_CONFIG_URL;
let apiName = "dj";
if (typeof options === "string") {
configUrl = options;
} else if (options && typeof options === "object") {
configUrl = options.configUrl || DEFAULT_CONFIG_URL;
apiName = options.apiName || "dj";
}
let baseConfig = null;
try {
const response = await fetchJson(configUrl);
baseConfig = parseConfigCenterResponse(response);
} catch (error) {
// 回退 develop 本地配置
}
if (!baseConfig?.axios?.appkey || !baseConfig?.axios?.secret) {
baseConfig = loadDevelopFallback();
}
const { baseURL, appkey, secret, configKey } = pickAxiosBlock(baseConfig, apiName);
return {
baseURL,
appkey,
secret,
env: baseConfig.env,
apiName,
configKey,
};
}
module.exports = {
loadAxiosConfig,
loadDevelopFallback,
pickAxiosBlock,
DEFAULT_CONFIG_URL,
};