apifox-openapi-codegen
v0.1.0
Published
从 Apifox 拉取 OpenAPI 并自动生成 `types.ts`、`client.ts`、`hooks.ts` 等文件。
Downloads
15
Maintainers
Readme
apifox-openapi-codegen
从 Apifox 拉取 OpenAPI 并自动生成 types.ts、client.ts、hooks.ts 等文件。
安装
npm i -D apifox-openapi-codegen配置
在项目根目录创建 apifox.config.js:
export default {
source: {
openapiUrl: 'https://api.apifox.com/api/v1/projects/<projectId>/export-openapi',
openapiFile: '',
},
request: {
method: 'POST',
body: {
scope: { type: 'ALL', excludedByTags: [] },
options: {
includeApifoxExtensionProperties: false,
addFoldersToTags: false,
},
oasVersion: '3.0',
exportFormat: 'JSON',
moduleId: '',
},
},
auth: {
token: '',
authHeader: '',
},
headers: {
'X-Apifox-Api-Version': '2024-03-28',
},
spec: {
output: {
dir: 'src/api/generated',
filename: 'apifox-openapi.json',
},
},
generated: {
output: {
dir: 'src/api/generated',
schema: 'schema.ts',
types: 'types.ts',
client: 'client.ts',
hooks: 'hooks.ts',
index: 'index.ts',
},
},
}命令
# 同步 OpenAPI 规范到本地
npx apifox-codegen sync
# 生成 TypeScript 类型、Axios 请求层和 React Query hooks
npx apifox-codegen gen
# 同步并生成, 是以上两步的组合命令, 会先同步 Apifox 的内容到本地, 然后根据同步的内容生成代码
npx apifox-codegen regen发布
在 scripts/apifox 目录执行:
npm publish