Files
MusicServer/sdk/api-source.ts
T
snowy-yang 354df2e739 refactor: 架构整固——分层单向化、全量 TypeScript、直接运行
- 移除 bd/mg 两平台及磁盘残留产物;清理 api-test 死脚本、musicSdk/message+options 死模块、infSign vendor
- musicSdk/ 改名 sdk/;src/common、src/auth、src/apis 整体移入 sdk/,依赖方向定为 src → sdk 严格单向
- src/ 手写 JS 全量转 .ts,删除 JS 类型垫片 modules.d.ts;源码 import 统一 .ts 后缀
- Node 原生类型剥离直接运行 .ts(engines >=22.18),取消 tsc 发射产物,tsc 仅类型检查(pnpm typecheck)
- 删除 kwdes 备用实现与 src/index.ts 转发垫片;测试移至 test/smoke-test.ts
- common/request.ts 补显式导出类型;修复 set-cookie 单值形态、ApiError 类字段等隐患

验证:tsc 零错误,smoke-test 41/41
2026-09-25 23:06:23 +08:00

55 lines
1.8 KiB
TypeScript

// 接口选择层:按 apiSource 配置把请求路由到外部聚合音源或服务器内置直连接口
import apiSourceInfo from './api-source-info.ts'
// 接口模式开关(替代原 lx-music 渲染进程 store 的服务器端替身):
// 默认 internal 使用内置直连接口;设置 SOURCE_ENABLED(任意非 0/false 值)启用外部聚合音源
const enabled = process.env.SOURCE_ENABLED
export const apiSource = {
value: enabled && enabled !== '0' && enabled !== 'false' ? 'source' : 'internal',
}
export const userApi = {
apis: {},
}
import internalApi from './apis/index.ts'
import { createApi as createSourceApi } from './apis/source.ts'
interface SourceApi {
getMusicUrl(songInfo: any, type: string): { promise: Promise<any>; cancelHttp(): void }
[key: string]: any
}
const allApi: Record<string, SourceApi> = {
source_kw: createSourceApi('kw'),
source_kg: createSourceApi('kg'),
source_tx: createSourceApi('tx'),
source_wy: createSourceApi('wy'),
}
const apiList: Record<string, SourceApi | undefined> = {}
const supportQuality: Record<string, any> = {}
for (const api of apiSourceInfo) {
supportQuality[api.id] = api.supportQualitys
for (const source of Object.keys(api.supportQualitys)) {
apiList[`${api.id}_api_${source}`] = allApi[`${api.id}_${source}`]
}
}
const getAPI = (source: string): SourceApi | undefined => apiList[`${apiSource.value}_api_${source}`]
export const apis = (source: string): SourceApi => {
if (/^user_api/.test(apiSource.value)) return userApi.apis[source]
// 服务器内置直连接口
if (apiSource.value == 'internal') {
const api = internalApi[source]
if (api) return api
throw new Error('Api is not found')
}
const api = getAPI(source)
if (api) return api
throw new Error('Api is not found')
}
export { supportQuality }