使用JSON文件初始化
Publish to npm / publish (push) Failing after 11s

This commit is contained in:
2026-05-04 19:56:48 +08:00
parent fa4032f239
commit 3ece81fc06
3 changed files with 102 additions and 265 deletions
+26 -15
View File
@@ -108,23 +108,34 @@ export class SimApiCore {
}
/**
* 从 window.simapi 读取配置并初始化
* 从 JSON 文件加载配置
*
* 支持字段:endpoints, defaultEndpoint, debug
* 业务回调(businessCallback / responseCallback)需在代码中处理
* 部署时替换 config.json 即可切换环境,无需重新构建。
*
* @param file - 配置文件路径,默认 'config.json'
* @example
* await api.loadFromFile()
* await api.loadFromFile('/env/prod.json')
*/
autoInit(): void {
const config = (window as any).simapi
if (!config) return
if (config.debug !== undefined) {
this.debug = config.debug
}
if (config.endpoints) {
this.api.endpoints = {...this.api.endpoints, ...config.endpoints}
}
if (config.defaultEndpoint) {
this.api.defaultEndpoint = config.defaultEndpoint
async loadFromFile(file: string = 'config.json'): Promise<void> {
try {
const resp = await fetch(file)
if (!resp.ok) {
this.logDebug(`配置文件 ${file} 加载失败: HTTP ${resp.status}`)
return
}
const config = await resp.json()
if (config.debug !== undefined) {
this.debug = config.debug
}
if (config.endpoints) {
this.api.endpoints = { ...this.api.endpoints, ...config.endpoints }
}
if (config.defaultEndpoint) {
this.api.defaultEndpoint = config.defaultEndpoint
}
} catch (err: any) {
this.logDebug(`配置文件 ${file} 加载失败: ${err.message}`)
}
}
+2 -4
View File
@@ -15,8 +15,8 @@ export const useSimApi = defineStore('simapi', {
},
actions: {
// 所有方法直接代理到 core
autoInit(): void {
this._core.autoInit()
async loadFromFile(file: string = 'config.json'): Promise<void> {
return this._core.loadFromFile(file)
},
configure(options: SimApiOptions): void {
@@ -81,5 +81,3 @@ export const useSimApi = defineStore('simapi', {
},
},
})