2026-03-12 13:05:20 +08:00
|
|
|
|
import { defineConfig } from 'orval';
|
|
|
|
|
|
|
2026-03-16 10:45:15 +08:00
|
|
|
|
/** 内联 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 } },
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-12 13:05:20 +08:00
|
|
|
|
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: {
|
2026-03-16 10:45:15 +08:00
|
|
|
|
// 使用 api:fetch 生成的 openapi.json(api:update 会自动先执行 api:fetch)
|
|
|
|
|
|
target: './openapi.json',
|
2026-03-14 16:50:54 +08:00
|
|
|
|
// 路径重写:确保 OpenAPI 文档中的路径正确
|
|
|
|
|
|
override: {
|
2026-03-19 09:34:54 +08:00
|
|
|
|
// 使用转换器修复路径 - 将 `/api/v1/xxx` 转换为 `/v1/xxx`(因为 baseURL 已经是 `/api`)
|
2026-03-16 10:45:15 +08:00
|
|
|
|
transformer: (spec) => {
|
|
|
|
|
|
const paths = spec.paths || {};
|
|
|
|
|
|
for (const path of Object.keys(paths)) {
|
2026-03-19 09:34:54 +08:00
|
|
|
|
let newKey = path.replace(/^\/api\/v1\//, '/v1/');
|
2026-03-14 16:50:54 +08:00
|
|
|
|
if (newKey !== path) {
|
2026-03-16 10:45:15 +08:00
|
|
|
|
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 };
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-14 16:50:54 +08:00
|
|
|
|
}
|
2026-03-16 10:45:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return spec;
|
2026-03-14 16:50:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-03-12 13:05:20 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|