Skip to content

Commit 516a266

Browse files
feat(openapi): support openapi plugins (#527)
* feat(openapi): support openapi plugins * fix bug * support joi * use openAPI * add more options * support doc * fix path * remoe unuse * remove unuse code * use default style * support windows
1 parent 4d1c077 commit 516a266

5 files changed

Lines changed: 2321 additions & 83 deletions

File tree

packages/plugin-layout/src/layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect } from 'react';
22
// @ts-ignore
3-
import { Link, useModel, history, formatMessage } from 'umi';
3+
import { Link, useModel, history } from 'umi';
44
import ProLayout, { BasicLayoutProps } from '@ant-design/pro-layout';
55
import './style.less';
66
import renderRightContent from './renderRightContent';

packages/plugin-openapi/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @umijs/plugin-openapi
2+
3+
> @umijs/plugin-openapi.
4+
5+
See our website [@umijs/plugin-openapi](https://umijs.org/plugins/plugin-openapi) for more information.
6+
7+
## Install
8+
9+
Using npm:
10+
11+
```bash
12+
$ npm install --save-dev @umijs/plugin-openapi
13+
```
14+
15+
or using yarn:
16+
17+
```bash
18+
$ yarn add @umijs/plugin-openapi --dev
19+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@umijs/plugin-openapi",
3+
"version": "1.0.4",
4+
"description": "@umijs/plugin-openapi",
5+
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
7+
"files": [
8+
"lib",
9+
"src"
10+
],
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/umijs/plugins"
14+
},
15+
"keywords": [
16+
"umi"
17+
],
18+
"authors": [
19+
"chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
20+
],
21+
"license": "MIT",
22+
"bugs": "http://github.com/umijs/plugins/issues",
23+
"homepage": "https://github.com/umijs/plugins/tree/master/packages/plugin-openapi#readme",
24+
"peerDependencies": {
25+
"umi": "3.x"
26+
},
27+
"publishConfig": {
28+
"access": "public"
29+
},
30+
"dependencies": {
31+
"@umijs/openapi": "^1.1.6",
32+
"serve-static": "^1.14.1",
33+
"swagger-ui-react": "^3.41.1"
34+
}
35+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { join } from 'path';
2+
import { IApi } from 'umi';
3+
import rimraf from 'rimraf';
4+
import serveStatic from 'serve-static';
5+
import { generateService, getSchema } from '@umijs/openapi';
6+
import { existsSync, mkdirSync, writeFileSync } from 'fs';
7+
8+
export default (api: IApi) => {
9+
api.describe({
10+
key: 'openAPI',
11+
config: {
12+
schema(joi) {
13+
return joi.object({
14+
requestLibPath: joi.string(),
15+
schemaPath: joi.string(),
16+
mock: joi.boolean(),
17+
projectName: joi.string(),
18+
});
19+
},
20+
},
21+
enableBy: api.EnableBy.config,
22+
});
23+
const { absNodeModulesPath, absTmpPath } = api.paths;
24+
const openAPIFilesPath = join(absNodeModulesPath!, 'umi_open_api');
25+
26+
try {
27+
if (existsSync(openAPIFilesPath)) {
28+
rimraf.sync(openAPIFilesPath);
29+
}
30+
mkdirSync(join(openAPIFilesPath));
31+
} catch (error) {
32+
// console.log(error);
33+
}
34+
35+
// 增加中间件
36+
api.addMiddewares(() => {
37+
return serveStatic(openAPIFilesPath);
38+
});
39+
40+
api.onGenerateFiles(() => {
41+
api.writeTmpFile({
42+
path: join('plugin-openapi', 'openapi.tsx'),
43+
content: `
44+
import SwaggerUI from "swagger-ui-react";
45+
import "swagger-ui-react/swagger-ui.css";
46+
47+
const App = () => (
48+
<div
49+
style={{
50+
padding: 24,
51+
}}
52+
>
53+
<SwaggerUI url="/umi-plugins_openapi.json" />
54+
</div>
55+
);
56+
export default App;
57+
`,
58+
});
59+
});
60+
61+
if (api.env === 'development') {
62+
api.modifyRoutes((routes) => {
63+
return [
64+
{
65+
path: '/umi/plugin/openapi',
66+
component: api.utils.winPath(
67+
join(absTmpPath!, 'plugin-openapi', 'openapi.tsx'),
68+
),
69+
},
70+
...routes,
71+
];
72+
});
73+
}
74+
75+
api.onDevCompileDone(async () => {
76+
try {
77+
const openAPIConfig = api.config.openAPI;
78+
const openAPIJson = await getSchema(openAPIConfig.schemaPath);
79+
writeFileSync(
80+
join(openAPIFilesPath, 'umi-plugins_openapi.json'),
81+
JSON.stringify(openAPIJson, null, 2),
82+
);
83+
} catch (error) {
84+
console.error(error);
85+
}
86+
});
87+
88+
api.registerCommand({
89+
name: 'openapi',
90+
fn: async () => {
91+
const openAPIConfig = api.config.openAPI;
92+
const pageConfig = require(join(api.cwd, 'package.json'));
93+
const mockFolder = openAPIConfig.mock ? join(api.cwd, 'mock') : undefined;
94+
const serversFolder = join(api.cwd, 'src', 'services');
95+
// 如果mock 文件不存在,创建一下
96+
if (mockFolder && !existsSync(mockFolder)) {
97+
mkdirSync(mockFolder);
98+
}
99+
// 如果mock 文件不存在,创建一下
100+
if (serversFolder && !existsSync(serversFolder)) {
101+
mkdirSync(serversFolder);
102+
}
103+
104+
await generateService({
105+
projectName: pageConfig.name.split('/').pop(),
106+
...openAPIConfig,
107+
serversPath: serversFolder,
108+
mockFolder,
109+
});
110+
api.logger.info('[openAPI]: execution complete');
111+
},
112+
});
113+
};

0 commit comments

Comments
 (0)