- 新增 scripts/fetch-openapi.js:拉取并修复 OpenAPI 文档 - 内联 ResultObject[] 的 \,移除非法 schema 名 - orval 使用本地 openapi.json,api:update 自动执行 api:fetch - AdminStatsController: Result<Object[]> 改为 Result<List<Map<String, Object>>> - .gitignore: 忽略 openapi.json Made-with: Cursor
69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
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/xxx` 转换为 `/api/v1/xxx`
|
||
transformer: (spec) => {
|
||
const paths = spec.paths || {};
|
||
for (const path of Object.keys(paths)) {
|
||
let newKey = path.replace(/\/v1\/v1\//g, '/v1/');
|
||
if (newKey === path) newKey = path.replace(/^\/api\/(?!v1\/)/, '/api/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;
|
||
},
|
||
},
|
||
},
|
||
},
|
||
});
|