import { defineConfig } from 'orval'; /** 内联 schema,避免 ResultObject[] 的 $ref 导致 oneOf 验证错误 */ const RESULT_OBJECT_ARRAY_SCHEMA = { type: 'object', properties: { code: { type: 'integer', format: 'int32' }, message: { type: 'string' }, data: { type: 'array', items: { type: 'object', additionalProperties: true } }, }, }; export default defineConfig({ readingPlatform: { output: { mode: 'split', target: 'src/api/generated/index.ts', schemas: 'src/api/generated/model', client: 'axios', override: { mutator: { path: 'src/api/generated/mutator.ts', name: 'customMutator', }, // 自定义类型名称 name: (type) => { // 移除命名空间前缀,简化类型名称 return type.replace(/^(Result|ResultPageResult)/, ''); }, }, // 导入优化 imports: { axios: true, }, }, input: { // 使用 api:fetch 生成的 openapi.json(api:update 会自动先执行 api:fetch) target: './openapi.json', // 路径重写:确保 OpenAPI 文档中的路径正确 override: { // 使用转换器修复路径 - 将 `/api/v1/xxx` 转换为 `/v1/xxx`(因为 baseURL 已经是 `/api`) transformer: (spec) => { const paths = spec.paths || {}; for (const path of Object.keys(paths)) { let newKey = path.replace(/^\/api\/v1\//, '/v1/'); if (newKey !== path) { paths[newKey] = paths[path]; delete paths[path]; } } for (const pathObj of Object.values(paths)) { for (const op of Object.values(pathObj)) { const content = op?.responses?.['200']?.content; if (!content) continue; for (const media of Object.values(content)) { if (media?.schema?.['$ref']?.endsWith('ResultObject[]')) { media.schema = { ...RESULT_OBJECT_ARRAY_SCHEMA }; } } } } return spec; }, }, }, }, });